C library functions -sigaction()
C Standard Library - <signal.h>
sigactionThe function is a C standard library function used to check or modify the handling action associated with a specific signal. WithsignalCompared with the function,sigactionProvides more control and better portability.
Syntax
int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact);
Parameter
int sig: The signal number to inspect or modify. Common signals includeSIGINT、SIGTERM、SIGKILLetc.const struct sigaction *act: A pointer to the new signal handling action. If it isNULL, the current signal handling action is not modified.struct sigaction *oldact: A pointer to save the old signal handling action. If it isNULL, the old signal handling action is not saved.
struct sigactionStructure
struct sigaction {
void (*sa_handler)(int); // 信号处理函数指针
void (*sa_sigaction)(int, siginfo_t *, void *); // 信号处理函数指针(扩展)
sigset_t sa_mask; // 在处理该信号时要阻塞的信号集
int sa_flags; // 修改信号行为的选项
void (*sa_restorer)(void); // 恢复函数,已废弃
};
sa_flagscommon options
SA_RESTART: Automatically restart system calls interrupted by the signal.SA_SIGINFO: Usesa_sigactioninstead ofsa_handleras a signal handling function.SA_NOCLDSTOP: ifsigYesSIGCHLD, the signal is not generated when a child process stops or continues.
Return Value
- Returns 0 on success.
- On failure, returns -1 and sets
errnoto indicate the type of error.
Example
The following is a simple example program demonstrating how to usesigactionFunction settingsSIGINTSignal handler:
Example
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
// Custom signal handling function
void handle_sigint(int sig) {
printf("Caught signal %d\n", sig);
}
int main() {
struct sigaction sa;
// Set the handler to handle_sigint
sa.sa_handler = handle_sigint;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
// Use sigaction to set the new signal handling action
if (sigaction(SIGINT, &sa, NULL) == -1) {
perror("sigaction");
return 1;
}
// Main loop, keeps running until a signal is caught
while (1) {
printf("Running...\n");
sleep(1);
}
return 0;
}
#include <signal.h>
#include <unistd.h>
// Custom signal handling function
void handle_sigint(int sig) {
printf("Caught signal %d\n", sig);
}
int main() {
struct sigaction sa;
// Set the handler to handle_sigint
sa.sa_handler = handle_sigint;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
// Use sigaction to set the new signal handling action
if (sigaction(SIGINT, &sa, NULL) == -1) {
perror("sigaction");
return 1;
}
// Main loop, keeps running until a signal is caught
while (1) {
printf("Running...\n");
sleep(1);
}
return 0;
}
After compiling and running the program, the output will be:
Running... Running... Running... ...
When Ctrl+C is pressed (sendingSIGINTsignal), the output will be:
Caught signal 2
The program caughtSIGINTsignal and execute a custom signal handling functionhandle_sigint, and then continue running the main loop.
Notes
sigactionThe function provides more thansignalThe function provides richer functionality, allowing you to set signal handlers, signal masks, and signal handling options.- The signal handling function should be as short as possible, because other signals are blocked while handling a signal. If the signal handling function runs for too long, it may affect the real-time performance of the program.
- Certain signals (such as
SIGKILLandSIGSTOP) cannot be caught or ignored.