Consider the use of the pause function:
1#include <stdio.h>2#include <signal.h>3#include <unistd.h>4 voidSignhand (intSigno)5 {6 inti;7 for(i=0;i<Ten; i++)8 {9printf"signhand number is%d\n", i);TenSleep1); One } A } - intMain () - { the inti; - signal (sigalrm,signhand); -Alarm3); - pause (); Process hangs + for(i=0;i<Ten; i++) - { +printf"main number is%d\n", i); ASleep1); at } - return 0; -}
Program running results: (in fact, the program is executed to pause after waiting for the signal to trigger, after the signal triggered, the execution of signal function, after the execution of the signal function, return to the main function to continue execution!) )
Signhand number is 0Signhand Number is 1Signhand Number is 2Signhand Number is 3Signhand Number is 4Signhand Number is 5Signhand Number is 6Signhand Number is 7Signhand Number is 8Signhand Number is 9Main number is 0Main number is 1Main number is 2Main number is 3Main number is 4Main number is 5Main number is 6Main number is 7Main number is 8Main number is 9
Look at a cow x program: We set the 10s, the process sends itself a SIGALRM signal
1#include <stdio.h>2#include <signal.h>3#include <unistd.h>4 voidSignhand (intSigno)5 {6 inti;7 for(i=0;i<5; i++)8 {9printf"signhand number is%d\n", i);TenSleep1); One } A } - intMain () - { the inti; - signal (sigalrm,signhand); -AlarmTen); - for(i=0;i<5; i++) + { -printf"main number is%d\n", i); +Sleep1); A } at return 0; -}
The results of this program are as follows:
1 is 0 2 is 1 3 is 2 4 is 3 5 is 4
You can see that the function does not trigger the function of the signal, in the main function of the operation of the operation of no signal at all!
Process suspend function Pause