What does signal(SIGCHLD, SIG_DFL); mean?

走远了吗. 提交于 2019-12-10 11:28:17

问题


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 SIGDFT then, will it work? How do I set SIGCHLD to SIGDFT? I want process to become zombie, so I can read the child status in parent after waitpid.


回答1:


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.)




回答2:


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.
}


来源:https://stackoverflow.com/questions/23075307/what-does-signalsigchld-sig-dfl-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!