Dining Philosophers Problem - Introduction to Operating System - Lecture Notes, Study notes of Operating Systems

Dining philosopher problem, Highlevel synchronization constructs, Critical region, Monitor, Condition variable, Violation of mutual Exclusion, Producer process. Above mentioned are key points of this lecture handout. Virtual University handout for introduction to operating system are in detail and explanatory.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

ahsen
ahsen 🇵🇰

4.6

(88)

84 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
120
Operating Systems--[CS-604] Lecture No. 25
Operating Systems
Lecture No. 25
Reading Material
Chapter 7 of the textbook
Lecture 25 on Virtual TV
Summary
Dining philosophers problem
High-level synchronization constructs
Critical region
Monitor
Dining Philosophers Problem
Several possibilities that remedy the deadlock situation discussed in the last lecture are
listed. Each results in a good solution for the problem.
Allow at most four philosophers to be sitting simultaneously at the table.
Allow a philosopher to pick up her chopsticks only if both chopsticks are
available (to do this she must pick them up in a critical section)
Use an asymmetric solution; that is, an odd philosopher picks up first her left
chopstick, whereas an even philosopher picks up her right chopstick and then her
left chopstick.
Removing the possibility of deadlock does not ensure that starvation does not occur.
Imagine that two philosophers are fast thinkers and fast eaters. They think fast and get
hungry fast. Then, they sit down in opposite chairs as shown below. Because they are so
fast, it is possible that they can lock their chopsticks and eat. After finish eating and
before their neighbors can lock the chopsticks and eat, they come back again and lock the
chopsticks and eat. In this case, the other three philosophers, even though they have been
sitting for a long time, they have no chance to eat. This is a starvation. Note that it is not a
deadlock because there is no circular waiting, and everyone has a chance to eat!
pf3
pf4
pf5

Partial preview of the text

Download Dining Philosophers Problem - Introduction to Operating System - Lecture Notes and more Study notes Operating Systems in PDF only on Docsity!

Operating Systems--[CS-604] Lecture No. 25

Operating Systems

Lecture No. 25

Reading Material

ƒ Chapter 7 of the textbook ƒ Lecture 25 on Virtual TV

Summary

ƒ Dining philosophers problem ƒ High-level synchronization constructs ƒ Critical region ƒ Monitor

Dining Philosophers Problem

Several possibilities that remedy the deadlock situation discussed in the last lecture are listed. Each results in a good solution for the problem. ƒ Allow at most four philosophers to be sitting simultaneously at the table. ƒ Allow a philosopher to pick up her chopsticks only if both chopsticks are available (to do this she must pick them up in a critical section) ƒ Use an asymmetric solution; that is, an odd philosopher picks up first her left chopstick, whereas an even philosopher picks up her right chopstick and then her left chopstick. Removing the possibility of deadlock does not ensure that starvation does not occur. Imagine that two philosophers are fast thinkers and fast eaters. They think fast and get hungry fast. Then, they sit down in opposite chairs as shown below. Because they are so fast, it is possible that they can lock their chopsticks and eat. After finish eating and before their neighbors can lock the chopsticks and eat, they come back again and lock the chopsticks and eat. In this case, the other three philosophers, even though they have been sitting for a long time, they have no chance to eat. This is a starvation. Note that it is not a deadlock because there is no circular waiting, and everyone has a chance to eat!

High-level Synchronization Constructs

We discussed the problems of deadlock, starvation, and violation of mutual exclusion caused by the poor use of semaphores in lecture 23. We now discuss some high-level synchronization constructs that help solve some of these problems.

Critical regions Although semaphores provide a convenient and effective mechanism for process synchronization, their incorrect usage can still result in timing errors that are difficult to detect, since these errors occur only if some particular execution takes place, and these sequences do not always happen. To illustrate how, let us review the solution to the critical section problem using semaphores. All processes share a semaphore variable mutex, which is initialized to 1. Each process must execute wait(mutex) before entering the critical section and signal(mutex) afterward. If this sequence is not observed, two processes may be in their critical sections simultaneously. To deal with the type of errors we outlined above and in lecture 23, a number of high- level constructs have been introduced. In this section we describe one fundamental high- level synchronization construct—the critical region. We assume that a process consists of some local data, and a sequential program that can operate on the data. Only the sequential program code that is encapsulated within the same process can access the local data. That is, one process cannot directly access the local data of another process. Processes can however share global data. The critical region high-level synchronization construct requires that a variable v of type T, which is to be shared among many processes, be declared as:

v:shared T;

The variable v can be accessed only inside a region statement of the following form:

region v when B do S;

This construct means that, while statement S is being executed, no other process can access the variable v. The expression B is a Boolean expression that governs the access to the critical region. When a process tries to enter the critical-section region, the Boolean expression B is evaluated. If the expression is true, statement S is executed. If it is false, the process relinquishes the mutual exclusion and is delayed until B becomes true and no other process is in the region associated with v. Thus if the two statements,

region v when(true) S1; region v when(true) S2;

are executed concurrently in distinct sequential processes, the result will be equivalent to the sequential execution “S1 followed by S2” or “S2 followed by S1”. The critical region construct can be effectively used to solve several certain general synchronization problems. We now show use of the critical region construct to solve the bounded buffer problem. Here is the declaration of buffer:

construct explicitly. While one process is active within a monitor, other processes trying to access a monitor wait outside the monitor. The following diagram shows the big picture of a monitor.

However, the monitor construct as defined so far is not powerful enough to model some synchronization schemes. For this purpose we need to define additional synchronization mechanisms. These mechanisms are provided by the condition construct (also called condition variable ). A programmer who needs to write her own tailor made synchronization scheme can define one or more variables of type condition.

condition x,y;

The only operations that can be invoked on a condition variable are wait and signal. The operation x.wait();

means that the process invoking this operation is suspended until another process invokes. x.signal();

The x.signal() operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect; that is, the state of x is as though the operation were never executed. This is unlike the signal operation on a semaphore, where a signal operation always increments value of the semaphore by one. Monitors with condition variables can solve more synchronization problems that monitors alone. Still only one process can be active within a monitor but many processes may be waiting for a condition variable within a monitor, as shown in the following diagram.

In the next lecture we will discuss a monitor-based solution for the dining philosophers problem.