Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- enum
- {
- STAT = -1
- };
- int
- main(void)
- {
- int a;
- int proc = 0;
- int status;
- while (1) {
- pid_t pid = fork();
- if (pid < 0) {
- printf("%d\n", STAT);
- if (!proc) {
- break;
- } else {
- _exit(1);
- }
- } else if (!pid) {
- proc = 1;
- if (!(scanf("%d", &a) > 0)) {
- _exit(0);
- }
- } else {
- wait(&status);
- if (!proc) {
- break;
- } else if (!WEXITSTATUS(status)) {
- printf("%d\n", a);
- _exit(0);
- } else {
- _exit(1);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement