Junior — Middle
Can you explain what the SIGCHLD signal means in an operating system?
sobes.tech AI
Answer from AI
The SIGCHLD signal in Unix/Linux operating systems is sent to the parent process when one of its child processes terminates or changes state (for example, stops or resumes).
The parent can handle this signal to learn about the child's termination and free associated resources (for example, by calling wait() or waitpid()), preventing zombie processes.
Example of handling SIGCHLD in C:
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
void sigchld_handler(int signo) {
int status;
while (waitpid(-1, &status, WNOHANG) > 0) {
printf("Child process terminated\n");
}
}
int main() {
signal(SIGCHLD, sigchld_handler);
if (fork() == 0) {
// child process
_exit(0);
}
// parent process
pause(); // wait for signals
return 0;
}
Thus, SIGCHLD helps efficiently manage the lifecycle of child processes.