Function prototypes
NAME - ANSI C signal handlingsynopsis <signal.h> void (*sighandler_t) (int ); sighandler_t signal (int Signum, sighandler_t handler);
The signal function has a registration function and nothing is done. Just tell the system, when the signal Signum, according to Handler way to deal with. This function is called only when the signal is coming. The signal does not come and will never call this function.
The user can request the kernel to generate a signal by entering CTRL + C, ctrl+\, or any other key assigned to the signal control character by the terminal driver.
CTRL + C--2) SIGINT
CTRL + \-3) Sigquit
Both of these can let the program exit.
Code
/************************************************************************* > File name:hello.c > Author:kr Ischou > Mail:[email protected] > Created Time:mon-09:25:15 AM CST ***************************** *******************************************/#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/stat.h>#include<sys/types.h>#include<fcntl.h>#include<assert.h>#include<signal.h>#include<sys/Select.h>voidHandlerintnum) {printf ("Sig_num:%d \ n", num);}intMainintargcChar*argv[]) { Charbuf[1024x768]; Signal (2, Handler); intIret; Fd_set Read_set, Ready_set; Fd_zero (&read_set); Fd_set (0, &read_set); while(1) {Ready_set=Read_set; Iret=Select(1, &ready_set, NULL, NULL, NULL); if(Iret = =0) { Continue ; }Else if(Iret = =-1) {perror ("Select"); Continue ; }Else{Iret= Read (0, BUF,1024x768); Buf[iret]=' /'; Write (1, buf, strlen (BUF)); } }
After the program runs, press CTRL + C and the results are as follows:
[email protected] review]$ gcc test.c[[email protected] review]$. /a. Out2Select: Interrupted system Callhelloworld! Helloworld!
Since SIGINT has been registered through signal (2, handler), it is clear that the signal processing function handler naturally captures the signal when CTRL + C is pressed, so it will show ^ Csig_num: 2 .
The Select function handles the interrupt signal. When the interrupt signal is received, it returns-1. Therefore, select: Interrupted system call Select is displayed.
Note: The Select function returns in 3, where the polling time is returned to the point. The received signal will return. Describes the have activity that is returned.
(Incidentally, read is a blocking function that processes the signal before it is received, and then continues to wait.) )
After that, the program is clearly running. There is input, select returns 1, what you enter, and what you print. Press Ctrl+d,select to return 0, the program continues to run.
Linux Signal Processing 1