Lec5_sync

Lec5_sync - Announcement CSE120 Principles of Operating...

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 Yuanyuan (YY) Zhou Lecture 5 Apr 11, 2011 4/10/2011 CSE 120 – Lecture 5 – Synchronization 2 Announcement Optional homework Project 1 due May 2 nd 4/10/2011 CSE 120 – Lecture 5 – Synchronization 3 Review Process: a program in execution Threads: a light weight process Process vs. threads What are private to a thread and what are shared? Context switch 4/10/2011 CSE 120 – Lecture 5 – Synchronization 4 Inter-Process Communication (IPC) Communication Pass information to each other Keep each other’s hair Proper sequencing The last one also applies to threads Any real life example for synchronization? Two people talking at the same time
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 4/10/2011 CSE 120 – Lecture 5 – Synchronization 5 A simple game Two volunteers Producer: produce 1 candy per iteration Step1: increment the counter on the board Step2: put one post-it on the table Consumer: Step1: read the counter LOUD Step2a: if the counter is zero, go back to step1 Step2b: if the counter is nonzero, take a post-it y from the table Step3: decrement counter on the board Rule: only one should “operate” at any time You are the OS You decide who should operate, who should freeze Can you get them into “trouble” before the post-it runs out? 4/10/2011 CSE 120 – Lecture 5 – Synchronization 6 First Round Stop Producer before step2 and let Consumer go. What happens? How to fix the code to avoid this problem? Producer: produce 1 candy per iteration Step1: increment the counter Step2: put one post-it on the table Consumer: Step1: check the counter to see if it is zero Step2a: if the counter is zero, go back to step1 Step2b: if the counter is nonzero, take the post-it from the table Step3: decrement the counter 4/10/2011 CSE 120 – Lecture 5 – Synchronization 7 Data Races Reason: data sharing What are shared in the game? Share the counter Share the cards 4/10/2011 CSE 120 – Lecture 5 – Synchronization 8 Shared Resources The problem is that two concurrent threads (or processes) accessed a shared resource (account) without any synchronization Known as a race condition (memorize this buzzword) We need mechanisms to control access to these shared resources in the face of concurrency So we can reason about how the program will operate Shared data structure Buffers, queues, lists, hash tables, etc.
Background image of page 2
3 Can you give me some real world examples? What are shared in real world and require some synchronization? 4/10/2011 CSE 120 – Lecture 5 – Synchronization 9 4/10/2011 CSE 120 – Lecture 5 – Synchronization 10 When Are Resources Shared? Local variables are
Background image of page 3

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

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

Page1 / 10

Lec5_sync - Announcement CSE120 Principles of Operating...

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