《操作系统原理》实验报告4--实验项目:Linux多进程编程.docx
操作系统原理实验报告实验序号:4实验项目:LinUX多进程编程学号姓名专业、班级实验地点指导教师实验时间一、实验目的及要求通过本实验的学习,掌握LinUX多进程编程的基本方法。二、实验设备(环境)及要求VMware虚拟机软件,Ubuntu系统三、实验内容与步骤1 .根据教材实践LinUX进程管理指令jonzubuntu:$ PID TTY3994 pts 528 pts jonz0ubuntu:$PS TIME CMD :60:oo bash :66:06 ps通过ps查看进程JonZUbUntu:5PS-UWarning:badpssyntax,perhapsabogus',?Seehttp:/USER Jonz jonzSTAT STARTTIMECOMMANDSs Sep21rbashR+ 06:116:66ps -UPID%CPU%HEMVSZRSSTTY3994.387283644pts5286.l49481132pts使用参数进行查看2 .利用LinUX多进程编程实现以下功能:创建两个子进程;一个子进程(生产者进程)依次向缓冲区写入整数0,12.,19;另一个子进程(消费者进程)暂停3s后,从缓冲区读数,每次读一个,并将读出的数字从缓冲区删除,然后将数字显示出来;父进程等待子进程2(消费者进程)的退出信息,待收集到该信息后,父进程就返回。Stnclude<stdto.h>fftncludeVUnlStd.h,MincludeVStdltb.h,“Include<strig.h>Stnclude<iystypes.h>einclude<syswatt.h>nat(vo)(tntt=;Int*nCount;tntpfd;charszBuff三;tntstatus;PtLtptd;ptpe(pfd);If(fork()三三)(Cout=MaIloC(SlZeOf(tnt)*);for(t三;t<+1)(Coutt=t;SPrIntf(SZBUff,szBuff,Coutt);)wrtte(pfd,szBuff,StrIen(SZBUff);free(nCount);close(pfd);close(pfd);)else(tf(fork()=)(prtntf(n);sleep();read(pfd,szBuff,);prtntf(,szBuff);close(pfd);close(pfd);)elseptd=watt(ftstatus);t=WEXITSTATUS(status);prttf(,ptd,t);ptd=Watt(Astatus);t=WEXITSTATUS(status);prtntf(,ptd,t);)close(pfd);close(pfd);编写实验代码jonzubuntu:*/CProgram$vimexam4.cjonz0ubutu:/CProgram$gcc-oexam4exam4.cjonzubuntu:/CProgram$.exam4sleep(3)!childis9812,exitstatus=0”23456789IelII213141516171819UhtIdts9813,exitStdtUS=e编译程序并运行。四、分析与讨论本实验涉及到多进程编程,是需要进一步学习的部分。五、教师评语成绩签名:日期:附:第二题的源代码#include<sldio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<systypes.h>#include<syswait.h>intmain(void)(inti=0;int*nCount;intpfd2;charszBuff3O=0);intstatus;pid_tpid;ie(pfd);if(fbrk()=O)(nCount=malloc(sizeof(int)*20);for(i=0;i<20;+i)(nCounti=i;sprintf(szBuff,"%s%d,szBuff,nCounti);)write(pfdll,szBuff,slrlen(szBuf11);free(nCount);close(pfd01);close(pfdll);elseif(fork()=0)printf("sleep!n");sleep(3);read(pfd0,szBuff,30);Printf("%sn",szBuff);close(pfd0);close(pfdl);1else(pid=wait(<festatus);i=WExiTSTATUS(Status);printf("childis%d,exitstatus=%dn",pid,i);pid=wait(<festatus);i=WExiTSTATUS(Status);printf("childis%d,exitstatus=%dn",pid,i);close(pfd0);close(pfdlj);return0;