当使用sigaction函数时,
不能使用getchar()阻塞进程,因为使用 `sigaction` 捕获信号后 `getchar()` 将从stdin流中读取到EOF,也就是返回了-1,所以进程会直接向下执行而终止。但是 `signal` 中则会一直阻塞在 `getchar()` 。
用sigaction只能捕捉一次SIGALRM的原因在于getchar被中断;
打印getchar的返回值为-1,perror打印其错误信息:Interrupted system call,对应的错误码为EINTR
在man文档第7章
man 7 signal
Interruption of system calls and library functions by signal handlers
If a signal handler is invoked while a system call or library function call is blocked, then either:
* the call is automatically restarted after the signal handler returns; or
* the call fails with the error EINTR.
sigaction中的sa_flags未设置SA_RESTART所以会是第二种行为,系统调用被中断,下面有讲到read从终端读取数据是会被signal handler中断,getchar底层应该调用的是read。
如果在sa_flags中使用了SA_RESTART会导致系统调用重启,getchat不会被中断,这样程序不会结束,sigaction就可以一直捕捉SIGALRM。