SIGCHLD Signal Processing
Asked Answered
C

2

9

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?

Conchoidal answered 23/3, 2012 at 20:18 Comment(1)
Yes, SIGCHLD is also sent for foreground processes, I believe. You could check with strace.Ashleaashlee
V
17

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.

Vargas answered 23/3, 2012 at 21:6 Comment(5)
When exactly does the parent send a SIGCHLDsignal? In the code I am writing, it appears it sends it as soon as it starts executing which seems wrong..Conchoidal
The exectuable does it as part of the process rundown - the part where it closes open file descriptors, etc., while your process is being shutdown. There is (depending on the implementation) a file crt0.o (may have other names), that is linked into each executable image. It has some entrypoints like _start(), _cleanup(), etc. These are invoked after your main() returns or the process calls _exit() or exit(). _start() calls main(), BTW. Anyway, this code sends the signal to the parent.Vargas
@jimmcnamara: SIGCHLD is sent by the kernel.Caressive
@ninjali - the kernel does indeed DELIVER the signal. In looking at HPUX 10i, for example, the linked in object raises signal via kill.Vargas
Isn't this backwards? According to the POSIX standard, the parent gets the pid of the child, whereas the child gets 0. pubs.opengroup.org/onlinepubs/009695399/functions/fork.htmlWilmer
P
4

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.

Plast answered 23/3, 2012 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.