I'm writing a C program on Sun Solaris and I have to deal with the
following problem.
A process, let's call it INIT, forks a child, call this CHILD1.
CHILD1 itself forks several grandchildren, which in turn may fork
still more processes, and so on.
When a certain time limit is reached, process INIT wants to kill
process CHILD1 together with all its children and grandchildren. How
does it do that?
If I do a kill(PID_OF_CHILD1, SIGKILL) in process INIT, I just kill
process CHILD1, but none of its childern. So this doesn't work.
Is there a system call to do what I need?
How can I find out the PIDs of all children of a process?
Where can I find documentation explaining the principles of processes
in UNIX?
I mean, the man pages are good for looking up details, but not so good
for getting the big picture.
Thanks in advance for all replies.
Regards,
Johannes
> I'm writing a C program on Sun Solaris and I have to deal with the
> following problem.
>
> A process, let's call it INIT, forks a child, call this CHILD1.
> CHILD1 itself forks several grandchildren, which in turn may fork
> still more processes, and so on.
>
> When a certain time limit is reached, process INIT wants to kill
> process CHILD1 together with all its children and grandchildren. How
> does it do that?
>
> If I do a kill(PID_OF_CHILD1, SIGKILL) in process INIT, I just kill
> process CHILD1, but none of its childern. So this doesn't work.
>
> Is there a system call to do what I need?
Depending on what you're doing, kill() should work. The man page on my
Solaris 8 box says:
"If pid is negative but not (pid_t)-1, sig will be sent to all
processes whose process group ID is equal to the absolute value of
pid and for which the process has permission to send a signal"
If you make the parent process the leader of its own process group,
then all the children it creates will be members of that group. As
long as the child processes don't change their process group, they can
all be killed by sending an appropriate signal to the group.
> How can I find out the PIDs of all children of a process?
Run ps -ef and trace the parent process ids. You don't want to do that
though. You could also have all the processes write their pids into a
file, but it doesn't sound like you need to do that.
> Where can I find documentation explaining the principles of
> processes in UNIX? I mean, the man pages are good for looking up
> details, but not so good for getting the big picture.
The comp.unix.programming FAQ has good info in it
(http://www.erlenstar.demon.co.uk/unix/). There are other worthwhile
things to look at on the web (try a search engine).
If you're serious though, get "Advanced Programming in the Unix
Environment" by W. Richard Stevens.
Joe