进程控制4 编写多进程

实验有 3 个进程,其中一个为父进程,其余两个是该父进程创建的子进程,其中一个子进程运行“ls -l”指令,另一个子进程在暂停 5s 之后异常退出,父进程先用阻塞方式等待第一个子进程的结束,然后用非阻塞方式等待另一个子进程的退出,待
收集到第二个子进程结束的信息,父进程就返回。

错误的编写程序如下:原因是创建了4个进程了,两个fork连续的应用,将会导致创建4个进程。

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

int main(void)
{
	pid_t child1, child2, child;

	/*创建两个子进程*/
	child1 = fork();
	child2 = fork();

	/*子进程1的出错处理*/
	if (child1 == -1)
	{
		printf("Child1 fork error\n");
		exit(1);
	}
	else if (child1 == 0) /*在子进程1中调用execlp函数*/
	{
		printf("In child1: execute 'ls -l'\n");

		if (execlp("ps", "ps", "-u", NULL) < 0)
		{
			printf("Child1 execlp error\n");
		}
  	}

  	if (child2 == -1) /*子进程2的出错处理*/
  	{
  		printf("Child2 fork error\n");
  		exit(1);
  	}
  	else if( child2 == 0 ) /*在子进程2中使其暂停5s*/
  	{
  		printf("In child2: sleep for 5 seconds and then exit\n");
  		sleep(5);
  		exit(0);
  	}
  	else /*在父进程中等待子进程2的退出*/
  	{
  		printf("In father process:\n");

  		child = waitpid(child1, NULL, 0);
  		if (child == child1)
  		{
  			printf("Get child1 exit code\n");
  		}
  		else
  		{
  			printf("Error occured!\n");
  		}

  		do
  		{
  			child = waitpid(child2, NULL, WNOHANG );

  			if (child == 0)
  			{
  				printf("The child2 process has not exited!\n");
  				sleep(1);
  			}
  		} while (child == 0);

  		if (child == child2)
  		{
  			printf("Get child2 exit code\n");
  		}
  		else
  		{
  			printf("Error occured!\n");
  		}
  	}

  	exit(0);
}

sfe1012@sfe1012-ThinkPad-Edge-E540:~/workspace/MultiProcess/Debug$ ./MultiProcess 
In father process:
In child2: sleep for 5 seconds and then exit
In child1: execute 'ls -l'
In child1: execute 'ls -l'
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
sfe1012   3705  0.6  0.0  29000  6012 pts/5    Ss   15:04   0:00 bash
sfe1012   3705  0.6  0.0  29000  6012 pts/5    Ss   15:04   0:00 bash
sfe1012   3813  0.0  0.0   4200   360 pts/5    S+   15:05   0:00 ./MultiProcess
sfe1012   3813  0.0  0.0   4200   360 pts/5    S+   15:05   0:00 ./MultiProcess
sfe1012   3814  0.0  0.0  22648  1296 pts/5    R+   15:05   0:00 ps -u
sfe1012   3814  0.0  0.0  22648  1296 pts/5    S+   15:05   0:00 ps -u
sfe1012   3815  0.0  0.0   4200    88 pts/5    S+   15:05   0:00 ./MultiProcess
sfe1012   3816  0.0  0.0  22648  1296 pts/5    R+   15:05   0:00 ps -u
sfe1012   3815  0.0  0.0   4200    88 pts/5    S+   15:05   0:00 ./MultiProcess
sfe1012   3816  0.0  0.0  22648  1296 pts/5    R+   15:05   0:00 ps -u
Get child1 exit code
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
Get child2 exit code

正确的编写方式如下:

<pre name="code" class="cpp">#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
	pid_t child1, child2, child;

	/*创建两个子进程*/
	child1 = fork();  //这个时候将会是出现两个分支,一个主进城,一个子进程

	/*子进程1的出错处理*/
	if (child1 == -1) //进程1创建失败
	{
		printf("Child1 fork error\n");
		exit(1);
	}
	else if (child1 == 0) /*在子进程1中调用execlp函数*/  //返回值为0 ,说明是子进程
	{
		printf("In child1: execute 'ls -l'\n");

		if (execlp("ls", "ls", "-l", NULL) < 0)  //调用ls -l命令
		{
			printf("Child1 execlp error\n");
		}
  	}
  	else /*在父进程中等待子进程2的退出*/
  	{
  		child2 = fork();//在第一个fork函数之后的父亲进程中,创建第二个进程,这个时候又会产生两个分支,一个是原来的父进程,另一个十子进程2
  		if (child2 == -1) /*子进程2的出错处理*/
  		{
  			printf("Child2 fork error\n");
  			exit(1);
  		}
  		else if( child2 == 0 ) /*在子进程2中使其暂停5s*/
  		{
  			printf("In child2: sleep for 5 seconds and then exit\n");
  			sleep(5);//执行停止5s
  			exit(0);//正常退出
  		}

  		printf("In father process:\n");

  		child = waitpid(child1, NULL, 0);//父进程中柱塞等待子进程1
  		if (child == child1)//如果返回值是子进程1的进程pid的时候。
  		{
  			printf("Get child1 exit code\n");
  		}
  		else
  		{
  			printf("Error occured!\n");
  		}

  		do
  		{
  			child = waitpid(child2, NULL, WNOHANG );//父进程以非阻塞的方式等待进程2的退出。
  			if (child == 0) //返回为0则说明进程2还没有结束
  			{
  				printf("The child2 process has not exited!\n");
  				sleep(1);//暂停1秒
  			}
  		} while (child == 0);//进程2还没有结束

  		if (child == child2)//如果返回值等有进程2的pid的时候
  		{
  			printf("Get child2 exit code\n");
  		}
  		else
  		{
  			printf("Error occured!\n");
  		}
  	}

  	exit(0);
}
 sfe1012@sfe1012-ThinkPad-Edge-E540:~/嵌入式linux应用程序开发标准教程/source/proc 

ess/7-4-1$ ./multi_proc 
In father process:
In child1: execute 'ls -l'
In child2: sleep for 5 seconds and then exit
总用量 32
-rw-rw-r-- 1 sfe1012 sfe1012   232 12月 19  2008 Makefile
-rwxrwxr-x 1 sfe1012 sfe1012 10108  1月 14 17:04 multi_proc
-rw-rw-r-- 1 sfe1012 sfe1012  1481 12月 19  2008 multi_proc.c
-rw-rw-r-- 1 sfe1012 sfe1012  5272  1月 14 17:04 multi_proc.o
-rw-rw-r-- 1 sfe1012 sfe1012  1465 12月 19  2008 multi_proc_wrong.c
Get child1 exit code
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
Get child2 exit code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值