1、实验7编写多进程程序实验7-编写多进程程序实验七 编写多进程程序学生姓名: 李亚军 学 号: 6100412196 专业班级: 卓越计科121班 1实验目的通过编写多进程程序,使读者熟练掌握fork()、exec()、wait()和waitpid()等函数的使用,进一步理解在Linux中多进程编程的步骤。2实验内容该实验有3个进程,其中一个为父进程,其余两个是该父进程创建的子进程,其中一个子进程运行“ls -l”指令,另一个子进程在暂停5s之后异常退出,父进程先用阻塞方式等待第一个子进程的结束,然后用非阻塞方式等待另一个子进程的退出,待收集到第二个子进程结束的信息,父进程就返回。3实验步骤(1
2、)画出该实验流程图该实验流程图如图所示。图 实验7.1流程图(2)实验源代码(multi_proc.c)先看一下下面的代码,这个程序能得到我们所希望的结果吗,它的运行会产生几个进程?请读者回忆一下fork()调用的具体过程。答:会产生四个进程/* multi_proc_wrong.c */#include #include #include #include #include int main(void) pid_t child1, child2, child; /*创建两个子进程*/ child1 = fork(); child2 = fork(); /*子进程1的出错处理*/ if (ch
3、ild1 = -1) printf(Child1 fork errorn); exit(1); else if (child1 = 0) /*在子进程1中调用execlp()函数*/ printf(In child1: execute ls -ln); if (execlp(ls, ls,-l, NULL)0) printf(Child1 execlp errorn); if (child2 = -1) /*子进程2的出错处理*/ printf(Child2 fork errorn); exit(1); else if( child2 = 0 ) /*在子进程2中使其暂停5s*/ printf
4、(In child2: sleep for 5 seconds and then exitn); sleep(5); exit(0); else /*在父进程中等待两个子进程的退出*/ printf(In father process:n); child = waitpid(child1, NULL, 0); /* 阻塞式等待 */ if (child = child1) printf(Get child1 exit coden); else printf(Error occured!n); do child =waitpid(child2, NULL, WNOHANG);/* 非阻塞式等待
5、*/ if (child = 0) printf(The child2 process has not exited!n); sleep(1); while (child = 0); if (child = child2) printf(Get child2 exit coden); else printf(Error occured!n); exit(0);编译和运行以上代码,并观察其运行结果。它的结果是我们所希望的吗?答:不是看完前面的代码之后,再观察下面的代码,它们之间有什么区别,会解决哪些问题。答:将child2进程的产生放在判断不是child1进程的执行代码中,在第一个例子中由于进程
6、1在执行的时候又fork出一个进程,导致产生了第四个进程,这个进程是child1的子进程,它会将ls命令在执行一遍,而在第二个例子中由于产生child代码放在判断中,故而可以避免在child1中重复fork新进程。/*multi_proc.c */#include #include #include #include #include int main(void) pid_t child1, child2, child; /*创建两个子进程*/ child1 = fork(); /*子进程1的出错处理*/ if (child1 = -1) printf(Child1 fork errorn);
7、 exit(1); else if (child1 = 0) /*在子进程1中调用execlp()函数*/ printf(In child1: execute ls -ln); if (execlp(ls, ls, -l, NULL) 0) printf(Child1 execlp errorn); else /*在父进程中再创建进程2,然后等待两个子进程的退出*/ child2 = fork(); if (child2 = -1) /*子进程2的出错处理*/ printf(Child2 fork errorn); exit(1); else if(child2 = 0) /*在子进程2中使其
8、暂停5s*/ printf(In child2: sleep for 5 seconds and then exitn); sleep(5); exit(0); printf(In father process:n); child = waitpid(child1, NULL, 0); /* 阻塞式等待 */ if (child = child1) printf(Get child1 exit coden); else printf(Error occured!n); do child = waitpid(child2, NULL, WNOHANG ); /* 非阻塞式等待 */ if (ch
9、ild = 0) printf(The child2 process has not exited!n); sleep(1); while (child = 0); if (child = child2) printf(Get child2 exit coden); else printf(Error occured!n); exit(0);(3)编译并运行程序例子1运行结果:In father process:In child2: sleep for 5 seconds and then exitIn child1: execute ls -lIn child1: execute ls -l
10、总用量 12-rwxrwxr-x. 1 chengchangfu chengchangfu 7530 5月 23 16:01 muti_proc-rw-rw-r-. 1 chengchangfu chengchangfu 1756 5月 23 15:59 muti_proc.c总用量 12-rwxrwxr-x. 1 chengchangfu chengchangfu 7530 5月 23 16:01 muti_proc-rw-rw-r-. 1 chengchangfu chengchangfu 1756 5月 23 15:59 muti_proc.cGet child1 exit codeTh
11、e 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例子2运行结果:In father process:In child2: sleep for 5 seconds and then exitIn child1: execute ls -l总用量 28-rwxrwxr-x.
12、1 chengchangfu chengchangfu 7530 5月 23 16:01 muti_proc-rwxrwxr-x. 1 chengchangfu chengchangfu 7531 5月 23 16:12 muti_proc2-rw-rw-r-. 1 chengchangfu chengchangfu 1845 5月 23 16:11 muti_proc2.c-rw-rw-r-. 1 chengchangfu chengchangfu 1843 5月 23 16:10 muti_proc2.c-rw-rw-r-. 1 chengchangfu chengchangfu 1756
13、 5月 23 15:59 muti_proc.cGet child1 exit codeThe 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 code4完成实验报告实验总结:答:整个fork函数可以这样理解:它复制当前进程内所有的状态,并在其复制处的代码开始执行子进程,在例子中ch
14、ild1产生的时候child2还没有产生,故而在它的进程里又重复fork了child2,从而造成错误。5思考问题在目标板上运行的结果如下所示(具体内容与各自的系统有关):因为几个子进程的执行有竞争关系,因此,结果中的顺序是随机的。读者可以思考,怎样才可以保证子进程的执行顺序呢?答:使用信号同步,wait()与signal()2 编写守护进程1实验目的通过编写一个完整的守护进程,使读者掌握守护进程编写和调试的方法,并且进一步熟悉如何编写多进程程序。2实验内容在该实验中,读者首先建立起一个守护进程,然后在该守护进程中新建一个子进程,该子进程暂停10s,然后自动退出,并由守护进程收集子进程退出的消息。在这里,子进程和守护进程的退出消息都在
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1