This section focuses on learning signals and signal-related processing functions, which are also updated later.
http://blog.csdn.net/xiaoliangsky/article/details/40264151
A signal
The signal is an event that occurs when UNIX and Linux systems respond to certain conditions, and the process that receives the signal takes some action accordingly. Usually the signal is generated by an error. However, they can also be used as a way to communicate or modify behavior between processes, explicitly sent to another process by one process. The generation of a signal is called generate, which receives a signal called capture.
Types of two signals
Signal Description
The SIGABRT is generated by calling the Abort function, and the process is not properly exited
SIGALRM Timer Timeout or Setitimer function set with alarm function interval Timer timeout
Sigbus a particular hardware exception, usually caused by memory access
Sigcancel is used internally by the Solaris Thread Library and is typically not used
When the SIGCHLD process is terminate or stop, SIGCHLD sends it to its parent process. By default, the signal is ignored
Sigcont automatically sends when the stop process resumes running
SIGEMT and implementation-related hardware exceptions
SIGFPE Math-related anomalies, such as being removed by 0, floating-point overflow, etc.
Sigfreeze Solaris dedicated, hiberate or suspended when sent
SIGHUP sent to controlling Process with terminal when terminal was disconnect
Sigill Illegal Instruction exception
Siginfo BSD Signal. Generated by status key, usually ctrl+t. Processes that are sent to all foreground group
SIGINT is generated by interrupt key, usually CTRL + C or delete. Processes that are sent to all foreground group
SIGIO Asynchronous IO Events
Sigiot implements the related hardware exception, the general correspondence SIGABRT
SIGKILL cannot be processed and ignored. Abort a process
SIGLWP used internally by the Solaris Thread Libray
Sigpipe sent when the pipe was written after reader abort.
Sigpoll sent when an event is sent to pollable device
Sigprof Setitimer The specified profiling Interval timer is generated
SIGPWR and system related. and UPS-related.
Sigquit input to quit key (ctrl+/) sent to all foreground group processes
SIGSEGV Illegal Memory access
Sigstkflt Linux dedicated, math coprocessor stack exception
SIGSTOP abort the process. cannot be processed and ignored.
Sigsys Illegal system calls
SIGTERM Request abort process, kill command is sent by default
Sigthaw Solaris dedicated, sent from suspend recovery time
SIGTRAP implements a related hardware exception. debugging exceptions are generally
SIGTSTP Suspend Key, usually is CTRL + Z. Processes that are sent to all foreground group
Sigttin sent when background group's process attempts to read terminal
Sigttou sent when background group's process attempts to write terminal
Sigurg may be sent when Out-of-band data is received
SIGUSR1 user-defined signal 1
SIGUSR2 user-defined signal 2
SIGVTALRM Setitimer function set by virtual Interval timer time-out
Sigwaiting Solaris Thread Library internal Implementation-specific
Sigwinch all processes sent to foreground group when Terminal's window size changes
SIGXCPU when the CPU time limit expires
Sigxfsz process exceeds file size limit
Sigxres Solaris dedicated, sending when process exceeds resource limit
Three-signal-related functions
1 signal function
void (*signal (int sig, Void (*FUNC) (int)))) (int);
Signal is a function with the sig and Func two parameters, and Func is a function pointer of type void (*) (int). The function returns a pointer of the same type as func, pointing to a function pointer that previously specified the signal handler function. The parameters for the signal to be captured are given by the sig, and the function to be called after the specified signal is received is given by the parameter func. In fact, the use of this function is quite simple, through the following example can be known. Note that the prototype of the signal processing function must be void func (int), or the following special value:
Sig_ign: Ignore Signal
SIG_DFL: Default behavior for recovering signals
This function can often be replaced with the Sigaction function below.
2 sigaction
int sigaction (int sig, const struct sigaction *act, struct sigaction *oact);
The function, like the signal function, is used to set the action associated with the signal sig, and if the oact is not a null pointer, it is used to preserve the position of the original action on the Signal, and act is used to set the action of the specified signal.
The parameter structure body sigaction is 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);
};
Signal SET SIGSET_T structure:
typedef struct
{
unsigned long sig[_nsig_words];
}sigset_t;
Signal processing functions can take either void (*sa_handler) (int) or void (*sa_sigaction) (int, siginfo_t *, void *). In the end, which depends on whether the sa_siginfo bit is set in Sa_flags, if it is set to use void (*sa_sigaction) (int, siginfo_t *, void *), you can send additional information to the handler function. By default, void (*sa_handler) (int) is used, at which time only the numeric value of the signal can be sent to the processing function.
Sa_handler This parameter is the same as the signal () parameter handler, which represents the new signal processing function, other meanings refer to signal ().
The sa_mask is used to set aside the signal set specified by Sa_mask temporarily when processing the signal.
Sa_restorer This parameter is not used.
Sa_flags is used to set up other related operations for signal processing, the following values are available:
Sa_flags can also set other flags:
Sa_resethand resets the signal processing function to the default value when the signal handler function is called SIG_DFL
Sa_restart if the signal interrupts a system call to the process, the system automatically starts the system call
Sa_nodefer generally, when the signal processing function is running, the kernel will block the given signal. However, if the sa_nodefer tag is set, the kernel will not block the signal when the signal handler is running.
return value
Successful execution returns 0, or 1 if there is an error.
Error code
EINVAL parameter Signum illegal or attempt to intercept sigkill/sigstopsigkill signal
Efault parameter act,oldact pointer address cannot be accessed.
Eintr This call is interrupted
In the following simple example, the Sigemptyset, kill function inside the program will be followed
ACTION.C Instance Code:
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <signal.h>void Sig_handler (int sig) {switch (SIG) {case 23:printf ("Child:the Signo is", hehe\n "); Return Case 22:printf ("Father:hello wordl 22!\n"); Return }}int Main () {struct Sigaction Act, oact; int status; Sigemptyset (&act.sa_mask); act.sa_flags = 0; Act.sa_handler = Sig_handler; Sigaction (23°c, &act, &oact); Sigaction (&act, &oact); pid_t PID, Ppid; if (! ( PID = fork ())) {printf ("Child begin\n"); Kill (Getppid (), 22); Sleep (3); printf ("Child over\n"); } else {printf ("Father begin\n"); Kill (Getpid (), 23); Wait (&status); if (wifsignaled (status)) {printf ("Child process receive siganl%d\n", Wtermsig (status)); } printf ("Father over\n"); } return 0;}
3 Sigaddset Sigdelset
int Sigaddset (sigset_t *set, int signum);
Sigaddset () is used to add the signal represented by the parameter signum to the set of parameter set signals.
return value
Successful execution returns 0, or 1 if there is an error.
Error code
Efault parameter set pointer address cannot be accessed
EINVAL parameter Signum non-legal signal number
int Sigdelset (sigset_t *set, int signum)
Sigdelset () is used to remove the signal represented by the parameter Signum from the parameter set signal set.
return value
Successful execution returns 0
Returns-1 if there is an error.
Error code
Efault parameter set pointer address cannot be accessed
EINVAL parameter Signum non-legal signal number
4 Sigemptyset Sigfillset
int Sigemptyset (sigset_t *set);
The Sigemptyset () is used to initialize and empty the parameter set signal sets.
return value
Successful execution returns 0;
Failed to return-1.
Error code
Efault parameter set pointer address cannot be accessed
int Sigfillset (sigset_t * set);
The Sigfillset () is used to initialize the parameter set signal set and then add all the signals to the signal set.
return value
Successful execution returns 0;
Failed to return-1.
Error code
Efault parameter set pointer address cannot be accessed
5 Sigismember
int Sigismember (const sigset_t *set,int signum);
Sigismember () is used to test whether the signal represented by the parameter Signum has been added to the set of parameter set signals. Returns 1 if the signal is already in the signal set, otherwise 0.
return value
The signal set already has the signal returned 1;
No then returns 0;
Returns-1 if there is an error.
Error code
Efault parameter set pointer address cannot be accessed
EINVAL parameter Signum non-legal signal number
6 sigpending
int sigpending (sigset_t *set);
Sigpending () returns the set of signals that are shelved by the parameter set pointer
return value
The row succeeds returns 0,
Returns-1 if there is an error.
Error code
Efault parameter set pointer address cannot be accessed
Eintr this call is interrupted.
7 Sigprocmask
int sigprocmask (int how, const sigset_t *set, sigset_t *oldset);
The signal mask for a process specifies the set of signals that are currently blocked and cannot be transmitted to the process. Sigprocmask () can be used to detect or change the current signal shielding word, its operating parameters how to determine if the parameter oldset is not a null pointer, then the current signal mask Word will be returned by this pointer. If set is a non-null pointer, how does the parameter indicate how to modify the current signal mask word. Each process has a set of signals to describe which signals will be blocked when they are delivered to the process, and all signals in that set will be blocked after they are transmitted to the process.
Parameter how:
Sig_block: The process of the new signal screen word is its current signal mask and set point to set the set of the assembly, set contains the signal we want to block the attachment.
Sig_unblock: The process new signal set screen word is the intersection of its current signal mask and set's complement of signal sets. The set contains the signals we want to unblock.
Sig_setmask: The process new signal mask is the value that the set points to.
Attention:
1) In addition to Sig_setmask, if set is a null pointer, it does not change the signal screen word of the process, and the value of how is meaningless.
2) The Sig_setmask is used in conjunction with the set NULL pointer, which clears all shielded signals.
return value:
Successful execution returns 0;
Failed to return-1.
Error code
Efault: Parameter set, Oldsset pointer address cannot be obtained
EINTR: This call is interrupted
Here is a test example that tests the masked signal:
Example code for SIGPROCMASK.C:
http://blog.csdn.net/xiaoliangsky/article/details/40264151
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <stdlib.h> #include <signal.h>void sig_handler (int signum) {switch (Signum) {Case sigint:printf (" The Signo is SIGINT%d received, hehe\n ", SIGINT); Return Case sigchld:printf ("The Signo is SIGCHLD%d received, hihi!\n", SIGCHLD); Return;case sigusr1:printf ("The Signo is SIGUSR1%d received, hihi!\n", SIGUSR1); Return;case sigio:printf ("The Signo is SIGIO%d received, hihi!\n", SIGIO); Return;case sigalrm:printf ("The Signo is Sigalrm%d received, hihi!\n", SIGALRM); return;default:printf ("The Signo%d is not processed\n", Signum); Return }}int Main () {int i;int status;pid_t pid;sigset_t set;sigset_t oldset; struct SIGAction act;struct sigaction oldact;act.sa_flags = 0;act.sa_handler = Sig_handler;sigemptyset (&act.sa_mask); Sigaction (SIGINT, &act, &oldact); Sigaction (SIGCHLD, &act, &oldact); Sigaction (SIGUSR1, &act, &oldact); Sigaction (SIGALRM, &act, &oldact); Sigaction (SIGIO, &act, &oldact); Sigemptyset (& Set); Sigaddset (&set, SIGINT); Sigaddset (&set, SIGUSR1); if (pid = fork ()) < 0) {fprintf (stdout, "fork error\n") ); exit (-1);} else if (PID > 0) {if (Sigprocmask (Sig_block, &set, &oldset) < 0)//shield Letter Singint, sigusr1{fprintf (stdout, " Sigprocmask error\n "); Kill (PID, SIGKILL); exit (-1);} /*waitpid (PID, &status, 0); if (wifsignaled (status)) {printf ("Child process receive siganl%d\n", WT Ermsig (status)); }*/pause ();//Receive Sigio?pause ();//Receive Sigalrm?pause ();//Receive Sigchld?//pause ();//pause ();} Else{sleep (1); Kill (Getppid (), SIGINT);//signal is masked by sleep (1), Kill (Getppid (), SIGIO), sleep (1), Kill (Getppid (), SIGUSR1);// The signal is masked sleep (1); Kill (GETPPID (), SIGALRM); sleep (2);//child process exit sends a SIGCHLD signal}return 0;}
8 Sigsuspend
int sigsuspend (const sigset_t *mask);
The function Sigsuspend sets the signal mask of the process to mask, and then waits for the signal to occur and executes the signal processing function just like the pause () function. After the signal processing function is executed, the signal shielding code of the process is set to the original shielding word, then the Sigsuspend function is returned.
return value
Sigsuspend always returns-1
Here is an example of a sigsuspend:
Example code for Sigsuspend:
#include <unistd.h> #include <signal.h> #include <stdio.h>void sig_handler (int sig) { if ( sig = = SIGINT) printf ("SIGINT sig\n"); else if (sig = = Sigquit) printf ("Sigquit sig\n"); else if (sig = = SIGUSR1) printf ("SIGUSR1 sig\n"); else if (sig = = SIGUSR2) printf ("SIGUSR2 sig\n"); else printf ("SIGCHLD sig\n");} int main () { int i; sigset_t new; sigset_t old; sigset_t wait; struct Sigaction Act; Act.sa_handler = sig_handler; act.sa_flags = 0; Sigemptyset (&act.sa_mask); Sigaction (SIGINT, &act, 0); Sigaction (sigquit, &act, 0); Sigaction (SIGUSR1, &act, 0); sigaction (SIGUSR2, &act, 0); sigaction (SIGCHLD, &act , 0); Sigemptyset (&new); Sigaddset (&new, SIGINT); Sigemptyset (&wait); Sigaddset (&wait, SIGUSR1); Sigaddset (&wait, SIGUSR2); Sigprocmask (SIg_block, &new, &old); for (i=0; i < ++i) { printf ("%d \ n ", i); sleep (1); } sigsuspend (&wait); printf ("After sigsuspend\n"); printf ("yyyy\n"); if ( -1 = = Sigprocmask (Sig_setmask, &old, NULL)) {perror ("Sigprocmask"); Exit (-1); } for (i=0; i < ++i) {printf ("%d\n", I), sleep (1); } printf ("xx Xxx\n "); return 0;}
The signal of the Linux process communication