等待子进程退出

目录

为什么等待子进程退出?

僵尸进程

相关函数

wait等待 

 waitpid等待

孤儿进程

为什么等待子进程退出?

要子进程干活,检查活干完了,还是没完(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){
        //子进程循环打印5次,父进程阻塞在这,收集到子进程的退出状态后,执行父进程循环
                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进程收留孤儿进程,变成孤儿进程的父进程。 

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
	pid_t pid;
	int cnt =0;
	int status = 10;

	pid = fork();

	if(pid > 0){
		printf("this is father print,pid = %d\n",getpid());
	}else if(pid == 0){
		
		while(1){
			printf("this is chilid print,pid = %d,py father pid = %d\n",getpid(),getppid());
			sleep(1);
			cnt++;
			if(cnt==5){
				exit(3);
			}
		}
	}

	return 0;
}
//父进程还在,父进程pid=42969
CLC@Embed_Learn:~/2_jingCheng$ ./guer
this is father print,pid = 42969
this is chilid print,pid = 42970,py father pid = 42969
//父进程不在了,子进程成孤儿了,由init进程成为新父进程,pid=1
CLC@Embed_Learn:~/2_jingCheng$ this is chilid print,pid = 42970,py father pid = 1
this is chilid print,pid = 42970,py father pid = 1
this is chilid print,pid = 42970,py father pid = 1
this is chilid print,pid = 42970,py father pid = 1

CLC@Embed_Learn:~/FILE$ ps -aux|grep guer
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
CLC      42970  0.0  0.0   4160    88 pts/2    S    08:38   0:00 ./guer
CLC      42972  0.0  0.0  13588   936 pts/1    S+   08:38   0:00 grep --color=auto guer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值