P_WAIT & P_NOWAIT

Hi, All;

When using spawnl () with P_NOWAIT to start a process, after this process
exit (the parent thread is still running), it seems its pid remains, I got
something like this:

pidin

pid tid name prio STATE
Blocked
1441832 ( n/a)

and also the pid reside under /proc:

pwd

/proc

ls

1 1220630 1441821 1441828 159760 2 98313
118794 1437719 1441822 1441829 159765 4099 boot
118795 1441816 1441823 1441832 1658921 4100 dumper
118796 1441817 1441824 155661 1658922 53253 ipstats
118798 1441818 1441825 155663 1798182 53254 mount
118802 1441819 1441826 155665 1798183 57351 self
118803 1441820 1441827 155668 1859627 94216

I don’t know how this happens, and if it is normal, how to explain? since
when I use P_WAIT, when the started process exits, its pid disappear, this
makes sense to me.

BTW, how can I check if a process is terminated? I had thought to check if
its pid exists, now it seems not reliable.

Any help is appreciated.

Xuedong

Xuedong Chen <Xuedong.Chen@igt.com> wrote:

Hi, All;

When using spawnl () with P_NOWAIT to start a process, after this process
exit (the parent thread is still running), it seems its pid remains, I got
something like this:

pidin

pid tid name prio STATE
Blocked
1441832 ( n/a)

and also the pid reside under /proc:

pwd

/proc

ls

1 1220630 1441821 1441828 159760 2 98313
118794 1437719 1441822 1441829 159765 4099 boot
118795 1441816 1441823 1441832 1658921 4100 dumper
118796 1441817 1441824 155661 1658922 53253 ipstats
118798 1441818 1441825 155663 1798182 53254 mount
118802 1441819 1441826 155665 1798183 57351 self
118803 1441820 1441827 155668 1859627 94216

I don’t know how this happens, and if it is normal, how to explain? since
when I use P_WAIT, when the started process exits, its pid disappear, this
makes sense to me.

The difference between P_WAIT and P_NOWAIT is just that with P_NOWAIT the
spawn call will return once the newly spawned process has been started, where
P_WAIT will not return until the newly spawned process has exited. The
pid you are seeing is the new process’s zombie, and can be checked for with
the wait/waitpid calls. If you want the process cleaned up automaticly you
likely want the P_NOWAITO flag.

BTW, how can I check if a process is terminated? I had thought to check if
its pid exists, now it seems not reliable.

If you are using the P_NOWAIT flag, then a call to wait will not return
until it has exited. If you want a non-blocking way to check this take a
look at waitpid() with the WNOHANG flag. If you use the P_NOWAITO flag
to the spawn, the pid should disapear from from when it exits.

-Peter

Any help is appreciated.

Xuedong