- 论坛徽章:
- 95
|
缓冲导致的问题:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <signal.h>
- void handler(int signo)
- {
- switch(signo)
- {
- case SIGUSR1:
- fprintf(stderr, "parent: catch SIGUSR1\n");
- break;
- case SIGUSR2:
- fprintf(stderr, "child: catch SIGUSR2\n");
- break;
- default:
- printf("never goes here\n");
- break;
- }
- return;
- }
- int main()
- {
- pid_t ppid,cpid;
-
- if(SIG_ERR == signal(SIGUSR1, handler))
- {
- perror("fail to set handler for SIGUSR1");
- exit(1);
- }
- if(SIG_ERR == signal(SIGUSR2, handler))
- {
- perror("fail to set handler for SIGUSR2");
- exit(1);
- }
-
- ppid = getpid();
-
- if((cpid = fork()) < 0)
- {
- perror("fail to fork");
- exit(1);
- }
- else if(0 == cpid)
- {
- printf("I'm child\n");
- if(-1 == kill(ppid, SIGUSR1))
- {
- perror("fail to send SIGUSR1");
- exit(1);
- }
- //while(1);
- sleep(3);
- exit(0);
- }
- else
- {
- sleep(1);
-
- if(-1 == kill(cpid, SIGUSR2))
- {
- perror("fail to send SIGUSR2");
- exit(1);
- }
- printf("now kill the child...\n");
- if(-1 == kill(cpid, SIGKILL))
- {
- perror("fail to kill child");
- exit(1);
- }
-
- if(-1 == wait(NULL))
- {
- perror("fail to wait");
- exit(1);
- }
- }
- return 0;
- }
复制代码 |
|