What is the SA_SIGINFO flag in the sigaction structure, and how does it modify the behavior of signal handling? Provide an example that demonstrates using SA_SIGINFO to handle the SIGINT signal and retrieve extended information about the signal.
The SA_SIGINFO flag provides more detailed information about the process such as the signal sending process id, signal number, and additional data. It modifies the behavior in the sense that the user gets to know all the detailed information about the handler.
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> void handle_signal(int sig, siginfo_t *info, void *context) { printf("Signal number: %d\n", info->si_signo); printf("Caught signal %d from process %d\n", sig, info->si_pid); printf("Signal code: %d\n", info->si_code); printf("Caught signal %d from process %d\n", sig, getpid()); } int main() { struct sigaction sa; sa.sa_sigaction = handle_signal; sa.sa_flags = SA_SIGINFO; // Use sa_sigaction instead of sa_handler sigemptyset(&sa.sa_mask); // Set up the signal handler for SIGINT if (sigaction(SIGINT, &sa, NULL) == -1) { perror("sigaction"); exit(EXIT_FAILURE); } // Loop indefinitely while (1) { printf("Running...\n"); sleep(1); } return 0; }