Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define MESSAGESIZE 4
- #define SEND 1
- #define RECV 0
- char messagebuffer[MESSAGESIZE];
- int main(int argc, char** argv)
- {
- int token = 0;
- int pipe1_2[2];
- int pipe2_3[2];
- int pipe3_1[2];
- if(argc!=2)
- {
- printf("\nInsufficient Arguments. Exiting...\n\n");
- exit(EXIT_FAILURE);
- }
- pipe(pipe1_2);
- pipe(pipe2_3);
- pipe(pipe3_1);
- if(fork()!=0)
- { //PROCESS #1
- token = atoi(argv[1]);
- while(1)
- {
- close(pipe1_2[RECV]);
- write(pipe1_2[SEND], &token, MESSAGESIZE);
- // printf("\n[P1] Inserted %d into P1_2", token);fflush(stdout);
- close(pipe3_1[SEND]);
- read(pipe3_1[RECV], &token, MESSAGESIZE);
- // printf("\n[P1] Received %d from P3_1", token);fflush(stdout);
- printf("\n[P1] Current lap: %d", token);fflush(stdout);
- sleep(1);
- token++;
- }
- }
- else if(fork()!=0)
- { // PROCESS #2
- while(1)
- {
- close(pipe1_2[SEND]);
- read(pipe1_2[RECV], &token, MESSAGESIZE);
- // printf("\n[P2] Received %d from P1_2", token);fflush(stdout);
- close(pipe2_3[RECV]);
- write(pipe2_3[SEND], &token, MESSAGESIZE);
- // printf("\n[P2] Inserted %d into P2_3", token);fflush(stdout);
- }
- }
- else
- {//PROCESS #3
- while(1)
- {
- close(pipe2_3[SEND]);
- read(pipe2_3[RECV], &token, MESSAGESIZE);
- // printf("\n[P3] Received %d from P2_3", token);fflush(stdout);
- close(pipe3_1[RECV]);
- write(pipe3_1[SEND], &token, MESSAGESIZE);
- // printf("\n[P3] Inserted %d into P3_1", token);fflush(stdout);
- }
- }
- }
Add Comment
Please, Sign In to add comment