Signal List
SIGABRT process stops running 6sigalrm warning Clock SIGFPE computation exception sighup system hangs Sigill illegal instruction SIGINT Terminal Interrupt 2 SIGKILL Stop process (this signal cannot be ignored or captured) Sigpipe writes data to a pipe that is not read SIGSEGV invalid memory segment access Sigqout terminal exits 3 SIGTERM terminating SIGUSR1 user-defined signal 1SIGUSR2 user-defined signal 2SIGCHLD child process has stopped or exited Sigcont If stopped then continue execution sigstop stop execution SIGTSTP terminal stop signal sigtout background Process request Write Sigttin background process request Read Operation
void (*sighandler_t) (int); sighandler_t signal (int Signum, sighandler_t handler); Signal function Role 1: Stand in the application's perspective, register a signal processing function 2: Ignore the signal, set the signal default processing signal installation and recovery parameters -signal is a function with Signum and handler two parameters, The signal ready to be captured or masked is given by the parameter Signum, and the function to be called when the specified signal is received is handler given --Handler The function must have an int type parameter (that is, the received signal code), which is of type void --handler can also be the following two special values: ①sig_ign mask the signal ②SIG_DFL restore default behavior
//Ignore, shielding signal
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<signal.h>intMainintArgChar*args[]) {pid_t PID=Fork (); if(pid==-1) {printf ("fork () failed! error message:%s\n", Strerror (errno)); return-1; } //registering a signal, masking the SIGCHLD signal, the child process exits, will not send a signal to the parent process, so there will be no zombie processsignal (sigchld,sig_ign); if(pid>0) {printf ("father is runing!\n"); Sleep (Ten); } if(pid==0) {printf ("I am child!\n"); Exit (0); } printf ("Game over!\n"); return 0;}
//Recovery signal#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<signal.h>voidCatch_signal (intSign ) { Switch(sign) { Casesigint:printf ("Ctrl + C was executed!\n"); //exit (0); Break; }}intMainintArgChar*args[]) { //Register terminal interrupt signalsignal (SIGINT, catch_signal); CharTEMPC =0; while((TEMPC = GetChar ())! ='a') {printf ("tempc=%d\n", TEMPC); //sleep () } //Restore Signalsignal (SIGINT, SIG_DFL); while(1) {pause (); } printf ("Game over!\n"); return 0;}
//The return value of the signal () function#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<signal.h>voidCatch_signal (intSign ) { Switch(sign) { Casesigint:printf ("Ctrl + C was executed!\n"); //exit (0); Break; }}intMainintArgChar*args[]) { /** The return value of the signal () function is the last behavior of the signal () function **/typedefvoid(*sighandler_t) (int); //because the first registration signal is SIGINT, the last behavior is the default behaviorsighandler_t old=signal (SIGINT, catch_signal); if(old==Sig_err) { //failed to register signalPerror ("Signal Error:"); } /*formal wording*/ if(Signal (sigquit,catch_signal) = =Sig_err) { //failed to register new numberPerror ("Signal Error:"); } CharTEMPC =0; while((TEMPC = GetChar ())! ='a') {printf ("tempc=%d\n", TEMPC); //sleep () } //to re-register the default behavior is not to restore the default signalsignal (SIGINT, old); while(1) {pause (); } printf ("Game over!\n"); return 0;}
Linux Signal Explanation