Concurrent Programming (RIO) 20.1.2012 Lesson 6 Synchronization with HW support • Disable interrupts – Good for short time wait, not good for long time wait – Not good for multiprocessors Semaphores • Interrupts are disabled only in the processor used • Test-and-set instruction (etc) Ch 6 [BenA 06] – Good for short time wait, not good for long time wait – Nor so good in single processor system • May reserve CPU, which is needed by the process holding the lock – Waiting is usually “busy wait” in a loop Semaphores • Good for mutex, not so good for general synchronization – E.g., “wait until process P34 has reached point X” Producer-Consumer Problem – No support for long time wait (in suspended state) Semaphores in C--, Java, • Barrier wait in HW in some multicore architectures – Stop execution until all cores reached barrier_waitinstruction Linux, Minix – No busy wait, because execution pipeline just stops – Not to be confused with barrier_wait thread operation 20.1.2012 Copyright Teemu Kerola 2012 1 20.1.2012 Copyright Teemu Kerola 2012 2 Semaphores (Basic) Semaphore semaphore S public create initial value integer value semafori public P(S) S.value private S.V Edsger W. Dijkstra V(S) http://en.wikipedia.org/wiki/THE_operating_system public private S.list S.L • Dijkstra, 1965, THE operating system queue of waiting processes • Protected variable, abstract data type (object) • P(S) WAIT(S), Down(S) – Allows for concurrency solutions if used properly – If value > 0, deduct 1 and proceed • Atomic operations – o/w, wait suspended in list (queue?) until released – Create (SemaName, InitValue) – P, down, wait, take, pend, • V(S) SIGNAL(S), Up(S) passeren, proberen, try, prolaad, try to decrease – If someone in queue, release one (first?) of them – V, up, signal, release, post, – o/w, increase value by one vrijgeven, verlagen, verhoog, increase 20.1.2012 Copyright Teemu Kerola 2012 3 20.1.2012 Copyright Teemu Kerola 2012 4 General vs. Binary Semaphores N • General Semaphore – Value range: 0, 1, 2, 3, …. • nr processes doing P(S) and advancing without delay • Value: “Nr of free units”, “nr of advance permissions” • Binary semaphore (or “mutex”) – Value range: 0, 1 • Someone (and just one!) must create S • Mutex lock (with suspended wait) – Value initialized to 1 (in this example) • Usually initial value 1 • V(S) can (should!) be called only when value = 0 • Possible wait in suspended state – By process in critical section (CS) – Long time, hopefully at least 2 process switches – Many processes can be in suspended in list Some (operating) systems have “semaphores” with (optional) – At most one process can proceed at a time busy wait (i.e., busy-wait semaphore). Beware of busy-wait locks hidden in such semaphores! 20.1.2012 Copyright Teemu Kerola 2012 5 20.1.2012 Copyright Teemu Kerola 2012 6 Lecture 6: Semaphores 1 Concurrent Programming (RIO) 20.1.2012 General Semaphore Implementation Semaphore Implementation • P(S) if (S.value > 0) Atomic S.value = S.value - 1 operations! • Use HW-supported busy-wait locks to solve else How? mutex-problem for semaphore operations suspend calling process P Use HW mutex support! place P (last?) in S.list – Short waiting times, a few machine instructions go to sleep … … wake up here call scheduler() Tricky part: • Use OS suspend operation to solve section of CS is in operating semaphore synchronization problem system – Possibly very long, unlimited waiting times • V(S) if (S.list == empty) S.value = S.value + 1 scheduler? – Implementation at process control level in OS else – Process waits in suspended waiting state take arbitrary (or 1st ?) process Q from S.list – This is the resume point for suspended process move Q to ready-to-run list • Deep inside in privileged OS-module call scheduler() 20.1.2012 Copyright Teemu Kerola 2012 7 20.1.2012 Copyright Teemu Kerola 2012 8 Semaphore Implementation Variants Semaphore Implementation Variants • Take first process in S.list in V(S)? • S.value can be negative – Important semantic change, affects applications – P(S) always deducts 1 from S.value – Fairness – Negative S.value gives the number of waiting – Strong semaphore processes? (vs. weak semaphore with no order in S.list) – Makes it easier to poll number of waiting processes • Add to/subtract from S.value first in P(S) and in V(S)? • New user interface to semaphore object? n = value(s); – Just another way to write code • Scheduler call every time or sometimes at P or V end? • Busy-wait semaphore – Semantic change, may affect applications – Wait in busy loop instead of in suspended state – Execution turn may (likely) change with P or V even – Really a busy-wait lock that looks like a semaphore when calling process is not suspended in wait – Signalled process may start execution immediately – Important semantic change, affects applications 20.1.2012 Copyright Teemu Kerola 2012 9 20.1.2012 Copyright Teemu Kerola 2012 10 Blocking Semaphore Producer-Consumer Problem • “Blocking” • Synchronization problem Tuottaja-kuluttaja Note: • Correct execution order -ongelma – Normal (counting) semaphore with initial value = 0 • Producer places data in buffer – First P(S) will block, unless V(S) was executed first This is not a critical – Waits, if finite size buffer full Producer • Example: synchronization between two processes section (CS) problem, but • Consumer takes data from buffer Q R Create( S, 0) CS’s may be – Same order as they were produced …. used in the – Waits, if no data available 4 7 …. Signal R R Q solution. Wait for Q • Variants …. …. …. Requirements …. (no wait) P(S) – Cyclic finite buffer – usual case Consumer time V(S) are too …. …. …. complex for – Infinite buffer Wait for Q …. plain CS. • Realistic sometimes – producer can not wait …. (wait) Signal R Will block if – External conditions rule out buffer overflow? …. executed first – Can be implemented with finite buffer! – Many producers and/or many consumers 20.1.2012 Copyright Teemu Kerola 2012 11 20.1.2012 Copyright Teemu Kerola 2012 12 Lecture 6: Semaphores 2 Concurrent Programming (RIO) 20.1.2012 (no waiting ever) • Synchronization only one way (producer never waits) – Synchronization from producer to consumer • Synchronization both ways, both can wait • Counting semaphore notEmpty • New semaphore notFull: value = nr of free slots in buffer – Value = nr of data items in buffer • Append/take might need to be indivisible operations • Split semaphore notEmpty & notFull – Protect with semaphores or busy-wait locks? – notEmpty.value + notFull.value = N in (p1, q4, …) – Not needed now? Maybe not? (only one producer/consumer) • When both at the beginning of loop, outside wait-signal area Discuss – wait(notFull)…signal(notEmpty), wait(notEmpty)…signal(notFull) 20.1.2012 Copyright Teemu Kerola 2012 13 20.1.2012 Copyright Teemu Kerola 2012 14 Prod/Consumers Size N buffer 0 Size N buffer One producer Many producers front rear Many consumers One consumer Need mutexes! Semaphores or busy wait? 0 Semaphore full for synchronization Does it work with Semaphore mutexF for mutex problem Does it work with one producer many producers or Why separate mutexD and mutexF? and one consumer? Yes. consumers? No. Mutex problem? No. Why not? (Andrews, Fig. 4.5) 20.1.2012 Copyright Teemu Kerola 2012 15 20.1.2012 Copyright Teemu Kerola 2012 16 Barz’s General mutex Udding’s No-Starvation Critical Section with Semaphore nr of permissions Weak Split Binary Simulation Semaphores • Starting point • Weak semaphore – Set, not a queue in wait – Have binary semaphore (typo in P • Split binary semaphore book) – Need counting semaphore 0 gate1+ gate2 1 someone – Realistic situation • Batch arrivals in p4? • Operating system or – Start service only when last in batch k = 4 programming language no more arrivals 4 in CS, 2 in gate library may have only – Close gate1 during service 1 completes CS binary semaphores • No starvation others What now? V in “batch” critical section to – gate1 opened again only after whole batch in gate2 2 complete CS? implement V is serviced last in batch 20.1.2012 Copyright Teemu Kerola 2012 17 20.1.2012 Copyright Teemu Kerola 2012 (Alg 6.14) Discuss 18 Lecture 6: Semaphores 3 Concurrent Programming (RIO) 20.1.2012 (Fig. 6.12 [Stal06]) /* mutex, one at a time */ Semaphore Features (Alg. 6.10 [BenA06]) • Utility provided by operating system or programming language library Trivial • Can be used solve almost any synchronization /* left fork */ problem /* right fork */ Solution • Need to be used carefully #1 – Easy to make profound errors • Forget V • Suspend process in critical section (with P) – No one can get CS to resume suspended process – Someone may be waiting in busy-wait loop • Possible deadlock – not good • Deadlock – All 5 grab left fork “at the same time” – Need strong coding discipline 20.1.2012 Copyright Teemu Kerola 2012 19 20.1.2012 Copyright Teemu Kerola 2012 20 (Fig. 6.13 [Stal06]) AS (good solution) /* only 4 at a time, 5th waits */ (Alg. 6.11 [BenA06]) Symmetric solutions? Even numbered Trivial philosophers? or Solution This way with 50% chance? #2 or • No deadlock, no starvation This way with 20% chance? • No extra blocking • Asymmetric solution – not so nice… Etc. etc. – All processes should execute the same code • No deadlock, no starvation • Waiting when resources are available – which scenario? – not good • Simple primitives, must be used properly 20.1.2012 Copyright Teemu Kerola 2012 21 20.1.2012 Copyright Teemu Kerola 2012 22 Minix Semaphore Minix Semaphore P Suspend in message queue! http://www.usenix.org/publications/login/2006-04/openpdfs/herder.pdf
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-