- 论坛徽章:
- 0
|
信号问题?
修改了最后部分,将属于父进程的代码独立出来:
- void signal_handle( int sig ){
- fprintf(stderr,"shut down sig: %d\n",sig);
- signal(SIGUSR1,signal_handle);
- return;
- }
- int main( int argc, char **argv )
- {
- pid_t pid;
- int i;
- int x;
- if( signal(SIGUSR1, signal_handle) == SIG_ERR ){
- fprintf(stderr,"signal error\n");
- return 0;
- }
- if( (pid=fork())==0 ){
- for( i=0; i<10; i++ ){
- fprintf(stderr,"child i=%d\n",i);
- if( i%3==0 ){
- kill( getpid(), SIGUSR1 );
- }
- }
- fprintf(stderr,"Child end\n");
- } else {
- waitpid(pid,&x,0);
- fprintf(stderr,"Parent end %d\n",x);
- return 0;
- }
- }
复制代码
运行结果:
- child i=0
- shut down sig: 30
- child i=1
- child i=2
- child i=3
- shut down sig: 30
- child i=4
- child i=5
- child i=6
- shut down sig: 30
- child i=7
- child i=8
- child i=9
- shut down sig: 30
- Child end
- Parent end 2560
复制代码 |
|