Advertisement
Guest User

Untitled

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