Facebook
From Scanty Stork, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 187
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6.  
  7. #define NUMBER_OF_PHILOSOPHERS 5
  8.  
  9. void *philosopher(void *);
  10. void think(int);
  11. void pickUp(int);
  12. void eat(int);
  13. void putDown(int);
  14.  
  15. pthread_mutex_t chopsticks[NUMBER_OF_PHILOSOPHERS];
  16. pthread_t philosophers[NUMBER_OF_PHILOSOPHERS];
  17. pthread_attr_t attributes[NUMBER_OF_PHILOSOPHERS];
  18.  
  19. int main() {
  20.         int i;
  21.         srand(time(NULL));
  22.         for (i = 0; i < NUMBER_OF_PHILOSOPHERS; ++i) {
  23.                 pthread_mutex_init(&chopsticks[i], NULL);
  24.         }
  25.  
  26.         for (i = 0; i < NUMBER_OF_PHILOSOPHERS; ++i) {
  27.                 pthread_attr_init(&attributes[i]);
  28.         }
  29.        
  30.         for (i = 0; i < NUMBER_OF_PHILOSOPHERS; ++i) {
  31.                 pthread_create(&philosophers[i], &attributes[i], philosopher, (void *)(i));
  32.         }
  33.  
  34.         for (i = 0; i < NUMBER_OF_PHILOSOPHERS; ++i) {
  35.                 pthread_join(philosophers[i], NULL);
  36.         }
  37.         return 0;
  38. }
  39.  
  40. void *philosopher(void *philosopherNumber) {
  41.         while (1) {
  42.                 think(philosopherNumber);
  43.                 pickUp(philosopherNumber);
  44.                 eat(philosopherNumber);
  45.                 putDown(philosopherNumber);
  46.         }
  47. }
  48.  
  49. void think(int philosopherNumber) {
  50.         int sleepTime = rand() % 3 + 1;
  51.         printf("Philosopher %d will think for %d secondsn", philosopherNumber, sleepTime);
  52.         sleep(1);
  53. }
  54.  
  55. void pickUp(int philosopherNumber) {
  56.        
  57.                 int right = (philosopherNumber + 1) % NUMBER_OF_PHILOSOPHERS;
  58.         int left = (philosopherNumber + NUMBER_OF_PHILOSOPHERS) % NUMBER_OF_PHILOSOPHERS;
  59.        
  60.                 printf("Philosopher %d is waiting to pick up chopstick %dn", philosopherNumber, left);
  61.                 pthread_mutex_lock(&chopsticks[left]);
  62.                 printf("Philosopher %d picked up chopstick %dn", philosopherNumber, left);
  63.                
  64. printf("Philosopher %d is waiting to pick up chopstick %dn", philosopherNumber, right);
  65.                 pthread_mutex_lock(&chopsticks[right]);
  66.                 printf("Philosopher %d picked up chopstick %dn", philosopherNumber, right);
  67.        
  68. }
  69.  
  70. void eat(int philosopherNumber) {
  71.         int eatTime = rand() % 3 + 1;
  72.         printf("Philosopher %d will eat for %d secondsn", philosopherNumber, eatTime);
  73.         sleep(eatTime);
  74. }
  75.  
  76. void putDown(int philosopherNumber) {
  77.         printf("Philosopher %d will will put down her chopsticksn", philosopherNumber);
  78.         pthread_mutex_unlock(&chopsticks[(philosopherNumber + 1) % NUMBER_OF_PHILOSOPHERS]);
  79.         pthread_mutex_unlock(&chopsticks[(philosopherNumber + NUMBER_OF_PHILOSOPHERS) % NUMBER_OF_PHILOSOPHERS]);
  80. }