简体   繁体   中英

Restarting a process from another process

I have two processes: ProcessA and ProcessB.

When i launch my application, i call ProcessA which uses CreateProcess() to launch ProcessB. ProcessA is killed by ProcessB when my application receives command A. Likewise, ProcessB should relaunch ProcessA when it receives command B.

Where i am stuck on is on the process of relaunching ProcessA. Since ProcessA has the code to relaunch ProcessB, i cannot stop it from relaunching another instance of ProcessB. Ideally, i want to have only 1 instance of ProcessB.

For creating ProcessB from ProcessA, i have the following code:

for(int i32Index = 0; i32Index < NUM_PROCESS; i32Index++)
        {
            wcscpy(wcPath,Directorypath.c_str());
            wcscat(wcPath,wcProcess[i32Index]);
            RETAILMSG(1,(_T("Path:%s\r\n"),wcPath));
            bCreateProcessSuccess = CreateProcess(wcPath,   // No module name (use command line)
                                    NULL,                   // Command line
                                    NULL,                   // Process handle not inheritable
                                    NULL,                   // Thread handle not inheritable
                                    FALSE,                  // Set handle inheritance to FALSE
                                    0,                      // No creation flags
                                    NULL,                   // Use parent's environment block
                                    NULL,                   // Use parent's starting directory 
                                    &si,                    // Pointer to STARTUPINFO structure
                                    &pi ) ;                 // Pointer to PROCESS_INFORMATION structure
             if(bCreateProcessSuccess == FALSE)
             {
                 RETAILMSG(1,(_T("Create process failed:%d\r\n"),GetLastError()));
             }
             else
             {
                RETAILMSG(1,(_T("Loading Exes\r\n")));
             }

Pretty straightforward, basic code. I basically repeat this in ProcessB, but so that it creates ProcessA.

Now, im stuck on how i would be able to implement the condition to launch ProcessA without it launching ProcessB again. I initially thought of using a flag, but that flag would be reset since launching ProcessA would reset the flag as it is local to the function.

Also, to clarify: this is in windows embedded compact environment so both processes exist as different subprojects so to access ProcessA from ProcessB would require IPC.

My next idea was to use CreateEvent() with WaitForSingleObject() to check if the event is signaled, but i realized that the wait duration would have to be infinite, which would cause an issue the first time i launch my application.

So, is there any type of windows(wince) api that would solve this? (Or some kind of fancy coding i cannot think of?)

There are a few ways you could do this but two options are:

  1. When you launch ProcessA again, pass it a command line argument using the lpCommandLine parameter (eg /nolaunch ). ProcessA can then get its command line using GetCommandLine when it starts up and see if it contains the /nolaunch string.

  2. In both ProcessA and ProcessB, use the CreateSemaphore or CreateMutex function to create a named semaphore/mutex. In ProcessB, that's all you have to do - just make sure you don't close the handle until the process exits. In ProcessA, after creating the semaphore/mutex check if GetLastError() returns ERROR_ALREADY_EXISTS - this indicates that ProcessB still has the semaphore/mutex open (and therefore is already running).

There are multiple ways you can sole this:

1. Holding a mutex

In processB you can hold a named mutex and lock it. When processB will be created, it will try to lock the mutex and fail, and than it will kill itself. You can also check if the mutex is locked in processA and prevent the processB creation from the beginning.

2. Sending an argument to process creation

CreateProcess supports sending command line arguments to the created process via the second argument. You can use it as an indicator to whether you should create processB or not.

3. Going through the process list

You can go through the current process list and check if ProcessB is already running. Might want to look into Taking a Snapshot and Viewing Processes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM