Advertisement
D_Pain

Dining Problem

Apr 1st, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. // Hyung Jun Hahn
  2.  
  3. #include <pthread.h>
  4. #include <time.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8.  
  9. #define NUM_PERSON 5
  10.  
  11. void think(int);
  12. void eat(int);
  13. void pickup_forks(int);
  14. void return_forks(int);
  15. void *person(void *count);
  16.  
  17. pthread_mutex_t forks[NUM_PERSON];
  18.  
  19. int main() {
  20.     pthread_t tid[NUM_PERSON];
  21.     pthread_attr_t attr;
  22.  
  23.     srand(time(NULL));
  24.     int i;
  25.     for (i = 0; i < NUM_PERSON; ++i) {
  26.         pthread_mutex_init(&forks[i], NULL);
  27.         pthread_attr_init(&attr);
  28.         pthread_create(&tid[i], &attr, person, (void *) i);
  29.     }
  30.  
  31.     for (i = 0; i < NUM_PERSON; ++i) {
  32.         pthread_join(tid[i], NULL);
  33.     }
  34.     return 0;
  35. }
  36.  
  37. void *person(void *count) {
  38.     int* num = (int *)count;
  39.     int temp = num;
  40.     while (1) {
  41.         think(temp);
  42.         pickup_forks(temp);
  43.         eat(temp);
  44.         return_forks(temp);
  45.     }
  46. }
  47.    
  48. void think(int num) {
  49.     // 1~3 seconds
  50.     int time = (rand() % 3) + 1;
  51.     printf("Person(%d) thinking for %d seconds\n", num, time);
  52.     sleep(time);
  53. }
  54.  
  55. void eat(int num) {
  56.     // 1~3 seconds
  57.     int time = (rand() % 3) + 1;
  58.     printf("Person(%d) eating for %d seconds\n", num, time);
  59.     sleep(time);
  60. }
  61.  
  62. void pickup_forks(int num) {
  63.     int left = (num + NUM_PERSON) % NUM_PERSON;
  64.     int right = (num + 1) % NUM_PERSON;
  65.  
  66.     if (num == 1) {
  67.         printf("Person(%d) waiting to pick up fork %d\n", num, right);
  68.         pthread_mutex_lock(&forks[right]);
  69.         printf("Person(%d) picked up fork %d\n", num, right);
  70.         printf("Person(%d) waiting to pick up fork %d\n", num, left);
  71.         pthread_mutex_lock(&forks[left]);
  72.         printf("Person(%d) picked up fork %d\n", num, left);
  73.     } else {
  74.         printf("Person(%d) waiting to pick up fork %d\n", num, left);
  75.         pthread_mutex_lock(&forks[left]);
  76.         printf("Person(%d) picked up fork %d\n", num, left);
  77.         printf("Person(%d) waiting to pick up fork %d\n", num, right);
  78.         pthread_mutex_lock(&forks[right]);
  79.         printf("Person(%d) picked up fork %d\n", num, right);
  80.     }
  81. }
  82.  
  83. void return_forks(int num) {
  84.     printf("Person(%d) putting down forks\n", num);
  85.     pthread_mutex_unlock(&forks[(num + 1) % NUM_PERSON]);
  86.     pthread_mutex_unlock(&forks[(num + NUM_PERSON) % NUM_PERSON]);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement