problem with Processess

  • Thread starter Thread starter Sudhakar
  • Start date Start date
S

Sudhakar

Hi,

I want to start a independent process from another process. Let me
explain my problem,

I have a "process A" running. Process B has to be started just before the
process A ends and the invocation of process B has to be triggered from
inside the process A.
So, when the process B started, process A would have been terminated but the
process B should not be stopped.

Please suggest how I can achieve this is VC++ ?

Thanks,
Sud
 
I have a "process A" running. Process B has to be started just before the
process A ends and the invocation of process B has to be triggered from
inside the process A.
So, when the process B started, process A would have been terminated but the
process B should not be stopped.

Please suggest how I can achieve this is VC++ ?

Processes are (largely) independent under Win32 so the instigator
process won't normally close the spawned process when it closes. Use
whichever API is most appropriate to your circumstance - CreateProcess
or ShellExecute.

Dave
 
I want to start a independent process from another process. Let me
explain my problem,

I have a "process A" running. Process B has to be started just before the
process A ends and the invocation of process B has to be triggered from
inside the process A.
So, when the process B started, process A would have been terminated but
the
process B should not be stopped.

Please suggest how I can achieve this is VC++ ?

Call CreateProcess, ShellExecute or system before process A exits
 
Sudhakar said:
Hi,

I want to start a independent process from another process. Let me
explain my problem,

I have a "process A" running. Process B has to be started just before the
process A ends and the invocation of process B has to be triggered from
inside the process A.
So, when the process B started, process A would have been terminated but
the
process B should not be stopped.

Please suggest how I can achieve this is VC++ ?

In addition to what's already been said, if you need to guarantee that
process A has already exited (because you're replacing it with an updated
version, for example) then you'll need to wait in process B:

in Process A
event = CreateEvent(... some unique name ...);
processB = CreateProcess(exeB, put processA pid in arg list);
wakeReason = WaitForMultipleObjects({processB, event});
if (wakeReason was event) ExitProcess();
else if (wakeReason was processB) MessageBox("ProcessB couldn't be
started");

in Process B
event = OpenEvent(... same unique name ...);
processA = OpenProcess(... pid parsed from args ...);
SetEvent(event);
WaitForSingleObject(processA);
.... now processA is definitely dead ...

This method takes care of the race conditions (what if Process A goes to
sleep between CreateProcess and ExitProcess), (what if another process
reuses Process A's PID before Process B calls OpenProcess) as well as the
case where Process B dies during startup and never sets the event.
 
Thanks Ben for the solution. It worked

Ben Voigt said:
In addition to what's already been said, if you need to guarantee that
process A has already exited (because you're replacing it with an updated
version, for example) then you'll need to wait in process B:

in Process A
event = CreateEvent(... some unique name ...);
processB = CreateProcess(exeB, put processA pid in arg list);
wakeReason = WaitForMultipleObjects({processB, event});
if (wakeReason was event) ExitProcess();
else if (wakeReason was processB) MessageBox("ProcessB couldn't be
started");

in Process B
event = OpenEvent(... same unique name ...);
processA = OpenProcess(... pid parsed from args ...);
SetEvent(event);
WaitForSingleObject(processA);
... now processA is definitely dead ...

This method takes care of the race conditions (what if Process A goes to
sleep between CreateProcess and ExitProcess), (what if another process
reuses Process A's PID before Process B calls OpenProcess) as well as the
case where Process B dies during startup and never sets the event.
 
Back
Top