Signal communication
Production situation:
1. When the user presses certain keys, the signal is generated.
2, the hardware abnormal generation of signals, invalid storage.
3. Process kills the process with the Kill function
4. Users can use the KILL command to send signals to other processes
First, signal type
SIGHUP SIGTRAP Sigiot Sigbus sigfpt
SIGINT SIGKILL SIGUSER1 SIGSEGV SIGUSER2
Sigquit sigpipe SIGALRM SIGTERM
Sigill SIGCHLD sigcont SIGSTOP SIGIO SIGPWR
SIGTSTP sigttin Sigttou Sigurg sigprof Sigxfsz
SIGHUP: End signal issued from terminal
SIGINT: Interrupt signal from the keyboard
SIGKILL: The signal ends the receiving signal process
A signal from the Sigterm:kill command.
SIGCHLD: Indicates the end of the child process, or stops
SIGSTOP: From keyboard ctrl-z, stop signal
Second, signal processing mechanism
1. Ignore this signal
2, the implementation of user-desired action signal to, according to User settings processing
3, the execution of the system torture action is generally the default is to terminate the process
Sending of signals
Kill can either send a signal to itself or send to another process;
Raise can only send to himself
int Kill (Pid_t,int Signo)
int raise (int signo)
The PID parameter of kill four kinds of case
1. pid>0 the PID process to send the signal to the process ID
2, Pid==0 send the signal to the same group of processes
3, Pid<0 send the signal to other process ID equals PID absolute process
4, Pid==-1 believe that it is good to send to all processes
third, the use of alarm function
Alarm function can also send signals, have timed action, send to yourself!
#include <unistd.h>
unsigned int alarm (unsigned int seconds)//Send yourself a signal in the coming time
The int pause function suspends using the calling process until a signal is captured
int pause (void)//wait until the signal handler function is suspended before it ends
Iv. use of the signal () function
void (*signal (int signo,void (*FUNC) (int)))) (int) is returned as a reference to the int type.
typedef void (*sighandler_t) (int)//define a Sighander function pointer
sighander_t signal (int signum,sighand_t Handr)
The possible values for Func are:
1, sig_ign ignore this signal
2, Sig_def system default
3, signal processing function name signal (int signo,void (*FUNC) (int)) can
A function that specifies when the signal is to be executed temporarily;
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
void My_func (int sign_no) This function must have only one parameter int type
{
if (sign_no==sigint)
printf ("I have get sigint\n");
else if (sign_no==sigquit)
printf ("I have get sigquit\n");
}
int main ()
{
printf ("Waiting for signal SIGINT or sigquit \ n");
/* Register signal Processing function */
Signal (SIGINT, my_func);
Signal (Sigquit, my_func);
Pause ();
Exit (0);
}
Send signal: kill-s sigquit 2324
Signal communication between processes, type, processing mechanism notes