- 論壇徽章:
- 0
|
本帖最后由 centos_71 于 2015-01-18 19:34 編輯
我寫了一個小程序,在fork出的子進程中使用prctl,設置父進程退出后自動發(fā)送給子進程退出信號。
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<signal.h>
- #include<sys/prctl.h>
- int main()
- {
- pid_t p = fork();
- if(p==0)//father
- {
- printf("father\n");
- getchar();
- return 0;
- }
- else if(p<0)
- exit(1);
-
- printf("child\n");
- prctl(PR_SET_PDEATHSIG,0,0,0,0);
- getchar();
- return 0;
- }
復制代碼 我預期的是,我運行程序以后,父進程和子進程都在getchar()阻塞。
我用ps看到父子兩個進程
- [a@localhost ~]$ ps -ef|grep a.out
- a 13238 2727 0 02:41 pts/1 00:00:00 ./a.out
- a 13239 13238 0 02:41 pts/1 00:00:00 ./a.out
- a 13255 13240 0 02:42 pts/2 00:00:00 grep a.out
- [a@localhost ~]$ kill -9 13238
- [a@localhost ~]$ ps -ef|grep a.out
- a 13239 1 0 02:41 pts/1 00:00:00 ./a.out
- a 13257 13240 0 02:42 pts/2 00:00:00 grep a.out
復制代碼 然后我殺死父進程(kill -9),我期待的是,子進程可以自動退出,因為子進程已經(jīng)調(diào)用過prctl,能接收到父進程的通知對吧?
但是實際運行的結(jié)果,如上所示,子進程并沒有退出,而是成了孤兒進城了。這是為什么呢?
為什么父進程退出以后,系統(tǒng)沒有能通知子進程父進程已經(jīng)退出了,然后子進程也退出?
可能我的程序或者理解有問題,還請高人指正,多謝! |
|