1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <signal.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <fcntl.h>
#define true 1 #define false 0 #define isTrue(x) ((x) != 0) #define isFalse(x) ((x) == 0)
void sigint_handler(int sig) { if(sig != SIGINT) { return; } printf("喵!\n"); printf("汪!\n"); }
int __siginterrupt(int __sig, int __interrupt) { struct sigaction sigact, sigact1; sigaction(__sig, &sigact1, &sigact); if(isTrue(__interrupt)) { sigact.sa_flags |= SA_RESTART; } else { sigact.sa_flags &= ~SA_RESTART; } return sigaction(__sig, &sigact, NULL); }
int main() { void *buf[BUFSIZ]; sigaction(SIGINT, &(struct sigaction) { .sa_handler = sigint_handler, .sa_flags = SA_NODEFER, }, NULL); __siginterrupt(SIGINT, false);
pid_t pid = fork();
if(pid == 0) { kill(getppid(), SIGINT); } else { int fd = open("/proc/self/status", O_RDONLY); size_t read_num = 0, write_num; if ((read_num = read(fd, buf, BUFSIZ)) == -1) { printf("read_num = %lu\n", read_num); printf("fail to read /proc/self/status, %s\n", strerror(errno)); return errno; } if ((write_num = write(STDOUT_FILENO, buf, read_num)) != read_num) { printf("write_num = %lu\n", write_num); printf("fail to write STDOUT_FILENO, %s\n", strerror(errno)); return errno; } } return 0; }
|