problem with Processess

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
 
D

David Lowndes

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
 
S

SvenC

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
 
B

Ben Voigt [C++ MVP]

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.
 
S

Sudhakar

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top