Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SIGCHLD Signal Processing

In Unix, when a child process in background terminates, it sends a SIGCHLD signal to the parent to inform it that it terminated.

Does the same happen even if the process was in foreground? If so, this means the parent will just ignore it.

Is this right? Or if it is in foreground, then no signal is sent at all?

like image 291
darksky Avatar asked Mar 23 '12 20:03

darksky


People also ask

What is the SIGCHLD signal?

The SIGCHLD signal is the only signal that the z/TPF system sends to a process. When a child process created by the tpf_fork function ends, the z/TPF system: Sends a SIGCHLD signal to the parent process to indicate that the child process has ended.

How do you handle the SIGCHLD signal?

When a child process stops or terminates, SIGCHLD is sent to the parent process. The default response to the signal is to ignore it. The signal can be caught and the exit status from the child process can be obtained by immediately calling wait(2) and wait3(3C).

What does SIGCHLD return?

The SIGCHLD handler will call waitpid once, and it will return the pid of the first child. The SIGCHLD handler will then loop around and call waitpid a second time. This second call will block until the second child exits three seconds later, preventing dad from returning to his nap.

What is the default action of SIGCHLD?

Note that the default action for SIGCHLD is to ignore this signal; nevertheless signal(SIGCHLD, SIG_IGN) has effect, namely that of preventing the transformation of children into zombies.


2 Answers

background and foreground are job control concepts, and are part of the the shell. They are applied to processes and do not affect which process spawned (exec-ed) another process.

A child process is the result of a fork()-exec() call. The child gets a parent pid of the process that executed the fork() call. This is the context of the SIGCHLD signal, the parent pid receives the SIGCHLD signal. It does not matter whether the child process is "foreground" or "background", only the ppid matters on exit of the process.

like image 164
jim mcnamara Avatar answered Oct 13 '22 15:10

jim mcnamara


There's no such thing as foreground child. The term background process is used to simply refer that we are mainly dealing with the parent process (which may create a child processes to do a part its job). When a child process exits SIGCHLD is always sent to parent process. However, the parent process usually ignores it. If parent wants to deal with exit of child Or to do some action only after the exit of child, then it can use the wait() system call to get the status of child process.

like image 42
P.P Avatar answered Oct 13 '22 13:10

P.P