View signal
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ kill -l
0-31: Standard signal
32-64: Real-time signal
NAME
signal - ANSI C signal handling
SYNOPSIS
#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
==>
void (*signal(int signum, void (*func)(int)))(int)
signal()将信号符号的配置设置为handler, handler可以是SIG_IGN、SIG_DFL,也可以是程序员定义的函数(“信号处理器”)的地址。
SIG_IGN:向内核表示忽略此信号
SIG_DFL:向内核表示接到此信息的动作是系统默认动作
当指定函数地址的时候,则在信号发生时,调用该函数,即调用信号处理函数。
The signal SIGINT refers to the program termination (interrupt) signal, which is sent when the user types the INTR character (usually Ctrl-C) to notify the foreground process group to terminate the process.
Experiment 1: Ignore the SIG_IGN signal
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
int main()
{
int i;
signal(SIGINT,SIG_IGN);
for(i=0 ; i<12 ; i++)
{
write(1,"*",1);
sleep(1);
}
exit(0);
}
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ gcc test.c
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ ./a.out
***^C*^C^C^C*^C^C***^C^C^C**^C^C*^C^C^C*mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$
You can see that the program will not be affected by Ctrl-C while running
Experiment 2: Responding to the SIG_IGN signal
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void sig_handler(int s)
{
write(1,"!",1);
}
int main()
{
int i;
signal(SIGINT,sig_handler);
for(i=0 ; i<12 ; i++)
{
write(1,"*",1);
sleep(1);
}
exit(0);
}
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ gcc test.c
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ ./a.out
*^C!*^C!*^C!*^C!*^C!*^C!*^C!*^C!*^C!**^C!*^C!mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ ^C
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$
Signals will interrupt blocked system calls
Experiment 3: Signals will interrupt blocked system calls
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void sig_handler(int s)
{
write(1,"!",1);
}
int main()
{
int i;
signal(SIGINT,sig_handler);
for(i=0 ; i<12 ; i++)
{
write(1,"*",1);
sleep(1);
}
exit(0);
}
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ ./a.out
*^C!*^C!*^C!*^C!*^C!*^C!*^C!*^C!*^C!*^C!*^C!*^C!mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ ^C
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ ^C
mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal$ ^C
If you keep pressing Ctrl-C during execution, the program will end quickly, taking less than 15 seconds.
This is because the signal will interrupt the blocking system call.
Such as: also explained in open()
EINTR While blocked waiting to complete an open of a slow device (e.g., a FIFO; see fifo(7)), the call was interrupted by a signal handler; see signal(7).
When blocking and waiting to open a slower device, this call may be interrupted by a signal.
If a device is being opened, the device may be slow, or the device is busy, and open() will take a while to open it, that is, open() will block. At this time, if there is a signal, the blocking will be interrupted. System call.
As explained in read()
EINTR The call was interrupted by a signal before any data was read; see signal(7)
Before I read anything, if there is a signal, it will be interrupted. That is, the signal will interrupt the blocking system call.
Therefore, the previous programs, such as open(), read(), need to add blocking error judgments:
The system call will not be interrupted during execution, but will wait, and will be interrupted during blocking. If waiting for opening, waiting for reading.