5

This is test example:

(1). simple program doing endless loop:

#include <iostream>
using namespace std;

int main() {
  int counter = 0;
  while (1) cout << ++counter << ": endless loop..." <<endl;
}

(2). another program that launches above example through system() command:

#include <iostream>
#include <windows.h>

using namespace std;

int main() {
  system("endless_loop.exe");
  cout << "back to main program" << endl;
}

When doing Ctrl+Break on this program text back to main program doesn't show. How to restrict this key combination to inside process and return execution pointer back to main app ?

Another thing is that I don't always have control over source code of inside program, so I can't change things there.

rsk82
  • 28,217
  • 50
  • 150
  • 240
  • 1
    Could you not register different signal handlers for SIGINT in both programs? This was already discussed [here](http://stackoverflow.com/questions/181413). – Matthias Jun 03 '12 at 19:25
  • 1
    You'll need to detach the started program from your console. Best done with CreateProcess and the DETACHED_PROCESS option. Not otherwise wrapped by the CRT. – Hans Passant Jun 03 '12 at 19:26
  • @Hans: I would expect DETACHED_PROCESS to result in control-break still interrupting the parent process but not the child process, exactly the opposite of what the OP is trying to achieve. – Harry Johnston Jun 04 '12 at 00:24

1 Answers1

3

Add this::

#include <signal.h>

 ...

signal (SIGINT, SIG_IGN);

After the signal() call, the program ignores Ctrl-Break. On Linux, ignoring signals propagates to child processes through fork()/exec().

I would expect Windows to reset the default signal handling on exec() due to the way the O/S + runtime library work. So if you want the child to ignore Break, add the code above to it too.

wallyk
  • 56,922
  • 16
  • 83
  • 148