Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. bool woodPileEmpty = false;
  2. bool woodPileBusy = false;
  3. int amountWoodInPile = 10;
  4.  
  5. Semafore mutexWoodInPile, mutexWoodPileEmpty;
  6.  
  7. void stoker(){
  8. while(1){
  9. if(!woodPileBusy){
  10. woodPileBusy = true;
  11. mutexWoodInPile.wait();
  12. if(amountWoodInPile > 0){
  13. amountWoodInPile -= 1; // Adding wood to fire
  14. } else {
  15. woodPileEmpty = true;
  16. woodPileBusy = false;
  17. mutexWoodInPile.signal();
  18. suspend();
  19. }
  20. woodPileBusy = false;
  21. }
  22. }
  23. }
  24.  
  25.  
  26.  
  27. void stacker(){
  28. while(1){
  29. if(!woodPileBusy){
  30. woodPileBusy = true;
  31. mutexWoodPileEmpty.wait();
  32. if(woodPileEmpty){
  33. amountWoodInPile = 10; // Refilling woodpile
  34. resume(stoker);
  35. woodPileEmpty = false;
  36. }
  37. mutexWoodPileEmpty.signal();
  38. woodPileBusy = false;
  39. }
  40. }
  41. The semaphores will protect the critical region, which is the IF statements, thus defendig it from data races.
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement