#include<stdio.h>
#include<signal.h>
#include<unistd.h>
void sig_handler(int signo)
{
if (signo == SIGINT)
printf("received SIGINT"); ###################################不会输出,行缓存
}
int main(void)
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
while(1)
sleep(1);
return 0;
}
原因是linux下默认的缓存方式引起的。linux默认的缓存方式如下:
屏幕、键盘和终端等交互作用设备 磁盘文件、管道等非交互作用设备
标准输出 行缓存 块缓存
标准出错 无缓存 无缓存
转载于:https://my.oschina.net/invictuslee/blog/264071