1 #include <signal.h> 2 3 #include <stdio.h> 4 5 static void handler(int signo) 6 { 7 if (signo == SIGINT) { 8 printf("Catch SIGINT\n"); 9 } else if (signo == SIGQUIT) { 10 printf("Catch SIGQUIT\n"); 11 } else { 12 printf("error\n"); 13 } 14 } 15 16 int main(void) 17 { 18 int i; 19 void (*save)(int); 20 21 // 存的默认行为函数 22 save = signal(SIGINT, handler); 23 signal(SIGQUIT, handler); 24 25 for (i = 0; ; i++) { 26 if (i == 10) { 27 signal(SIGINT, save); 28 } 29 pause(); 30 } 31 32 return 0; 33 }
1 #include <signal.h> 2 3 #include <stdio.h> 4 5 int main(void) 6 { 7 sigset_t set, oset, pend; 8 int i; 9 10 sigemptyset(&set); 11 sigaddset(&set, SIGINT); 12 13 sigprocmask(SIG_BLOCK, &set, &oset); 14 15 for (i = 0; ; i++) { 16 if (i == 10) { 17 sigprocmask(SIG_SETMASK, &oset, NULL); 18 } 19 sleep(1); 20 sigpending(&pend); 21 if (sigismember(&pend, SIGINT)) { 22 printf("i = %d, SIGINT pending\n", i); 23 } else { 24 printf("i = %d\n", i); 25 } 26 } 27 28 return 0; 29 }
1 #include <signal.h> 2 #include <unistd.h> 3 4 #include <stdio.h> 5 6 void handler(int unuse) 7 { 8 printf("Catch SIGINT\n"); 9 } 10 11 int main(void) 12 { 13 sigset_t set, save, tmp, unblock; 14 15 signal(SIGINT, handler); 16 17 sigemptyset(&set); 18 sigaddset(&set, SIGINT); 19 20 sigprocmask(SIG_UNBLOCK, &set, &save); 21 sigprocmask(SIG_BLOCK, &set, &unblock); 22 23 while (1) { 24 #if 0 25 sigprocmask(SIG_SETMASK, &unblock, &tmp); 26 pause(); 27 sigprocmask(SIG_SETMASK, &tmp, NULL); 28 #else 29 sigsuspend(&unblock); 30 #endif 31 printf("pause() return\n"); 32 sleep(3); 33 printf("sleep() return\n"); 34 } 35 sigprocmask(SIG_SETMASK, &save, NULL); 36 37 return 0; 38 }
1 #include <signal.h> 2 #include <unistd.h> 3 4 #include <stdio.h> 5 6 int main(void) 7 { 8 int i; 9 pid_t pid; 10 struct sigaction act; 11 12 act.sa_handler = SIG_IGN; 13 sigemptyset(&act.sa_mask); 14 act.sa_flags = SA_NOCLDWAIT; 15 sigaction(SIGCHLD, &act, NULL); 16 17 for (i = 0; i < 6; i++) { 18 pid = fork(); 19 /* if error */ 20 if (pid == 0) { 21 return 0; 22 } 23 } 24 25 while (1) { 26 pause(); 27 } 28 29 return 0; 30 }
1 #include <signal.h> 2 #include <unistd.h> 3 4 #include <stdio.h> 5 6 #define BUFSIZE 128 7 8 void handler(int unuse) 9 { 10 alarm(4); 11 printf("call handler()\n"); 12 } 13 14 int main(void) 15 { 16 char buf[BUFSIZE]; 17 int ret; 18 19 signal(SIGALRM, handler); 20 21 alarm(20); 22 sleep(3); 23 ret = alarm(30); 24 printf("ret = %d\n", ret); 25 26 while (1) { 27 printf("will call read()\n"); 28 read(0, buf, BUFSIZE); 29 printf("read() return\n"); 30 } 31 32 return 0; 33 }
1 #include <signal.h> 2 3 #include <stdio.h> 4 5 int main(void) 6 { 7 sigset_t set, oset; 8 int i; 9 10 sigemptyset(&set); 11 sigaddset(&set, SIGINT); 12 13 sigprocmask(SIG_BLOCK, &set, &oset); 14 15 for (i = 0; ; i++) { 16 if (i == 10) { 17 sigprocmask(SIG_SETMASK, &oset, NULL); 18 } 19 sleep(1); 20 printf("i = %d\n", i); 21 } 22 23 return 0; 24 }
1 #include <signal.h> 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 /* ./mykill signo pid val */ 7 int main(int argc, char **argv) 8 { 9 int signo; 10 pid_t pid; 11 union sigval val; 12 13 if (argc != 4) { 14 fprintf(stderr, "argment...\n"); 15 return 1; 16 } 17 18 signo = atoi(argv[1]); 19 pid = atoi(argv[2]); 20 val.sival_int = atoi(argv[3]); 21 22 sigqueue(pid, signo, val); 23 24 return 0; 25 }
1 #include <sys/types.h> 2 #include <sys/wait.h> 3 #include <signal.h> 4 #include <unistd.h> 5 6 #include <stdio.h> 7 8 void handler(int unuse) 9 { 10 pid_t pid; 11 12 while (1) { 13 pid = waitpid(-1, NULL, WNOHANG); 14 if (pid == 0 || pid == -1) { 15 break; 16 } 17 } 18 } 19 20 int main(void) 21 { 22 int i; 23 pid_t pid; 24 25 signal(SIGCHLD, handler); 26 27 for (i = 0; i < 6; i++) { 28 pid = fork(); 29 /* if error */ 30 if (pid == 0) { 31 return 0; 32 } 33 } 34 35 while (1) { 36 sleep(2); 37 printf("xxx\n"); 38 } 39 40 return 0; 41 }
1 #include <signal.h> 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 /* ./mykill signo pid */ 7 int main(int argc, char **argv) 8 { 9 int signo; 10 pid_t pid; 11 12 if (argc != 3) { 13 fprintf(stderr, "argment...\n"); 14 return 1; 15 } 16 17 signo = atoi(argv[1]); 18 pid = atoi(argv[2]); 19 20 kill(pid, signo); 21 22 return 0; 23 }
1 #include <sys/time.h> 2 #include <signal.h> 3 #include <unistd.h> 4 5 #include <stdio.h> 6 7 #define BUFSIZE 128 8 9 void handler(int unuse) 10 { 11 printf("call handler()\n"); 12 } 13 14 int main(void) 15 { 16 char buf[BUFSIZE]; 17 int ret; 18 struct itimerval ival = { 19 {2, 0}, 20 {6, 0}, 21 }; 22 23 signal(SIGALRM, handler); 24 25 setitimer(ITIMER_REAL, &ival, NULL); 26 27 while (1) { 28 printf("will call read()\n"); 29 read(0, buf, BUFSIZE); 30 printf("read() return\n"); 31 } 32 33 return 0; 34 }
1 #include <signal.h> 2 3 #include <stdio.h> 4 5 static void handler(int signo, siginfo_t *info, void *unuse) 6 { 7 if (signo == SIGINT) { 8 printf("Catch SIGINT, "); 9 } else if (signo == SIGQUIT) { 10 printf("Catch SIGQUIT, "); 11 } else { 12 printf("error\n"); 13 } 14 15 printf("val = %d\n", info->si_int); 16 } 17 18 int main(void) 19 { 20 int i; 21 struct sigaction act, save; 22 23 //save = signal(SIGINT, handler); 24 act.sa_sigaction = handler; 25 sigemptyset(&act.sa_mask); 26 sigaddset(&act.sa_mask, SIGINT); 27 sigaddset(&act.sa_mask, SIGQUIT); 28 act.sa_flags = SA_RESTART | SA_SIGINFO; 29 sigaction(SIGINT, &act, &save); 30 31 sigaction(SIGQUIT, &act, NULL); 32 33 while (1) { 34 pause(); 35 } 36 37 return 0; 38 }
1 #include <signal.h> 2 3 #include <stdio.h> 4 5 static void handler(int signo) 6 { 7 if (signo == SIGINT) { 8 printf("Catch SIGINT\n"); 9 } else if (signo == SIGQUIT) { 10 printf("Catch SIGQUIT\n"); 11 } else { 12 printf("error\n"); 13 } 14 } 15 16 int main(void) 17 { 18 int i; 19 struct sigaction act, save; 20 21 //save = signal(SIGINT, handler); 22 act.sa_handler = handler; 23 sigemptyset(&act.sa_mask); 24 sigaddset(&act.sa_mask, SIGINT); 25 sigaddset(&act.sa_mask, SIGQUIT); 26 act.sa_flags = SA_RESTART; 27 sigaction(SIGINT, &act, &save); 28 29 sigaction(SIGQUIT, &act, NULL); 30 31 while (1) { 32 pause(); 33 } 34 35 return 0; 36 }
1 #include <signal.h> 2 3 #include <stdio.h> 4 5 static void handler(int signo, siginfo_t *info, void *unuse) 6 { 7 if (signo == SIGINT) { 8 printf("Catch SIGINT, "); 9 } else if (signo == SIGQUIT) { 10 printf("Catch SIGQUIT, "); 11 } else { 12 printf("error\n"); 13 } 14 15 printf("val = %d\n", info->si_int); 16 } 17 18 int main(void) 19 { 20 int i; 21 struct sigaction act, save; 22 23 //save = signal(SIGINT, handler); 24 act.sa_sigaction = handler; 25 sigemptyset(&act.sa_mask); 26 sigaddset(&act.sa_mask, SIGINT); 27 sigaddset(&act.sa_mask, SIGQUIT); 28 act.sa_flags = SA_RESTART | SA_SIGINFO; 29 sigaction(SIGINT, &act, &save); 30 31 sigaction(SIGQUIT, &act, NULL); 32 33 while (1) { 34 pause(); 35 } 36 37 return 0; 38 }
原文:http://www.cnblogs.com/takeaction/p/4359374.html