最近再看unix编程的书籍,了解下操作系统。对于sinal信号的缺点的描述有一些似懂非懂的地方,就按照例子做一个实验,看看系统如何处理。代码如下:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#define INPUTLEN 100
void inthandler(int s){
printf("received a signal %d\n",s);
sleep(2);
printf("leaving inthandler\n");
}
void quithandler(int s){
printf("received a signal %d\n",s);
sleep(3);
printf("leaving quithandler\n");
}
int main(int ac,char *av[]){
char input[INPUTLEN];
int nchars;
signal(SIGINT,inthandler);
signal(SIGQUIT,quithandler);
do{
printf("\n type a message\n");
nchars=read(0,input,(INPUTLEN-1));
if(nchars==-1)
perror("read return an error");
else{
input[nchars]='\0';
printf("you typed ,%s",input);
}
}while(strncmp(input,"quit",4)!=0);
}
测试场景(在centos7 系统下测试):
- 连续多次中断信号
通过上图的演示,在发出第一个中断信号后,捕获信号处理。在多次发出终端信号后,信号被阻塞,信号处理程序处理完成后,堵塞的相同的信号被捕获一次。
2.间隔发送中断信号和QUIT信号
发送一个中断信号后,系统捕获信号处理。在发出一个QUIT信号后,从中断信号处理的函数跳转导QUIT信号处理函数处理。
3、被中断的系统调用
当输入“hello” 后发生了中断,中断完成后输入“world n",最后只打印出 "world".看来在centos,并不是在read 时发生中断返回-1.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。