Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 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. pid_t cpid, w;
  8. int status;
  9. cpid = fork();
  10. if (cpid == -1){
  11. perror("fork");
  12. exit(EXIT_FAILURE);
  13. }
  14. if(cpid == 0){
  15. printf("Child PID is %ld\n", (long)getpid());
  16. if(argc == 1)
  17. pause();
  18. _exit(atoi(argv[1]));
  19. }
  20. else{
  21. do{
  22. w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
  23. if(w == -1){
  24. perror("waitpid");
  25. exit(EXIT_FAILURE");
  26. }
  27. if(WIFEXITED(status)){
  28. printf("exited, status=%d\n, WEXITSTATUS(status));
  29. }
  30. else if(WIFSIGNALED(status)){
  31. printf("killed by signal %d\n", WTERMSIG(status));
  32. }
  33. else if (WIFSTOPPED(status)){
  34. printf("stopped by signal %d\n", WSTOPSIG(status));
  35. }
  36. else if (WIFCONTINUED(status)){
  37. printf("continued\n");
  38. }
  39. } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  40. exit(EXIT_SUCCESS);
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement