can achieve ....
Signal, this function is relatively simple, given a signal, the signal processing function can be, of course, the function is simple, its function is relatively simple many, simply give a function example as follows:
[CPP] View plain copy 1 #include <signal.h> 2 #include <stdio.h> 3 #include <unistd.h> 4 5 void ouch (int sig) 6 { 7 printf ("i got signal %d\n", sig); 8 // (void) signal (SIGINT,&NBSP;SIG_DFL); 9 //(void) Signal (Sigint, ouch); 10 11 } 12 13 14 15 int main () 16 { 17 (void) signal (sigint, ouch); 18 19 while (1) 20 { 21 printf ("hello world...\n"); 22 sleep (1); 23 } 24 } of course, in practical use, You need different to the signal set to signal processing functions, sig_ign ignore/SIG_DFL default, these two macros can also be used as signal processing functions. At the same time Sigstop/sigkill these two signals can not be captured and ignored. Note that the signal function also blocks the signal that is currently being processed, but there is no way to block other signal, such as processing Sig_int, and then a sig_int will be blocked, but the sig_quit will be interrupted if Sig_ Quit have processing, then need to wait sig_quit processing finished, Sig_int will then just deal with.
Sigaction, this relatively troublesome, functional prototype is as follows:
int sigaction (int sig, const struct sigaction *act, struct sigaction);
The key to the function is struct sigaction[CPP]View Plain copy stuct sigaction {void (*) (int) Sa_handle; sigset_t Sa_mask; int sa_flags; }
[CPP] View plain copy 1 #include <signal.h> 2 #include <stdio.h> 3 #include <unistd.h> 4 5 6 void ouch (int sig) 7 { 8 printf ("oh, got a signal %d\n ", sig); 9 10 int i = 0; 11 for (i = 0; i < 5; i++) 12 { 13 printf ("signal func %d\n", i); 14 sleep (1); 15 } &nBsp 16 } 17 18 19 int main () 20 { 21 struct sigaction act; 22 act.sa_handler = ouch; 23 sigemptyset (&act.sa_mask); 24 Sigaddset (&act.sa_mask, sigquit); 25 // act.sa_ flags = sa_resethand;