zoukankan      html  css  js  c++  java
  • windows系统调用 利用事件对象实现进程通信

     1 #include "iostream"
     2 #include "windows.h"
     3 #include "cstring"
     4 using namespace std;
     5 
     6 static LPCTSTR g_szContinueEvent="w2kdg.EventDemo.event.Continue";
     7 
     8 BOOL CreateChild(){
     9     TCHAR szFilename[MAX_PATH];
    10     GetModuleFileName(NULL,szFilename,MAX_PATH);
    11 
    12     TCHAR szCmdLine[MAX_PATH];
    13     sprintf_s(szCmdLine,""%s" child",szFilename);
    14 
    15     STARTUPINFO si;
    16     ZeroMemory(reinterpret_cast<void*>(&si),sizeof(si));
    17     si.cb=sizeof(si);
    18 
    19     PROCESS_INFORMATION pi;
    20 
    21     BOOL bCreateOK=CreateProcess(
    22         szFilename,
    23         szCmdLine,
    24         NULL,
    25         NULL,
    26         FALSE,
    27         0,
    28         NULL,
    29         NULL,
    30         &si,
    31         &pi
    32         
    33         );
    34 
    35     if(bCreateOK){
    36         CloseHandle(pi.hProcess);
    37         CloseHandle(pi.hThread);
    38     }
    39 
    40     return (bCreateOK);
    41 
    42 }
    43 
    44 void WaitForChild(){
    45     HANDLE hEventContinue=CreateEvent(
    46         NULL,
    47         TRUE,
    48         FALSE,
    49         g_szContinueEvent
    50         );
    51 
    52     if(hEventContinue!=NULL){
    53         printf("Event creates.
    ");
    54         if(CreateChild()){
    55             printf("Child process created.
    ");
    56             printf("Parent process waiting on child process.
    ");
    57             WaitForSingleObject(hEventContinue,INFINITE);
    58             printf("Parent process received the event signaling from child process.
    ");
    59         }
    60 
    61         CloseHandle(hEventContinue);
    62     }
    63 
    64 }
    65 
    66 void SignalParent(){
    67     HANDLE hEventContinue=OpenEvent(
    68         EVENT_MODIFY_STATE,
    69         FALSE,
    70         g_szContinueEvent
    71         );
    72 
    73     if(hEventContinue!=NULL){
    74         SetEvent(hEventContinue);
    75         printf("Child process begining...
    ");
    76     }
    77 
    78     CloseHandle(hEventContinue);
    79 }
    80 
    81 int main(int argc,char *argv[]){
    82     if(argc>1&&strcmp(argv[1],"child")==0){
    83         SignalParent();
    84     }
    85     else{
    86     
    87     WaitForChild();
    88     printf("Parent process released.
    ");
    89     }
    90     getchar();
    91     return 0;
    92 
    93 }
  • 相关阅读:
    Embeded Linux 之 PHY
    Embeded Linux之网络子系统
    语言之内联函数
    Embeded Linux之海思UART
    Windows 之samba问题
    Embeded linux 之 CIFS 文件操作源码分析
    zookeeper 都有哪些使用场景?
    如何保证分布式系统中接口调用的顺序性?
    分布式系统中接口的幂等性该如何保证?比如不能重复扣款?
    Redis 的并发竞争问题是什么?如何解决这个问题?了解 redis 事务的 CAS 方案吗?
  • 原文地址:https://www.cnblogs.com/593213556wuyubao/p/3786414.html
Copyright © 2011-2022 走看看