Quick viewing(Text Mode)

Semaphores Semaphores (Basic) Semaphore General Vs. Binary

Semaphores Semaphores (Basic) Semaphore General Vs. Binary

Concurrent Programming (RIO) 20.1.2012

Lesson 6 Synchronization with HW support • Disable – 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- 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 holding the – 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 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 queue of waiting processes • Protected variable, abstract (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, , 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 (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 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 – 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 !

http://www.usenix.org/publications/login/2006-04/openpdfs/herder.pdf 20.1.2012 Copyright Teemu Kerola 2012 23 20.1.2012 Copyright Teemu Kerola 2012 24

Lecture 6: Semaphores 4 Concurrent Programming (RIO) 20.1.2012

Minix Semaphore V Semaphores in Linux http://fxr.watson.org/fxr/source/include/asm-sh/semaphore.h?v=linux-2.4.22 Mutex? • semaphore.h • Low level process/thread control • In assembly language, in OS kernel • struct semaphore { atomic_t count; int sleepers; wait_queue_head_t wait; } • sema_init(s, val) • init_MUTEX(s), init_MUTEX_LOCKED(s) • down(s), int down_interruptible(s), int down_trylock(s) • up(s)

20.1.2012 Copyright Teemu Kerola 2012 25 20.1.2012 Copyright Teemu Kerola 2012 26

Semaphores in BACI with C-- • Weak semaphore C-- – S.list is a set, not a queue Semaphore Example – Awakened process chosen in random semexample.cm • Counting semaphore: semaphore count; • Binary semaphore: binarysem mutex; • Operations – Initialize (count, 0); – P() and V() – Also wait() and signal() in addition to P() and V() – Value can be used directly: n = count; cout << count;

current value of semaphore count (BACI C- - User’s Guide)

20.1.2012 Copyright Teemu Kerola 2012 27 20.1.2012 Copyright Teemu Kerola 2012 28

C- - Semaphore Example Semaphores in Java • Class Semaphore in package java.util.concurrent • 3 possible outcomes http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Semaphore.html • S.value is S.permits in Java – how? – Permit value can be positive and negative • Permits can be initialized to negative numbers • Semaphore type – how? – fair (= strong) & nonfair (§ busy-wait ??), default) • Wait(S):

– how?

– Why no other possible outcome? • Signal(S): (BACI C- - User’s Guide) • Many other features 20.1.2012 Copyright Teemu Kerola 2012 29 20.1.2012 Copyright Teemu Kerola 2012 30

Lecture 6: Semaphores 5 Concurrent Programming (RIO) 20.1.2012

Semaphore Summary Java Example • Most important high level synchr. primitive • Simple Java-solution with semaphore – Implementation needs OS assistance – Wait in suspended state vera: javac Plusminus_sem.java – Should wait relatively long time vera: java Plusminus_sem • Costs 2 process switches (wait – resume) http://www.cs.helsinki.fi/u/kerola/rio/Java/examples/Plusminus_sem.java • Can do anything • Still fairly complex – Just like assembly language coding… – Not as streamlined as P() and V() • Many variants • How does it really work? – Counting, binary, split, blocking, neg. values, mutex – Busy wait or suspended wait? • Programming language interfaces vary – Fair queueing? • No need for areas – Overhead when no competition for CS? – Enough to invoke semaphore operations in OS or programming language libraries 20.1.2012 Copyright Teemu Kerola 2012 31 20.1.2012 Copyright Teemu Kerola 2012 32

Summary

• Semaphore structure, implementation, and use – “Busy wait semaphores” • Producer-Consumer problem and its variants – Semaphores for synchronization and for mutex • Emulate advanced semaphores with simpler ones – Barz, Udding • Semaphores in Linux (C), C--, Java

20.1.2012 Copyright Teemu Kerola 2012 33

Lecture 6: Semaphores 6