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.