Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //controller.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #define BUFSIZE 256
- main()
- {
- int c1p[2]; //pipe to link c1 to c2
- int c1pid; //parent ID
- int c1status;
- int c2p[2]; //pipe
- int c2pid; //parent ID
- int c2status;
- int c3p[2]; //pipe
- int c3pid; //parent ID
- int c3status;
- //buffer to store user words
- char buffer[BUFSIZE];
- //initialize pipes
- pipe(c1p);
- pipe(c3p);
- //begin child process of c1
- c1pid = fork();
- if (c1pid == 0) {
- //set the newfd to be the duplicate descrition of the program
- int newfd = dup2(c1p[1],1);
- //if the duplication failed
- if (newfd != 1)
- {
- fprintf(stderr,"unable to duplicate newfd \n");
- exit(1);
- }
- //execute the program
- execvp("./c1", NULL);
- //close the program
- close(c1p[0]);
- } else {
- //wait for the child process to finish
- c1pid = wait(&c1status);
- //print the exit status
- int es = WEXITSTATUS(c1status);
- printf("c1 finished \n");
- printf("Exit status was %d\n\n", es);
- }
- //begin child process of c2
- c2pid = fork();
- if (c2pid == 0) {
- //rediredt the stdout to the stdin of c2
- dup2(c1p[1],0);
- close(c1p[1]);
- //set the newfd to be the duplicate descrition of the program
- int newfd = dup2(c2p[1],1);
- //if the duplication failed
- if (newfd != 1)
- {
- fprintf(stderr,"unable to duplicate newfd \n");
- exit(1);
- }
- //execute the program
- execvp("./c2", NULL);
- //close the prog
- close(c2p[0]);
- } else {
- //wait for the child process to finish
- c2pid = wait(&c2status);
- int es = WEXITSTATUS(c2status);
- //print the exit status
- printf("c2 finished \n");
- printf("Exit status was %d\n\n", es);
- }
- //begin child process of c3
- c3pid = fork();
- if (c3pid == 0) {
- //rediredt the stdout to the stdin of c3
- dup2(c3p[0],0);
- int newfd = dup2(c3p[0],0);
- //ecxecute the program
- execvp("./c3", NULL);
- //close the program
- close(c3p[1]);
- } else {
- //wait for the child process to finish
- c3pid = wait(&c3status);
- //print the exit status
- int es = WEXITSTATUS(c3status);
- printf("c3 finished \n");
- printf("Exit status was %d\n\n", es);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement