fork()的使用;僵尸进程和wait/waitpid函数的使用

#include <stdio.h>
#include <unistd.h>

using namespace std;

int main(int argc ,char *argv[])
{
	pid_t  pid;
	pid_t  pid_num;
	pid_t  farther_pid_num;
	farther_pid_num=getpid();
	printf("farther_pid_num = %d \n",farther_pid_num);
	static  int  static_num=10;
	pid=fork();
	if(pid<0)
	{
		printf("fork error\n");
		
	}else if(pid==0)
	{
		printf("this  is son  thread\n");
		pid_num=getpid();
		printf("pid_num = %d \n",pid_num);
	}
	else
	{
		printf("this  is farther  thread\n");
		pid_num=getpid();
		printf("pid_num = %d \n",pid_num);
	}		
	
	for (int i=0;i<10;++i)
	{   
        static_num++;
		printf("i = %d ,static_num=%d\n",i,static_num);
		
	}
}

执行结果如下:


/* 
 *  fork_test.c 
 *  version 2 
 *  Created on: 2010-5-29 
 *      Author: wangth 
 */  
#include <unistd.h>  
#include <stdio.h>  
int main(void)  
{  
   int i=0;  
   printf("i son/pa   ppid   pid   fpid\n");  
   //ppid指当前进程的父进程pid  
   //pid指当前进程的pid,  
   //fpid指fork返回给当前进程的值  
   for(i=0;i<2;i++){  
       pid_t fpid=fork();  
       if(fpid==0)  
           printf("%d child  %4d  %4d  %4d\n",i,getppid(),getpid(),fpid);  
       else  
           printf("%d parent %4d  %4d  %4d\n",i,getppid(),getpid(),fpid);  
   }  
   return 0;  
}  

2.僵尸进程和wait/waitpid

孤儿进程:父进程退出,子进程还在运行;

僵尸进程:子进程运行结束,但是父进程未调用wait/waitpid函数获取状态信息;

避免僵尸进程的方法:(1)使用wait/waitpid;(2)使用两次fork函数。



//wait/taipid 
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>

using namespace std;

int main(int argc ,char *argv[])
{
	pid_t  pid;
	pid_t  pid_num;
	pid_t  farther_pid_num;
	farther_pid_num=getpid();
	printf("farther_pid_num = %d \n",farther_pid_num);
	
	pid=fork();
	if(pid<0)
	{
		printf("fork error\n");
		
	}else if(pid==0)
	{   
        printf("============================\n");
		printf("this  is son  thread\n");
		pid_num=getpid();
		printf("son pid_num = %d \n",pid_num);
	}else{
	printf("++++++++++++++++++++++++++++++++\n");
	printf("this  is farther  thread\n");
	printf("son  pid = %d\n",pid);//父进程返回的是子进程的ID
	pid_num=getpid();
	printf("farther pid_num = %d \n",pid_num);	
	system("ps -ef|grep fork");
	sleep(10);	
	int ret = waitpid(-1,NULL,WNOHANG);
	if(ret < 0)
	{
		printf("ret = %d\n",ret);
	}
	system("ps -ef|grep fork");
	
   }


}

运行结果 :

//两次fork
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>

using namespace std;

int main(int argc ,char *argv[])
{
	pid_t  pid;
	pid_t  pid_num;
	pid_t  farther_pid_num;
	farther_pid_num=getpid();
	pid_t second_pid_num;
	printf("farther_pid_num = %d \n",farther_pid_num);
	
	pid=fork();
	if(pid<0)
	{
		printf("fork error\n");
		
	}else if(pid==0)
	{   
        printf("============================\n");
		printf("this  is son  thread\n");
		pid_num=getpid();
		printf("son pid_num = %d \n",pid_num);
		second_pid_num =fork();
		if(second_pid_num<0)
		{
			printf("second fork error\n");
		}else if(second_pid_num==0){
			sleep(30);
			printf("second fork son pid_num = %d\n",getpid());
			system("ps -ef|grep fork");
			
		}else{
			printf("second fork father pid_num = %d\n",getpid());	
            system("ps -ef|grep fork");			
		}		
		
	}else{
	printf("++++++++++++++++++++++++++++++++\n");
	printf("this  is farther  thread\n");
	printf("son  pid = %d\n",pid);//父进程返回的是子进程的ID
	pid_num=getpid();
	printf("farther pid_num = %d \n",pid_num);	
	
	sleep(10);	
	int ret = waitpid(-1,NULL,WNOHANG);
	if(ret < 0)
	{
		printf("ret = %d\n",ret);
	}
	
   }


}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值