
实验7 编写多进程程序.docx
7页实验七编写多进程程序学生姓名:李亚军 学 号:6100412196专业班级:卓越计科121班1.实验目的通过编写多进程程序,使读者熟练掌握fork()、exec()、wait()和waitpid()等函数的使用,进一步理解 在Linux中多进程编程的步骤2•实验内容该实验有3个进程,其中一个为父进程,其余两个是该父进程创建的子进程,其中一个子进程运行1 -1”指令,另一个子进程在暂停5s之后异常退出,父进程先用阻塞方式等待第一个子进程的结束,然后用 非阻塞方式等待另一个子进程的退出,待收集到第二个子进程结束的信息,父进程就返回3. 实验步骤(1) 画出该实验流程图该实验流程图如图所示图实验7.1流程图(2) 实验源代码(m ulti_proc.c)先看一下下面的代码,这个程序能得到我们所希望的结果吗,它的运行会产生几个进程?请读者回 忆一下fork()调用的具体过程答:会产生四个进程/* multi_proc_wrong.c */#include vstdio.h>#include vstdlib.h>#include vsys/types.h>#include vunistd.h>#include
它的结果是我们所希望的吗?答:不是看完前面的代码之后,再观察下面的代码,它们之间有什么区别,会解决哪些问题答:将child2进程的产生放在判断不是childl进程的执行代码中,在第一个例子中由于进程1在执行的时候又fork出一个进程,导致产生了第四个进程,这个进程是child 1的子进程,它 会将Is命令在执行一遍,而在第二个例子中由于产生child代码放在判断中,故而可以避免在child 1中重复fork新进程/*multi_proc.c */#include vstdio.h>#include vstdlib.h>#include vsys/types.h>#include vunistd.h>#include vsys/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(l);}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:*");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)编译并运行程序例子1运行结果:In father process:In child2: sleep for 5 seconds and then exitIn child1: execute 'Is -l'In child1: execute 'ls -l'总用量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.c Get 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 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-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 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函数可以这样理解:它复制当前进程内所有的状态,并在其复制处的代码开始执行子进 程,在例子中child1产生的时候child2还没有产生,故而在它的进程里又重复fork 了 child2,从而造成错误5. 思考问题在目标板上运行的结果如下所示(具体内容与各自的系统有关):因为几个子进程的执行有竞争关系,因此,结果中的顺序是随机的。
读者可以思考,怎样才可以保 证子进程的执行顺序呢?答:使用信号同步,wait()与signal2编写守护进程1. 实验目的通过编写一个完整的守护进程,使读者掌握守护进程编写和调试的方法,并且进一步熟悉如何编写 多进程程序2. 实验内容图实验7..2流程图在该实验中,读者首先建立起一个守护进程,然后在该守护进程中新建一个子进程,该子进程暂停 1 Os,然后自动退出,并由守护进程收集子进程退出的消息在这里,子进程和守护进程的退出消息都在系 统日志文件(例如“/var/log/messageS”志文件的全路径名因版本的不同可能会有所不同)中输出子 进程退出后,守护进程循环暂停,其间隔时间为10s3. 实验步骤(1)画出该实验流程图该程序流程图如图所示2具体代码设置如下:/* daemon_proc.c */#include vstdio.h>#inc。












