Question

I'm writing a program for some homework that's going to entail some forking, but I'm a little unclear on sharing variables and dealing with zombie processes.

  1. If I have global variables, do the parent and all children work with the same "copy" of those global variables? If not, is there some way I can get them to (vfork?)?

  2. I know what zombie processes are, but I'm not clear on how to get rid of them. My program will be spinning off lots of temporary processes so I don't know that I can wait() for each of them individually. When the parent process terminates, that gets rid of all zombies associated with it, right? What if the parent is terminated before a child? Will the child leave behind a zombie when it finishes (are these the ones init() clears out periodically)?

  3. Possibly sidestepping question 2 entirely, since I don't actually care about the results from the child processes, is there any way to have them not leave behind zombies at all? I saw something about signal(SIGCHLD, SIG_IGN) but I'm not sure how to use it, and the manpage for it I found was somewhat obtuse.

Was it helpful?

Solution

1) If I have global variables, do the parent and all children work with the same "copy" of those global variables? If not, is there some way I can get them to (vfork?)?

The stack will be copied completely. Copied, not shared. Thus if you want your parent and child to communicate you have to use sockets or shared memory. Or threads.

Skipping question 2:

3) Possibly sidestepping question 2 entirely, since I don't actually care about the results from the child processes, is there any way to have them not leave behind zombies at all? I saw something about signal(SIGCHLD, SIG_IGN) but I'm not sure how to use it, and the man for it I found was somewhat... obtuse.

In POSIX you can use special signals for your program. For example ctrl+c will send a interrupt signal (SIGINT), which will terminate your program if you didn't define a SIGINT handler.

SIGCHLD is a signal your program receives if a child process gets terminated. It is ignored by default. Well, why don't we write ourself a little signal handler? A signal handler is a void function with a int as only argument:

void cleanup_child(int signal) {
    wait();
}

Now register your signal handler at the very beginning of your main function and your done:

int main(...){
    signal(SIGCHLD,cleanup_child);
    ...

Now all zombies get cleaned automatically. Please keep in mind that a signal interrupts your current program, calls the specific signal handler and resumes your program.

OTHER TIPS

1) The two processes do not share global variables.

2) using waitid (2) may help you. Look man.

If a parent process terminates before child, then the child gets new parent - the process with PID=1, i.e. init. If the child is zombie, init resolves this problem automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top