操作系统实验报告-Linux进程创建与通信.doc

操作系统实验报告-Linux进程创建与通信.doc

ID:54849979

大小:106.50 KB

页数:20页

时间:2020-04-22

操作系统实验报告-Linux进程创建与通信.doc_第1页
操作系统实验报告-Linux进程创建与通信.doc_第2页
操作系统实验报告-Linux进程创建与通信.doc_第3页
操作系统实验报告-Linux进程创建与通信.doc_第4页
操作系统实验报告-Linux进程创建与通信.doc_第5页
资源描述:

《操作系统实验报告-Linux进程创建与通信.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、2011-2012学年第一学期计算机操作系统实验报告专业:班级:学号:姓名:提交日期:2011年11月1实验二Linux进程创建与进程通信【实验目的】1.熟悉有关Linux系统调用;2.学习有关Linux的进程创建,理解进程创建后两个并发进程的执行;3.通过系统调用wait()和exit(),实现父子进程同步;4.掌握管道、消息缓冲等进程通信方法并了解其特点和使用限制。【实验内容】1.父进程创建子进程实现父进程创建一个子进程,返回后父子进程分别循环输出字符串“Theparentprocess.”及“Thechildprocess.”5次,每次输出

2、后使用sleep(1)延时一秒,然后再进入下一次循环。给出源程序代码和运行结果。20程序代码:main(){intp1,i;while((p1=fork())==-1);if(p1>0)for(i=0;i<5;i++){printf("Iamparent.");sleep(1);}elsefor(i=0;i<5;i++){printf("Iamchild.");sleep(1);}}运行结果:Theparentprocess.Thechildprocess.Theparentprocess.Thechildprocess.Theparen

3、tprocess.Thechildprocess.20Theparentprocess.Thechildprocess.Theparentprocess.Thechildprocess.202.父子进程同步修改上题程序,使用exit()和wait()实现父子进程同步,其同步方式为父进程等待子进程的同步,即:子进程循环输出5次,然后父进程再循环输出5次。给出源程序代码和运行结果。20程序代码:main(){intp1,i;while((p1=fork())==-1);if(p1>0){wait(0);for(i=0;i<5;i++){printf(

4、"Iamparent.");sleep(1);}}else{for(i=0;i<5;i++){printf("Iamchild.");sleep(1);}exit(0);}}运行结果:Iamparent.Iamparent.Iamparent.Iamparent.Iamparent.Iamchild.Iamchild.Iamchild.Iamchild.Iamchild.203.Linux管道通信编写一个程序,实现以下功能。给出源程序代码和运行结果。(1)父进程使用系统调用pipe()创建一个无名管道;(2)创建两个子进程,分别向管道发送

5、一条信息后结束;子进程1发送:Child1issendingamessagetoparent!子进程2发送:Child2issendingamessagetoparent!(3)父进程从管道中分别接收两个子进程发来的消息并显示在屏幕上,父进程结束。两个子进程发送信息没有先后顺序要求。源程序代码:#include#include#include20main(){intp1,p2,fd[2];charoutpipe[50];charinpipe1[50]="Child1issendingame

6、ssagetoparent!";charinpipe2[50]="Child2issendingamessegetoparent!";pipe(fd);while((p1=fork())==-1);if(p1==0){lockf(fd[1],1,0);write(fd[1],inpipe1,50);exit(0);}else{while((p2=fork())==-1);if(p2==0){lockf(fd[1],1,0);write(fd[1],inpipe2,50);exit(0);}else{wait(0);read(fd[0],outpi

7、pe,50);20lockf(fd[1],0,0);printf("Parenthasreceivedfirstmessage:");printf("%s",outpipe);wait(0);read(fd[0],outpipe,50);lockf(fd[1],0,0);printf("Parenthasreceivedsecondmessage:");printf("%s",outpipe);exit(0);}20}}20运行结果:Child1issendingamessagetoparent!Child2issendingam

8、essagetoparent!204.Linux消息缓冲通信编写一个程序,实现以下功能。给出源程序代码和运行结果。(1)父进程创建一个消

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。