OSE 2013 – synchronization (lec4) 1 Operating Systems Engineering Sleep & Wakeup [chapter #5] By Dan Tsafrir, 2013-04-17.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Interprocess Communication
1 Mutual Exclusion: Primitives and Implementation Considerations.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
CS510 Concurrent Systems Class 1 Linux Kernel Locking Techniques.
Jonathan Walpole Computer Science Portland State University
Jonathan Walpole Computer Science Portland State University
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
OSE 2013 – synchronization (lec3) 1 Operating Systems Engineering Locking & Synchronization [chapter #4] By Dan Tsafrir,
COP 4600 Operating Systems Spring 2011 Dan C. Marinescu Office: HEC 304 Office hours: Tu-Th 5:00-6:00 PM.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
CS510 Concurrent Systems Introduction to Concurrency.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Chapter 28 Locks Chien-Chung Shen CIS, UD
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Kernel Locking Techniques by Robert Love presented by Scott Price.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Lecture 6 Page 1 CS 111 Summer 2013 Concurrency Solutions and Deadlock CS 111 Operating Systems Peter Reiher.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
CSE 120 Principles of Operating
CS703 – Advanced Operating Systems
Anton Burtsev November, 2017
Kernel Synchronization II
COP 4600 Operating Systems Spring 2011
COT 5611 Operating Systems Design Principles Spring 2014
Jonathan Walpole Computer Science Portland State University
COT 5611 Operating Systems Design Principles Spring 2012
Chapter 30 Condition Variables
Implementing Mutual Exclusion
CSCI1600: Embedded and Real Time Software
Implementing Mutual Exclusion
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
Kernel Synchronization II
CSE 451: Operating Systems Autumn 2004 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2004 Module 6 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
CSCI1600: Embedded and Real Time Software
CSE 451: Operating Systems Autumn 2009 Module 7 Synchronization
CSE 153 Design of Operating Systems Winter 2019
Presentation transcript:

OSE 2013 – synchronization (lec4) 1 Operating Systems Engineering Sleep & Wakeup [chapter #5] By Dan Tsafrir,

OSE 2013 – synchronization (lec4) 2 FINE-GRAINED SYNCHRONIZATION When critical section is very short in the previous lecture….

OSE 2013 – synchronization (lec4) 3 COARSE-GRAINED SYNCHRONIZATION When critical section is long, need to allow for sleep-waiting in this lecture….

OSE 2013 – synchronization (lec4) 4 Big picture  Multiple thread executing in kernel  Sharing memory, data structures, devices  We need to allow them to utilize the multiprocessor  Already covered:  Locking and per-CPU context switching in first past of lecture  Next:  How to allow threads to wait for each other for time scales too long for spinning  Called “sequence coordination” or “conditional synchronization”

OSE 2013 – synchronization (lec4) 5 Example – thread #1 waits for threads #2 to terminate  Spinning is out of the question  Wasteful  Polling is somewhat better (periodically chk if thread #2 is alive)  Tradeoff between: latency and wasted CPU cycles  Not as wasteful as spinning, but still  Not are elegant as event-based…  We can do better with sequence coordination  T1 tells thread-manager to make it sleep until T2 terminates  But sequence coordination interacts with locks & context switching  In this lecture we will see why…

OSE 2013 – synchronization (lec4) 6 Agenda  xv6’s sequence coordination  (OS literature has rich set of other suitable primitives)  By using  sleep & weakup (p. 25–26)  …we will implement  pipewrite, piperead (pp. 60–61)  wait, exit (p. 23–24)

OSE 2013 – synchronization (lec4) 7 Producer-consumer queue pcq struct pcq { void *ptr; /* initialized to 0 */ }; send void send(struct pcq *q, void *p) { while(q->ptr != 0) ; q->ptr = p; } recv void* recv(struct pcq *q) { void *p; while((p = q->ptr) == 0) ; q->ptr = 0; return p; }  Rules  Queue allows one process to send a nonzero pointer to another process  Assuming  There is only one sender and one receiver, and  They execute on different CPUs Then implementation on right is correct  Problem: it’s wasteful

OSE 2013 – synchronization (lec4) 8 Sleep semantics  Sleep(channel)  Gets a “channel” (aka “wait channel”)  Any 32bit number  Mark caller as “SLEEPING” on channel & puts its to sleep  Yield CPU  More than one process can sleep on channel  Wakeup(channel)  Mark all sleeping processes on channel as “RUNNABLE”  (Only those who are already sleeping)  Wakes them up  If no processes are waiting, wakeup does nothing  Channel is just an arbitrary number  => Callers of sleep / wakeup must agree on a convention

OSE 2013 – synchronization (lec4) 9 Pseudo code sleep(chan): curproc->chan = chan curproc->state = SLEEPING sched() /* => yield + go to sleep*/ wakeup(chan): foreach p in proc[]: if (p->chan == chan) && (p->state == SLEEPING): p->state = RUNNABLE /*+ wake up p*/

OSE 2013 – synchronization (lec4) 10 Producer-consumer queue: take #2 recv void* /* consumer */ recv(struct pcq *q) { void *p; if( q->ptr == 0 ) sleep(q); p = q->ptr; q->ptr = 0; wakeup(q); return p; } send void /* producer */ send(struct pcq *q, void *p) { if( q->ptr != 0 ) sleep(q); q->ptr = p; wakeup(q); } (assumption that there’s only one producer and one consumer still holds)

OSE 2013 – synchronization (lec4) 11 Problem: lost wakeup  recv void* recv(struct pcq *q) { void *p; if( q->ptr == 0 ) // yes! sleep(q); // entered p = q->ptr; q->ptr = 0; wakeup(q); return p; } send void send(struct pcq *q, void *p) { if( q->ptr != 0 ) // no! sleep(q); // skipped q->ptr = p; wakeup(q); } (further assume q->ptr=0 and the following denotes a schedule)

OSE 2013 – synchronization (lec4) 12 Problem: lost wakeup  recv void* recv(struct pcq *q) { void *p; if( q->ptr == 0 ) // yes! sleep(q); // entered p = q->ptr; q->ptr = 0; wakeup(q); return p; } send void send(struct pcq *q, void *p) { if( q->ptr != 0 ) // no! sleep(q); // skipped q->ptr = p; wakeup(q); } We need the if & sleep() to be atomic, but…

OSE 2013 – synchronization (lec4) 13 Problem: lost wakeup  recv void* recv(struct pcq *q) { void *p; acquire( &q->lock ); if( q->ptr == 0 ) sleep(q); release( &q->lock ); // … } send void send(struct pcq *q, void *p) { acquire( &q->lock ); if( q->ptr != 0 ) sleep(q); release( &q->lock ); //… } We can’t do this… (deadlock)

OSE 2013 – synchronization (lec4) 14 So the conclusion is…  Sleep must know about locks!  Sleep should somehow  1) Atomically release the lock and put the process to sleep  2) Reacquire the lock before it returns  => we’ll add the lock to the prototype of sleep  Let us add a lock to the queue: struct pcq { void *ptr; struct spinlock lock; };

OSE 2013 – synchronization (lec4) 15 Producer-consumer queue: take #3 recv void* recv(struct pcq *q) { void *p; acquire( &q->lock ); if( q->ptr == 0 ) sleep(q, &q->lock); // release+acquire p = q->ptr; q->ptr = 0; wakeup(q); release( &q->lock ); return p; } send void send(struct pcq *q, void *p) { acquire( &q->lock ); if( q->ptr != 0 ) sleep(q, &q->lock); // release+acquire q->ptr = p; wakeup(q); release( &q->lock ); }

OSE 2013 – synchronization (lec4) 16 Producer-consumer queue: take #3 recv void* recv(struct pcq *q) { void *p; acquire( &q->lock ); if( q->ptr == 0 ) sleep(q, &q->lock); // release+acquire p = q->ptr; q->ptr = 0; wakeup(q); release( &q->lock ); return p; } send void send(struct pcq *q, void *p) { acquire( &q->lock ); if( q->ptr != 0 ) sleep(q, &q->lock); // release+acquire q->ptr = p; wakeup(q); release( &q->lock ); } Why is wakeup() inside the locked region? (otherwise lost wakeup can occur again)

OSE 2013 – synchronization (lec4) 17 Producer-consumer queue: take #3 recv void* recv(struct pcq *q) { void *p; acquire( &q->lock ); if( q->ptr == 0 ) sleep(q, &q->lock); // release+acquire p = q->ptr; q->ptr = 0; wakeup(q); release( &q->lock ); return p; } send void send(struct pcq *q, void *p) { acquire( &q->lock ); if( q->ptr != 0 ) sleep(q, &q->lock); // release+acquire q->ptr = p; wakeup(q); release( &q->lock ); } Would it work for multiple consumers and producers?

OSE 2013 – synchronization (lec4) 18 Producer-consumer queue: take #4 (multiple consumers-producers) recv void* recv(struct pcq *q) { void *p; acquire( &q->lock ); while( q->ptr == 0 ) sleep(q, &q->lock); // release+acquire p = q->ptr; q->ptr = 0; wakeup(q); release( &q->lock ); return p; } send void send(struct pcq *q, void *p) { acquire( &q->lock ); while( q->ptr != 0 ) sleep(q, &q->lock); // release+acquire q->ptr = p; wakeup(q); release( &q->lock ); }

OSE 2013 – synchronization (lec4) 19 XV6 CODE

OSE 2013 – synchronization (lec4) 20 sleep/wakeup  Read code  sleep (2553), wakeup (2614), wakeup1 (2603)  Claim  If following rules, sleep() & wakeup1() don’t miss each other  Proof  weakeup1() always runs while holding two locks:  (i) lk, and  (ii) ptable.lock  Whereas sleep() holds at least one (or both) until process actually sleeps

OSE 2013 – synchronization (lec4) 21 sleep/wakeup  Sometimes multiple processes sleep on same channel  E.g., trying to read from same pipe  Yet, a single call to wakeup() wakes them all  One of them will win (e.g., read the data on the pipe)  Others will find there’s nothing to do (read)  => For them, wakeup event was “spurious” (מלאכותי, מזויף)  For this reason  sleep is always called inside a loop  Looping also helps if different uses of sleep/wakeup accidentally choose exact same channel  Simply means more spurious wakeups

OSE 2013 – synchronization (lec4) 22 Using sleep/wakeup – pipes  Waiting for wait?  Reader: to have something to read  Writer: to have enough buffer space to write  Read code  struct pipe (6011), pipewrite (6080), piperead (6101)

OSE 2013 – synchronization (lec4) 23 Using sleep/wakeup – wait/exit  Waiting for what?  A parent waits for its child to terminate  E.g., shell running fg processes  Recall  When a child dies it turns into a zombie  Deallocated only after it is wait()ed for by its parent  If parent dies before child => init becomes the parent  Read code  wait (2403), exit (2354)  kill (2625, exit=itself; kill=another); challenges:  killed process might be running on another core  killed process might be sleep-waiting for an event, holding some kernel resource