Skip to content

Instantly share code, notes, and snippets.

@anhnguyen1618
Created December 7, 2019 11:51
Show Gist options
  • Save anhnguyen1618/c55de3b5a26549aaa8c0a48b9aafe12d to your computer and use it in GitHub Desktop.
Save anhnguyen1618/c55de3b5a26549aaa8c0a48b9aafe12d to your computer and use it in GitHub Desktop.
Semaphore[] forks = {1, 1, 1, 1, 1}
eat(i) {
left= (i + 4) % 5
right = (i + 1) % 5
if (i == 5) {
forks[right].acquire()
forks[left].acquire()
eat()
forks[right].release()
forks[left].release()
return;
}
forks[left].acquire()
forls[right].acquire()
eat()
forks[right].release()
forks[left].release()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment