进程通信实验报告.docx

上传人:彩** 文档编号:88278417 上传时间:2023-04-24 格式:DOCX 页数:46 大小:161.53KB
返回 下载 相关 举报
进程通信实验报告.docx_第1页
第1页 / 共46页
进程通信实验报告.docx_第2页
第2页 / 共46页
点击查看更多>>
资源描述

《进程通信实验报告.docx》由会员分享,可在线阅读,更多相关《进程通信实验报告.docx(46页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、西安电子科技大学操作系统原理试验报告题目:进程通信试验报告班级:030912 姓名:王增祥学号:03091168试验内容补充说明: 一、分析和设计1. 理论分析每个Windows进程都是由一个执行体进程块EPROCESS表示。API函数CreatProcess 可以创立进程,承受管道技术可以实现进程间的相互通信。建立pipe,进程以及其子进程就 可以对该管道进程读写共享,管道读写操作利用,write、read、close进展。父进程利用pipe 发送消息,子进程利用该pipe接收父进程发来的消息;子进程利用管道向父进程发送应答, 父进程利用该pipe承受应答。2. 总体设计1、利用CreatP

2、rocess函数创立进程。2、创立管道,实现进程间的通信二、具体实现1、创立界面,承受Botton、列表框等控件创立父子界面如以下图: 父进程界面:子进程界面:其中父进程各个空间创立类向导如图:子进程创立类向导如图:2. 父进程编写(1) 创立管道:(2) 创立子进程:(3) 消息发送(4) 消息承受3. 子进程编写(1) 发送消息(2) 读消息三、试验结果点击创立子进程按钮:在创立子进程之后进展进程间的通信如以下图四、心得体会1、从试验的角度了解了进程间是怎样利用管道进展通信的,了解了进程间通信的实际过程2、进一步把握了MFC的初步编程技巧,知道了怎样调试程序。3进一步了解了,API函数的应

3、用,明白了怎样进展界面编程。4、进一步生疏了在进展进程通信的编写过程中的各个细节。六、附录Process_Father.cpp#include “stdafx.h“#include “Process_Father.h“ /包含已编写的Process_Father.h头文件#include “Process_FatherDlg.h“ /包含已编写的Process_FatherDlg.h头文件/进展宏定义#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE =FILE; #endif/创立父进程BEGIN_M

4、ESSAGE_MAP(CProcess_FatherApp, CWinApp)/AFX_MSG_MAP(CProcess_FatherApp)/ NOTE - the ClassWizard will add and remove mapping macros here./DO NOT EDIT what you see in these blocks of generated code!/AFX_MSGON_COMMAND(ID_HELP, CWinApp:OnHelp) END_MESSAGE_MAP/ CProcess_FatherApp constructionCProcess_Fat

5、herApp:CProcess_FatherApp/ TODO: add construction code here,/ Place all significant initialization in InitInstance/ The one and only CProcess_FatherApp object CProcess_FatherApp theApp;/ CProcess_FatherApp initializationBOOL CProcess_FatherApp:InitInstanceAfxEnableControlContainer; #ifdef _AFXDLLEna

6、ble3dControls;/ Call this when using MFC in a shared DLL #elseEnable3dControlsStatic;/ Call this when linking to MFC statically #endifCProcess_FatherDlg dlg; m_pMainWnd = &dlg;int nResponse = dlg.DoModal; if (nResponse = IDOK)/ TODO: Place code here to handle when the dialog is/ dismissed with OKels

7、e if (nResponse = IDCANCEL)/ TODO: Place code here to handle when the dialog is/ dismissed with Cancel/ Since the dialog has been closed, return FALSE so that we exit the/ application, rather than start the application”s message pump. return FALSE;Process_FatherDlg.cpp/ Process_FatherDlg.cpp : imple

8、mentation file/#include “stdafx.h“ #include “Process_Father.h“#include “Process_FatherDlg.h“#ifdef _DEBUG#define new DEBUG_NEW #undef THIS_FILEstatic char THIS_FILE =FILE; #endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg;/ Dialog Data/AFX_DATA(CAboutDlg)enu

9、m IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg) protected:virtual void DoDataExchange(CDataExchange* pDX);/ DDX/DDV support/AFX_VIRTUAL/ Implementation protected:/AFX_MSG(CAboutDlg)/AFX_MSG DECLARE_MESSAGE_MAP;CAboutDlg:CAboutDlg : CDialog(CAb

10、outDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAP END_MESSAGE_MAP/ CProcess_FatherDlg dialog

11、CProcess_FatherDlg:CProcess_FatherDlg(CWnd* pParent /*=NULL*/): CDialog(CProcess_FatherDlg:IDD, pParent)/AFX_DATA_INIT(CProcess_FatherDlg)/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp-LoadIcon(IDR_MAINFRAME);void CProcess_FatherDlg:DoDataEx

12、change(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CProcess_FatherDlg)DDX_Control(pDX, IDC_BT_CreateChildProcess, m_BT_CreateChildProcess);DDX_Control(pDX, IDC_Send, m_Send);DDX_Control(pDX, IDC_LISTBOX_Record, m_LISTBOX_Record); DDX_Control(pDX, IDC_EDIT_Message, m_EDIT_Message);/A

13、FX_DATA_MAPBEGIN_MESSAGE_MAP(CProcess_FatherDlg, CDialog)/AFX_MSG_MAP(CProcess_FatherDlg) ON_WM_SYSCOMMANDON_WM_PAINT ON_WM_QUERYDRAGICONON_BN_CLICKED(IDC_BT_CreateChildProcess, OnBTCreateChildProcess) ON_BN_CLICKED(IDC_Send, OnSend)/AFX_MSG_MAPON_MESSAGE(WM_CHILD_SEND,OnReceiveMsg)END_MESSAGE_MAP/

14、CProcess_FatherDlg message handlersBOOL CProcess_FatherDlg:OnInitDialogCDialog:OnInitDialog;/ Add “About.“ menu item to system menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_ST

15、RING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the application”s main window is not a dialogSetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE);/ Set big icon/ Set small icon/ TODO: Add extra initialization herereturn TRUE; / return TRUE unl

16、ess you set the focus to a controlvoid CProcess_FatherDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout; dlgAbout.DoModal;elseCDialog:OnSysCommand(nID, lParam);/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. Fo

17、r MFC applications using the document/view model,/ this is automatically done for you by the framework.void CProcess_FatherDlg:OnPaintif (IsIconic)CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc, 0);/ Center icon in client rectangleint cxIcon = G

18、etSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect;GetClientRect(&rect);int x = (rect.Width - cxIcon + 1) / 2; int y = (rect.Height - cyIcon + 1) / 2;/ Draw the icon dc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint;/ The system calls this to obtain the cursor to display w

19、hile the user drags/ the minimized window.HCURSOR CProcess_FatherDlg:OnQueryDragIconreturn (HCURSOR) m_hIcon;void CProcess_FatherDlg:OnBTCreateChildProcess/创立管道SECURITY_ATTRIBUTES sa;sa.nLength=sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor=NULL; sa.bInheritHandle=TRUE;:CreatePipe(&hPipeRead,&

20、hPipeWrite,&sa,0);:CreatePipe(&hPipeRead2,&hPipeWrite2,&sa,0);/创立子进程STARTUPINFO StartupInfo;memset(&StartupInfo,0,sizeof(STARTUPINFO) ;StartupInfo.cb=sizeof(STARTUPINFO); StartupInfo.dwFlags=STARTF_USESTDHANDLES; StartupInfo.hStdInput=hPipeRead; StartupInfo.hStdOutput=hPipeWrite; StartupInfo.hStdErr

21、or=GetStdHandle(STD_ERROR_HANDLE);PROCESS_INFORMATION ProcessInfo;:CreateProcess(“Process_Child.exe“,NULL,NULL,NULL,TRUE,0,NULL,NULL,&Startu pInfo,&ProcessInfo);m_BT_CreateChildProcess.EnableWindow(FALSE);void CProcess_FatherDlg:OnSendCString str;char ss20=“Father:“; m_EDIT_Message.GetWindowText(str

22、); DWORD dwWritten;if(!WriteFile(hPipeWrite,str,40,&dwWritten,NULL)MessageBox(TEXT(“写错误“),“警告“,MB_OK|MB_ICONWARNING);CString strWinName = “Process_Child“;CWnd *pWnd=CWnd:FindWindow(NULL,strWinName); if(pWnd)pWnd-SendMessage(WM_FATHER_SEND,0,0);strcat(ss,str); m_LISTBOX_Record.InsertString(-1,ss); m_

23、EDIT_Message.SetWindowText(“);elseMessageBox(“没有觉察子进程“,“错误“);void CProcess_FatherDlg:OnReceiveMsg(WPARAM wParam,LPARAM lParam)DWORD dwRead;TCHAR s40;HANDLE hPipeRead2; hPipeRead2=GetStdHandle(STD_INPUT_HANDLE); if(!ReadFile(hPipeRead,s,40,&dwRead,NULL)MessageBox(TEXT(“读错误!“),“警告“,MB_OK|MB_ICONWARNIN

24、G);char str60=“Child: “; strcat(str,s);m_LISTBOX_Record.InsertString(-1,str);Process_Father.h/ Process_Father.h : main header file for the PROCESS_FATHER application/#if !defined(AFX_PROCESS_FATHER_H1F9659A2_2B93_4C1E_89C5_5A88971D3DDAINCLUD ED_)#define AFX_PROCESS_FATHER_H1F9659A2_2B93_4C1E_89C5_5A

25、88971D3DDAINCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000#ifndefAFXWIN_H #error include ”stdafx.h” before including this file for PCH #endif#include “resource.h“/ main symbols/ CProcess_FatherApp:/ See Process_Father.cpp for the implementation of this class/class CProcess_FatherApp : pu

26、blic CWinApppublic:CProcess_FatherApp;/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CProcess_FatherApp) public:virtual BOOL InitInstance;/AFX_VIRTUAL/ Implementation/AFX_MSG(CProcess_FatherApp)/ NOTE - the ClassWizard will add and remove member functions here./DO NOT EDIT

27、 what you see in these blocks of generated code !/AFX_MSG DECLARE_MESSAGE_MAP;/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif/ !defined(AFX_PROCESS_FATHER_H1F9659A2_2B93_4C1E_89C5_5A88971D3DDAINCLUDED_)Process_FatherDlg.h/ Pro

28、cess_FatherDlg.h : header file/#if !defined(AFX_PROCESS_FATHERDLG_H69E2942A_7A5F_413F_B4A3_AFB8C1F51DFEINC LUDED_)#defineAFX_PROCESS_FATHERDLG_H69E2942A_7A5F_413F_B4A3_AFB8C1F51DFEINCLUDED_ #define WM_FATHER_SEND WM_USER+100#define WM_CHILD_SEND WM_USER+101#if _MSC_VER 1000#pragma once#endif / _MSC_

29、VER 1000/ CProcess_FatherDlg dialogclass CProcess_FatherDlg : public CDialog/ Construction public:CProcess_FatherDlg(CWnd* pParent = NULL);/ standard constructor/ Dialog Data/AFX_DATA(CProcess_FatherDlg)enum IDD = IDD_PROCESS_FATHER_DIALOG ;CButton m_BT_CreateChildProcess; CButton m_Send;CListBoxm_L

30、ISTBOX_Record;CEditm_EDIT_Message;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CProcess_FatherDlg) protected:virtual void DoDataExchange(CDataExchange* pDX);/ DDX/DDV support/AFX_VIRTUAL/ Implementation protected:HICON m_hIcon;/ Generated message map functions/AFX_MSG(CPro

31、cess_FatherDlg) virtual BOOL OnInitDialog;afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint;afx_msg HCURSOR OnQueryDragIcon;afx_msg void OnReceiveMsg(WPARAM wParam, LPARAM lParam); afx_msg void OnBTCreateChildProcess;afx_msg void OnSend;/AFX_MSG DECLARE_MESSAGE_MAPprivate:HAND

32、LE hPipeWrite2;HANDLE hPipeRead2;HANDLE hPipeWrite;HANDLE hPipeRead;/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before theprevious line.#endif/ !defined(AFX_PROCESS_FATHERDLG_H69E2942A_7A5F_413F_B4A3_AFB8C1F51DFEINCL UDED_)子进程代码Process_Child.cpp/ Process

33、_Child.cpp : Defines the class behaviors for the application./#include “stdafx.h“ #include “Process_Child.h“#include “Process_ChildDlg.h“#ifdef _DEBUG#define new DEBUG_NEW #undef THIS_FILEstatic char THIS_FILE =FILE; #endif/ CProcess_ChildAppBEGIN_MESSAGE_MAP(CProcess_ChildApp, CWinApp)/AFX_MSG_MAP(

34、CProcess_ChildApp)/ NOTE - the ClassWizard will add and remove mapping macros here./DO NOT EDIT what you see in these blocks of generated code!/AFX_MSGON_COMMAND(ID_HELP, CWinApp:OnHelp) END_MESSAGE_MAP/ CProcess_ChildApp constructionCProcess_ChildApp:CProcess_ChildApp/ TODO: add construction code h

35、ere,/ Place all significant initialization in InitInstance/ The one and only CProcess_ChildApp objectCProcess_ChildApp theApp;/ CProcess_ChildApp initializationBOOL CProcess_ChildApp:InitInstanceAfxEnableControlContainer;/ Standard initialization/ If you are not using these features and wish to redu

36、ce the size/ of your final executable, you should remove from the following/ the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls;/ Call this when using MFC in a shared DLL#elseEnable3dControlsStatic;/ Call this when linking to MFC statically #endifCProcess_ChildDlg dl

37、g; m_pMainWnd = &dlg;int nResponse = dlg.DoModal; if (nResponse = IDOK)/ TODO: Place code here to handle when the dialog is/ dismissed with OKelse if (nResponse = IDCANCEL)/ TODO: Place code here to handle when the dialog is/ dismissed with Cancel/ Since the dialog has been closed, return FALSE so t

38、hat we exit the/ application, rather than start the application”s message pump. return FALSE;Process_ChildDlg.cpp/ Process_ChildDlg.cpp : implementation file/#include “stdafx.h“ #include “Process_Child.h“#include “Process_ChildDlg.h“#ifdef _DEBUG#define new DEBUG_NEW #undef THIS_FILEstatic char THIS_FILE =FILE; #endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg;

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

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁