Advertisement
rfmonk

make_zombie.c

Jan 19th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <signal.h>
  2. #include <libgen.h>
  3. #include "tlpi_hdr.h"
  4.  
  5. #define CMD_SIZE 200
  6.  
  7. int
  8. main(int argc, char *argv[])
  9. {
  10.     char cmd[CMD_SIZE];
  11.     pid_t childPid;
  12.  
  13.     setbuf(stdout, NULL);  // disable buffering of stdout
  14.  
  15.     printf("Parent PID=%ld\n", (long) getpid());
  16.  
  17.     switch (childPid= fork()) {
  18.     case -1:
  19.         errExit("fork");
  20.  
  21.     case 0:  //Child: immediately exits to become zombie
  22.         printf("Child (PID=%ld) exiting\n", (long) getpid());
  23.         _exit(EXIT_SUCCESS);
  24.  
  25.         default:  //Parent
  26.         sleep(3); //Give child a chance to start and exit
  27.         snprintf(cmd, CMD_SIZE, "ps | grep %s", basename(argv[0]));
  28.         cmd[CMD_SIZE - 1] = '\0';
  29.         system(cmd);
  30.  
  31.         // Now send the "sure kill" signal to the zombie
  32.    
  33.         if (kill(childPid, SIGKILL) == -1)
  34.             errMsg("kill");
  35.         sleep(3);
  36.         printf("After sending SIGKILL to zombie (PID=%ld):\n", (long) childPid;
  37.         system(cmd);
  38.  
  39.         exit(EXIT_SUCCESS);
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement