a. Describe the dining philosopher's problem and suggest any three ways to solve the problem without any starvation or deadlocks b. What are the necessary and sufficient conditions for a deadlock to take place? Describe each condition c. Describe a semaphore and the wait() and signal() methods associated with a semaphore d. How would you use a semaphore to achieve mutual exclusion?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

a. Describe the dining philosopher's problem and suggest any three ways to solve the problem without any
starvation or deadlocks

b. What are the necessary and sufficient conditions for a deadlock to take place? Describe each condition

c. Describe a semaphore and the wait() and signal() methods associated with a semaphore

d. How would you use a semaphore to achieve mutual exclusion?

Expert Solution
Step 1

a)

Suppose that there is a bowl of noodles kept on a circular table and there are 5 people sitting around it. This means that there will be a total of 5 plates and 5 chopsticks. If any person wants to eat noodles they can only eat them by using both left and right chopsticks. So, while one person is eating, the neighbors to their immediate left and right will have to wait, think, and starve as well.

The Dining Philosophers Problem is used to design a scheduling technique such that no philosopher will starve.

Solution to the problem

One way to solve the problem is by using Semaphores.

A Semaphore is a tool used for concurrent processes that works by assigning an integer value. The two functions that are operated using semaphore are wait() and signal() .

Take a look at the solution:

void Philosopher  
{
  while(1)  
  {  
    wait(take_chopstick[x]);  
    wait(take_chopstick[(x + 1) % 5]) ;  
    
    //  EATING THE NOODLE  
 
    signal(put_chopstick[x]);  
    signal(put_chopstick[(x + 1) % 5]) ;  
 
    // THINKING
  }  
}  

Explanation:

The above code suggests that the wait() operation is first performed where take_chopstick[x] and take_chopstick[(x+1) % 5] are executed. This corresponds to the philosopher picking up both the left and the right chopsticks and then starting to eat while the others wait and think until their turn comes.
After eating, the signal() operation is performed where put_chopstick[x] and put_chopstick[(x+1) % 5] are executed. This means that the philosopher has eaten, after which the philosopher puts down the chopstick and starts to think again.
This is the solution to the problem, but it has some drawbacks as it will create a deadlock. For example, when all the philosophers pick up the left chopstick and then have to wait for the right one – this will create a deadlock situation.

To avoid this deadlock, some measures that could have been taken are:

There could be, at most, 4 people at the table.
The philosophers can alternatively eat and think. For example, if the first philosopher is eating, then the second should wait and think while the third eats, and so on.

Every philosopher must request each of their (shared) chopsticks from a waiter, who may refuse the request at first in order to avoid a deadlock. For convenience, we assume that all philosophers request their left chopstick first, then their right chopstick

 Deadlock Avoidance:

  • It is the simplest and most friendly method.
  • It requires that each process declare the maximum number of resources of each type that it will need.
  • The deadlock-avoidance algorithm dynamically checks the resource-allocation state to ensure the system can never be in a circular-wait condition.
  • When a process requests a resource, the system must make sure that the allocation would leave the system in a safe state.
  • It the system is in a safe state, then there would be no deadlock. However, if it is in an unsafe state, that there is a possibility (not certainty) of a deadlock.
  • The avoidance approach requires that knowledge of all processes, all the resources available, the
    • resources allocated presently and the future requests by the processes.
    • For a single instance of a resource type we use the resource allocation graph.
    • For multiple instance of a resource type, we use the banker’s algorithm.
    • A major drawback of this method is that it is difficult to know at the beginning itself of the maximum resource required.
steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Bare Bones Programming Language
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education