1 / 12

Operating Systems, 371-1-1631 Winter Semester 2011

Operating Systems, 371-1-1631 Winter Semester 2011. Practical Session 7 Synchronization (C). Monitor. Monitor – a synchronization primitive. A monitor is a collection of procedures, variables and data structures, grouped together.

walt
Download Presentation

Operating Systems, 371-1-1631 Winter Semester 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating Systems, 371-1-1631Winter Semester 2011 Practical Session 7 Synchronization (C)

  2. Monitor • Monitor – a synchronization primitive. • A monitor is a collection of procedures, variables and data structures, grouped together. • Mutual Exclusion – only one process can be active within a monitor at any given time. • Programming language construct!The compiler of the language will know that monitors procedures are different than other procedures, and will treat them differently. That means that the compiler is in charge of the mutual exclusion implementation. Java synchronized methods

  3. A quick recap • Condition variables • A way for processes to block when they can’t continue. • Despite its name, it is used to indicate an event and not as a regular valued variable. A CV is not a counter! • Two operations: wait, signal.Wait: causes the process to block, and allows entry of other threads to the monitor.Signal: last operation within the monitor, which wakes up waiting processes (waiting on the same variable). • Not true for Java.

  4. A quick recap • Message passing • Used on distributed systems (when there is no shared memory). • Uses send(), and receive() system calls. • Introduces a new set of problems, such as acknowledgments, sequencing, addressing, authentication, etc’… • Barriers • Usually used with groups of processes. • When a process reaches the barrier it is blocked until all other processes have reached it as well. • A good example is a multi particle physical experiment.

  5. The Sleeping Barber Write a solution to the sleeping barber problem using monitors and condition variables. Reminder, the sleeping barber: • The barber cuts peoples hair in his shop, which has 2 doors – entrance and exit. When people are in his shop, he gives them a hair cut, one at a time. When none are in his shop, he sleeps on his chair. • When a customer arrives and finds the barber sleeping, he awakens him and sits in the barber’s chair to receive his haircut. After the cut is done, the barber sees the customer out through the exit door. • If the barber is busy when a customer arrives, the customer waits in one of the chairs in the shop. If all are occupied, he goes away. • After serving a customer the barber looks to see if any are waiting and if so proceeds to serve one of them. Otherwise, he sleeps again in his chair.

  6. The Sleeping Barber barbershop: monitor waiting : integer := 0; % customers waiting for haircut customers : condition :=0; % used by barber, wait for a customer barber : condition := 0; % used by customer, wait for barber procedureseek-customer( ) % called by the barber begin if waiting==0 then WAIT (customers); % sleeps if no customers cut-hair(); waiting = waiting-1; % one less customer waiting SIGNAL (barber); % free a waiting customer endseek-customer;

  7. The Sleeping Barber procedureget-haircut( ) % called by a customer begin % is there a free chair to sit and wait? % if no free chairs just go away if waiting < chairs then { waiting = waiting+1; % one more customer waiting SIGNAL (customers) % if the barber is asleep WAIT (barber); % wait for turn with the barber } endget-haircut; end barbershop; % End of monitor

  8. Reader/Writer problem with MP Write a solution to the reader/writer problem using Message Passing. Assume the following: • Three groups of processes: readers, writer, manager. • Multiple readers may access the DB simultaneously. • A writer needs exclusive access to the DB. • Readers have preference.

  9. Reader/Writer problem with MP Reader: while (true){ SEND (manager, start_read); RECEIVE (manager, msg); % wait for confirmationread_db(); SEND (manager, end_read);use_data(); } Writer: while (true){generate_data(); SEND (manager, start_write); RECEIVE (manager, msg); % wait for confirmationwrite_to_db(); SEND (manager, end_write); }

  10. Reader/Writer problem with MP Manager: intreaders_count=0; % number of readers accessing DBboolean writing=false; % writing flagMessagemsg; QueuereadQ, writeQ; % Queues for waiting readers and writerProcessIDsrc; % pidwhile (true){src = RECEIVE(msg);switchmsg.type{case (start_read):if (not writing){ send(src, ok);readers_count++; } elsereadQ.add(src);

  11. Reader/Writer problem with MP case (end_read):readers_count--;if (readers_count==0 && notwriteQ.empty){src=writeQ.remove;SEND (src, ok); writing = true; }case (start_write):if (readers_count==0 && not writing){SEND (src, ok); writing = true; } elsewriteQ.add(src);

  12. Reader/Writer problem with MP case (end_write): writing = false;if (readQ.empty && notwriteQ.empty){src = writeQ.remove; SEND(src, ok); writing = true; } else {while (not readQ.empty){src = readQ.remove; send(src, ok);readers_count++; } } } % switch } % while

More Related