Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <wait.h>
  5. #include <signal.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <sys/ipc.h>
  10. #include <sys/sem.h>
  11. #include <sys/shm.h>
  12. #include <limits.h>
  13.  
  14. int
  15. run(char *cmd)
  16. {
  17.     pid_t pid = fork();
  18.     if (pid < 0) {
  19.         return 0;
  20.     } else if (!pid) {
  21.         execlp(cmd, cmd, NULL);
  22.         _exit(1);
  23.     } else {
  24.         int status;
  25.         wait(&status);
  26.         return (WIFEXITED(status) && !WEXITSTATUS(status));
  27.     }
  28. }
  29.  
  30. int
  31. main(int argc, char **argv)
  32. {
  33.     int ok1 = 0, ok2 = 0;
  34.     unsigned long long ok_sum = 0;
  35.  
  36.     while ((ok1 = run(argv[1])) || (ok2 = run(argv[2]))) {
  37.         ok_sum += ok1 + ok2;
  38.         for (int i = 3; i < argc; ++i) {
  39.                 if (!fork()) {
  40.                 execlp(argv[i], argv[i], NULL);
  41.                 _exit(1);
  42.             }
  43.         }
  44.         int status;
  45.         while (wait(&status) != -1) {
  46.             ok_sum += (WIFEXITED(status) && !WEXITSTATUS(status));
  47.         }
  48.         ok1 = ok2 = 0;
  49.     }
  50.  
  51.     printf("%llu\n", ok_sum);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement