Sigaction
≈ 2 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computing, is a function API defined by POSIX to give the programmer access to what a program's behavior should be when receiving specific OS signals.
In computing, is a function API defined by POSIX to give the programmer access to what a program's behavior should be when receiving specific OS signals. struct sigaction { void (*sa_handler)(int); // address of signal handler sigset_t sa_mask; // additional signals to block int sa_flags; // signal options // alternate signal handler void (*sa_sigaction)(int, siginfo_t*, void*); }; Implementations are free to define additional, possibly non-portable fields. The member specifies the address of a function to be called when the process receives the signal. The signal number is passed as an integer argument to this function. The member specifies additional signals to be blocked during the execution of signal handler. must be initialized with . The member specifies some additional flags. is an alternate signal handler with different set of parameters. Only one signal handler must be specified, either or . If it is desired to use instead of sa_handler, flag must be set. include include include include include include define NUM_CHILDREN 10 // Global variable counting number of exited child processes sig_atomic_t exited_children_count = 0; void sigchld_handler(int signo, siginfo_t *sinfo, void *context) { pid_t proc; while ((proc = waitpid(-1, NULL, WNOHANG)) > 0) { // signal main thread ++exited_children_count; // note: printf() is not signal-safe! // don't use it in a signal handler. // si_code is the full 32 bit exit code from the child (see also waitid()). printf("sinfo->si_pid = %ld\nproc = %ld\nexit code %d exit reason %d\n", (long)sinfo->si_pid, (long)proc, sinfo->si_status, sinfo->si_code); } } int main() { struct sigaction act = { .sa_sigaction = sigchld_handler, .sa_flags = SA_SIGINFO }; sigemptyset(&act.sa_mask); if (sigaction(SIGCHLD, &act, NULL) == -1) { perror("sigaction()"); return EXIT_FAILURE; } for (int i = 0; i
Text derived from https://en.wikipedia.org/w/index.php?oldid=1353624103 under CC BY-SA 4.0; modifications have been made.