Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- void
- work(int snum, FILE *in, FILE *out, int max)
- {
- int a;
- while (fscanf(in, "%d", &a) == 1) {
- printf("%d %d\n", snum, a);
- fflush(stdout);
- a++;
- if (a > max)
- exit(0);
- fprintf(out, "%d ", a);
- fflush(out);
- }
- exit(0);
- }
- int main(int argc, char *argv[])
- {
- int max = atoi(argv[1]);
- int p12[2];
- int p21[2];
- pipe(p12);
- pipe(p21);
- if (!fork()) {
- close(p21[1]);
- close(p12[0]);
- FILE *in1 = fdopen(p21[0], "r");
- FILE *out1 = fdopen(p12[1], "w");
- work(1, in1, out1, max);
- }
- if (!fork()) {
- close(p12[1]);
- close(p21[0]);
- FILE *in2 = fdopen(p12[0], "r");
- FILE *out2 = fdopen(p21[1], "w");
- work(2, in2, out2, max);
- }
- int x = 1;
- FILE *out = fdopen(p21[1], "w");
- fprintf(out, "%d ", x);
- fflush(out);
- close(p12[0]);
- close(p12[1]);
- close(p21[0]);
- close(p21[1]);
- wait(NULL);
- wait(NULL);
- printf("Done\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement