#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void sigint_handler(int);
int main()
{
signal(SIGINT, sigint_handler);
while (1){
pause();
}
printf("Out..\n");
return 0;
}
void sigint_handler(int sig)
{
printf("killing process %d\n",getpid());
exit(0);
}
I dont get: Out… after ctrl+c
So never returns on while or getting out. How can i fix it, to continiue in main, and print “Out…”?
Because the signal handler immediately ends the process so
mainnever gets the chance of actually doing the printf. I suspect you don’t actually want toexitin the handler. You should take thepauseout of the loop and wait until the signal gets delivered.This isn’t the best way of waiting for a signal. For starters it is conceivable that the user could send the SIGINT before you get the chance to
pause. The signal would be lost andpause(2)would block until the end of time.You should probably use
sigsuspendor the simplersigpause.