系统相关
首页 > 系统相关> > 等待子进程

等待子进程

作者:互联网

为什么等待子进程?

要子进程干活,检查活干完了,还是没完(abort还是被杀死了)

僵尸进程

父进程等待子进程退出 并收集子进程的退出状态

子进程退出状态不被收集,变成僵死进程(僵尸进程)

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
        pid_t pid;

        int cnt = 0;

        pid = vfork();

        if(pid > 0){
                while(1){
                        printf("father print , pid = %d\n",getpid());
                        printf("cnt = %d\n",cnt);
                        sleep(1);

                }
        }else if(pid == 0){

                while(1){
                        printf("son print , pid = %d\n",getpid());

                        sleep(1);
                        cnt++;
                        if(cnt == 5){
                                exit(0);
                        }
                }
        }


        return 0;
}
CLC@Embed_Learn:~/2_jingCheng$ gcc demo12.c -o newpro
CLC@Embed_Learn:~/2_jingCheng$ 
CLC@Embed_Learn:~/2_jingCheng$ 
CLC@Embed_Learn:~/2_jingCheng$ ./newpro 
son print , pid = 30749
son print , pid = 30749
son print , pid = 30749
son print , pid = 30749
son print , pid = 30749
father print , pid = 30748
cnt = 5
father print , pid = 30748
cnt = 5
father print , pid = 30748
cnt = 5
father print , pid = 30748
cnt = 5
father print , pid = 30748
cnt = 5
father print , pid = 30748
cnt = 5
CLC@Embed_Learn:~$ ps -aux|grep newpro
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
CLC      30748  0.0  0.0   4160   428 pts/27   S+   13:01   0:00 ./newpro
CLC      30749  0.0  0.0      0     0 pts/27   Z+   13:01   0:00 [newpro] <defunct>
CLC      30820  0.0  0.0  13588   940 pts/31   S+   13:02   0:00 grep --color=auto newpro

子进程pid = 30749 ,  Z+表示僵尸进程

相关函数

 区别: waitpid使调用者阻塞,waitpid有一个选项,可以使调用者不阻塞

wait等待 

​​include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
        pid_t pid;
        int status = 10;
        int cnt = 0;

        pid = fork();

        if(pid > 0){
                wait(&status);
                printf("child quit, status = %d\n",WEXITSTATUS(status));
                while(1){
                        printf("father print , pid = %d\n",getpid());
                        printf("cnt = %d\n",cnt);
                        sleep(1);

                }
        }else if(pid == 0){

                while(1){
                        printf("son print , pid = %d\n",getpid());

                        sleep(1);
                        cnt++;
                        if(cnt == 5){
                                exit(3);
                        }
                }
        }


        return 0;
}
CLC@Embed_Learn:~/2_jingCheng$ ./a.out 
son print , pid = 30946
son print , pid = 30946
son print , pid = 30946
son print , pid = 30946
son print , pid = 30946
child quit, status = 3
father print , pid = 30945
cnt = 0
father print , pid = 30945
cnt = 0
father print , pid = 30945
cnt = 0
father print , pid = 30945

进程:没有僵尸进程

CLC@Embed_Learn:~$ ps -aux|grep ./a.out 
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
CLC      30945  0.0  0.0   4156   348 pts/27   S+   13:10   0:00 ./a.out
CLC      30946  0.0  0.0   4160    84 pts/27   S+   13:10   0:00 ./a.out
CLC      30948  0.0  0.0  13588   952 pts/31   S+   13:10   0:00 grep --color=auto ./a.out

 waitpid等待

 status参数: 是一个整型数指针

非空: 子进程退出状态放在它所指向的地址中。

空: 不关心退出状态

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
        pid_t pid;
        int status = 10;
        int cnt = 0;

        pid = fork();

        if(pid > 0){

                waitpid(pid,&status,WNOHANG);
                printf("child quit, status = %d\n",WEXITSTATUS(status));
                while(1){
                        printf("father print , pid = %d\n",getpid());
                        printf("cnt = %d\n",cnt);
                        sleep(1);

                }
        }else if(pid == 0){

                while(1){
                        printf("son print , pid = %d\n",getpid());

                        sleep(1);
                        cnt++;
                        if(cnt == 5){
                                exit(3);
                        }
                }
        }


        return 0;
}
CLC@Embed_Learn:~/2_jingCheng$ ./a.out 
child quit, status = 0
father print , pid = 31060
cnt = 0
son print , pid = 31061
son print , pid = 31061
father print , pid = 31060
cnt = 0
son print , pid = 31061
father print , pid = 31060
cnt = 0
father print , pid = 31060
cnt = 0
son print , pid = 31061
CLC@Embed_Learn:~$ ps -aux|grep a.out 
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
CLC      31060  0.0  0.0   4160   352 pts/27   S+   13:19   0:00 ./a.out
CLC      31061  0.0  0.0   4160    88 pts/27   S+   13:19   0:00 ./a.out
CLC      31063  0.0  0.0  13588   948 pts/31   S+   13:19   0:00 grep --color=auto a.out

孤儿进程

父进程如果不等待子进程退出,在子进程之前就结束了自己的“生命”,此时子进程叫做孤儿进程。

Linux避免系统存在过多孤儿进程,init进程收留孤儿进程,变成孤儿进程的父进程。 

标签:cnt,0.0,pid,CLC,print,进程,等待,father
来源: https://blog.csdn.net/qq_53550531/article/details/123305869