Implementing Signal Handlers

The sigaction system call is used to install a new handler function.

#include<signal.h> #include<stdio.h>

struct sigaction sa; int count;

/* Initialize the signal handler structure */ sa.sa_handler = handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0;

/* Assign a new handler function to the SIGTERM signal */ sigaction(SIGTERM, &sa, NULL);

sigprocmask(&sa.sa_mask); /* Accept all signals */ /* Block and wait until a signal arrives */ while (1) {

sigsuspend(&sa.sa_mask); printf("loop\n");

return 0;

If no user-specific handler function is assigned to a signal, the kernel automatically installs predefined functions to provide reasonable standard operations and to deal with the specific situation.

The definition of the field of type sigaction used to describe the new handler is platform-specific but has practically the same contents on all architectures.

Continue reading here: Info

Was this article helpful?

0 0