1.实验目的
通过编写多进程程序,使读者熟练掌握 fork()、exec()、wait()和 waitpid()等函数的使用,进一步理解在 Linux
中多进程编程的步骤。
2.实验内容
该实验有 3 个进程,其中一个为父进程,其余两个是该父进程创建的子进程,其中一个子进程运行“ls -l”
指令,另一个子进程在暂停 5s 之后异常退出,父进程先用阻塞方式等待第一个子进程的结束,然后用非阻
塞方式等待另一个子进程的退出,待收集到第二个子进程结束的信息,父进程就返回。
3.实验步骤
(1)画出该实验流程图。
该实验流程图如图 7.8 所示。
(2)实验源代码。
先看一下下面的代码,这个程序能得到我们所希望的结果吗,它的运行会产生几个进程?请读者回忆一下
fork()调用的具体过程。
/* multi_proc_wrong.c */
#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("ls", "ls","-l", 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 /*在父进程中等待两个子进程的退出*/
{
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);
}
编译和运行以上代码,并观察其运行结果。它的结果是我们所希望的吗?
看完前面的代码之后,再观察下面的代码,它们之间有什么区别,会解决哪些问题。
/*multi_proc.c */
#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)
{
printf("Child1 fork error\n");
exit(1);
}
else if (child1 == 0) /*在子进程 1 中调用 execlp()函数*/
{
printf("In child1: execute 'ls -l'\n");
if (execlp("ls", "ls", "-l", NULL) < 0)
{
printf("Child1 execlp error\n");
}
}
else /*在父进程中再创建进程 2,然后等待两个子进程的退出*/
{
child2 = fork();
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);
}
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);
}
(3)首先在宿主机上编译调试该程序:
$ gcc multi_proc.c –o multi_proc(或者使用 Makefile) (4)在确保没有编译错误后,使用交叉编译该程序:
$ arm-linux-gcc multi_proc.c –o multi_proc (或者使用 Makefile) (5)将生成的可执行程序下载到目标板上运行。
4.实验结果
在目标板上运行的结果如下所示(具体内容与各自的系统有关):
$ ./multi_proc
In child1: execute 'ls -l' /* 子进程 1 的显示, 以下是“ls –l”的运行结果 */
total 28
-rwxr-xr-x 1 david root 232 2008-07-18 04:18 Makefile
-rwxr-xr-x 1 david root 8768 2008-07-20 19:51 multi_proc
-rw-r--r-- 1 david root 1479 2008-07-20 19:51 multi_proc.c
-rw-r--r-- 1 david root 3428 2008-07-20 19:51 multi_proc.o
-rw-r--r-- 1 david root 1463 2008-07-20 18:55 multi_proc_wrong.c
In child2: sleep for 5 seconds and then exit /* 子进程 2 的显示 */
In father process: /* 以下是父进程显示 */
Get child1 exit code /* 表示子进程 1 结束(阻塞等待)*/
The child2 process has not exited! /* 等待子进程 2 结束(非阻塞等待)*/
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 /* 表示子进程 2 终于结束了*/
因为几个子进程的执行有竞争关系,因此,结果中的顺序是随机的。读者可以思考,怎样才可以保证子进
程的执行顺序呢?