p rocess s ynchronization
play

[P ROCESS S YNCHRONIZATION ] Shrideep Pallickara Computer Science - PDF document

CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University CS 370: O PERATING S YSTEMS [P ROCESS S YNCHRONIZATION ] Shrideep Pallickara Computer Science Colorado State University CS370: Operating Systems [Fall


  1. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University CS 370: O PERATING S YSTEMS [P ROCESS S YNCHRONIZATION ] Shrideep Pallickara Computer Science Colorado State University CS370: Operating Systems [Fall 2018] September 25, 2018 L11.1 Dept. Of Computer Science , Colorado State University Frequently asked questions from the previous class survey ¨ What is the difference between a semaphore and a mutex? ¤ Mutex: locking mechanism, semaphore: signaling mechanism ¨ What is preemption? ¨ Remainder section? CS370: Operating Systems [Fall 2018] L11. 2 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University L11.1 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  2. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University Topics covered in the lecture ¨ Classical process synchronization problems ¤ Producer-Consumer problem ¤ Readers Writers ¤ Dining philosopher’s problem ¨ Monitors ¤ Solving dining philosopher's problem using monitors ¨ Midterm CS370: Operating Systems [Fall 2018] L11. 3 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University C LASSIC P ROBLEMS OF S YNCHRONIZATION CS370: Operating Systems [Fall 2018] September 25, 2018 L11.4 Dept. Of Computer Science , Colorado State University L11.2 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  3. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University The bounded buffer problem ¨ Binary semaphore ( mutex ) ¤ Provides mutual exclusion for accesses to buffer pool ¤ Initialized to 1 ¨ Counting semaphores ¤ empty : Number of empty slots available to produce n Initialized to n ¤ full : Number of filled slots available to consume n Initialized to 0 CS370: Operating Systems [Fall 2018] L11. 5 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University Some other things to bear in mind ¨ Producer and consumer must be ready before they attempt to enter critical section ¨ Producer readiness? ¤ When a slot is available to add produced item n wait(empty) : empty is initialized to n ¨ Consumer readiness? ¤ When a producer has added new item to the buffer n wait(full) : full initialized to 0 CS370: Operating Systems [Fall 2018] L11. 6 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University L11.3 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  4. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University The Producer wait till slot available do { produce item nextp wait( empty ); Only producer OR consumer wait( mutex ); can be in critical section add nextp to buffer Allow producer OR consumer signal( mutex ); to (re)enter critical section signal( full ); remainder section signal consumer that a slot is available } while (TRUE); CS370: Operating Systems [Fall 2018] L11. 7 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University The Consumer wait till slot available do { for consumption wait( full ); Only producer OR consumer wait( mutex ); can be in critical section remove item from buffer (nextc) Allow producer OR consumer to (re)enter critical section signal( mutex ); signal( empty ); consume nextc signal producer that a slot is available to add } while (TRUE); CS370: Operating Systems [Fall 2018] L11. 8 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University L11.4 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  5. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University T HE R EADERS -W RITERS P ROBLEM CS370: Operating Systems [Fall 2018] September 25, 2018 L11.9 Dept. Of Computer Science , Colorado State University The Readers-Writers problem ¨ A database is shared among several concurrent processes ¨ Two types of processes ¤ Readers ¤ Writers CS370: Operating Systems [Fall 2018] L11. 10 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University L11.5 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  6. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University Readers-Writers: Potential for adverse effects ¨ If two readers access shared data simultaneously? ¤ No problems ¨ If a writer and some other reader (or writer) access shared data simultaneously? ¤ Chaos CS370: Operating Systems [Fall 2018] L11. 11 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University Writers must have exclusive access to shared database while writing ¨ FIRST readers-writers problem: ¤ No reader should wait for other readers to finish; simply because a writer is waiting n Writers may starve ¨ SECOND readers-writers problem: ¤ If a writer is ready it performs its write ASAP n Readers may starve CS370: Operating Systems [Fall 2018] L11. 12 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University L11.6 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  7. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University Solution to the FIRST readers-writers problem ¨ Variable int readcount ¤ Tracks how many readers are reading object ¨ Semaphore mutex {1} ¤ Ensure mutual exclusion when readcount is accessed ¨ Semaphore wrt {1} ① Mutual exclusion for the writers ② First (last) reader that enters (exits) critical section n Not used by readers, when other readers are in their critical section CS370: Operating Systems [Fall 2018] L11. 13 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University The Writer: When a writer signals either a waiting writer or the readers resume do { When: writer in critical section wait( wrt ); and if n readers waiting 1 reader is queued on wrt writing is performed (n-1) readers queued on mutex signal( wrt ); } while (TRUE); CS370: Operating Systems [Fall 2018] L11. 14 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University L11.7 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  8. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University The Reader process mutex for mutual wait( mutex ); do { exclusion to readcount readcount++; if (readcount ==1) { wait( wrt ); When: } writer in critical section signal( mutex ); and if n readers waiting reading is performed 1 is queued on wrt wait( mutex ); (n-1) queued on mutex readcount--; if (readcount ==0) { signal( wrt ); } signal( mutex ); } while (TRUE); CS370: Operating Systems [Fall 2018] L11. 15 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University T HE D INING P HILOSOPHERS P ROBLEM CS370: Operating Systems [Fall 2018] September 25, 2018 L11.16 Dept. Of Computer Science , Colorado State University L11.8 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  9. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University The situation CS370: Operating Systems [Fall 2018] L11. 17 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University The Problem ① Philosopher tries to pick up two closest { LR } chopsticks ② Pick up only 1 chopstick at a time Cannot pick up a chopstick being used ¤ ③ Eat only when you have both chopsticks ④ When done; put down both the chopsticks CS370: Operating Systems [Fall 2018] L11. 18 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University L11.9 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  10. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University Why is the problem important? ¨ Represents allocation of several resources ¤ AMONG several processes ¨ Can this be done so that it is: ¤ Deadlock free ¤ Starvation free CS370: Operating Systems [Fall 2018] L11. 19 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University Dining philosophers: Simple solution ¨ Each chopstick is a semaphore ¤ Grab by executing wait() ¤ Release by executing signal() ¨ Shared data ¤ semaphore chopstick[5] ; ¤ All elements are initialized to 1 CS370: Operating Systems [Fall 2018] L11. 20 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University L11.10 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

  11. CS370: Operating Systems [Fall 2018] Dept. Of Computer Science , Colorado State University What if all philosophers get hungry and grab the same {L/R} chopstick? do { Deadlock: If all processes wait( chopstick[i] ); access chopstick with wait( chopstick[(i+1)%5]) ; same hand //eat signal( chopstick[i] ); signal( chopstick[(i+1)%5]) ; //think We will look at solution with monitors } while (TRUE); CS370: Operating Systems [Fall 2018] L11. 21 September 25, 2018 Professor: S HRIDEEP P ALLICKARA Dept. Of Computer Science , Colorado State University M ONITORS CS370: Operating Systems [Fall 2018] September 25, 2018 L11.22 Dept. Of Computer Science , Colorado State University L11.11 S LIDES C REATED B Y : S HRIDEEP P ALLICKARA

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend