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?
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.
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).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With