Transcript

Classical Problem Of Synchronization

Submitted To: Namita mam

Submitted By:- Shakshi Kanwar Ranawat

Contents Of Discussion The Dining-Philosophers Problem The Reader Writers Problem The Bounded Buffer Problem

Dining-Philosophers Problem:

Rice

Dining-Philosophers Problem

• Table, a bowl of rice in center, five chairs, and fivechopsticks, also in each chair there is a philosopher.• A philosopher may pick up only one chopstick at atime.• When a hungry philosopher has both her chopsticksat the same time, she eats without releasing herchopsticks.• When she is finished eating, she puts down both ofher chopsticks and starts thinking.• From time to time, a philosopher gets hungry and tries

to pick up the two chopsticks that are closest to her (chopsticks between her left and right neighbors).

The Structure of philosopher ido{

wait(chopstick[i]);wait(chopstick[(i+1)%5]);….//eat…..signal(chopstick[i]);signal(chopstick[(i+1)%5]);…..//think…..

}while(TRUE);

Dining-Philosophers Problem (Cont.)

Solution: Present each chopstick by a semaphore. A philosopher tries to grab the chopstick by executing a wait operation on that semaphore. She releases her chopsticks by executing the signal operation on the appropriate semaphores.This solution guarantees that no two neighbors are eating simultaneously, but it could cause a deadlock.Suppose all five philosophers become hungry simultaneously, and each grabs her left chopstick.When each philosopher tries to grab her right chopstick, she will cause the possibility that the philosophers will starve to death.

Dining-Philosophers Problem (Cont.)

We present a solution to the dining-philosophers problem that ensure freedom from deadlock.– Allow at most 4 philosophers to be sitting simultaneously at the table.– Allow a philosopher to pick up her chopstick only if both chopsticks are available.– An odd philosopher picks up first her left chopstick and then her right chopstick, whereas, an even philosopher picks up her right chopstick first and then her left chopstick.• Note: A deadlock-free solution does not necessarilyeliminate the possibility of starvation.

A semaphore solution:// represent each chopstick with a semaphore

Semaphore chopstick[] = new Semaphore[5]; // all = 1 initially

process philosopher_i {

while (true) {

// pick up left chopstick

chopstick[i].acquire();

Cont… // pick up right chopstick

chopstick[(i+1) % 5].acquire();

// eat

// put down left chopstick

chopstick[i].release();

// put down right chopstick

chopstick[(i+1) % 5].release();

// think

}

}

Cont…

This solution guarantees no two neighboring philosophers eat simultaneously, but has the possibility of creating a deadlock

Uses of semaphores protecting acccess to a critical section (e.g., db in the R/W

problem)

as a mutex lock (e.g., to protect the shared variables used in solving a probem such as readerCount above)

to protect a relationship (e.g., empty and full as used in the P/C problem)

to support atomicity (e.g., you pickup either both chopsticks as the same time or neither, you cannot pickup just one)

Application

The dining philosophers problem represents a situation that can occur in large communities of processes that share a sizeable pool of resources.

Bounded Buffer Problem also called the Producers and Consumers problem. A finite supply of containers is available. Producers

take an empty container and fill it with a product. Consumers take a full container, consume the product and leave an empty container.

The main complexity of this problem is that we must maintain the count for both the number of empty and full containers that are available.

Producers produce a product and consumers consume the product, but both use of one of the containers each time.

The Bounded Buffer ProblemConsider:a buffer which can store n itemsa producer process which creates the items (1 at a time)a consumer process which processes them (1 at a time)A producer cannot produce unless there is an empty buffer slot to fill.A consumer cannot consume unless there is at least one produced item.

Cont..

Bounded-Buffer Problem Process

Shared datasemaphore full, empty, mutex;

Initialisation:full = 0, empty = n, mutex = 1

Producer Process

do {…produce an item in nextp…wait(empty);wait(mutex);…add nextp to buffer…signal(mutex);signal(full);} while (1);

Consumer Process

do {wait(full)wait(mutex);…remove an item from buffer to nextc…signal(mutex);signal(empty);…consume the item in nextc…} while (1);

Application

A pipe or other finite queue (buffer), is an example of the bounded buffer problem.

Reader Writer Problem

If one notebook exists where writers may write information to, only one writer may write at a time. Confusion may arise if a reader is trying read at the same as a writer is writing. Since readers only look at the data, but do not modify the data, we can allow more than one reader to read at the same time.

The main complexity with this problems stems from allowing more than one reader to access the data at the same time.

Reader Writer Problem

The Readers-Writers Problem

A data item such as a file is shared among several processes.

Each process is classified as either a reader or writer.

Multiple readers may access the file simultaneously.

A writer must have exclusive access (i.e., cannot share with either a reader or another writer).

A solution gives priority to either readers or writers.

Cont…. readers' priority: no reader is kept waiting unless a

writer has already obtained permission to access the database

writers' priority: if a writer is waiting to access the database, no new readers can start reading

A solution to either version may cause starvation in the readers' priority version, writers may starve in the writers' priority version, readers may starve A semaphore solution to the readers' priority

version (without addressing starvation):

Reader Writer Problem

Shared datasemaphore mutex, wrt;Initiallymutex = 1, wrt = 1, readcount = 0wait(wrt);…

writing is performed…signal(wrt);

Readers-Writers Problem Reader Process

wait(mutex);readcount++;if (readcount == 1)wait(rt);signal(mutex);…reading is performed…wait(mutex);readcount--;if (readcount == 0)signal(wrt);signal(mutex):

A semaphore solution to the readers priority version :

Semaphore mutex = 1;Semaphore db = 1;int readerCount = 0;

Cont….

process writer {

db.acquire(); // write db.release();}

Conti…

process reader { // protecting readerCount mutex.acquire(); ++readerCount; if (readerCount == 1) db.acquire(); mutex.release(); // read // protecting readerCount mutex.acquire(); --readerCount; if (readerCount == 0) db.release; mutex.release();}readerCount is a <cs> over which we must maintain control and we use mutex to do so.

ApplicationA message distribution system is an example of Readers and Writers. We must keep track of how many messages are in the queue

Thank You for listening

top related