Hello,

I have written a test code for signal handling. The code is compataable for both Windows and Linux. The code is as:

#include <signal.h>
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#ifdef WIN32
	#include <windows.h>
#else
	#include <unistd.h>
#endif

void intr_hdlr(void);
void intr_hdlr2(void);
int cnter=0;
int sig_cnter=0;

int main ()
{
        printf("Before calling signal Handler\n");
		#ifdef WIN32
			signal(SIGINT, (void (__cdecl*)(int)) intr_hdlr);
		#else
			signal(SIGINT, (void (*)()) intr_hdlr);	
		#endif
        printf("After calling signal handler\n");
        while(cnter!=5)
        {
                if(sig_cnter == 1)
                {
                        intr_hdlr2();
                }
        }
        printf("Out of while loop\n");
        return 0;
}


void intr_hdlr(void)
{
        printf("Inside Signal Handler\n");
        sig_cnter = 1;
        printf("Before leaving signal handler\n");
        return ;
}

void intr_hdlr2(void)
{
        int i=0;
        printf("Inside Signal Handler2\n");
        for(i=0;i<100000;i++)
                printf("[%d]\n",i);
        cnter = 5;
        printf("Before leaving signal handler2\n");
        exit(1) ;
}

The issue is as follows:
When I run this code on linux platform, After sending the signal SIGINT using cntrl+C, the numbers are printed from 1 to 100000. In case I press cntrl+c again during the time my code is executing the for loop for pirnting numbers between 1 and 100000, it doesnt have any effect on the printing. The exe stops only when it has finished printing upto 99999 and reaches exit(1).
On the contrary, on windows if i press cntrl+c second time, the Exe stops immediately. How call I disable the signal catching second time. I want to execute a particular function of my code completely, once I receive the signal. the exe should execute the complete function even if it is receiving the SIGINT signal multiple times.
The behaviour is fine on Linux but on Windows issue is coming. Please advice.

Regards.

on linux platform, After sending the signal SIGINT using cntrl+C, the numbers are printed from 1 to 100000. In case I press cntrl+c again during the time my code is executing the for loop for pirnting numbers between 1 and 100000, it doesnt have any effect on the printing.

Are you 100% sure that e.g. "Inside Signal Handler" doesn't get printed the second time at that time?

on windows if i press cntrl+c second time, the Exe stops immediately.

That is because Windows has restored the handler i.e. your handler is not in effect anymore after the initial Ctrl+C got handled.

How call I disable the signal catching second time.

You might re-install the handler inside the handler itself.
However, on Windows you'd probably be better off using SetConsoleCtrlHandler().
http://msdn2.microsoft.com/en-us/library/ms686016.aspx

Note that your handler function's signature
void intr_hdlr(void);
is not correct. You should be able to call signal() without casting the handler argument like you do now.

Thanks Mitrmkar,

My issue is resolved by using SetConsoleCtrlHandler. Regarding the responses you gave me:
1. "Inside Signal Handler" is printed each time I press Cntrl+C on linux. But the Exe does not stop till the numbers are printed.
2. I didnt understand why u asked me to change the prototype of the signal handler.

Regards.

This question has already been answered. Start a new discussion instead.