Skip to content

Instantly share code, notes, and snippets.

@complxalgorithm
Created May 8, 2016 07:07
Show Gist options
  • Save complxalgorithm/db24ae8fea471630b56dd1a242ad3c30 to your computer and use it in GitHub Desktop.
Save complxalgorithm/db24ae8fea471630b56dd1a242ad3c30 to your computer and use it in GitHub Desktop.
C program that ignores Ctrl-C for 5 seconds; afterward, the loop can be killed.
#include <stdio.h>
#include <signal.h>
main ()
{
void (*oldHandler) (); /* To hold old handler value */
int count = 0; /* Loop counter, initialized at 0 */
oldHandler = signal (SIGINT, SIG_IGN); /* Ignore Control-C */
printf ("I've started looping and I can't be killed with ^C\n");
while(1)
{
sleep (1);
printf("Still looping...");
count++;
if (count == 5)
{
signal (SIGINT, oldHandler); /* Restore old handler */
printf ("I'm still looping but I can be killed with ^C now\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment