Catching the death of someone else's child

Catching the death of someone else's child

Post by Tom Cowel » Wed, 23 Jun 1999 04:00:00



Just a quick question - I can't see this in the FAQ, and I think it's a
simple yes or no (in fact, I think it's no).

Is there a function like waitpid() that will block until a specified
process exits? waitpid() will only do this if the calling process is the
parent of the specified process.

Tom Cowell

 
 
 

Catching the death of someone else's child

Post by Kenneth C Stah » Wed, 23 Jun 1999 04:00:00


How close does the timing have to be? Do you know the pid of the process you
want to wait for?

If you know the pid, then try using the getpgid(2) function. You pass it a
pid and it returns the progess group id of the target pid, but more
importantly it will return -1 and set errno to ESRCH if the pid is no longer
in memory. You may have to run this from root either root or as the owner of
the pid that you are trying to detect - the man page leads me to believe
that you might get an EPERM if you try to access another account's
processes.

If you know that a particular process ignores a specific signal and if you
are priviledged to send a signal to that process (same conditions above,
either you own it or you are root), then use kill(2) or sigsend(2) to send
the ignored signal. Again, you'll get an ESRCH.

I'd strongly advise against puting this in a real tight loop or else your
consume a lot of cpu. If your timing is not critical, use SIGALRM to set a
timer and then check the target pid.

Ken


> Just a quick question - I can't see this in the FAQ, and I think it's a
> simple yes or no (in fact, I think it's no).

> Is there a function like waitpid() that will block until a specified
> process exits? waitpid() will only do this if the calling process is the
> parent of the specified process.

> Tom Cowell


 
 
 

Catching the death of someone else's child

Post by P.Rogi » Wed, 23 Jun 1999 04:00:00


|> Just a quick question - I can't see this in the FAQ, and I think it's a
|> simple yes or no (in fact, I think it's no).
|>
|> Is there a function like waitpid() that will block until a specified
|> process exits? waitpid() will only do this if the calling process is the
|> parent of the specified process.
|>
|>
|> Tom Cowell

You are right, the answer is no.
 but you can always use kill(pid,0) to check if the process is still alive

--
*======================================================================*
| Pierre Rogier                                         Bull SA        |
| WEB: http://www-frec.bull.com/                                       |

*======================================================================*

 
 
 

Catching the death of someone else's child

Post by Bas de Bakke » Thu, 24 Jun 1999 04:00:00


    Tom> Is there a function like waitpid() that will block until a
    Tom> specified process exits? waitpid() will only do this if the
    Tom> calling process is the parent of the specified process.

No.

But if you're on Solaris and don't mind writing non-portable code:
Recent versions of Solaris have the /usr/proc/bin/pwait command that
does what you want.  A quick look with truss tells us:

open("/proc/22362/psinfo", O_RDONLY)            = 3
poll(0x00021720, 1, -1)         (sleeping...)
poll(0x00021720, 1, -1)                         = 1
        fd=3  ev=POLLPRI rev=POLLHUP

So open /proc/<pid>/psinfo and poll the resulting fd for POLLPRI.

Regards,
Bas.

 
 
 

Catching the death of someone else's child

Post by Tom Cowel » Thu, 24 Jun 1999 04:00:00


Thanks, folks

That was what I thought - I wanted to be able to wait for the process to
exit, but since timing isn't critical a polling solution will be OK.

Since I am, in fact, on Solaris, I will fiddle with Bas's suggestion as
well.

Tom

 
 
 

1. HELP: child process catching death of parent

If I've forked and exec'ed a child, and it's happily running, how
do I kill it if the parent dies.

I know about installing signal handlers in the parent to catch any
relevant signals, but is there any way I can have a something like a
non-blocking waitpid
which would act as a signal handler in the child so I could just kill myself
if the parent goes away for whatever reason.

thanks!!

2. problems starting X

3. Republican's caught chanting 'death to America!'

4. for Xkernel 2.0a, stuck on bootparamd

5. Child informed of parent's death

6. 1998 Business Promotion

7. Getting a child and child's child process-id

8. New to Linux - problem with Netscape

9. What's the best way to terminate children on parent death?

10. Getting a child and child's child process-id

11. child's child redirection

12. fork() and child death.

13. death of child