文档介绍:实验题目管道通信机制和消息缓冲机小组合作否制姓名班级学 号一、实验目的1、理解和掌握管道通信机制中使用的系统调用命令的格式和如何利用系统调用命令进行进程通信编程。2、理解和掌握消息缓冲机制中使用的系统调用命令的格式和如何利用系统调用命令进行进程通信编程。、VMwareWorkstation工作平台2、Linux系统三、实验内容与步骤一、管道通信机制1、无名管道的通信(1)创建无名管道的格式#include<sys/>#include<>#include<>intpipe(intfiledes[2]);正确返回:0;错误返回:-1。(2)无名管道pipe()、使用无名管道 pipe()进行父子进程之间的通信源程序代码如下:#include<sys/>#include<>#include<>intpipe(intfiledes[2]);charparent[]="munication.\n";main(){intpid,chan1[2];charbuf[100];pipe(chan1);pid=fork();if(pid<0){printf("tocreatechilderror\n");exit(1);}if(pid>0){close(chan1[0]);printf("parentprocesssendsamessagetochild.\n");write(chan1[1],parent,sizeof(parent));close(chan1[1]);printf("parentprocesswaitsthechildtoterminate.\n");wait(0);printf("parentprocessterminate.\n");}else{close(chan1[1]);read(chan1[0],buf,100);printf("Themessagereadbychildprocessfromparentis:%s.\n",buf);close(0);printf("childprocessterminate\n");}}实验运行结果如图所示:2、以命令行为参数的管道通信(1)命令格式#include<>#include<sys/>#include<>FiLepopen(constcharcmdstring,constchartype);(2)打开一个以命令行为参数的管道文件,、以命令行为参数的管道文件的示例假设有一个可执行程序 chcase,从标准输入设备读字符,将小写字母转换成大写字母并进行输出。 主程序使用popen创建管道,实现将某文本文件中的字母转换成大写字母, 期中的文本文件名作为参数传进来。源程序代码如下:#include<sys/>#include<>#defineMAXLINE100intmain(intargc,char*argv[]){charline[MAXLINE];FILE*fpin,*fpout;if(argc!=2){fprintf(stderr,"usage:<pathname>\n");exit(1);}if((fpin=fopen(argv[1],"r"))==NULL){fprintf(stderr,"can'topen%s\n",argv[1]);exit(1);}if((fpout=popen("/root/LiFang/","w"))==NULL){fprintf(stderr,"popenerror\n");exit(1);}while((fgets(line,MAXLINE,fpin))!=NULL){if(fputs(line,fpout)==EOF){fprintf(stderr,"fputserrortopipe.\n");exit(1);}}if(ferror(fpin)){fprintf(stderr,"fgetserror.\n");exit(1);}if(pclose(fpout)==-1){fprintf(stderr,"pcloseerror.\n");exit(1);}exit(0);}实验运行结果如下图所示:3、有名管道的通信(1)创建一个有名管道的系统调用 mknod()#include<sys/>#include<sys/>#include<>#include<>intmknod(constchar*pathname,mode_tmode,