Advertisement
Aqib12

dining_philosopher

Aug 8th, 2020
1,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. //
  2. // Created by aqib on 8/9/20.
  3. //
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. #include <pthread.h>
  8. #include <semaphore.h>
  9. #include <cstdio>
  10. #include <unistd.h>
  11. #include <ctime>
  12.  
  13. #define N 5
  14. #define THINKING 2
  15. #define HUNGRY 1
  16. #define EATING 0
  17. #define LEFT (philosopher_number + 4) % N
  18. #define RIGHT (philosopher_number + 1) % N
  19.  
  20. //int j=0;
  21. int flag[N] = {0};
  22. char status[][10] = {"EATING", "HUNGRY", "THINKING"};
  23.  
  24. int state[N];
  25. int phil[N] = {0, 1, 2, 3, 4};
  26.  
  27. sem_t mutex;
  28. sem_t S[N];
  29.  
  30. void showStatus() {
  31.     printf("\n");
  32.     for (int k = 0; k < 5; k++) {
  33.         if (flag[k] != 0)
  34.             printf("Philosopher %d is %s \t", k + 1, status[state[k]]);
  35.     }
  36.     printf("\n");
  37.     sleep(3);
  38. }
  39.  
  40.  
  41. void test(int philosopher_number) {
  42.     if (state[philosopher_number] == HUNGRY
  43.         && state[LEFT] != EATING
  44.         && state[RIGHT] != EATING) {
  45.         // state that eating
  46.         state[philosopher_number] = EATING;
  47.         sleep(2);
  48.         printf("\nPhilosopher %d takes fork %d and %d\n", philosopher_number + 1, LEFT + 1, philosopher_number + 1);
  49.         showStatus();
  50.         // sem_post(&S[philosopher_number]) has no effect
  51.         // during take_fork
  52.         // used to wake up hungry philosophers
  53.         // during put_fork
  54.         sem_post(&S[philosopher_number]);
  55.     }
  56. }
  57.  
  58. // take up chopsticks
  59. void take_fork(int philosopher_number) {
  60.     sem_wait(&mutex);
  61.     // state that hungry
  62.     state[philosopher_number] = HUNGRY;
  63.     showStatus();
  64.     // eat if neighbours are not eating
  65.     test(philosopher_number);
  66.     sem_post(&mutex);
  67.     // if unable to eat wait to be signalled
  68.     sem_wait(&S[philosopher_number]);
  69.     sleep(1);
  70. }
  71.  
  72. // put down chopsticks
  73. void put_fork(int philosopher_number) {
  74.     sem_wait(&mutex);
  75.     // state that thinking
  76.     state[philosopher_number] = THINKING;
  77.     printf("\nPhilosopher %d putting fork %d and %d down\n",
  78.            philosopher_number + 1, LEFT + 1, philosopher_number + 1);
  79.     showStatus();
  80.     test(LEFT);
  81.     test(RIGHT);
  82.     sem_post(&mutex);
  83. }
  84.  
  85. void *philosopher(void *num) {
  86.     for (int j = 0; j < 3; j++) {
  87.         int *i = (int *) num;
  88.         sleep(1);
  89.         take_fork(*i);
  90.         sleep(0);
  91.         put_fork(*i);
  92.         printf("\n\n");
  93.     }
  94.     return num;
  95. }
  96.  
  97.  
  98. int main() {
  99.     std::ios_base::sync_with_stdio(false);
  100.     std::cin.tie(nullptr);
  101.     pthread_t thread_id[N];
  102.     struct timespec stop{}, start{};
  103.     clock_gettime(CLOCK_REALTIME, &start);
  104.     std::cout << start.tv_sec << " " << start.tv_nsec << '\n';
  105.     // initialize the semaphores
  106.     sem_init(&mutex, 0, 1);
  107.     for (auto & i : S)
  108.         sem_init(&i, 0, 0);
  109.     for (int i = 0; i < N; i++) {
  110.         // create philosopher processes
  111.         pthread_create(&thread_id[i], nullptr,
  112.                        philosopher, &phil[i]);
  113.         clock_gettime(CLOCK_REALTIME, &stop);
  114.         printf("\nAt %lu micro-seconds Philosopher %d is created and he starts thinking\n", ((stop.tv_nsec - start.tv_nsec) / 1000), i + 1);
  115.         flag[i] = 1;
  116.     }
  117.     for (unsigned long i : thread_id) pthread_join(i, nullptr);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement