Program repeatedly restarts when its console is closed

Arthur Norman acn1@cam.ac.uk
Mon Dec 6 23:07:00 GMT 2010


At the end of this message is a short program. It is linked as a 
windows-mode executable so when it is launched it does not have a console. 
It then allocates one and uses it. The expected behaviour is that when I 
run it a fresh DOS-style console appears and I can type a few chars in and 
they get echoed.

This happens happily. EXCEPT that if before typing in 5 chars I close the 
console either via its file/close menu or by clicking the little "x" at the 
right of its title bar and IF I launched the program from cygwin using 
execv/execve (as the cygwin bash shell does) the application console winow 
closess but the program is instantly relauched. I see this with both the 32 
& 64-bit versions of the non-cygwin compilatets - ie replace 
x86_64-w64-mingw32-gcc with just "gcc -mno-cygwin" has the same behaviour.

If instead of launching the binary via cygwin's execv I launch it direct 
from a plain old DOS shell then closing the console window causes the 
program to terminate in the way I expected.

It is not clear to me whether this is an issue of the mingw bits not 
handling things and exiting badly or of the cygwin execv behaving 
improperly. Eg it I write a very short program that goes basically
execv("./bad0",...) and compile that using cygwin but then run that 
executable from a normal DOS shell I get the hydra-like behaviour where 
when I cut of a console a fresh one instantly re-grows, even though 
launching bad0 directly does not suffer that way. So if mingw people can 
confirm that this is a problem on the cygwin side the issue can be 
forwarded that way...

When I posted this for mingw64 people to comment on the response I got 
back was
"This indicates the problem to be with Cygwin.  Have you asked them?  I
know that Cygwin doesn't have control of the exec-ed "native" program
but it may be trapping the signals and causing the primary program to
not end so that it reopens the console making it appear to be
"restarting"."


Many thanks,

{I am on Windows 7 64-bit with the x86_64 compiler versions that cygwin 
installs for me, but I have tried the bad0 executable on a windows 2000 
(virtual) machine and observe the same bad effects in the 32-bit version 
there, so actually this is not just a 64-bit issue and not just a Windows 7 
one).

              Arthur


===========================================================
/*
  * x86_64-w64-mingw32-gcc -mno-cygwin bad0.c \
  *     -Wl,--subsystem=windows -o bad0
  * ./bad0
  */

#include <stdio.h>
#include <windows.h>

int main(int argc, char *argv[])
{
     int i;
     if (AllocConsole())
     {   freopen("CONIN$", "r", stdin);
         freopen("CONOUT$", "w", stdout);
         freopen("CONOUT$", "w", stderr);
     }
     for (i=0; i<5; i++)
     {   int c = getchar();
         printf("char %d is %#x\n", i, c);
         fflush(stdout);
     }
     return 0;
}
=============================================================

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list