操作系统实验的报告材料材料进程地管道及消息通信_第1页
操作系统实验的报告材料材料进程地管道及消息通信_第2页
操作系统实验的报告材料材料进程地管道及消息通信_第3页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

1、实验题目进程的管道及消息通信小组合作否姓名班级学号一、实验目的1、为了掌握linux进程通信系统调用的功能,这里给出了进程通信 实现机制中使用的系统调用命令的格式和如何利用系统调用命令进 行进程通信编程,以便通过学习,提高学生对进城通信系统调用的编 程能力。2、理解进程通信的原理,掌握管道通信机制,熟悉无名管道和有名 管道通信的实现。二.实验环境Windows xp、VMware、Linux 系统三、实验内容与步骤(1) 编写两个程实现进程的无名管道和有名管道通信。要求分别调用 pipe()、close()、write()、read()、popen()、pclose()、mknod()、 mk

2、fifo()、open()实现多个进程间的通信。(2) 调试并分析结果。对观察到的内容做详细记录分析,并写出实验报告。四、头验过程与分析1.使用无名管道pipe(),进行父子进程之间的通信。编写的程序如下:.h j r parentl = A ht st sage t c p ipo conmin l L ion An: 咋mt ) r(int pidchanl2tchar biif100;p i严:r hq n】兀p id=fo rW ::if(pid0)pr in i f to trea it child er r?rn ); cxi t(l)tLIilCpidXBto ch i Id.

3、n*);io Le rmina le.n*):clojief ctianl 0):pr in l f( * pa 1 en I proce s sa tie s s jg:pre 1 sein 1 f paien1pn $ yte-r mi iiB te . n ):c lose( cttan I 11?:read1 chu n (l) I tbnJ 1UV):pr in I b The n?(; s a p e read by child p rcce s t f rom parent : ;. n buf); closet chinl01):pr in I f( cti i Id pro

4、ce s s terinina te bn:运行结果如下:roDlJoca I has t Ichv丸 cc I6l .c -o Iti-1 .exrooiloci I hcs t Idv # ./1G-1 .cxepareni prctss sends a nrssagt to chi Idparern prccffls wnitw lhe ch iId to icrminat.Ttie iidsrojd by ch i Id proce$ froin par:A nr sjiage ld pipe CGiirrun itd L ioncti i Id p rcce ss termini l

5、e .parent process 洱说卩和rrootlociIhst Id亚1 # |结果分析:父进程首先被调用时,运行结果为:p-u rein process seiid a rn? s Jiugc io ch i Id. pjreiiL pi4ei U pro fs termuniie.Ttie u世rjd by ch i Id proce a fmm parent i 5: A ntto p ipe Tomiunicdt ion.thi Id pruceiiK t*rminaie 之后父进程被唤醒,调度运行,输出如下结果后程序退出。parent process termina tc对于

6、以上的结果:首先父进程使用pipe( chan1 )系统调用打开一个无名管道,之后创建一个子进程。子进程复制父进程的打开文件表。 为了正确通信,父进程关闭读通道close (chan10),子进程关闭写通道close ( chan11)。父进程向管道写,子进程从管道读。完 成一次通信之后,父进程分别关闭自己的写/读通信,管道文件消失。2.以命名行为参数的管道文件的示例。(假设有一个可执行程序chcase,从标准输入设备读字符,将小写字母转化成大写字母并输 出。主程序使用popen创建管道,实现蒋某文本文件中的字幕转化 成大写字母,其中的文本文件名作为参数传进来。)编写的程序如下:Qinclud

7、e4 iiic lude#df ine 讪XU用 Wflin t rmirbfini argtkFILE * fpin.* fpoutji f( a rgc 1 -2)fp r in 1 f ( s Ider r usaje ; a .on tpa t hna iif V n ex j i(1);Fi dt, ( fp m- fopeii( a r gv 1) , r 1 ) =NLLL)fpr intf s iderr can l open n harg: 1 】ex i t(1);*if ( fpou i=popen( chrast J =NLLljfpr in Lf( sLdf r r

8、/pupen trrorn1): exil(I);卜Mhik) !-TUL J ( fpu t st lint* fpoul ) =if F3fpr intf (s tderj / (pu Is er rur g pipe u; Xlt(Iif( fcrrr( fpin)(fpr in t f( tde rj J F罠e I s firor + nT); exit( O:Ilft pc losef fpou i) =-l fpr in 1 fC s 1 i r f pclcse error In); ex i i(1);exiH 0) |运行结果是:roDlMoca Ihos t I dw#1

9、6-5 .e!teL iudaw I.IUDAWEIIr rooV!1oca I Itos r I du# |结果分析:通过程序运行结果可知,先打开文本文件,如果文本打开 失败,则执行exit( 1),退出程序,如果文本通过函数开成功,则 popen创建一个可写管道,将命令行chcase的输入与管道的输入连 接起来,然后向管道输入数据,此时命令行就可以通过管道接受文本 文件的数据了,在从文件中读出数据时,独处的内容放在line数组中,fpin表示从刚打开的文件里读出。之后要编写字母大小写转化函 数,来实现小写字母转化成大写字母。3.创建有名管道。编写的程序如下:# me lude f ctj

10、t I .hch .i r r ing = ths i i s a e j( any le tof i f o rorrmmi rj t ion irra inf jrgcrgv)int argc;ch jr * argv iIini rd;chj bu f25b:ini i:ntnod(,fifoGQ?77,0i Krgc2)J1ld=Dpen i i to O_VRCKLW :id=open i i I o X)RDCKLY): For(i-0;i2G;i+*1i 1( arg c=2)pr intft n I livc wr u ic : s1 .sir ing;wr i ic ids

11、L i uig ,4S):str iQg(O汁T ;rc Iseread(fd,buf:pr i n t f(nTh# cent d t hv I have read i$: ! %sT ,buf)j buffl=0 jJIc td);运行结果是:r oo tlocd Ums L. / 16- S .excIhaveBTC leIhave钛TO l1huveWOLCIm i?帖TO LChavWO IEIhavr|eliavrVTO 16havemt心 Le1hvr(j Le1ha、?秸it leVTO l e1tiavruto letiuvcwo le1havewo le1havewo le

12、haewro l e1havewo leIhave钛TO It1tmeWTO LChawMineIhd、ryto icIh3 on toimun ka t ion ctimiuii ica t iun infill ncj t uonttis i i-s a exa np le t show f i fo coimun ica I ion uhA i i s a examp k to f?hi*w f i fn conmin ia I ionThecan tex Lby1havereadi sThecantex Lhyhavereatli名Theenntex tbyhavereadi sThe

13、contrx tbyhavereadi sTherontex tby1havfreadi 5Thetontflx tby1havtreadi sTheruntex Lby1havereddi sThecon ttx Lby1liatrcdi $Theccntex lby1haveread1 5Thecantex Lhy1haverendi sThecontex Lbyhavereatl1 5Thernntex thyhavereadi sTherontex tby1hivereadi云Thrrontex tby1havfreadi 5Thetontex tby1havfreadi sThero

14、ntflx Lby1haveredi sTheconlcxLby1hivereadisThecunttx Lby1havereadi sThecan tex Lhy1havereadi sThecantex LhyhavereatlI sThecontex LbyhaveradisiTherontex Cby1havtreadis:Thrcontex tby1havfreadis:Therontex tby1havfreadis:/l6-3.eiex 11 1 2625前台运行结果:I rvoL Idw #./ ltj-3 xe1 hu vcwrote:kh Hi sSitos hewfifo

15、cornTuii i ci k1 0111 h J YVuruIE:uh i s1 sckj trp Itg5 kovfifuturilTLill 1 Cd LIQI11 tiaEle;vh ih1 sacip kto$ hovfifacvrimi) i ca tion1 tiavrato I e ; i si sesa rrptof ifarrnnun i ca tinnIhavcurn Ic;xh i wi sacxa np kto$liFoeoriTTLIll i tionI haveurate:yh i si s3eo nrp Letoi howrifocorimi! i ca 1io

16、n后台运行结果:rm tloca Ihoi t Idw# 16-3 .exe M1 27结果分析:此程序是把管道和命令联系起来,read()的系统调用格式 是 read(fd,buf,n),参数定义是 int read(fd,buf,n); int fd; char *buf; unsigned n;它的功能是从fd所指示的文件中读出n个字节 的数据,并将它们送至由指针buf所指示的缓冲区中。如该文件被加 锁,等待,直到锁打开为止;write()系统调用格式是read(fd,buf,n), 它的功能是把n个字节的数据,参数定义同read(),而父进程则从 管道中读出来自两个进程的信息,显示在屏幕上。从buf所指向的缓 冲区写到由fd所指向的文件中。程序中的string0+=1 表示字符串 中的第一个字符的ASCII加1对以上程序的整体分析:管道

温馨提示

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

评论

0/150

提交评论