11.07.2015 Views

Tutorial # 2 - Dr. Aiman Hanna

Tutorial # 2 - Dr. Aiman Hanna

Tutorial # 2 - Dr. Aiman Hanna

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

COMP346/5461 - Operating SystemsRevision 1.3Feb 3rd, 201010/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 1


Topics• Synchronization in Details• Semaphores• Introducing Semaphore.java10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 2


Synchronization• What is it?• An act of communication between unrelatedprocesses to sync their activities to achieve somegoals and solve some common problems ofmultiprogramming:• Mutual exclusion and critical sections• Specific execution order must be maintained• Improper sync may cause a very big problem in allOperating Systems – a deadlock.10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 3


Synchronization• Improper synchronization leads to• a corruption to shared file / object / data.• Deadlock , one process holds a lock for long long timewhile other process is waiting for that lock.• Proper synchronization helps to avoid race conditions.• Our goal is to• Synchronize between process and• Maximize concurrency.• Make the CPU busy doing useful tasks most of the time.10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 4


A Typical Example• Shared account (between spouses John and Jane)• Current account balance: $400• John and Jane happened to be at the ATM indifferent places, but at almost the same time.• Let say John wants to withdraw $200, and Janewants to withdraw $300.• First scenario: both of them see the currentbalance of $400 and relatively at the same timeperform request to withdraw. John goes first…10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 5


A Typical Example (2)• A DB system takes $400, subtracts $200, and getsinterrupted (e.g. network congestion), thus theremaining balance of $200 wasn’t recorded yet.• Jane’s request goes through and the system writesdown $100 balance remaining.• Then John’s request finally goes through, and systemupdates the balance to $200.• The bank is a victim in that case because John and Janewere able to withdraw $500 and have $200 remaining,when initially account’s balance was $400!10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 6


A Typical Example (4)class John extends Thread {}run() {}balance = ATM.getBalance();if(balance >= $200)ATM.withdraw($200);class ATM{ …int withdraw(amount){if(amount = $300)ATM.withdraw($300);A trouble may occur atthe points marked withthe red arrows. The codeMUST NOT be interruptedat those places.10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 7


Solution: Use Semaphores• Obvious: make the critical section part atomic.• One way of doing it: Semaphores• Semaphores are system-wide OS objects (alsoresources) used to• Protect critical section (mutexes – for MutualExclusion),• Coordinate other process’ activities.• Semaphores are NOT shared memory segments!But they both are often used together.10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 8


Semaphores for CS• There are two main operations on semaphores –Wait() and Signal().• A process wishing to enter the critical section tries toacquire the semaphore (a lock in a human world) bycalling Wait(sem), sem.Wait() (a.k.a. P()).• If the lock isn’t there (i.e. “in use”), the execution of the process callingWait() is suspended (put asleep).• Otherwise, it acquires the semaphore and does the CS stuff.• When a process is over with the CS, it notifies the restof the processes waiting on the same semaphore thatthey can go in by calling Signal(sem),Sem.signal() (a.k.a. V()).• The awakened process goes back to the ready queueto compete again to enter the CS.10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 9


A Typical Example Solutionclass John extends Thread {}run() {}mutex.Wait();balance = ATM.getBalance();if(balance >= $200)ATM.withdraw($200);mutex.Signal();class ATM{ …Semaphore mutex = 1;int withdraw(amount) {if(amount = $300)ATM.withdraw($300);mutex.Signal();A trouble may occur atthe points marked withred. The code MUST NOT beinterrupted at those places.10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 10


Semaphores for Barrier Sync• Take a look at the typical problem:semaphore s1 = 0, s2 = 0;process P1process P2 V (s1)V (s2)P (s2)P (s1)• all processes must finish their phase I before any ofthem starts phase II• processes must proceed to their phase II in a specificorder, for example: 1, 2, 3…• This is called barrier synchronization.10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 11


Barrier Sync for Three Processes• A possible copy-cat attempt:semaphore s1 = 0, s2 = 0, s3 = 0;process P1 process P2 process P3 V (s1) V (s2) V (s3)P (s3) P (s1) P (s2) • Why it doesn’t work?10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 12


Barrier Sync for Three Processes• A possible copy-cat attempt:semaphore s1 = 0, s2 = 0, s3 = 0;process P1 process P2 process P33 1 4 V (s1) V (s2) 2 V (s3)5 P (s3) P (s1) P (s2)6 • Why it doesn’t work?• Scenario: 1-6 , process 2 even hasn’t started!• None of the requirements are met10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 13


Barrier Sync for Three Processes (2)• Another attempt:semaphore s1 = 0, s2 = 0, s3 = 0;process P1 process P2 process P3 P (s1) V (s1) V (s1)P (s1) P (s2) P (s3)V (s2)V (s3) • What’s wrong now?10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 14


Barrier Sync for Three Processes (2)• Another attempt:semaphore s1 = 0, s2 = 0, s3 = 0;process P1 process P2 process P37 4 1 8 P (s1) 5 V (s1) 2 V (s1)9 P (s1) 6 P (s2) 3 P (s3)10 V (s2) V (s3) • What’s wrong now?• Scenario: 1-10, so far so good, but after…• The second requirement isn’t met10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 15


Barrier Sync for Three Processes (3)• Last attempt:semaphore s1 = 0, s2 = 0, s3 = 0;process P1 process P2 process P3 P (s1) V (s1) V (s1)P (s1) P (s2) P (s3) V (s2)V (s3)• A bit “optimized”:semaphore s1 = -1, s2 = 0, s3 = 0;process P1 process P2 process P3 V (s1)V (s1)P (s1) P (s2) P (s3) V (s2)V (s3)10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 16


Barrier Sync: Need for the General Solution• Problem with the proposed solution: # of semaphores== # of processes.• Semaphores as any other resource are limited and takespace => overhead• Imagine you need to sync 10 processes in the samemanner? 100? 1000?• Complete mess and a high possibility of a deadlock!10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 17


Barrier Sync: Need for the General Solution (2)• Attempt for the first requirement:semaphore s1 = -n + 2, s2 = 0;process P1 process P2 ... process Pn ... P (s1) V (s1) ... V (s1)V (s2) P (s2) ... P (s2)V (s2) ... V (s2) ... • The second requirement is left as an exercise to thecurious student :-)10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 18


Introducing the Semaphore Class• NOTE: Operations Signal and Wait areguaranteed to be atomic!class Semaphore{private int value;public Semaphore(int value){this.value = value;}public Semaphore(){this(0);}...10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 19


Introducing the Semaphore Class (2)...public synchronized void Wait(){while(this.value


Introducing the Semaphore Class (3)}...public synchronized void Signal(){++this.value;notify();}public synchronized void P(){this.Wait();}public synchronized void V(){this.Signal();}10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 21


References• http://users.encs.concordia.ca/~mokhov/comp346/10/27/2012 Serguei A. Mokhov, mokhov@cs.concordia.ca 22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!