Signals are generated by the shell and terminal manager due to certain error conditions
Signal can be used as a way to pass messages or modify behavior between processes, explicitly passing a process to another process
Signals can be generated, captured, responded to, or ignored.
The program can use the Signal library function to process the signal
1 #include <signal.h>2void (* signal (intvoid (* func) (int ))) (int);
Signal is a function with the sig and func two parameters
Signals prepared for capture or neglect are given by the parameter sig
The function to be called after receiving the specified signal is given by the parameter func
The signal processing function must have an int type parameter that is the receive signal code
And the return value is void
The signal function itself also returns a function of the same type, which is the function previously used to process the signal.
The function that processes the signal can be replaced by the following two values:
Sig_ign Ignore Signal
SIG_DFL Restore default behavior
Signal Processing Experiment Program:
1#include <signal.h>2#include <stdio.h>3#include <unistd.h>4 5 voidOuchintSig//define the Ouch function and process the signal when the signal is intercepted6 {7printf"ouch!-I got signal%d\n", SIG);//print a word first8(void) signal (SIGINT, SIG_DFL);//then, the general SIGINT signal generated by the CTRL + C to return to the default form of processing9 }Ten One intMain () A { -(void) signal (SIGINT, ouch);//signal processing function, the first parameter is a signal to be captured or ignored, the second parameter is the processing function - //here refers to the terminal interrupt signal, when the signal is intercepted, no signal when the time has been executed to greet the while(1) { -printf"Hello world!\n");//one second to cycle the printing of a saying hello -Sleep1); -}//when the terminal interrupt signal is pressed again, the function of terminating the program is played. +}
Execution effect:
1 [Email protected]:~/c_program/-blp3e/chapter11$./ 2 Hello world! 3 Hello world! 4 Hello world! ^couch! -I got signal 2 6 Hello World! 7 Hello world! ^c 9 [Email protected]:~/c_program/544977 -blp3e/chapter11$ 10 // The value of the signal SIGINT in the program is exactly 2
This is an old signal processing function, and the signal function returns a pointer to a signal handler function that previously processed the specified signal.
If no signal processing function is defined, return Sig_err and set errno to a positive value
If an invalid signal is given, or if the signal being tried is not a catch or a non-negligible signal
For example Sigkill signal, errno will be set to Einval
Send Signal
1 #include <sys/types.h>2 #include <signal.h>3intint sig);
This function sends a signal given by the sig to the parameter PID to give the city a well-specified process, and returns 0 on success.
To send a signal, the sending process must have the appropriate permissions.
This usually means that both processes have the same user ID
This means that the user can only send signals to their own processes, and the super-user may send a signal to any process
Returns 1 when failed and sets the errno variable
Errno is set to einval because the given signal is invalid
Errno is set to Eperm because the send process has insufficient permissions
Errno is set to Esrch because the target process does not exist
Alarm function:
1 #inlcude <unistd.h>2intint seconds);
This function is called to send a sigalrm signal after seconds seconds.
Due to the delay of processing and uncertainty of time schedule, the actual alarm time will be slightly later than the first schedule.
Parameter seconds set to 0 cancels all alarm requests that have been set
If you call the alarm function again after receiving SIGALRM, the alarm will start again
Each process can have only one alarm time, and the return value of the alarm function is the number of seconds remaining in the previously set alarm time
Failure to return-1
The program of the Alarm Clock experiment:
1#include <signal.h>2#include <stdio.h>3#include <unistd.h>4 5 Static intalarm_fired =0;6 7 voidDingintSig)8 {9alarm_fired =1;//set an integer type of 1Ten } One A intMain () - { - intpid; theprintf"Alarm Application starting\n"); - if(PID = fork ()) = =0) - { -Sleep5);//wait 5 seconds in a child process +Kill (Getppid (), SIGALRM);//sends an alarm signal to the parent process -Exit0); + } A atprintf"waiting for alarm to go off\n"); -(void) signal (SIGALRM, ding);//arranging signal processing to capture the alarm signal into the Ding function - -Pause ();//function pauses until the signal is generated and the program continues to run - if(alarm_fired) -printf"ding!\n"); in -printf"done\n"); toExit0); +}
The execution effect of the program:
1 [email protected]:~/c_program/544977-blp3e/chapter11$./Alarm2Alarm Application starting3 for alarm to go off4 ding! 5 Done 6 [email protected]:~/c_program/544977
Robust Signal Interface
1 #include <signal.h>2 int sigaction (int sig, const Span style= "color: #0000ff;" >struct sigaction * Act, struct sigaction * oact);
This function successfully returns 0 when the failure returns-1
First parameter sig to receive the signal
The second argument is a struct pointer
Sigaction This structure is defined as the action that should be taken after receiving the signal specified by the parameter sig.
This structure has at least the following three members:
Void (*) (int) sa_handler//is a function pointer to the signal handler function that will be called when the signal is received
sigset_t sa_mask//Specifies a set of signals that will be added to the signal screen word before the signal processing function is called.
int Sa_flags
The third argument is a struct pointer
If the pointer is a null pointer, Sigaction will write the previous action on the signal to the point where
If the signal is given or attempts to capture or ignore a signal that is not allowed to be captured or ignored
The error variable errno will be set to Einval
Use this function instead of the signal processing function signal Program:
1#include <signal.h>2#include <stdio.h>3#include <unistd.h>4 5 voidOuchintSig)6 {7printf"ouch!-I got signal%d\n", SIG);8 }9 Ten intMain () One { A structSigaction Act; - -Act.sa_handler =Ouch; theSigemptyset (&act.sa_mask); -Act.sa_flags =0; - -Sigaction (SIGINT, &act,0);//signal processing function, encountered this signal to go to the specified function central processing + //This function can process the SIGINT signal continuously . - while(1) { +printf"Hello world!\n"); ASleep1);//one second before I met you, hello. at } -}//terminating the program requires a sigquit signal. Provided by Ctrl+\
The effect of program execution:
1[Email protected]:~/c_program/544977-blp3e/chapter11$./CTRLC22Hello world!3Hello world!4^couch! -I got Signal25Hello world!6Hello world!7Hello world!8^couch! -I got Signal29Hello world!TenHello world! OneHello world! A^\ Exit (core dump) -[Email protected]:~/c_program/544977-blp3e/chapter11$
Signal Set
Header file signal.h defines the type sigset_t and functions used to process the signal set
Sigaction and other functions will use these signal sets to modify the behavior of the process when it receives the signal
1#include <signal.h>2 intSigaddset (sigset_t *Set,intSigno);//add a given signal from the signal set3 intSigemptyset (sigset_t *Set);//initialize the signal set to null4 intSigfillset (sigset_t *Set);//initialize the signal set to include all known signals5 intSigdelset (sigset_t *Set,intSigno);//reduce a given signal from a signal set6 //Returns 0 on successful return-1 and sets errno (set to einval when the given signal is invalid)
1 #include <signal.h>2intsetint signo); // The function is to detect a given signal when it is a member of a signal set 3 // if it is, return 1 if not, return 0 . 4 // the given signal is invalid, return 1 and set errno to Einval
#include <signal.h>int sigprocmask (intconstset, signal_t * oset); /* The signal shielding Word is set up and checked by this function. The signal screen Word is a set of signals that are currently blocked, and they cannot be received by the current process the function according to how provided by the method, set a new signal shielding word from the set to specify the old signal mask word saved to the signal set Oset How to Value and meaning: sig_block//Add the signal in the parameter set to the signal screen word sig_setmask//set the signal shielding word to the signal in the parameter set sig_unblock// Remove a signal from a signal in the set if set is a null pointer. The purpose of the function call is to save the value of the current signal-blocking word in Oset. Returns 0 if how is invalid returns 1 and sets errno to Einval */
1 #include <signal.h>2intset); // the function is to write a set of signals in the blocking signal to the signal set that the parameter set points to. 3 // Success returns 0 when the failure returns-1
1 #include <signal.h>2int sigsuspend (const sigset_t * sigmask); // The function replaces the signal screen word of the process with the signal set given by the parameter Sigmask 3 // the execution of the program is then suspended, and the program resumes execution after the signal processing function has finished executing . 4 // if the received signal aborts the program then this function will not return 5 // if the received signal does not abort the program then this function returns-1 and sets the errno to Eintr
Reference: Linux programming Neil Matthew
Time: Monday, June 29, 2015 17:09:28 CST
The first lesson in signal learning--basics