This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

ctrl-c handling problem with win32 native apps.


I am on a team developing a windows console application, and when I run it
under a cygwin shell, the ctrl-c handling does not work properly, but it does
when run under cmd or 4nt.

What happens is that the application exits almost immediately after ctrl-c is
pressed, and the ctrl-c handler does not have time to complete, so the
application does not shutdown properly.

I have created the following program which demonstrates on my system that a
program compiled with cygwin works properly, but the same program compiled
with gcc -mno-cygwin exhibits the same behavior I described above, but works
correctly in a windows native shell.

************************************
#include <stdio.H>
#include <signal.h>

#ifdef CYGWIN
#include <unistd.h>
#else
#include <windows.h>
#endif

int flag = 0;

#ifndef CYGWIN
void sleep(int sec)
{
    Sleep(sec*1000);
}
#endif

static void siggy(int signo)
{
    if (signo == SIGINT)
    {
        flag = 1;
        signal(SIGINT, SIG_DFL);
    }
}


int main()
{
    if (signal(SIGINT, siggy) == SIG_ERR)
    {
        printf("could not set SIGINT\n");
    }

    printf("polling for SIGINT...\n");
    while (!flag)
    {
        sleep(1);
        printf("waiting for SIGINT\n");
    }

    printf("SIGINT received\n");
    printf("sleeping...\n");
    sleep(5);
    printf("exiting\n");
}
********************************

The #ifdef'd code is to sleep() will work the same in both executables.

Compile the program like so:

gcc testprog.c -o cygtestprog.exe
gcc -mno-cygwin testprog.c -o wintestprog.exe

cygtestprog and wintestprog behave the same under a windows shell (cmd or 4nt)
but wintestprog exits immediately under bash and zsh while cygtestprog behaves
properly.

The application actually uses SetConsoleCtrlHandler() instead of signal, but
the observed behavior is the same, so the test app demonstrates the problem
more simply.

Is this a known problem? or perhaps a problem with my setup?

Thank you for any help you can offer.


rob



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]