این کد از کتاب Linux programming interface برداشته شده است.
#include <stdio.h> #include <stdlib.h> #include <err.h> #include <unistd.h> #include <signal.h> #include <libgen.h> #define CMD_SIZE 200 int main(int argc, char *argv[]) { char cmd[CMD_SIZE]; pid_t childPid; setbuf(stdout, NULL); printf("Parent PID=%ld\n", (long) getpid()); switch (childPid = fork()) { case -1: err(EXIT_FAILURE, "fork"); case 0: printf("Child (PID=%ld) exiting\n", (long) getpid()); _exit(EXIT_SUCCESS); default: sleep(3); snprintf(cmd, CMD_SIZE, "ps | grep %s", basename(argv[0])); cmd[CMD_SIZE - 1] = '\0'; system(cmd); if (kill(childPid, SIGKILL) == -1) err(EXIT_FAILURE, "kill"); sleep(3); printf("After sending SIGKILL to zombie (PID=%ld):\n", (long) childPid); system(cmd); exit(EXIT_SUCCESS); } }
نمونه خروجی برنامه به صورت زیر است.
$ ./a.out Parent PID=4719 Child (PID=4721) exiting 4719 pts/2 00:00:00 a.out 4721 pts/2 00:00:00 a.out <defunct> After sending SIGKILL to zombie (PID=4721): 4719 pts/2 00:00:00 a.out 4721 pts/2 00:00:00 a.out <defunct>
کلیه حقوق برای دارندهی سایت محفوظ است.