#include #include #include #include #include #include #include #include #include int do_parent(pid_t); void do_child(int); int main(void) { int devnul; pid_t pid; devnul = open("/dev/null", O_WRONLY); pid = fork(); if (pid) { /* Parent */ return do_parent(pid); } else { /* Child */ do_child(devnul); } } int do_parent(pid_t child_pid) { FILE *f; char buf[120]; char filename[20]; printf("child pid: %d\n", child_pid); sleep(10); printf("sending SIGKILL\n"); kill(child_pid, SIGKILL); sleep(10); sprintf(filename, "/proc/%d/stat", child_pid); printf("reading %s\n", filename); f = fopen(filename, "r"); if (f == NULL) { printf("fopen error [%d]: %s\n", errno, strerror(errno)); if (!access(filename, R_OK)) { printf("but the file exists and is readable\n"); } } else { fread(buf, sizeof(char), 120, f); printf(buf); } return wait4(child_pid, NULL, 0, NULL); } void do_child(int out) { char *argv[2] = { "/usr/bin/yes", NULL }; dup2(out, 1); execv(argv[0], argv); exit(0); }