1、进程通信实验报告西安电子科技大学操作系统原理实验报告题目: 进程通信实验报告 班级: 030912 姓名: 王增祥 学号: 03091168 实验内容补充说明:一、分析和设计1 理论分析 每个Windows进程都是由一个执行体进程块(EPROCESS)表示。API函数CreatProcess可以创建进程,采用管道技术可以实现进程间的相互通信。建立pipe,进程以及其子进程就可以对该管道进程读写共享,管道读写操作利用,write、read、close进行。父进程利用pipe发送消息,子进程利用该pipe接收父进程发来的消息;子进程利用管道向父进程发送应答,父进程利用该pipe接受应答。2 总体设
2、计1、利用CreatProcess函数创建进程。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_MESSA
4、GE_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_MSG ON_COMMAND(ID_HELP, CWinApp:OnHelp)END_MESSAGE_MAP()/ CProcess_FatherApp constructionCProcess_
5、FatherApp:CProcess_FatherApp() / TODO: add construction code here, / Place all significant initialization in InitInstance/ The one and only CProcess_FatherApp objectCProcess_FatherApp theApp;/ CProcess_FatherApp initializationBOOL CProcess_FatherApp:InitInstance() AfxEnableControlContainer();#ifdef
6、_AFXDLL Enable3dControls(); / Call this when using MFC in a shared DLL#else Enable3dControlsStatic(); / Call this when linking to MFC statically#endif CProcess_FatherDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse = IDOK) / TODO: Place code here to handle when the dialog is
7、/ dismissed with OK else 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 applications message pump. return FALSE;Process_FatherDlg.cpp/ Pro
8、cess_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
9、 /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/ Implementationprotected: /AFX_MSG(CAboutDlg) /AFX_MSG DECLARE_MESSAGE_MAP()
10、;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_MA
11、PEND_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()-LoadI
12、con(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
13、); DDX_Control(pDX, IDC_EDIT_Message, m_EDIT_Message); /AFX_DATA_MAPBEGIN_MESSAGE_MAP(CProcess_FatherDlg, CDialog) /AFX_MSG_MAP(CProcess_FatherDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BT_CreateChildProcess, OnBTCreateChildProcess) ON_BN_CLICKED(IDC_Send, OnSend)
14、/AFX_MSG_MAP ON_MESSAGE(WM_CHILD_SEND,OnReceiveMsg)END_MESSAGE_MAP()/ CProcess_FatherDlg message handlersBOOL CProcess_FatherDlg:OnInitDialog() CDialog:OnInitDialog(); / Add About. menu item to system menu. / IDM_ABOUTBOX must be in the system command range. ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUT
15、BOX); ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR); pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); / Set the icon for this dialog. The framework does this automatically / when the applications main window is not a dialog SetIcon(m_hIcon, TRUE); / Set big icon SetIcon(m_hIcon, FALSE); /
16、Set small icon / TODO: Add extra initialization here return 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(); else CDialog:OnSysCommand(nID, lParam); / If yo
17、u 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:OnPaint() if (IsIconic() CPaintDC dc(this); / device context for painting SendMessa
18、ge(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); / Center icon in client rectangle int 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 ic
19、on dc.DrawIcon(x, y, m_hIcon); else CDialog:OnPaint(); / The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CProcess_FatherDlg:OnQueryDragIcon() return (HCURSOR) m_hIcon;void CProcess_FatherDlg:OnBTCreateChildProcess() /创建管道 SECURITY_ATTRIBUTES s
20、a; 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); Sta
21、rtupInfo.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,&StartupInfo,&ProcessInfo); m_BT_CreateChi
22、ldProcess.EnableWindow(FALSE); void CProcess_FatherDlg:OnSend() CString 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:Find
23、Window(NULL,strWinName); if(pWnd) pWnd-SendMessage(WM_FATHER_SEND,0,0); strcat(ss,str); m_LISTBOX_Record.InsertString(-1,ss); m_EDIT_Message.SetWindowText(); else MessageBox(没有发现子进程,错误); void CProcess_FatherDlg:OnReceiveMsg(WPARAM wParam,LPARAM lParam) DWORD dwRead; TCHAR s40; HANDLE hPipeRead2; hPi
24、peRead2=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 !def
25、ined(AFX_PROCESS_FATHER_H_1F9659A2_2B93_4C1E_89C5_5A88971D3DDA_INCLUDED_)#define AFX_PROCESS_FATHER_H_1F9659A2_2B93_4C1E_89C5_5A88971D3DDA_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000#ifndef _AFXWIN_H_ #error include stdafx.h before including this file for PCH#endif#include resource.
26、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()
27、; /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_H_1F9659A2_2B93_4C1E_89C5_5A88971D3DDA_INCLUDED_)Process_FatherDlg.h/ Process_FatherDlg.h : header file/#if !defined(AFX_PROCESS_FATHERDLG_H_69E2942A_
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1