进程通信实验报告.docx
《进程通信实验报告.docx》由会员分享,可在线阅读,更多相关《进程通信实验报告.docx(36页珍藏版)》请在冰豆网上搜索。
![进程通信实验报告.docx](https://file1.bdocx.com/fileroot1/2023-4/18/a8d61b94-a647-49cf-ba2d-6f40f3c2d8eb/a8d61b94-a647-49cf-ba2d-6f40f3c2d8eb1.gif)
进程通信实验报告
西安电子科技大学
《操作系统原理》实验报告
题目:
进程通信实验报告
班级:
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
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
//创建父进程
BEGIN_MESSAGE_MAP(CProcess_FatherApp,CWinApp)
//{{AFX_MSG_MAP(CProcess_FatherApp)
//NOTE-theClassWizardwilladdandremovemappingmacroshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG
ON_COMMAND(ID_HELP,CWinApp:
:
OnHelp)
END_MESSAGE_MAP()
//CProcess_FatherAppconstruction
CProcess_FatherApp:
:
CProcess_FatherApp()
{
//TODO:
addconstructioncodehere,
//PlaceallsignificantinitializationinInitInstance
}
//TheoneandonlyCProcess_FatherAppobject
CProcess_FatherApptheApp;
//CProcess_FatherAppinitialization
BOOLCProcess_FatherApp:
:
InitInstance()
{
AfxEnableControlContainer();
#ifdef_AFXDLL
Enable3dControls();//CallthiswhenusingMFCinasharedDLL
#else
Enable3dControlsStatic();//CallthiswhenlinkingtoMFCstatically
#endif
CProcess_FatherDlgdlg;
m_pMainWnd=&dlg;
intnResponse=dlg.DoModal();
if(nResponse==IDOK)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithOK
}
elseif(nResponse==IDCANCEL)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithCancel
}
//Sincethedialoghasbeenclosed,returnFALSEsothatweexitthe
//application,ratherthanstarttheapplication'smessagepump.
returnFALSE;
}
Process_FatherDlg.cpp
//Process_FatherDlg.cpp:
implementationfile
//
#include"stdafx.h"
#include"Process_Father.h"
#include"Process_FatherDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CProcess_FatherDlgdialog
CProcess_FatherDlg:
:
CProcess_FatherDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CProcess_FatherDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CProcess_FatherDlg)
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCProcess_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_MAP
}
BEGIN_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)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CHILD_SEND,OnReceiveMsg)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CProcess_FatherDlgmessagehandlers
BOOLCProcess_FatherDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCProcess_FatherDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCProcess_FatherDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCProcess_FatherDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
voidCProcess_FatherDlg:
:
OnBTCreateChildProcess()
{
//创建管道
SECURITY_ATTRIBUTESsa;
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor=NULL;
sa.bInheritHandle=TRUE;
:
:
CreatePipe(&hPipeRead,&hPipeWrite,&sa,0);
:
:
CreatePipe(&hPipeRead2,&hPipeWrite2,&sa,0);
//创建子进程
STARTUPINFOStartupInfo;
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_INFORMATIONProcessInfo;
:
:
CreateProcess("Process_Child.exe",NULL,NULL,NULL,TRUE,0,NULL,NULL,&StartupInfo,&ProcessInfo);
m_BT_CreateChildProcess.EnableWindow(FALSE);
}
voidCProcess_FatherDlg:
:
OnSend()
{
CStringstr;
charss[20]="Father:
";
m_EDIT_Message.GetWindowText(str);
DWORDdwWritten;
if(!
WriteFile(hPipeWrite,str,40,&dwWritten,NULL))
{
MessageBox(TEXT("写错误"),"警告",MB_OK|MB_ICONWARNING);
}
CStringstrWinName="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("");
}
else
{
MessageBox("没有发现子进程","错误");
}
}
voidCProcess_FatherDlg:
:
OnReceiveMsg(WPARAMwParam,LPARAMlParam)
{
DWORDdwRead;
TCHARs[40];
HANDLEhPipeRead2;
hPipeRead2=GetStdHandle(STD_INPUT_HANDLE);
if(!
ReadFile(hPipeRead,s,40,&dwRead,NULL))
{
MessageBox(TEXT("读错误!
"),"警告",MB_OK|MB_ICONWARNING);
}
charstr[60]="Child:
";
strcat(str,s);
m_LISTBOX_Record.InsertString(-1,str);
}
Process_Father.h
//Process_Father.h:
mainheaderfileforthePROCESS_FATHERapplication
//
#if!
defined(AFX_PROCESS_FATHER_H__1F9659A2_2B93_4C1E_89C5_5A88971D3DDA__INCLUDED_)
#defineAFX_PROCESS_FATHER_H__1F9659A2_2B93_4C1E_89C5_5A88971D3DDA__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
#ifndef__AFXWIN_H__
#errorinclude'stdafx.h'beforeincludingthisfileforPCH
#endif
#include"resource.h"//mainsymbols
/////////////////////////////////////////////////////////////////////////////
//CProcess_FatherApp:
//SeeProcess_Father.cppfortheimplementationofthisclass
//
classCProcess_FatherApp:
publicCWinApp
{
public:
CProcess_FatherApp();
//Overrides
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CProcess_FatherApp)
public:
virtualBOOLInitInstance();
//}}AFX_VIRTUAL
//Implementation
//{{AFX_MSG(CProcess_FatherApp)
//NOTE-theClassWizardwilladdandremovememberfunctionshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
defined(AFX_PROCESS_FATHER_H__1F9659A2_2B93_4C1E_89C5_5A88971D3DDA__INCLUDED_)
Process_FatherDlg.h
//Process_FatherDlg.h:
headerfile
//
#if!
defined(AFX_PROCESS_FATHERDLG_H__69E2942A_