进程通信实验报告.docx
西安电子科技大学操作系统原理试验报告题目:进程通信试验报告班级:030912 姓名:王增祥学号:03091168试验内容补充说明: 一、分析和设计1. 理论分析每个Windows进程都是由一个执行体进程块EPROCESS表示。API函数CreatProcess 可以创立进程,承受管道技术可以实现进程间的相互通信。建立pipe,进程以及其子进程就 可以对该管道进程读写共享,管道读写操作利用,write、read、close进展。父进程利用pipe 发送消息,子进程利用该pipe接收父进程发来的消息;子进程利用管道向父进程发送应答, 父进程利用该pipe承受应答。2. 总体设计1、利用CreatProcess函数创立进程。2、创立管道,实现进程间的通信二、具体实现1、创立界面,承受Botton、列表框等控件创立父子界面如以下图: 父进程界面:子进程界面:其中父进程各个空间创立类向导如图:子进程创立类向导如图:2. 父进程编写(1) 创立管道:(2) 创立子进程:(3) 消息发送(4) 消息承受3. 子进程编写(1) 发送消息(2) 读消息三、试验结果点击创立子进程按钮:在创立子进程之后进展进程间的通信如以下图四、心得体会1、从试验的角度了解了进程间是怎样利用管道进展通信的,了解了进程间通信的实际过程2、进一步把握了MFC的初步编程技巧,知道了怎样调试程序。3进一步了解了,API函数的应用,明白了怎样进展界面编程。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_MESSAGE_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_FatherApp: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 _AFXDLLEnable3dControls;/ 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 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 that we exit the/ application, rather than start the application”s message pump. return FALSE;Process_FatherDlg.cpp/ Process_FatherDlg.cpp : implementation 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)enum 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(CAboutDlg: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 dialogCProcess_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:DoDataExchange(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);/AFX_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/ 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 < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL)CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty)pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, 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 unless 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. For 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 = GetSystemMetrics(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 while 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,&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.hStdError=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); 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_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_ICONWARNING);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_5A88971D3DDAINCLUDED_#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 : public 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 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/ Process_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_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_LISTBOX_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(CProcess_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:HANDLE 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_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(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 here,/ 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 reduce 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 dlg; 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 that 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;