Presentation is loading. Please wait.

Presentation is loading. Please wait.

Day 13 Concurrency.

Similar presentations


Presentation on theme: "Day 13 Concurrency."— Presentation transcript:

1 Day 13 Concurrency

2 Monitors - Programming language support

3 Monitors Programming language construct that guarantees mutual exclusion A software module that consists of procedures, an initialization routine, and local data. Local data is accessible only to the procedures of the monitor. A process can enter a monitor by invoking one of its procedures. Only one process may be executing in the monitor at any time. Other processes that would like to invoke a monitor procedure will be blocked.

4 Example: Monitor Student { private float age;
public synchronized void setAge(float age){ ..} public synchronized float getAge(){..} } If we make the data fields private, they can be accessed only thru the functions. If we make the functions synchronized, only one process can execute any one function at a time. So, mutual exclusion is guaranteed.

5 Mutual exclusion Mutual exclusion is guaranteed:
only one process allowed in the monitor place the shared data structure in a monitor.

6 Synchronization using monitors
condition variables - contained within the monitor and accessible only within the monitor A queue is associated with each condition variable cwait(c) – suspend execution of process on condition c – Monitor is now available for use by another process. csignal(c) – Resume execution of some process blocked on c. If there are none, then do nothing.

7 Readers/Writers problem
Many readers Many writers Any number of readers can be allowed to read simultaneously While a writer is writing, no others can write. No readers should read either.

8 Monitors – Readers/Writers problem
Monitor ReaderWriter; /* The following can only be accessed by monitor functions. */ int readers = 0; boolean writelock = false; Condition canWrite; Condition canRead; Functions: beginRead(), endRead(), beginWrite(), endWrite()

9 Reader(){ Writer(){ while(true){ beginRead(); Read(); endRead(); }
beginWrite(); Write(); endWrite(); }

10 Monitor procedures for the Reader(Hoare’s model)
void beginRead(){ if(writeLock || queue(canWrite)){ cwait(canRead); } readers++; csignal(canRead); void endRead(){ readers--; if(readers == 0){ csignal(canWrite); }

11 Monitor procedures for the Writers(Hoare’s model)
void beginWrite(){ if(readers > 0 || writeLock){ cwait(canWrite); } writeLock = true; void endWrite(){ writeLock = false; if(queue(canRead) csignal(canRead); else if (queue(canWrite)) csignal(canWrite); }

12 Hoare’s model Hoare’s model requires that a process
signals at the end of the procedure and leaves the monitor. If signal() is not the last operation, then the process making the signal must block and instead of being placed on any of the conditions queues or the entry queue, will be placed in the urgent queue. The signal will wake the first process in the queue for that condition. If no process is waiting, the signal is lost.

13 Monitors

14 Disadvantages of Hoare’s model
If the process that signaled is not done with the monitor, then there is an un-necessary process switch. Process scheduling must be very strictly enforced.

15 Reader 1 is blocked on canRead because writeLock is true.
Writer 1: void endWrite(){ writeLock = false; if(queue(canRead) csignal(canRead); else if (queue(canWrite)) csignal(canWrite); } Reader 1: void beginRead(){ if(writeLock || queue(canWrite)){ cwait(canRead); } readers++; signal(canRead); Writer 2: void beginWrite(){ if(readers > 0 || writeLock){ cwait(canWrite); } writeLock = true; Reader 1 is blocked on canRead because writeLock is true. Writer 1 signals canRead. Reader 1 is un-blocked and is ready, but is not immediately scheduled. Writer 2 is scheduled – sets writeLock to true and is ready to write - IS WRITING when timed out. Reader 1 gets scheduled to run – Will increment “readers”, signals to any waiting readers and then PROCEED TO READ.

16 Lampson and Redell suggested a modification
cnotify() instead of csignal() - Process will notify the first process waiting on “c”. But, the process waiting need not be scheduled immediately and the notifying process need not quit or block. However, the code must change, such that any process that has to continue will check the condition again.

17 Monitor procedures for the Reader(Lampson and Redell’s model)
void beginRead(){ while(writeLock || queue(canWrite)){ cwait(canRead); } readers++; cnotify(canRead); void endRead(){ readers--; if(readers == 0){ cnotify(canWrite); }

18 Monitor procedures for the Writers(Hoare’s model)
void beginWrite(){ while(readers > 0 || writeLock){ cwait(canWrite); } writeLock = true; void endWrite(){ writeLock = false; if(queue(canRead) cnotify(canRead); else if (queue(canWrite)) cnotify(canWrite); }

19 cbroadcast() cbroadcast(c) – Wake all processes that are waiting on a condition. For example, if n processes are waiting on space in memory. If “j” bytes were available and now “k” more bytes become free. The memory manager does not know which processes waiting can run with “k + j” bytes. Using cbroadcast(), it can wake all the processes that are waiting and the process that can run with “k + j” bytes will run.

20 How do we prevent more than one process from being in the monitor at any given time?
Every method in the monitor becomes a critical section and any of the methods we have seen earlier can be used to ensure that while a process is in a CS, no other process can be allowed to enter any of the other functions.


Download ppt "Day 13 Concurrency."

Similar presentations


Ads by Google