/* sigdemo3.c
* 目的:回答以下问题
* 问题1:信号到达后,处理程序是否仍然有效?
* 问题2:如果SignalX在处理signalX时到达,该怎么办?
* 问题3:如果在处理signal Y时signal X到达该怎么办?
* 问题4:当信号到达时read()会发生什么?
*/
#include <stdio.h>
#include <signal.h>
#define INPUTLEN 100
main(int ac,char *av[])
{
void inthandler(int);
void quithandler(int);
char input[INPUTLEN];
int nchars;
signal(SIGINT,inthandler);
signal(SIGQUIT,quithandler);
do{
printf("
Type a message
");
//从标准输入(stdin)中读取(INPUTLEN-1)个字符
nchars=read(0,input,(INPUTLEN-1));
if(nchars==-1) perror("read returned an error");
else {
input[nchars]=' ';
printf("You typed:%s",input);
}
}
while(strncmp(input,"quit",4)!=0);//strncpy()返回值为0证明两字符相等
}
void inthandler(int s)
{
printf("Received signal%d...waiting
",s);
sleep(2);
printf("Leaving inthandler
");
}
void quithandler(int s)
{
printf("Received signal%d...waiting
",s);
sleep(3);
printf("Leaving quithandler
");
}