Question

In: Computer Science

We consider the dining philosophers problem. If all philosophers pick up their left fork, that causes...

We consider the dining philosophers problem. If all philosophers pick up their left fork, that causes deadlock.

Question: If one (and only one) of the philosophers instead tries to pick up their right fork first, can this system still deadlock? Why/ why not?

Solutions

Expert Solution

1. If only one of the philosophers pickup their right forks, this may remove the deadlock but it might cause starvation.

Ex. If 2 philosophere eat and think very fast and they are sitting in front of each other on the table, then the other 3 philosophers might be in a state of starving.

2. Other way to avoid the deadlock is :

  • An even philosopher should pick the right chopstick and then the left chopstick while an odd philosopher should pick the left chopstick and then the right chopstick.
  • A philosopher should only be allowed to pick their chopstick if both are available at the same time.

3. The forkd will be numbered from 1 through 5 and each philosopher will always pick up the lower numbered for first, and then the higher numbered fork, from among the 2 forks they plan to use. The order in which each philosopher puts down the  forks does not matter. In this case, if 4 of the 5 philosophers imultaneously pick up their lower-numbered fork, only the highest-numbered fork will remain on the table, so the fifth philosopher will not be able to pick up any fork. Moreover, only one philosopher will have access to that highest-numbered fork, so they will be able to eat using two forks.

Threading Solutions :

require 'thread'

class Philosopher
  def initialize(name, left, right)
    @name = name
    @left = left
    @right = right
    @has_left = false
    @has_right = false
    @plates = 0
  end

  def pick_up_left(chopstick)
    if chopstick.locked?
      @has_left = false
      puts "Philosopher #{@name} couldn't pick up the left chopstick"
    else
      chopstick.lock
      @has_left = true
      puts "Philosopher #{@name} picked up a left chopstick."
    end
  end

  def pick_up_right(chopstick)
    if chopstick.locked?
      @has_right = false
      puts "Philosopher #{@name} couldn't pick up the right chopstick"
    else
      chopstick.lock
      @has_right = true
      puts "Philosopher #{@name} picked up a right chopstick."
    end
  end

  def eat
    until @plates == 5          # arbitrarily, a meal == 5 plates
      pick_up_left(@left)
      pick_up_right(@right)
      if @has_left && @has_right
        puts "Philosopher #{@name} is eating now."
        @plates += 1
        @left.unlock
        @right.unlock
      elsif @has_left
        @left.unlock
        puts "Philosopher #{@name} is putting down the left chopstick."
      elsif @has_right
        @right.unlock
        puts "Philosopher #{@name} is putting down the right chopstick."
      end
    end
  end  
end

class Meal
  def initialize(num)           # num is the number of philosophers
    @chopsticks = []
    num.times { @chopsticks << Mutex.new }
    @philosophers = []
    (0..(num-2)).each { |i| @philosophers << Philosopher.new(i, @chopsticks[i], @chopsticks[i+1]) }
    #handle special case philospher who uses first and last chopstick in list
    @philosophers << Philosopher.new(num-1, @chopsticks[num-1], @chopsticks[0])
    @threads = []
    @philosophers.each do |p| 
      @threads << Thread.new { p.eat }
    end
    @threads.each {|t| t.join}
  end
end

m = Meal.new(12) 

Related Solutions

Operating Systems: We have learned two methods for the Dining Philosophers Problem. Can you come up...
Operating Systems: We have learned two methods for the Dining Philosophers Problem. Can you come up with other solutions? Make sure your solution works. Wrong solution does not count. (20 points)
C programming: Using the following rules of the Dining philosophers problem: • N philosophers spend their...
C programming: Using the following rules of the Dining philosophers problem: • N philosophers spend their lives thinking and eating rice. • There are only N chopstick on the table, one between every two philosophers. • In order to eat, a philosopher needs to get hold of the two chopsticks that are closest to her. • A philosopher cannot pick up a chopstick that is already taken by a neighbor. Implement the dining philosophers problem using pthreads and monitor. Your...
Briefly explain the deadlock situation that can occur in the dining philosophers and how we could...
Briefly explain the deadlock situation that can occur in the dining philosophers and how we could resolve the problem.
What is the most Environmental Problems we have? 5 Causes of that problem 5 Consequences of...
What is the most Environmental Problems we have? 5 Causes of that problem 5 Consequences of that problem to the Environment and People 5 Solutions to the problem 5 Preventions to the problem
(Sampling). For this Pause Problem, pick out a sampling strategy from the ones we just discussed...
(Sampling). For this Pause Problem, pick out a sampling strategy from the ones we just discussed and give me a study idea where you would WANT to use that strategy and a study example where you would NOT want to use that strategy
Java Programming Question The problem is to count all the possible paths from top left to...
Java Programming Question The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move to right or down.Input: The first line of input contains an integer T, denoting the number of test cases. The first line of each test case is M and N, M is number of rows and N is number of columns.Output: For each test case, print the...
. Approximately 10% of all people are left-handed, then the rest of people are right-handed. Consider...
. Approximately 10% of all people are left-handed, then the rest of people are right-handed. Consider a group of 15 people, answer the following question: (You cannot use the binomial table for this problem) (a) State the random variable ?. (2 points) (b) Explain why this is a binomial experiment. There should be 4 conditions to check. (State the probability of left-handed people ?, the possible outcomes, and the number of trials ? in the conditions.) (4 points) (c) Find...
There are many variations on the Knapsack problem. In this question, consider the same set up...
There are many variations on the Knapsack problem. In this question, consider the same set up as the 0-1 Knapsack problem except suppose there are as many copies of each item as you would like. Thus, if one of the items is small but very valuable, it is possible to put many exact duplicates of that item into the knapsack. To maximize the value of items in the knapsack, the thief may decide to put in as many of the...
Suppose that 8% of all Canadians are left-handed. A) If we randomly select 200 Canadians, what...
Suppose that 8% of all Canadians are left-handed. A) If we randomly select 200 Canadians, what is the approximate probability that less than 5% of them are left-handed? (Keep 6 decimal places in intermediate calculations and report your final answer to 4 decimal places. B) If we randomly select 200 Canadians, what is the approximate probability that more than 20 of them are left-handed? (Keep 6 decimal places in intermediate calculations and report your final answer to 4 decimal places.
1. In this problem, we assume for convenience that we consider call options for only one...
1. In this problem, we assume for convenience that we consider call options for only one share of a stock. We consider only one stock, and all options are for this stock. We denote the expiration date of the options by T, and we assume that it is the same date for all options considered below. We denote prices as pure numbers, omitting any notation for a currency such as the dollar sign. You may assume that the price C(K)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT