Guest User

Untitled

a guest
May 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define MESSAGESIZE 4
  6. #define SEND 1
  7. #define RECV 0
  8.  
  9. char messagebuffer[MESSAGESIZE];
  10.  
  11. int main(int argc, char** argv)
  12. {
  13.  
  14. int token = 0;
  15.  
  16. int pipe1_2[2];
  17. int pipe2_3[2];
  18. int pipe3_1[2];
  19.  
  20. if(argc!=2)
  21. {
  22. printf("\nInsufficient Arguments. Exiting...\n\n");
  23. exit(EXIT_FAILURE);
  24. }
  25.  
  26. pipe(pipe1_2);
  27. pipe(pipe2_3);
  28. pipe(pipe3_1);
  29.  
  30. if(fork()!=0)
  31. { //PROCESS #1
  32.  
  33. token = atoi(argv[1]);
  34.  
  35. while(1)
  36. {
  37.  
  38. close(pipe1_2[RECV]);
  39. write(pipe1_2[SEND], &token, MESSAGESIZE);
  40. // printf("\n[P1] Inserted %d into P1_2", token);fflush(stdout);
  41.  
  42. close(pipe3_1[SEND]);
  43. read(pipe3_1[RECV], &token, MESSAGESIZE);
  44. // printf("\n[P1] Received %d from P3_1", token);fflush(stdout);
  45. printf("\n[P1] Current lap: %d", token);fflush(stdout);
  46.  
  47. sleep(1);
  48. token++;
  49. }
  50.  
  51. }
  52. else if(fork()!=0)
  53. { // PROCESS #2
  54.  
  55. while(1)
  56. {
  57. close(pipe1_2[SEND]);
  58. read(pipe1_2[RECV], &token, MESSAGESIZE);
  59. // printf("\n[P2] Received %d from P1_2", token);fflush(stdout);
  60.  
  61. close(pipe2_3[RECV]);
  62. write(pipe2_3[SEND], &token, MESSAGESIZE);
  63. // printf("\n[P2] Inserted %d into P2_3", token);fflush(stdout);
  64.  
  65. }
  66.  
  67. }
  68. else
  69. {//PROCESS #3
  70. while(1)
  71. {
  72. close(pipe2_3[SEND]);
  73. read(pipe2_3[RECV], &token, MESSAGESIZE);
  74. // printf("\n[P3] Received %d from P2_3", token);fflush(stdout);
  75.  
  76. close(pipe3_1[RECV]);
  77. write(pipe3_1[SEND], &token, MESSAGESIZE);
  78. // printf("\n[P3] Inserted %d into P3_1", token);fflush(stdout);
  79. }
  80. }
  81. }
Add Comment
Please, Sign In to add comment