- 论坛徽章:
- 0
|
回复 1# zouyp1989
我不知道楼主用的什么Linux,我用的CentOS 6.5,在稍微新一点的Linux系统中(其实主要是gnu libc的版本),signal其实和sigaction调用的是同一个系统调用。所以,效果是一样的,只sigatciont多一些控制而已。我测试的结果是不会出现楼主的情况。下面是我的测试代码,程序里面有个计数,会在重复收到SIGINT 6次以上才会退出(CentOS6.5 测试通过):- #include <signal.h>
- #include <stdio.h>
- #include <unistd.h>
- volatile sig_atomic_t stop_flag = 0;
- volatile sig_atomic_t int_count = 0;
- void handler_sigint(int sig)
- {
- char msg[] = "I got signal SIGINT\n";
- write(STDERR_FILENO, msg, sizeof(msg));
- if (++int_count > 5) {
- stop_flag = 1;
- }
- }
- int main()
- {
- signal(SIGINT, handler_sigint);
- while (!stop_flag) {
- printf(".\n");
- sleep(1);
- }
- printf("Done!\n");
- return 0;
- }
复制代码 |
|