Reputation: 207
I have an executable loop.exe
on my Windows machine that was compiled using MinGW's gcc.
The code for loop.c
is behaviorally similar to:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
static int sigintReceived = 0;
void sigint_handler(int signum){
sigintReceived = 1;
}
int main(){
signal(SIGINT, sigint_handler);
while(1){
sleep(1);
if(sigintReceived){
printf("SIGINT received");
exit(0);
}
}
}
If I open a Cygwin terminal using Cygwin.bat
and run ./loop.exe
and then press Ctrl+C I will see the output:
SIGINT received
However if I open two terminals using Cygwin.bat
and run ./loop.exe
in one and kill -2 <LOOP_EXE_PID>
in the other, I do not see the output at all.
The code is acting as if no signal handler exists when I run kill -## <LOOP_EXE_PID>
with any signal and handler (for example if I kill -10
I'll get a Bus error
or if I kill -12
I'll get a Bad system call
or if I kill -11
I'll get a Segmentation fault
)
I have looked around and come across this answer which I believe might relate to what I'm encountering, however loop.exe
is being terminated in each case. In this answer Bogdan mentions that Windows executables are not run directly by the Bash shell but rather through an intermediate Bash process, and I can see this process in my Windows Task Manager when I run ./loop.exe
, but I cannot see this intermediate Bash process from inside Cygwin.
Any ideas as to how I can get kill -2 <LOOP_EXE_PID>
to act the same as Ctrl+C? Or, any ideas how I can see this intermediate Bash process from inside Cygwin?
Note: I am running Cygwin using C:\cygwin\Cygwin.bat
; I am not using mintty.
Upvotes: 2
Views: 1830
Reputation: 19
There is no such thing as signal handling in MinGW. Cygwin has its own signal handling behaviors that can not be handled from outside of Cygwin. Your best bet is to compile with Cygwin's GCC or take a look at Microsoft signals.
Upvotes: 1
Reputation: 58666
The program is compiled with MinGW; therefore it is not a Cygwin program.
MinGW programs do not have real POSIX signal handling; only a minimal signal API from ISO C.
They cannot respond to external signals in the Cygwin environment. There is no relationship between the signal
function in the Microsoft MSVCRT.DLL
and Cygwin's signal handling.
Sending a signal from one process to another is not a concept in MS Windows. Cygwin brings that about in its own domain, the same way it brings about other POSIX things like fork
and exec
, termios
over the console and whatever else.
Upvotes: 2