tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

subshell differences between /bin/sh and /bin/ksh



Could someone please explain the output below?  I have the following
shell script to inspect the PIDs of child processes:

	echo ">>> sleep"
	echo "  > parent: $$"
	/bin/sleep 1 &
	echo "  > child: $!"
	ps -o "pid,ppid,command"
	wait
	
	echo ">>> sleep in subshell"
	echo "  > parent: $$"
	( /bin/sleep 1 ) &
	echo "  > child: $!"
	ps -o "pid,ppid,command"
	wait
	
	echo ">>> sleep twice in subshell"
	echo "  > parent: $$"
	( /bin/sleep 1; /bin/sleep 2 ) &
	echo "  > child: $!"
	ps -o "pid,ppid,command"
	wait

If I run this script using /bin/sh, I get the following output:

	>>> sleep
	  > parent: 13544
	  > child: 4947
	  PID  PPID COMMAND
	  139 13544 ps -o pid,ppid,command 
	 4947 13544 /bin/sleep 1 
	 6189 13986 -ksh 
	13544  6189 /bin/sh t.sh 
	>>> sleep in subshell
	  > parent: 13544
	  > child: 14447
	  PID  PPID COMMAND
	 4630 13544 ps -o pid,ppid,command 
	 6189 13986 -ksh 
	13544  6189 /bin/sh t.sh 
	14447 13544 /bin/sh t.sh 
	17445 14447 /bin/sleep 1 
	>>> sleep twice in subshell
	  > parent: 13544
	  > child: 1487
	  PID  PPID COMMAND
	 1487 13544 /bin/sh t.sh 
	 1548  3239 /bin/sleep 1 
	 3239  1487 /bin/sh t.sh 
	 6189 13986 -ksh 
	13544  6189 /bin/sh t.sh 
	24087 13544 ps -o pid,ppid,command 

In the "sleep twice in subshell" (3rd) case, why is there another
subshell process with PID 3239?  The process chain looks like:

	/bin/sh t.sh > /bin/sh t.sh > /bin/sh t.sh > /bin/sleep 1

If I run this script using /bin/ksh, I get the following output:

	>>> sleep
	  > parent: 9269
	  > child: 5229
	 PID  PPID COMMAND
	 544  9269 ps -o pid,ppid,command 
	5229  9269 /bin/sleep 1 
	6189 13986 -ksh 
	9269  6189 /bin/ksh t.sh 
	>>> sleep in subshell
	  > parent: 9269
	  > child: 5139
	  PID  PPID COMMAND
	 5139  9269 /bin/sleep 1 
	 6189 13986 -ksh 
	 9269  6189 /bin/ksh t.sh 
	14597  9269 ps -o pid,ppid,command 
	>>> sleep twice in subshell
	  > parent: 9269
	  > child: 22025
	  PID  PPID COMMAND
	 5997 22025 /bin/sleep 1 
	 6189 13986 -ksh 
	 9269  6189 /bin/ksh t.sh 
	16165  9269 ps -o pid,ppid,command 
	22025  9269 /bin/ksh t.sh 

In the "sleep in subshell" (2nd) case, why is there no subshell?

	/bin/sh t.sh > /bin/sleep 1

I checked with bash-4.4.012 and zsh-5.1 and their output matches
with /bin/ksh.  This is on NetBSD-7.0.1.

Regards,

Johnny C. Lam
jlam%NetBSD.org@localhost


Home | Main Index | Thread Index | Old Index