1. Signals are soft interrupts that provide a mechanism for handling asynchronous events
An asynchronous event can originate from outside the system (for example, user input ctrl-c) or from within the system (for example, except 0) the kernel uses one of the following three methods to process the signal: (1)Ignore this signal。Sigkill and Sigstop cannot be ignored。 (2)capture and process the signal。 The kernel would suspend execution of the process ' s current code path and jump to a previously registered function. Sigkill and Sigstop cannot be captured (2)perform the default action。
SIGCHLD: When the process terminates, the kernel sends a SIGCHLD signal to its parent process, which, by default, is ignored if the parent process requires the child process to terminate the information and needs to be explicitly processed, usually calling the wait function SIGINT: User input interrupt character Ctrl-csigkill, Sigstop:kill system call signal, cannot be ignored, cannot be captured, the result always terminates the process SIGSEGV: Segment error 2. Basic Signal Management
#include <signal.h>void (*sighandler_t) (int); sighandler_t Signal ( int Signo, sighandler_t handler);
Signal () removes the current action taken on receipt of the signal signo and instead handles the signal with the Signal handler specified by handler
#include <stdlib.h>#include<stdio.h>#include<unistd.h>#include<signal.h>/*handler for SIGINT and SIGTERM*/Static voidSignal_handler (intSigno) { if(Signo = =SIGINT) printf ("caught sigint!\n"); Else if(Signo = =SIGTERM) printf ("caught sigterm!\n"); Else { /*This should never happen*/fprintf (stderr,"unexpected signal!\n"); Exit (Exit_failure); } exit (exit_success);}intMain (void){ /** Register Signal_handler as our signal handler * for SIGINT. */ if(Signal (SIGINT, signal_handler) = =Sig_err) {fprintf (stderr,"cannot handle sigint!\n"); Exit (Exit_failure); } /** Register Signal_handler as our signal handler * for SIGTERM. */ if(Signal (SIGTERM, signal_handler) = =Sig_err) {fprintf (stderr,"cannot handle sigterm!\n"); Exit (Exit_failure); } /*Reset sigprof ' s behavior to the default.*/ if(Signal (sigprof, SIG_DFL) = =Sig_err) {fprintf (stderr,"cannot reset sigprof!\n"); Exit (Exit_failure); } /*Ignore SIGHUP.*/ if(Signal (SIGHUP, sig_ign) = =Sig_err) {fprintf (stderr,"cannot ignore sighup!\n"); Exit (Exit_failure); } for (;;) Pause (); return 0;}
#include <signal.h>intSigaction (intSigno,Const structSigaction *act,structSigaction *oldact);structsigaction {void(*sa_handler) (int);/*signal handler or action*/ void(*sa_sigaction) (int, siginfo_t *,void*); sigset_t Sa_mask; /*signals to block*/ intSa_flags;/*Flags*/ void(*sa_restorer) (void);/*Obsolete and Non-posix*/};
Sigaction () Changes the behavior of the signal identified by Signo, Signo cannot be sigkill and Sigstopif Act is not NULL, the system Call changes the current behavior of the signal as specified by actInheritance of signaling behavior: 3. Send the signal:
int= Kill (1722, SIGHUP); if (ret) perror ("kill");
The code above indicates that sending a sighup signal to a process with a PID of 1722 is equivalent to the following shell statement:
Kill 1722
/* */<signal.h>int raise (int signo);
Raise (Signo);
Equivalent to:
Kill (Getpid (), signo);
4. Can be re-entered a reentrant function is a function which is the safe to call from within itself (or concurrently, from another the thread in The same process). In order to ensure reentrant, the function
cannot manipulate static variable, you can only manipulate stack-allocated data, and you cannot call a non-reentrant function