当前位置: 技术问答>linux和unix
求大神分析一段Linux进程的小代码
来源: 互联网 发布时间:2017-05-02
本文导语: 看第一次输入 ,感觉定时器已经打破了pause函数造成的挂起状态,然而为啥一下子输出 “Would you want to quit? alarm!alarm! //① Would you want to quit?” 然后五秒后又出现定时信号的处理信息:alarm!alarm! ①处的,那...
看第一次输入
,感觉定时器已经打破了pause函数造成的挂起状态,然而为啥一下子输出
“Would you want to quit? alarm!alarm! //①
Would you want to quit?”
然后五秒后又出现定时信号的处理信息:alarm!alarm!
①处的,那个getchar(),根本没有接受字符啊!! 而且为何那三句是同时出现!!!
#include
#include
#include
#include
#include
void handle(int signal)
{
switch(signal)
{
case SIGALRM:
printf("alarm! alarm! aaaaaaan");break;
case SIGABRT:
printf("inabnormally quit!n");break;
case SIGINT:
printf("Catch Signal : %dn",signal);break;
}
}
int main()
{
printf("Current process ID is: %dn",getpid());
signal(SIGINT,handle);
signal(SIGALRM,handle);
signal(SIGABRT,handle);
int time=5;
while(1)
{
alarm(time);
printf("would want to quit ?");
if(getchar()=='y') abort();
else pause();
};
return 0;
}
,感觉定时器已经打破了pause函数造成的挂起状态,然而为啥一下子输出
“Would you want to quit? alarm!alarm! //①
Would you want to quit?”
然后五秒后又出现定时信号的处理信息:alarm!alarm!
①处的,那个getchar(),根本没有接受字符啊!! 而且为何那三句是同时出现!!!
#include
#include
#include
#include
#include
void handle(int signal)
{
switch(signal)
{
case SIGALRM:
printf("alarm! alarm! aaaaaaan");break;
case SIGABRT:
printf("inabnormally quit!n");break;
case SIGINT:
printf("Catch Signal : %dn",signal);break;
}
}
int main()
{
printf("Current process ID is: %dn",getpid());
signal(SIGINT,handle);
signal(SIGALRM,handle);
signal(SIGABRT,handle);
int time=5;
while(1)
{
alarm(time);
printf("would want to quit ?");
if(getchar()=='y') abort();
else pause();
};
return 0;
}
|
明确两点:
1.标准输出默认是有缓冲的;
2.信号处理函数要调用异步信号安全函数(Async-signal-safe functions),哪些函数是async-signal-safe的,可参考man 7 singal里的描述。
1.标准输出默认是有缓冲的;
2.信号处理函数要调用异步信号安全函数(Async-signal-safe functions),哪些函数是async-signal-safe的,可参考man 7 singal里的描述。