Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/wait.h>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.  
  9. pid_t cpid, w;
  10. int status;
  11. cpid = fork();
  12.  
  13. if (cpid == -1)
  14. {
  15. perror("fork");
  16. exit(EXIT_FAILURE);
  17. }
  18.  
  19. if (cpid == 0)
  20. {
  21. printf("Child PID is %ld\n", (long)getpid());
  22. if (argc == 1)
  23. pause();
  24. _exit(atoi(argv[1]));
  25. }
  26. else
  27. {
  28. do
  29. {
  30. w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
  31. if (w == -1)
  32. {
  33. perror("waitpid");
  34. exit(EXIT_FAILURE);
  35. }
  36. if (WIFEXITED(status))
  37. {
  38. printf("exited, status=%d\n", WEXITSTATUS(status));
  39. }
  40. else if (WIFSIGNALED(status))
  41. {
  42. printf("killed by signal %d\n", WTERMSIG(status));
  43. }
  44. else if (WIFSTOPPED(status))
  45. {
  46. printf("stopped by signal %d\n", WSTOPSIG(status));
  47. }
  48. else if (WIFCONTINUED(status))
  49. {
  50. printf("continued\n");
  51. }
  52. } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  53. exit(EXIT_SUCCESS);
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement