Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4.  
  5. int forks[5][2]; // Each fork near one philosopher
  6. bool P[5]; // Philosophers done or not
  7.  
  8. int check_right(int pid) {
  9.     if(forks[pid % 5][0] == 0)
  10.         return 1;
  11.     else
  12.         return 0;
  13. }
  14.  
  15. int check_left(int pid) {
  16.     if(forks[((pid - 2) + 5) % 5][1] == 0)
  17.         return 1;
  18.     else
  19.         return 0;
  20. }
  21.  
  22. void take_right(int pid, int done_or_not) {
  23.     if(done_or_not == 1) {
  24.         //printf("P%i is done\n", pid);
  25.     }
  26.     else if(check_right(pid)) {
  27.         forks[pid - 1][1] = 1;
  28.     }
  29.     else {
  30.         //printf("P%i can't take the right one\n", pid);
  31.     }
  32. }
  33.  
  34.  
  35. void take_left(int pid, int done_or_not) {
  36.     if(done_or_not == 1) {
  37.         //printf("P%i is done\n", pid);
  38.     }
  39.     else if(check_left(pid)) {
  40.         forks[pid - 1][0] = 1;
  41.     }
  42.     else {
  43.         //printf("P%i can't take the left one\n", pid);
  44.     }
  45. }
  46.  
  47. int take_fork(int pid, int done_or_not, const char direction[]) {
  48.     if(!strcmp(direction, "left")) {
  49.         take_left(pid, done_or_not);
  50.         return 1;
  51.     }
  52.     else if(!strcmp(direction, "right")) {
  53.         take_right(pid, done_or_not);
  54.         return 0;
  55.     }
  56. }
  57.  
  58. int main() {
  59.     int i = 0, i2 = 0, counter = 1, p1, p2, p3, p4, p5;
  60.     const char *direction[2];
  61.     direction[0] = "left";
  62.     direction[1] = "right";
  63.  
  64.     for(i = 0; i < 5; ++i) {
  65.         for(i2 = 0; i2 < 2; ++i2)
  66.             forks[i][i2] = 0;
  67.  
  68.         P[i] = 0;
  69.     }
  70.  
  71.     while(P[0] == 0 || P[1] == 0 || P[2] == 0 || P[3] == 0 || P[4] == 0) {
  72.         printf("---------------- #%i ----------------\n", counter);
  73.         // Argument is which philosopher that should take the right or left fork
  74.         // Change to left or right for different preferences for the philosophers
  75.         p1 = take_fork(1 ,P[0] , "left");
  76.         p2 = take_fork(2 ,P[1] , "left");
  77.         p3 = take_fork(3 ,P[2] , "left");
  78.         p4 = take_fork(4 ,P[3] , "right");
  79.         p5 = take_fork(5 ,P[4] , "right");
  80.  
  81.         // The opposite of their first try
  82.         take_fork(1 ,P[0] , direction[p1]);
  83.         take_fork(2 ,P[1] , direction[p2]);
  84.         take_fork(3 ,P[2] , direction[p3]);
  85.         take_fork(4 ,P[3] , direction[p4]);
  86.         take_fork(5 ,P[4] , direction[p5]);
  87.  
  88.         for(i = 0; i < 5; ++i)
  89.             printf("%i P%i %i || ", forks[i][0] , i + 1, forks[i][1]);
  90.  
  91.         printf("\n\n");
  92.  
  93.         for(i = 0; i < 5; ++i)
  94.             if(forks[i][0] == 1 && forks[i][1] == 1) {
  95.                 printf("P%i can eat and will do so \n", i + 1);
  96.                 P[i] = 1;
  97.  
  98.                 // Releases the forks so that they can be used by someone else
  99.                 forks[i][0] = forks[i][1] = 0;
  100.             }
  101.             else if(P[i] == 0){
  102.                 printf("P%i is starving\n", i + 1);
  103.             }
  104.  
  105.         printf("\n");
  106.  
  107.         for(i = 0; i < 5; ++i)
  108.             printf("%i P%i %i || ", forks[i][0] , i + 1, forks[i][1]);
  109.  
  110.         printf("\n");
  111.         ++counter;
  112.         sleep(5);
  113.     }
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement