1

I have a program (lets call it prog1.exe) that launches another program (lets call it prog2.exe) with:

 // in prog1.exe:
 boost::process::spawn("prog2.exe", ...);

If I launch cmd.exe:

C:\>

and then run prog1.exe:

C:\> prog1.exe

then:

  1. prog1.exe starts running
  2. prog1.exe launches prog2.exe and prog2.exe starts running
  3. prog1.exe completes successfully.
  4. prog2.exe continues running.

If I then close cmd.exe while prog2.exe is still running, it will terminate prog2.exe before it has completed.

Why does that happen? Why does prog1.exe ending NOT terminate prog2.exe, while cmd.exe ending DOES terminate prog2.exe ?

Is there any way to make it so that closing cmd.exe will not terminate prog2.exe?

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • Maybe you close the cmd before the other process is run? – Tony Tannous Aug 08 '20 at 21:56
  • @TonyTannous: No, it is definately running. I can see it in procexp and observe its side effects. – Andrew Tomazos Aug 08 '20 at 21:58
  • Current hunch is it has something to do with "consoles": https://learn.microsoft.com/en-us/windows/console/creation-of-a-console – Andrew Tomazos Aug 08 '20 at 22:04
  • 1
    You probably killed the console window when you closed it. The one that prog2 inherited from prog1. A console-mode process won't keep running without a console, its floor mat got jerked. If prog2 is a native Windows app then there's no problem. No problem either when prog2 creates its own console window, the kind of [boost question](https://stackoverflow.com/questions/57072843/is-there-a-way-to-create-a-new-console-window-for-process-spawned-with-boost) that can't seem to get an answer. – Hans Passant Aug 08 '20 at 22:08
  • @HansPassant: Yes prog1.exe and prog2.exe are both console programs. I'm going to try calling FreeConsole and AllocConsole from prog2.exe and see if that works. – Andrew Tomazos Aug 08 '20 at 22:09
  • console process force terminated if it console closed – RbMm Aug 08 '20 at 22:11
  • *I'm going to try calling FreeConsole and AllocConsole from prog2.exe* - better use `CREATE_NEW_CONSOLE` when start prog2 – RbMm Aug 08 '20 at 22:13
  • @RbMm: Why is that better? – Andrew Tomazos Aug 08 '20 at 22:16
  • because this more clean and direct way. just create new console window for new process – RbMm Aug 08 '20 at 22:18
  • If the console host process (conhost.exe) is terminated, then clients attached to the console can persist. Their console handles are invalid, so usually clients just exit. If the console has a window (i.e. not `CREATE_NO_WINDOW`), and it gets closed, the console host sends a message to the session server (csrss.exe) to make it inject a control thread in each client process for a `CTRL_CLOSE_EVENT`. The default handler routine calls `ExitProcess`. If a control handler is set that doesn't return immediately, csrss.exe waits on the thread for 5 seconds before calling `NtTerminateProcess`. – Eryk Sun Aug 09 '20 at 16:10

0 Answers0