实验7 编写多进程程序_第1页
实验7 编写多进程程序_第2页
实验7 编写多进程程序_第3页
实验7 编写多进程程序_第4页
实验7 编写多进程程序_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验七 编写多进程程序学生姓名: 李亚军 学 号: 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 <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>int main(void) pid_t child1, child2, child; /*创

3、建两个子进程*/ child1 = fork(); child2 = fork(); /*子进程1的出错处理*/ if (child1 = -1) printf("Child1 fork errorn"); exit(1); else if (child1 = 0) /*在子进程1中调用execlp()函数*/ printf("In child1: execute 'ls -l'n"); if (execlp("ls", "ls","-l", NULL)<0) printf

4、("Child1 execlp errorn"); if (child2 = -1) /*子进程2的出错处理*/ printf("Child2 fork errorn"); exit(1); else if( child2 = 0 ) /*在子进程2中使其暂停5s*/ printf("In child2: sleep for 5 seconds and then exitn"); sleep(5); exit(0); else /*在父进程中等待两个子进程的退出*/ printf("In father process:n&q

5、uot;); 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 (child = 0) printf("The child2 process has not exited!n"); sleep(1); while

6、(child = 0); if (child = child2) printf("Get child2 exit coden"); else printf("Error occured!n"); exit(0);编译和运行以上代码,并观察其运行结果。它的结果是我们所希望的吗?答:不是看完前面的代码之后,再观察下面的代码,它们之间有什么区别,会解决哪些问题。答:将child2进程的产生放在判断不是child1进程的执行代码中,在第一个例子中由于进程1在执行的时候又fork出一个进程,导致产生了第四个进程,这个进程是child1的子进程,它会将ls命令在执行

7、一遍,而在第二个例子中由于产生child代码放在判断中,故而可以避免在child1中重复fork新进程。/*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

8、) printf("Child1 fork errorn"); 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 errorn"); else /*在父进程中再创建进程2,然后等待两个子进程的退出*/

9、 child2 = fork(); if (child2 = -1) /*子进程2的出错处理*/ printf("Child2 fork errorn"); exit(1); else if(child2 = 0) /*在子进程2中使其暂停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); /* 阻

10、塞式等待 */ if (child = child1) printf("Get child1 exit coden"); 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(&q

11、uot;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 -l'In child1: execute 'ls -l'总用量 12-rwxrwxr-x. 1 chengchangfu chengchangfu 7530 5月 23 16:01 muti_

12、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 codeThe child2 process has not exited!The child2 process has not exited!Th

13、e 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. 1 chengchangfu chengchangfu 7530 5月 23 16:01 muti_proc-rwx

14、rwxr-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 5月 23 15:59 muti_proc.cGet child1 exit codeThe child2 pro

15、cess 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函数可以这样理解:它复制当前进程内所有的状态,并在其复制处的代码开始执行子进程,在例子中child1产生的时候child2还没有产生,故而在它的进程里又重复fork了child2,从而造成错误。5思考问题在目

16、标板上运行的结果如下所示(具体内容与各自的系统有关):因为几个子进程的执行有竞争关系,因此,结果中的顺序是随机的。读者可以思考,怎样才可以保证子进程的执行顺序呢?答:使用信号同步,wait()与signal()2 编写守护进程1实验目的通过编写一个完整的守护进程,使读者掌握守护进程编写和调试的方法,并且进一步熟悉如何编写多进程程序。2实验内容在该实验中,读者首先建立起一个守护进程,然后在该守护进程中新建一个子进程,该子进程暂停10s,然后自动退出,并由守护进程收集子进程退出的消息。在这里,子进程和守护进程的退出消息都在系统日志文件(例如“/var/log/messages”,日志文件的全路径名

17、因版本的不同可能会有所不同)中输出。子进程退出后,守护进程循环暂停,其间隔时间为10s。3实验步骤(1)画出该实验流程图。该程序流程图如图所示。图 实验7.2流程图(2具体代码设置如下:/* daemon_proc.c */#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>#include <syslog.h>int main(void) pid_t child1,child2

18、; int i; /*创建子进程1*/ child1 = fork(); if (child1 = 1) perror("child1 fork"); exit(1); else if (child1 > 0) exit(0); /* 父进程退出*/ /*打开日志服务*/ openlog("daemon_proc_info", LOG_PID, LOG_DAEMON); /*以下几步是编写守护进程的常规步骤*/ setsid(); chdir("/"); umask(0); for(i = 0; i < getdtable

19、size(); i+) close(i); /*创建子进程2*/ child2 = fork(); if (child2 = 1) perror("child2 fork"); exit(1); else if (child2 = 0) /* 进程child2 */ /*在日志中写入字符串*/ syslog(LOG_INFO, " child2 will sleep for 10s "); sleep(10); syslog(LOG_INFO, " child2 is going to exit! "); exit(0); else /* 进程child1*/ waitpid(child2, NULL, 0); syslog(LOG_INFO, " child

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论