ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

等待子进程

2022-03-06 13:32:56  阅读:152  来源: 互联网

标签:cnt 0.0 pid CLC print 进程 等待 father


为什么等待子进程?

要子进程干活,检查活干完了,还是没完(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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有