|
- #include <sys/types.h>
- #include <signal.h>
- #include <unistd.h>
- #include <stdio.h>
- static int alarm_fired = 0;
- void ding(int sig)
- {
- alarm_fired = 1;
- }
- int main(void)
- {
- pid_t pid;
- printf("alarm application starting\n");
- pid = fork();
- switch(pid)
- {
- case -1:
- perror("fork failed\n");
- exit(1);
- case 0:
- sleep(1);
- kill(getppid(), SIGALRM);
- exit(0);
- }
- printf("waiting for alarm to go off\n");
- pause();
- printf("Hello World\n");
- if (alarm_fired) printf("Ding!\n");
- printf("done\n");
- return 0;
- }
复制代码
printf("Hello World\n");
if (alarm_fired) printf("Ding!\n");
printf("done\n");
为什么不运行?
运行结果
- [root@book cai]# ./y
- alarm application starting
- waiting for alarm to go off
- 闹钟
复制代码 |
|