#include <stdio.h> /* printf */
#include <signal.h>
#include <stdlib.h> /* exit(0) */
#include <time.h> /* ctime */
/* Interupt handler (called when program recieves operating system signal */
void ihandler (int cursig) {
/* Set flag to terminate main() polling loop
* when excution reaches bottom */
// isig_m = 1;
/* FLUSH BUFFERS */
fflush (stdout);
/* RE-INSTALL SIGNAL HANDLER */
signal (cursig, SIG_DFL);
time_t seconds;
time (&seconds);
fprintf(stderr,"Interrupt - received signal number <%i> ", cursig);
fprintf(stderr,"on %s",ctime(&seconds));
if (cursig == SIGINT)
{
exit(0);
}
/* Explain why pipe broke */
if (cursig == SIGPIPE)
fprintf(stderr,"SIGPIPE is received when writing to \n");
}
int main ()
{
while(1)
{
signal (SIGINT, ihandler); /* intercepts ^C */
signal (SIGTERM, ihandler); /* intercepts ^kill <PID> */
}
return 0;
}
Result:
SIGINT Type :
./happy
^Cipband received signal number <2> on Wed Jan 28 16:07:24 2015
SIGTERM Type :
./happy &
[1] 29694
kill 29694
ipband received signal number <15> on Wed Jan 28 16:07:35 2015
Reference:
1. [OpenSource] ipband
0 意見:
張貼留言