120-l6 - HW 2 Due Tuesday 10/18 Lecture 6 Semaphores and Monitors CSE 120 Principles of Operating Systems Alex C Snoeren Higher-Level

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 6: Lecture 6: Semaphores and Monitors Semaphores and Monitors CSE 120: Principles of Operating Systems Alex C. Snoeren HW 2 Due Tuesday 10/18
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 120 – Lecture 6 2 Higher-Level Synchronization Higher-Level Synchronization We looked at using locks to provide mutual exclusion Locks work, but they have some drawbacks when critical sections are long Spinlocks – inefficient Disabling interrupts – can miss or delay important events Instead, we want synchronization mechanisms that Block waiters Leave interrupts enabled inside the critical section Look at two common high-level mechanisms Semaphores : binary (mutex) and counting Monitors : mutexes and condition variables Use them to solve common synchronization problems
Background image of page 2
CSE 120 – Lecture 6 3 Semaphores Semaphores Semaphores are another data structure that provides mutual exclusion to critical sections Block waiters, interrupts enabled within CS Described by Dijkstra in THE system in 1968 Semaphores can also be used as atomic counters More later Semaphores support two operations: wait(semaphore): decrement, block until semaphore is open » Also P(), after the Dutch word for test, or down() signal(semaphore): increment, allow another thread to enter » Also V() after the Dutch word for increment, or up()
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 120 – Lecture 6 4 Blocking in Semaphores Blocking in Semaphores Associated with each semaphore is a queue of waiting processes When wait() is called by a thread: If semaphore is open, thread continues If semaphore is closed, thread blocks on queue Then signal() opens the semaphore: If a thread is waiting on the queue, the thread is unblocked If no threads are waiting on the queue, the signal is remembered for the next thread » In other words, signal() has “history” (c.f. condition vars later) » This “history” is a counter
Background image of page 4
CSE 120 – Lecture 6 5 Semaphore Types Semaphore Types Semaphores come in two types Mutex semaphore Represents single access to a resource Guarantees mutual exclusion to a critical section Counting semaphore Represents a resource with many units available, or a resource that allows certain kinds of unsynchronized concurrent access (e.g., reading) Multiple threads can pass the semaphore Number of threads determined by the semaphore “count” » mutex has count = 1, counting has count = N
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 120 – Lecture 6 6 Using Semaphores Using Semaphores Use is similar to our locks, but semantics are different struct Semaphore { int value; Queue q; } S; withdraw (account, amount) { wait(S); balance = get_balance(account); balance = balance – amount; put_balance(account, balance); signal(S); return balance; } wait(S); balance = get_balance(account); balance = balance – amount; wait(S); put_balance(account, balance); signal(S); wait(S); signal(S); signal(S); Threads block It is undefined which thread runs after a signal
Background image of page 6
CSE 120 – Lecture 6 7 Semaphores in Nachos Semaphores in Nachos thread_sleep() assumes interrupts are disabled
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/18/2012 for the course INFORMATIK 2011 taught by Professor Phanthuongcang during the Winter '11 term at Cornell University (Engineering School).

Page1 / 30

120-l6 - HW 2 Due Tuesday 10/18 Lecture 6 Semaphores and Monitors CSE 120 Principles of Operating Systems Alex C Snoeren Higher-Level

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online