kapilddit
kapilddit

Reputation: 1769

What does signal(SIGCHLD, SIG_DFL); mean?

I am not handling SIGCHLD in my code. Still my process is removed immediately after termination. I want it to become zombie process.

If I set SIGCHLD to SIG_DFL then, will it work? How do I set SIGCHLD to SIG_DFL?

I want process to become zombie, so I can read the child status in parent after waitpid.

Upvotes: 4

Views: 9626

Answers (2)

Duck
Duck

Reputation: 27542

From your question history you seem to be tying yourself in knots over this. Here is the outline on how this works:

  1. The default disposition of SIGCHLD is ignore. In other words, if you do nothing, the signal is ignored but the zombie exists in the process table. This why you can wait on it at any time after the child dies.

  2. If you set up a signal handler then the signal is delivered and you can reap it as appropriate but the (former) child is still a zombie between the time it dies and the time you reap it.

  3. If you manually set SIGCHLD's disposition to SIG_IGN via signal then the semantics are a little different than they are in item 1. When you manually set this disposition the OS immediately removes the child from the process table when it dies and does not create a zombie. Consequently there is no longer any status information to reap and wait will fail with ECHILD. (Linux kernels after 2.6.9 adhere to this behavior.)

Upvotes: 11

qqibrow
qqibrow

Reputation: 3022

So your final target is to read return code in parent process after your child process exit? I don't see this has any matter with signal. Some example code is:

short pid;
if((pid == fork()) == 0) {
// Child process do some thing here.
exit(n);
} else {
  int returnCode;
  while(pid != wait(&returnCode));
  // the child has terminated with returnCode
  // wait is blocking system call so u don't need to worry about busy waiting.
}

Upvotes: 0

Related Questions