Use the Sigaction function:
The signal function is simple to use, but is not a POSIX standard, and is not implemented on all types of UNIX platforms, so its use is subject to
to a certain limit. The POSIX standard-defined signal processing interface is the Sigaction function, which is followed by an oral file and prototype as follows:
#include <signal.h>
int sigaction (int signum, const struct sigaction *act, struct sigaction *oldact);
Signum: the signal to operate.
Act: A new way to set up the signal to be processed.
Oldact: The original signal processing method.
The return value: 0 indicates success, and 1 indicates an error occurred.
The struct sigaction type is used to describe the processing of the signal, defined as follows:
struct Sigaction {
void (*sa_handler) (int);
void (*sa_sigaction) (int, siginfo_t *, void *);
sigset_t Sa_mask;
int sa_flags;
void (*sa_restorer) (void);
};
In this struct, the member Sa_handler is a function pointer, which has the same meaning as the signal handler function in the signal function.
Member Sa_sigaction is another signal processing function that has three parameters to obtain more detailed information about the signal.
When the value of the Sa_flags member contains the SA_SIGINFO flag, the system uses the Sa_sigaction function as the signal processing function, otherwise sa_handler is used as the signal processing function.
In some systems, the member Sa_handler and Sa_sigaction are placed in the Union, so do not set it at the same time. The Sa_mask member is used to specify a signal that needs to be masked during the execution of a signal processing function, especially when a signal is processed, and it itself is
The signal mask of the process is automatically placed, so this signal will not occur again during the execution of the signal processing function. The Sa_flags member is used to specify the behavior of the signal processing, which can be a bitwise OR combination of values.
Sa_restart: Automatically re-initiates the system call that is interrupted by the signal.
Sa_nocldstop: Causes the parent process not to receive a SIGCHLD signal when its child processes are paused or continue to run.
Sa_nocldwait: The parent process does not receive a SIGCHLD signal when its child process exits, and the child process does not become a zombie process if it exits.
Sa_nodefer: Invalidates the shielding of the signal, which can still be emitted during the execution of the signal processing function.
Sa_resethand: After signal processing, reset to the default processing mode.
Sa_siginfo: Use sa_sigaction members instead of Sa_handler as signal processing functions.
The Re_restorer member is a data field that has been discarded and is not used.
The following example illustrates the use of the Sigaction function with the following code:
1 #include <stdio.h>2 #include <unistd.h>3 #include <signal.h>4 #include <errno.h>56Staticvoid Sig_usr (IntSignum7{8if (Signum = =SIGUSR1)9{Ten printf ("SIGUSR1 received\n");11}12Elseif (Signum = =SIGUSR2)13{printf ("SIGUSR2 received\n");15}16Else17{printf ("Signal%d received\n", Signum);19}20}21st22int main (void)23{24Char buf[512];25IntN26structSigaction sa_usr;Sa_usr.sa_flags =0;Sa_usr.sa_handler = SIG_USR;//Signal processing functions29Sigaction (SIGUSR1, &SA_USR, NULL);Sigaction (SIGUSR2, &SA_USR, NULL);32printf ("My PID is%d\n", Getpid ());3435While1)36{37if (n = Read (Stdin_fileno, buf,511)) = =-1)38{39if (errno = =EINTR)40{printf ("Read is interrupted by signal\n");42}43}44 else45 {46 buf[n] = " \0 "; printf ( "%d bytes read:%s\n "48 }49 }50 51 return" Span style= "COLOR: #800080" >0;
In this routine, the Sigaction function is used to register the handler function for the SIGUSR1 and SIGUSR2 signals and then read the characters from the standard input.
After the program runs, first output its own PID, such as: My PID is 5904
If you send a SIGUSR1 or SIGUSR2 signal from another terminal to the process, use a command similar to the following: KILL-USR1 5904
The program will continue to output the following: SIGUSR1 received read is interrupted by signal
This means that the system call that is interrupted by the signal is not automatically re-initiated when the signal processing function is registered with Sigaction. If automatic re-initiation is required, set the SA_RESTART flag,
For example, in the above routines can be similar to the setting: Sa_usr.sa_flags = Sa_restart;
Note that the Sa_mask member of the ACT structure must be initialized with the Sigemptyset function. No guarantee: Act.sa_mask = 0; will do the same thing.
We have tried to set the SA_RESTART flag for all signals except SIGALRM, so the system calls that were interrupted by these signals can be restarted automatically. The reason why you do not want to restart a system call that is interrupted by the SIGALRM signal is that we want to set a time limit for I/O operations.
Linux sigaction function Usage explanation