Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6.  
  7. enum
  8. {
  9. STAT = -1
  10. };
  11.  
  12. int
  13. main(void)
  14. {
  15. int a;
  16. int proc = 0;
  17. int status;
  18. while (1) {
  19. pid_t pid = fork();
  20. if (pid < 0) {
  21. printf("%d\n", STAT);
  22. if (!proc) {
  23. break;
  24. } else {
  25. _exit(1);
  26. }
  27. } else if (!pid) {
  28. proc = 1;
  29. if (!(scanf("%d", &a) > 0)) {
  30. _exit(0);
  31. }
  32. } else {
  33. wait(&status);
  34. if (!proc) {
  35. break;
  36. } else if (!WEXITSTATUS(status)) {
  37. printf("%d\n", a);
  38. _exit(0);
  39. } else {
  40. _exit(1);
  41. }
  42. }
  43. }
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement