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 Oct 7, 2010 10/6/2010 CSE 120 – Lecture 5 – Synchronization 2 Announcement Optional homework released Project 1 due Oct 26th 10/6/2010 CSE 120 – Lecture 5 – Synchronization 3 Review Process: a program in execution Threads: a light weight process 10/6/2010 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 10/6/2010 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 candy 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 candy 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 candies run out? 10/6/2010 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 candy 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 candy from the table Step3: decrement the counter switch 10/6/2010 CSE 120 – Lecture 5 – Synchronization 7 Second Round Stop Producer before step2 and let Consumer go. What happens? Two volunteers Producer: produce 1 candy per iteration Step1: put one candy on the table Step2: increment the counter 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 candy from the table Step3: decrement the counter 10/6/2010 CSE 120 – Lecture 5 – Synchronization 8 Data Races Reason: data sharing First round: producer and consumer Share the counter Share the cards
Background image of page 2
3 10/6/2010 CSE 120 – Lecture 5 – Synchronization 9 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. 10/6/2010
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