Skip to content

Instantly share code, notes, and snippets.

@harrifeng
Created February 19, 2014 10:10
Show Gist options
  • Save harrifeng/9089230 to your computer and use it in GitHub Desktop.
Save harrifeng/9089230 to your computer and use it in GitHub Desktop.
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <string.h>
#include <unistd.h>
void sig_handler(int signum)
{
printf("in handler\n");
sleep(1);
printf("handler return\n");
}
int main(int argc, char **argv)
{
char buf[100];
int ret;
struct sigaction action, old_action;
action.sa_handler = sig_handler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
/********************************/
/* version 1: set this flag */
/* version 2: NOT set this flag */
/********************************/
action.sa_flags |= SA_RESTART;
/**************************/
/* ctrl + c is for SIGINT */
/**************************/
sigaction(SIGINT, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN) {
sigaction(SIGINT, &action, NULL);
}
bzero(buf, 100);
ret = read(0, buf, 100);
if (ret == -1) {
perror("read");
}
printf("read %d bytes:\n", ret);
printf("%s\n", buf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment