Last active
December 7, 2019 11:33
-
-
Save anhnguyen1618/8bd150896f07f595f0c123c4e2563e29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Class Monitor { | |
| string[] states; | |
| cond[] q | |
| pickup() { | |
| states[i] = 'Hungry' | |
| while (check(i) == 'Hungry') { | |
| queue[i].wait() | |
| } | |
| } | |
| drop() { | |
| states[i] = 'hungry' | |
| queue[(i + 1) % 5].signal() | |
| queue[(i + 4) % 5].signal() | |
| } | |
| check(i) { | |
| if ( | |
| states[(i + 4) % 5] != 'eating' && | |
| states[(i + 1) % 5] != 'eating') { | |
| states[i] = 'eating' | |
| return 'eating' | |
| } | |
| return 'hungry' | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment