lec6_sem

lec6_sem - Review Critical Section CSE120 Principles of...

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

View Full Document Right Arrow Icon
1 CSE120 Principles of Operating Systems Prof Yuanyuan (YY) Zhou Lecture 6 Apr 13 , 2011 4/12/2011 CSE 120 – Lecture 5 – Synchronization 2 Review: Critical Section Process { while (true) { ENTER CRITICAL SECTION Access shared variables; // Critical Section; LEAVE CRITICAL SECTION Do other work } } 4/12/2011 CSE 120 – Lecture 5 – Synchronization 3 Using Locks What happens when blue tries to acquire the lock? Why is the “return” outside the critical section? What if not? What happens when a third thread calls acquire? withdraw (account, amount) { acquire(lock); balance = get_balance(account); balance = balance – amount; put_balance(account, balance); release(lock); return balance; } acquire(lock); balance = get_balance(account); balance = balance – amount; balance = get_balance(account); balance = balance – amount; put_balance(account, balance); release(lock); acquire(lock); put_balance(account, balance); release(lock); Critical Section 4/12/2011 CSE 120 – Lecture 5 – Synchronization 4 How do we implement locks? Here is one attempt: This is called a spinlock because a thread spins waiting for the lock to be released Does this work? Implementing Locks (1) struct lock { int held = 0; } void acquire (lock) { while (lock->held); lock->held = 1; } void release (lock) { lock->held = 0; } busy-wait (spin-wait) for lock to be released
Background image of page 1

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

View Full Document Right Arrow Icon
2 4/12/2011 CSE 120 – Lecture 5 – Synchronization 5 Using Test-And-Set Here is our lock implementation with test-and-set: When will the while return? What is the value of held? Does it work? Does it work on multiprocessors? struct lock { int held = 0; } void acquire (lock) { while (test-and-set(&lock->held)); } void release (lock) { lock->held = 0; } 4/12/2011 CSE 120 – Lecture 5 – Synchronization 6 Other Similar Hardware Instruction Swap = TSL void Swap (char* x,* y); \\ All done atomically { char temp = *x; *x = *y; *y = temp } Do it yourself Use Swap to implement lock acquire and release 4/12/2011 CSE 120 – Lecture 6 – Semaphores and Monitors 7 struct lock { int held = 0; } void acquire (lock) { } void release (lock) { } Fill in code here Fill in code here 4/12/2011 CSE 120 – Lecture 6 – Semaphores and Monitors 8 Higher-Level Synchronization We looked at using locks to provide mutual exclusion Those locks work, but they have some drawbacks when critical sections are long Spinlocks – inefficient Disabling interrupts – can miss or delay important events, and do not work on multicores. 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
3 4/12/2011 CSE 120 – Lecture 6 – Semaphores and Monitors 9 Semaphores Semaphores are an abstract data type that provide mutual
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 14

lec6_sem - Review Critical Section CSE120 Principles of...

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

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