Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Dining Philosophers Problem With Ron Swanson #1061

Open
guevara opened this issue Oct 9, 2018 · 0 comments
Open

The Dining Philosophers Problem With Ron Swanson #1061

guevara opened this issue Oct 9, 2018 · 0 comments

Comments

@guevara
Copy link
Owner

guevara commented Oct 9, 2018

The Dining Philosophers Problem With Ron Swanson



https://ift.tt/1eA1rfo






      <h1>The Dining Philosophers Problem With Ron Swanson</h1>

Written May 11, 2013

Give me all the bacon and eggs you have.

- Plato

The dining philosophers problem is a classic concurrency problem dealing with synchronization. Gather round and I'll tell you how it goes:

Five philosophers are sitting at a table.

(image credit goes to the super talented Dustin D'Arnault)

Now, each philosopher has two forks: left fork and right fork. If a Swanson gets two forks, he can eat!

If he only has one fork he can't eat :(

So the Swansons need to learn to share forks:

The dining philosophers problem is: how do you make sure every Swanson gets to eat?

Deadlock

Don't get me wrong, I love Swansons. But damn they are competitive. As soon as I told them to start eating, every Swanson grabbed a fork:

And then they waited for someone to give up their fork so they could eat. But of course, "a Swanson never gives up his fork!" sigh So they waited forever and eventually died in their stupid log cabin. Great job, guys. When all Swansons are stuck, that is called deadlock.

Livelock

Alright Rons. I'm going to set a time limit. If you have been waiting for a fork for 10 minutes, you need to give up your fork. And wait 10 minutes before you try to pick up a fork again. By then, someone else will be done and you can use his fork:

Now you can't have deadlock. But if all Swansons do these actions at the exact same time, you now have livelock:

Yay! Nothing can go wrong now! Right?

30 minutes later

So let me get this straight...

  1. You all picked up your fork at the exact same time.
  2. 10 minutes later, you all put down your forks.
  3. 10 minutes later, you just picked up your forks again! This will go on forever!

What do, nephew? You Swansons will starve!

Resource Hierarchy

Let's number the forks:

Now Rons, the rule is, if you're using two forks, you need to pick up the lower numbered fork first.

Now let's see what happens:

So fork #5 goes to Ron #4 – no deadlock!

Cool, resource hierarchy avoids deadlocks! But it is slow. Suppose you have forks #3 and #5. Then you decide you need fork #2. Well forks #3 and #5 are larger numbers. So you'll have to:

  1. put down fork 卢小波 难吃的月饼勾画出了中国的人际关系 #5
  2. put down fork 5 Easy Ways to Save an Article Feedly Blog #3 (the order you put these down in doesn't matter)
  3. pick up fork 如何收集竞争情报财报解读 #2
  4. pick up fork 5 Easy Ways to Save an Article Feedly Blog #3
  5. pick up fork 卢小波 难吃的月饼勾画出了中国的人际关系 #5

Ugh, what a waste of time!

Semaphores

Here's an easier solution: if there are 5 forks, only 4 Swansons should be allowed at the table. We'll have a waiter control access to the table:

(Waiter by talented artist Patricia Reed)

If there are < 4 Swansons on the table, you can join. Otherwise you have to wait until there are < 4 Swansons!

Ok, this prevents deadlock, but you can still have starvation...a Swanson could wait forever and never get to sit at the table. Unless he kills off one of the other Swansons.

Chandy/Misra

Now I've got it. All forks can be clean or dirty:

Initially all forks are dirty:

Now:

  1. Number all the Swansons

  2. For every pair of Swansons, give the fork to the guy with the smaller id:

  1. When a Swanson needs a fork, he asks his neighbor for his fork. When his neighbor gets the request:
  • if his fork is clean, he keeps it.
  • if his fork is dirty, he cleans it and sends it over.

When a Swanson has eaten, all his forks become dirty.

Bam! Starvation problem solved! The guys who are starving get a higher priority than the guys who are eating!

Q. Swansons 3 and 4 both only have one fork. Suppose Swanson #3 requests a second fork from Swanson #4. Will Swanson #4 give up his fork even though he didn't eat?

A. YES. According to the rules of Chandy-Misra, Swanson #4 would have to clean his fork and send it over to Swanson #3.

References

Check out part 2 of this post.

Please enable JavaScript to view the comments powered by Disqus.

blog comments powered by Disqus

    </div>
  </div><br>





via adit.io http://adit.io

October 9, 2018 at 04:48PM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant