Re: HPUX 11.23i - process grouping and subshells (186 Views)
Reply
Occasional Advisor
Rob Lindsay
Posts: 9
Registered: ‎10-19-2006
Message 1 of 8 (186 Views)

HPUX 11.23i - process grouping and subshells

I have a question regarding the scenario below. When grouping the sleep and the echo and putting them both in the background it produces what looks like 6 child copies of the current script running the sleeps as children. Note that if you do not group the commands and just run the sleep in the background you do not get the 6 copies and the sleeps run as children of the original script (i.e. PID 26076).

My question is, are these actually 6 running copies of the script or are they just subshells given the name of the current script.

Script that was run:
#!/bin/sh
x=1
while [ ${x} -le 6 ]
do
( nohup sleep 10; echo ${x} ) &
x=$(expr ${x} + 1)
done
sleep 10

Processes list while the script was running:

systest 26090 26089 0 16:31:12 pts/19 0:00 sleep 10
systest 26086 26076 0 16:31:12 pts/19 0:00 /bin/sh /app/systest/runs/foo2.sh
root 8777 933 0 11:11:50 ? 0:00 sshd: systest [priv]
systest 8909 8907 0 11:11:57 pts/19 0:00 -ksh
systest 26082 26080 0 16:31:12 pts/19 0:00 sleep 10
systest 22879 22816 0 10:43:10 ? 0:00 sshd: systest@pts/14
systest 26078 26077 0 16:31:12 pts/19 0:00 sleep 10
systest 26095 26076 0 16:31:12 pts/19 0:00 sleep 10
systest 26080 26076 0 16:31:12 pts/19 0:00 /bin/sh /app/systest/runs/foo2.sh
systest 26077 26076 0 16:31:12 pts/19 0:00 /bin/sh /app/systest/runs/foo2.sh
systest 26092 26076 0 16:31:12 pts/19 0:00 /bin/sh /app/systest/runs/foo2.sh
systest 22881 22879 0 10:43:10 pts/14 0:00 -ksh
systest 26076 8909 0 16:31:12 pts/19 0:00 /bin/sh /app/systest/runs/foo2.sh
systest 26089 26076 0 16:31:12 pts/19 0:00 /bin/sh /app/systest/runs/foo2.sh
systest 26088 26086 0 16:31:12 pts/19 0:00 sleep 10
systest 8907 8777 0 11:11:56 ? 0:01 sshd: systest@pts/19
systest 26104 22881 0 16:31:17 pts/14 0:00 grep systest
systest 26083 26076 0 16:31:12 pts/19 0:00 /bin/sh /app/systest/runs/foo2.sh
systest 26094 26092 0 16:31:12 pts/19 0:00 sleep 10
systest 26103 22881 5 16:31:17 pts/14 0:00 ps -ef
systest 26085 26083 0 16:31:12 pts/19 0:00 sleep 10
root 22816 933 0 10:43:06 ? 0:00 sshd: systest [priv]
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 8 (186 Views)

Re: HPUX 11.23i - process grouping and subshells

Hi:

While these are subshells (children) insofar as they can't alter their parent's variables, they do inherit all of their parent's environment when initiated. In that sense they are copies of the parent (as in 'fork' and 'exec').

Regards!

...JRF...
Occasional Advisor
Rob Lindsay
Posts: 9
Registered: ‎10-19-2006
Message 3 of 8 (186 Views)

Re: HPUX 11.23i - process grouping and subshells

Thanks JR,

Further to my question, as far as I can see the six child processes of the original script (PID 26076) cannot actually be running copies of that script even though the COMMAND listed in ps is the same name. If they were running copies then 6 more sleeps would be forked and the script would never end ...
Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 4 of 8 (186 Views)

Re: HPUX 11.23i - process grouping and subshells

>Processes list while the script was running:

Please use the following to provide a nice indented list:
$ UNIX95=1 ps -H -fu systest

You'll also need to attach it so it doesn't get reformatted.

>are these actually 6 running copies of the script or are they just subshells given the name of the current script.

The latter. These are subshells executing a pipeline.

>cannot actually be running copies of that script even though the COMMAND listed in ps is the same name.

Right. They are only executing parts of the original script but since they were forked, everything in ps(1) looks the same.

>JRF: In that sense they are copies of the parent (as in 'fork' and 'exec').

You mean as in just "fork" and nothing else.
Occasional Advisor
Rob Lindsay
Posts: 9
Registered: ‎10-19-2006
Message 5 of 8 (186 Views)

Re: HPUX 11.23i - process grouping and subshells

Dennis, Thanks for the formatted ps output suggestion this displays heirachy well!

A formatted and order ps list is now attached.
Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 6 of 8 (186 Views)

Re: HPUX 11.23i - process grouping and subshells

>displays hierarchy well!

This shows the 6 subshells, each with a sleep child. And a sleep in the original shell.
Occasional Advisor
Rob Lindsay
Posts: 9
Registered: ‎10-19-2006
Message 7 of 8 (186 Views)

Re: HPUX 11.23i - process grouping and subshells

Yes. It is clear now what is happening.

It is a shame ps displays these subshells with same name as their parent though, quite confusing until you find out what's going on.

Thanks for your help !
Occasional Advisor
Rob Lindsay
Posts: 9
Registered: ‎10-19-2006
Message 8 of 8 (186 Views)

Re: HPUX 11.23i - process grouping and subshells

Explaination above.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.