Advertisement
Guest User

Untitled

a guest
May 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include "thread.h"
  2. #include "semaphore.h"
  3. #include <stdio.h>
  4. #include "intlock.h"
  5.  
  6. #define N 5
  7. #define THINKING 2
  8. #define HUNGRY 1
  9. #define EATING 0
  10. #define LEFT (phnum + 4) % N
  11. #define RIGHT (phnum + 1) % N
  12.  
  13. int state[N];
  14. int treshold = 10000;
  15.  
  16. Semaphore mutex(1); //moguca greska??
  17. Semaphore* S[N];
  18.    
  19. void test(int phnum)
  20. {
  21.     if (state[phnum] == HUNGRY
  22.         && state[LEFT] != EATING
  23.         && state[RIGHT] != EATING) {
  24.         // state that eating
  25.         state[phnum] = EATING;
  26.  
  27.         sleep(2);
  28.         intLock
  29.         std::cout << "Philosopher " << phnum + 1 << " takes fork " << LEFT + 1 << " and " << phnum + 1 << std::endl;
  30.         std::cout << "Philosopher " << phnum + 1 << " is Eating\n";
  31.         intUnlock
  32.        
  33.         S[phnum]->signal();
  34.     }
  35. }
  36.  
  37. void take_fork(int phnum)
  38. {
  39.     mutex.wait();
  40.  
  41.     // state that hungry
  42.     state[phnum] = HUNGRY;
  43.  
  44.     intLock
  45.     std::cout << "Philosopher " << phnum + 1 << " is Hungry\n";
  46.     intUnlock
  47.  
  48.     // eat if neighbours are not eating
  49.     test(phnum);
  50.  
  51.     mutex.signal();
  52.    
  53.     S[phnum]->wait();
  54.     sleep(1);
  55. }
  56.  
  57. // put down chopsticks
  58. void put_fork(int phnum)
  59. {
  60.     mutex.wait();
  61.     // state that thinking
  62.     state[phnum] = THINKING;
  63.  
  64.     intLock
  65.     std::cout << "Philosopher " << phnum + 1 << " putting fork " << LEFT + 1 << " and " << phnum +1 << " down\n";
  66.     std::cout << "Philosopher " << phnum +1  << " is thinking\n";
  67.     intUnlock
  68.  
  69.     test(LEFT);
  70.     test(RIGHT);
  71.  
  72.     mutex.signal();
  73. }
  74.  
  75. void* philospher(void num)
  76. {
  77.     int i = 0;
  78.     while(i++ < treshold) {
  79.         int i = num;
  80.         sleep(1);
  81.         take_fork(i);
  82.         sleep(0);
  83.         put_fork(i);
  84.     }
  85. }
  86.  
  87. class Philosopher::public Thread{
  88. public:
  89.     Philosopher(int i) { num = i; }
  90.     virtual ~Philosopher() {
  91.         waitForComplete();
  92.     }
  93. protected:
  94.     void run() { philosopher(num); }
  95. private:
  96.     int num;
  97. }
  98.  
  99. void tick()
  100. {
  101. }
  102.  
  103. int userMain()
  104. {
  105.     Thread* threads[N];
  106.  
  107.     for (i = 0; i < N; i++)
  108.         S[i] = new Semaphore(0);
  109.  
  110.     for (i = 0; i < N; i++) {
  111.         threads[i] = new Philosopher(i);
  112.         intLock
  113.         std::cout << "Philosopher "<< i + 1 << " is thinking\n";
  114.         intUnlock
  115.        
  116.     }
  117.  
  118.     for (i = 0; i < N; i++)
  119.         threads[i]->waitForComplete();
  120.        
  121.     for (i = 0; i < N; i++)
  122.         delete S[i];
  123.  
  124.     for (i = 0; i < N; i++)
  125.         delete Philosopher(i);
  126.    
  127.     std::cout << "Test passed!\n"; 
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement