Top Banner
Classical Problem Of Synchronization Submitted To: Namita mam Submitted By:- Shakshi Kanwar Ranaw
30
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Classical problem of synchronization

Classical Problem Of Synchronization

Submitted To: Namita mam

Submitted By:- Shakshi Kanwar Ranawat

Page 2: Classical problem of synchronization

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

Page 3: Classical problem of synchronization

Dining-Philosophers Problem:

Rice

Page 4: Classical problem of synchronization

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).

Page 5: Classical problem of synchronization

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);

Page 6: Classical problem of synchronization

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.

Page 7: Classical problem of synchronization

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.

Page 8: Classical problem of synchronization

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();

Page 9: Classical problem of synchronization

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

}

}

Page 10: Classical problem of synchronization

Cont…

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

Page 11: Classical problem of synchronization

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)

Page 12: Classical problem of synchronization

Application

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

Page 13: Classical problem of synchronization

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.

Page 14: Classical problem of synchronization

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.

Page 15: Classical problem of synchronization

Cont..

Page 16: Classical problem of synchronization

Bounded-Buffer Problem Process

Shared datasemaphore full, empty, mutex;

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

Page 17: Classical problem of synchronization

Producer Process

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

Page 18: Classical problem of synchronization

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);

Page 19: Classical problem of synchronization

Application

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

Page 20: Classical problem of synchronization

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.

Page 21: Classical problem of synchronization

Reader Writer Problem

Page 22: Classical problem of synchronization

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.

Page 23: Classical problem of synchronization

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):

Page 24: Classical problem of synchronization

Reader Writer Problem

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

writing is performed…signal(wrt);

Page 25: Classical problem of synchronization

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):

Page 26: Classical problem of synchronization

A semaphore solution to the readers priority version :

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

Page 27: Classical problem of synchronization

Cont….

process writer {

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

Page 28: Classical problem of synchronization

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.

Page 29: Classical problem of synchronization

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

Page 30: Classical problem of synchronization

Thank You for listening