Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdlib.h>
- int main(int argc, char* argv[]) {
- int pfd1[2];
- pipe(pfd1);
- int pfd2[2];
- pipe(pfd2);
- int pid1, pid2;
- FILE* out = fdopen(pfd2[1], "w");
- fprintf(out, "%d", 1);
- fflush(out);
- fclose(out);
- int n = strtol(argv[1], NULL, 10);
- if(!(pid1 = fork())) {
- close(pfd1[0]); close(pfd2[1]);
- FILE* in = fdopen(pfd2[0], "r");
- FILE* out = fdopen(pfd1[1], "w");
- int y;
- while(fscanf(in, "%d", &y) > 0) {
- if(y >= n) {
- break;
- }
- printf("%d %d\n", y, 1);
- fflush(stdout);
- y++;
- fprintf(out, "%d", y);
- fflush(out);
- }
- close(pfd1[1]);
- close(pfd2[0]);
- fclose(in); fclose(out);
- _exit(0);
- }
- if(!(pid2 = fork())) {
- close(pfd2[0]); close(pfd1[1]);
- FILE* in = fdopen(pfd1[0], "r");
- FILE* out = fdopen(pfd2[1], "w");
- int y;
- while(fscanf(in, "%d", &y) > 0) {
- if(y >= n) {
- break;
- }
- printf("%d %d\n", y, 2);
- fflush(stdout);
- y++;
- fprintf(out, "%d", y);
- fflush(out);
- }
- close(pfd2[1]);
- close(pfd1[0]);
- fclose(in); fclose(out);
- _exit(0);
- }
- close(pfd1[0]); close(pfd1[1]);
- close(pfd2[0]); close(pfd2[1]);
- wait(0); wait(0);
- printf("Done\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement