Sobes.tech
Junior — Middle

Kas saaksite selgitada, mida tähendab signaal SIGCHLD operatsioonisüsteemis?

sobes.tech AI

Vastus AI-lt

In Unix/Linux operating systems, the SIGCHLD signal 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 find out when a child process has finished and to 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;
}

This way, SIGCHLD helps efficiently manage the lifecycle of child processes.