管道通信机制和消息缓冲机制

上传人:桔**** 文档编号:564952900 上传时间:2023-05-26 格式:DOCX 页数:14 大小:152.61KB
返回 下载 相关 举报
管道通信机制和消息缓冲机制_第1页
第1页 / 共14页
管道通信机制和消息缓冲机制_第2页
第2页 / 共14页
管道通信机制和消息缓冲机制_第3页
第3页 / 共14页
管道通信机制和消息缓冲机制_第4页
第4页 / 共14页
管道通信机制和消息缓冲机制_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《管道通信机制和消息缓冲机制》由会员分享,可在线阅读,更多相关《管道通信机制和消息缓冲机制(14页珍藏版)》请在金锄头文库上搜索。

1、实验题目管道通信机制和消息缓冲机 制小组合作否姓名班级学号一、实验目的1、理解和掌握管道通信机制中使用的系统调用命令的格式和如何利 用系统调用命令进行进程通信编程。2、理解和掌握消息缓冲机制中使用的系统调用命令的格式和如何利 用系统调用命令进行进程通信编程。二实验环境1、VMware Workstation 工作平台2、Linux 系统三、实验内容与步骤一、管道通信机制1、无名管道的通信(1) 创建无名管道的格式#include#include#includeint pipe(int filedes2);正确返回:0;错误返回:-1。(2) 无名管道pipe ()的使用16.1、使用无名管道p

2、ipe ()进行父子进程之间的通信 源程序代码如下:#include#include#includeint pipe(int filedes2);char parent=A message to pipecommunication.n; main()int pid,chan12;char buf100;pipe(chan1);pid二fork。;if(pid0)close(chan10);printf(parent process sends a message to child.n);write(chan11,parent,sizeof(parent);close(chan11);print

3、f(parent process waits the child to terminate.n); wait(O);printf(parent process terminate.n);elseclose(chan11);parentread(chan10,buf,100);printf(The message read by child process fromis:%s.n,buf);close(0);printf(child process terminaten);实验运行结果如图所示:|Ihot LiFang# ./16. L .exepaien t process sc nds a

4、rre ssage to cti i Id up-ajent pro-cess waitzs ttie ch i Id to tt jmijia te bTheread! by child process f r)rri pa r e n t is : A rressage to p ipe Drmun ica t k)ihUch i Id process tc r mina tes2、以命令行为参数的管道通信(1) 命令格式#include#include#includeFiLe popen(const char cmdstring,const char type);(2) 打开一个以命令行

5、为参数的管道文件,完成进程之间的通信16.2、以命令行为参数的管道文件的示例假设有一个可执行程序chcase,从标准输入设备读字符,将小写字母 转换成大写字母并进行输出。主程序使用popen创建管道,实现将某 文本文件中的字母转换成大写字母,期中的文本文件名作为参数传进源程序代码如下:#includesys/wait .h#includestdio.h#define MAXLINE 100 int main(int argc,char*argv)char lineMAXLINE;FILE*fpin,*fpout; if(argc!=2)fprintf(stderr,usage:a.outn);

6、 exit(l);if(fpin=fopen(argv1,r)=NULL)fpri ntf(st derr,can t open%sn,argvl); exit(l);if(fpout二popen(/root/LiFang/chcase.exe,w)=NULL) fprintf(stderr,popen errorn); exit(1);while(fge ts(line,MAXLINE,fpin)!=NULL) if(fpu ts(line,fpo ut)二二EOF) fpri ntf(st derr,fp uts error to pipe.n); exit(1);if(ferror(fp

7、in)fpri ntf(st derr,fge ts error.n); exit(1); if(pclose(fpo ut)=-1) fpri ntf(st derr,pclose error.n); exit(1);exi t(0);实验运行结果如下图所示:V ITY1 I I . tx ca t I i . tx./16 .2 .exerooIhost LlFang# rooIhost LlFang#Idjds frooIhost LlFang# usage :a .outrooIhost LlFang# VCEgSMKLDJD6Fdo tloca I hos t L l Fang ff

8、3、有名管道的通信(1)创建一个有名管道的系统调用mknod ()#include#include#include#includemode ,dev_t dev);int mknod (const char * pathname,mode_t(2) 打开一个有名管道 open (pathname, oflg);(3) 有名管道的使用16.3、创建有名管道的例子源程序代码如下:#includefcntl .hchar string二this is a example to show fifo communication; main(argc,argv)int argc; char * argv;

9、int fd;char buf256;int i;mknod(fifo,010777,0);if(argc=2) fd=open(fifo,0_WR0NLY);else fd=open(fifo,0_RD0NLY);for(i=0;i26;i+)if(argc=2)pri ntf (n I have wrot e:%s,s tring); writ e(fd,s tring,45);string0+=1;elseread(fd,buf,256);printf (nThe context by I have read is:! %s,buf); buf0=0; close(fd);试验运行结果如

10、下图所示:1心o倔loca Ihost i DDt gcc 16 t3 Lc -o lfi t3 Lexe roQtlocaIbost rout# ./16 .3.exe 1应1 5946rootlocaIhost rootiff./16.3.exehavevTote : th isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote :uti isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote : vh i sisaexa nrp letoshowf i faDITITLLn ica

11、t LDJ1havevToteisisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote :xh isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote :yh isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote : zti isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote : I ti isisaexa nrp letoshowf i faDITITLLn icat LDJ

12、1havevTote ; ti isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote : Hi isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevToteisisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote ; ti isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote :?h isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote : isisaexa nrp letoshowf i faDITITLLn icat LDJ1havew*t亡:isisaexa nrp letoshowf i faDITITLLn icat LDJ1havevTote :常 Eisaexa nrp letoshowf i faDITITLLn icat LDJ1havew*t亡:HH1 isisaexa nrp letoshowf i faDITITLLn icat LDJ1havew*t亡:SF isisa

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号