您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > Windows进程和进程间通信
Windows进程和进程间通信Windows进程nWin32操作系统平台提供了强大的多任务功能,其中“进程”(Process)和“线程”(Thread)是其控制多任务的两个重要概念n进程有独自的内存空间、程序代码、信息以及一堆大大小小的系统资源nWindows中的每个Win32进程都由一个执行体进程块(executiveprocessblock)表示,执行体进程块描述进程的基本信息,并指向其他与进程控制相关的数据结构Windows的进程n进程属性:PID,PCB,AccessToken,BasePriority,句柄表,指向进程环境块PEB指针,默认和处理器集合等n在Windows中,PCB也称为内核进程对象KPROCESSn执行体进程对象EPROCESSnEPOCESS和KPROCESS位于内核空间,PEB位于用户空间kernelprocess(KPROCESS)blockprocessenvironmentblock(PEB)Win32子系统的进程控制系统调用nWin32子系统的进程控制系统调用主要有:CreateProcess、ExitProcess、TerminateProcesslCreateProcess用于进程创建lExitProcess和TerminateProcess用于进程退出创建进程nCreateProcess()函数用于创建新进程及其主线程,以执行指定的程序n新进程可以继承:打开文件的句柄、各种对象(如进程、线程、信号量、管道等)的句柄、环境变量、当前目录、原进程的控制终端、原进程的进程组(用于发送Ctrl+C或Ctrl+Break信号给多个进程)--每个句柄在创建或打开时能指定是否可继承n新进程不能继承:优先权类、内存句柄、DLL模块句柄创建子进程API函数n创建进程函数,CreateProcess函数,其原型为:BOOLCreateProcess(LPCTSTRlpApplicationName,LPTSTRlpCommandLine,LPSECURITY_ATTRIBUTESlpProcessAttributes,LPSECURITY_ATTRIBUTESlpThreadAttributes,BOOLbInheritHandles,DWORDdwCreationFlags,LPVOIDlpEnvironment,LPCTSTRlpCurrentDirectory,LPSTARTURINFOlpStartupInfo,LPPROCESS_INFORMATIONlpProcessInformation);结束processn如果某个process想停止执行,可调用ExitProcess(),不过我们通常不直接调用它,而是调用C程序库中的exit(),exit()在自动执行一些清除垃圾的工作之后,再调用ExitProcess()。VOIDExitProcess(UNTuExitCode);n如果processA想要迫使processB停止执行,可以在取得processB的handle之后,调用TerminateProcess():BOOLTerminateProcess(HANDLEhProcess,UNITuExitCode);与进程相关的API函数FunctionDescriptionnCreateProcessCreatesanewprocessandthreadusingthecaller'ssecurityidentificationnCreateProcessAsUserCreatesanewprocessandthreadwiththespecifiedalternatesecuritytokennCreateProcessWithLogonWCreatesanewprocessandthreadtorununderthecredentialsofthespecifiedusernameandpasswordnCreateProcessWithTokenWCreatesanewprocessandthreadwiththespecifiedalternatesecuritytoken,withadditionaloptionssuchasallowingtheuserprofiletobeloadednOpenProcessReturnsahandletothespecifiedprocessobjectnExitProcessEndsaprocess,andnotifiesallattachedDLLsnTerminateProcessEndsaprocesswithoutnotifyingtheDLLsnFlushInstructionCacheEmptiesthespecifiedprocess'sinstructioncachenGetProcessTimesObtainsaprocess'stiminginformation,describinghowmuchtimetheprocesshasspentinuserandkernelmodenGetExitCodeProcessReturnstheexitcodeforaprocess,indicatinghowandwhytheprocessshutdownnGetCommandLineReturnsapointertothecommand-linestringpassedtothecurrentprocessnGetCurrentProcessReturnsapseudohandleforthecurrentprocessnGetCurrentProcessIdReturnstheIDofthecurrentprocessnGetProcessVersionReturnsthemajorandminorversionsoftheWindowsversiononwhichthespecifiedprocessexpectstorunnGetStartupInfoReturnsthecontentsoftheSTARTUPINFOstructurespecifiedduringCreateProcessnGetEnvironmentStringsReturnstheaddressoftheenvironmentblocknGetEnvironmentVariableReturnsaspecificenvironmentvariablenGet/SetProcessShutdownParametersDefinestheshutdownpriorityandnumberofretriesforthecurrentprocessnGetGuiResourcesReturnsacountofUserandGDIhandles例子-子进程nChildProcess:#includestdafx.h#includeconio.hint_tmain(intargc,_TCHAR*argv[]){wprintf(LParamTestOutput:\n);wprintf(LNumberofparameters:%d\n,argc);wprintf(LParameterInfo:\n);for(intc=0;cargc;c++){wprintf(LParam#%d:%s\n,c,argv[c]);}return0;}例子-父进程#includestdafx.h#includewindows.h#includestrsafe.h#includedirect.h#includestring.hint_tmain(intargc,_TCHAR*argv[]){//AssumesParamTest.exeisinthecurrentworkingdirectoryRunTest(Li:\\childpro.exe);return0;}例子-父进程voidRunTest(TCHAR*AppName,TCHAR*CmdLine){PROCESS_INFORMATIONprocessInformation;STARTUPINFOstartupInfo;memset(&processInformation,0,sizeof(processInformation));memset(&startupInfo,0,sizeof(startupInfo));startupInfo.cb=sizeof(startupInfo);nBOOLresult;nresult=::CreateProcess(AppName,CmdLine,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&startupInfo,&processInformation);例子-父进程if(result==0){wprintf(LERROR:CreateProcessfailed!);}else{WaitForSingleObject(processInformation.hProcess,INFINITE);CloseHandle(processInformation.hProcess);CloseHandle(processInformation.hThread);}}Windows进程间通信Windows进程间通信方式18Windows基于文件映射的共享存储区返回n将整个文件映射为进程虚拟地址空间的一部分来访问。在CreateFileMapping和OpenFileMapping时可指定对象名称。lCreateFileMapping为指定文件创建一个文件映射对象,返回对象指针;lOpenFileMapping打开一个命名的文件映射对象,返回对象指针;lMapViewOfFile把文件映射到本进程的地址空间,返回映射地址空间的首地址;n这时可利用首地址进行读写;lFlushViewOfFile可把映射地址空间的内容写到物理文件中;lUnmapViewOfFile拆除文件映射与本进程地址空间间映射关系;n随后,可利用CloseHandle关闭文件映射对象;例子-主程序#includestdafx.h#includewindows.h#includestdio.h#includeconio.h#includetchar.h#defineBUF_SIZE256TCHARszName[]=TEXT(MyFileMappingObject111);TCHARszMsg[]=TEXT(Messagefromfirstprocess.);int_tmain(){HANDLEhMapFile;LPCTSTRpBuf;例子-主程序hMapFile=CreateFileMapping(INVALID_HANDLE_VALUE,//usepagingfileNULL,//defaultsecurityPAGE_READWRITE,//read/writeaccess0,//maximumobjectsize(high-orderDWORD)BUF_SIZE,//maximumobjectsize(low-orderDWORD)szName);//nameofmappingobjectif(hMapFile==NULL){_tprintf(TEXT(Couldnotcreatefilemappingobject(%d).\n),GetLastError());return1;}例子-主程序pBuf=(LPTSTR)MapViewOfFile(hMapFile,//handletomapobjectFILE_MAP_ALL_ACCESS,//read/writepermission0,0,BUF_SIZE);if(pBuf==NULL){_tprintf(TEXT(Couldnotmapviewoffile(%d).\n)
本文标题:Windows进程和进程间通信
链接地址:https://www.777doc.com/doc-3262723 .html