ECE344-Lecture10-Synchronization

ECE344-Lecture10-Synchronization - Lecture 10:...

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

View Full Document Right Arrow Icon
1 Lecture 10: Synchronization David Lie ECE344 University of Toronto
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 ECE344: Operating Systems Overview Need for synchronization Different synchronization primitives – Locks Conditional Variables Semaphores Common Synchronization Problems Deadlocks, Livelock, starvation Tradeoffs with implementing synchronization
Background image of page 2
3 ECE344: Operating Systems char receive() { while (in == out) { cv_wait(notempty); } msg = buf[out]; if ((in – out)%n == n–1) cv_signal(notfull); out = (out + 1) % n; return msg; } void send(char msg) { while ((in - out)%n == n-1) { cv_wait(notfull); } buf[in] = msg; if (in == out) cv_signal(notempty); in = (in + 1) % n; } Producer-Consumer with Conditional Variables cv *notfull; cv *notempty; Does this work? Why or why not?
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 ECE344: Operating Systems char receive() { lock(l); while (in == out) { cv_wait(notempty); } msg = buf[out]; if ((in – out)%n == n–1) cv_signal(notfull); out = (out + 1) % n; unlock(l); return msg; } void send(char msg) { lock(l); while ((in - out)%n == n-1) { cv_wait(notfull); } buf[in] = msg; if (in == out) cv_signal(notempty); in = (in + 1) % n; unlock(l); } Producer-Consumer with Conditional Variables cv *notfull; cv *notempty; struct *lock l;
Background image of page 4
5 ECE344: Operating Systems Monitor Abstraction What the previous solution implements is called a monitor . Monitor is defined as an object where: – All data in the object must be accessed through methods – Methods must execute atomically Note that the lock in the pervious example locks the entire method, thus it is a monitor. – Monitors guarantee mutual exclusion. Why? – Also guarantee freedom from deadlocks. More on this later…
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 ECE344: Operating Systems Producer-Consumer with Semaphores Why are buf, in, out not accessed in a critical section? Global variables: char buf[n]; int in = 0; // place to write int out = 0; // place to read sem full_slot = sem_create(“”,0); sem empty_slot = sem_create(“”,n); 1 2 in out n-1 0 char receive() { P(full_slot); msg = buf[out]; out = (out + 1) % n; V(empty_slot); return msg; } void send(char msg) { P(empty_slot); buf[in] = msg; in = (in + 1) % n; V(full_slot); }
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

ECE344-Lecture10-Synchronization - Lecture 10:...

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

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