2

MinTTY does not seem to raise a signal to my mingw-w64 program when I hit CTRL+C. In CMD with the same identical program the signal is correctly raised. Why is this?

The program is compiled under msys2 mingw-w64 with g++ -static -static-libstdc++ -std=c++14 -Wall -Wextra -pedantic testan.cpp. In both cases, signal() does not return SIG_ERR so the handler seems to be correctly installed.


code:

#include <chrono>
#include <thread>
#include <iostream>
#include <csignal>
using namespace std;

void signalHandler( int x ) {
    cout << "Interrupt: " << x << endl;
    exit( 123 );
}

int main () {
    if( signal(SIGINT, signalHandler) == SIG_ERR )
        cout << "received SIG_ERR" << endl;

    while( true ) {
        cout << "waiting for CTRL+C" << endl;
        this_thread::sleep_for( 1s );
    }

    return 0;
}

mintty output:

$ ./a.exe
waiting for CTRL+C
waiting for CTRL+C
waiting for CTRL+C

$

CMD output:

C:\Users\Xunie\Desktop\project>a.exe
waiting for CTRL+C
waiting for CTRL+C
Interrupt: 2

C:\Users\Xunie\Desktop\project>
Xunie
  • 437
  • 4
  • 21
  • According to the [mintty tips](https://github.com/mintty/mintty/wiki/Tips), this should work in Win10, and for earlier systems, [winpty](https://github.com/rprichard/winpty) can be used as a workaround. – ssbssa May 01 '20 at 15:28
  • 1
    FYI, natively there are no signals like `SIGINT` in Windows. The C runtime emulates the 6 required signals plus `SIGBREAK`. It has an OS exception handler for emulating `SIGILL`, `SIGSEGV`, and `SIGFPE`. Two are just internal in the runtime: `SIGABRT` and `SIGTERM`. And in a console application it has a console control handler that maps `CTRL_C_EVENT` to `SIGINT` and `CTRL_BREAK_EVENT` and `CTRL_CLOSE_EVENT` to `SIGBREAK`. – Eryk Sun May 22 '20 at 14:49

1 Answers1

3

MinTTY is a POSIX-oriented terminal emulator, it's using Cygwin/MSYS2 PTYs which don't interface well with native (non-Cygwin non-MSYS2) programs. This includes signals, detection of interactive input etc. MinTTY doesn't attempt to fix this, but Cygwin has recently (since v3.1.0) improved its support of this use case by using the new ConPTY API. As of May 2020, MSYS2 hasn't yet integrated these changes to its runtime, so you can't see the benefits there yet. In the meantime (and on older Windows versions), you can use the winpty wrapper, installable using pacman.

David Macek
  • 868
  • 4
  • 10
  • It is still off by default in MSYS2, but can be enabled with [MSYS=enable_pcon](https://github.com/msys2/msys2-runtime/pull/6). – ssbssa Aug 19 '20 at 17:52