资源描述:
《实验四使用命名管道实现进程通信.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、《操作系统》实验报告学生姓名:张鹏学号:实验地点:数计学院407实验室实验课时:3学时实验器材:计算机课程名称:计算机操作系统实验名称:2.4实验四:使用命名管道实现进程通信一、实验目的1、进一步掌握windows系统环境下进程通信机制。2、熟悉进程通信API二、实验环境及工具Windows7操作系统,VC6三、实验内容使用Windows系统提供的命名管道完成两个进程之间的通信,要求能正确使用创建命名管道CreateNamePipe()、连接命名管道ConnectNamePipe()、拆除命名管道的连接DisconnectNamePipe()、连接
2、服务器已建立的命名管道CallNamePipe()、等待命名管道WaitNamePipe()等API.四、实验步骤服务端//PipeServer.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdafx.h"#include"PipeServer.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif//////////////////////////////
3、/////////////////////////////////////////////////TheoneandonlyapplicationobjectCWinApptheApp;usingnamespacestd;-6-《操作系统》实验报告int_tmain(intargc,TCHAR*argv[],TCHAR*envp[]){intnRetCode=0;interr;BOOLrc;HANDLEhPipeHandle1;charlpName[]="\\.\pipe\myPipe";charInBuffer[50]="";charOu
4、tBuffer[50]="";DWORDBytesRead,BytesWrite;hPipeHandle1=CreateNamedPipe((LPCTSTR)lpName,PIPE_ACCESS_DUPLEX
5、FILE_FLAG_OVERLAPPED
6、WRITE_DAC,PIPE_TYPE_MESSAGE
7、PIPE_READMODE_BYTE
8、PIPE_WAIT,1,20,30,NMPWAIT_USE_DEFAULT_WAIT,(LPSECURITY_ATTRIBUTES)NULL);if((hPipeHandle1==INVALID_HANDLE
9、_VALUE)
10、
11、(hPipeHandle1==NULL)){err=GetLastError();printf("ServerPipeCreatFail!err=%d",err);exit(1);}elseprintf("ServerPipeCreatSuccess!");while(1){rc=ConnectNamedPipe(hPipeHandle1,(LPOVERLAPPED)NULL);if(rc==0){err=GetLastError();printf("ServerPipeConnectFailerr=%d",err);
12、exit(2);}elseprintf("ServerPipeConnectSuccess");strcpy(InBuffer,"");strcpy(OutBuffer,"");rc=ReadFile(hPipeHandle1,InBuffer,sizeof(InBuffer),&BytesRead,(LPOVERLAPPED)NULL);if(rc==0&&BytesRead==0)-6-《操作系统》实验报告{err=GetLastError();printf("ServerReadPipeFailerr=%d",err);exit(3)
13、;}else{printf("ServerReadPipeSuccess!DATAfromClientis=%s",InBuffer);}rc=strcmp(InBuffer,"end");if(rc==0)printf("ServerWritePipeFail!");elseprintf("ServerWritePipeSuccess!");DisconnectNamedPipe(hPipeHandle1);rc=strcmp(OutBuffer,"end");if(rc==0)break;}printf("NowServerbe
14、END!");CloseHandle(hPipeHandle1);/*//initializeMFCandprinta