r ~ c.. Q.) 0\ 7 < - \1") ::J - ::r 3 ::J,... ::J Q.) 0!:t. !:t. ::J ::J (/') C

Similar documents
Synchroniza+on. Today: Implementa+on issues

SE350: Operating Systems. Lecture 3: Concurrency

Synchronization. CISC3595/5595 Fall 2015 Fordham Univ.

Semaphores and Monitors: High-level Synchronization Constructs

Lecture 5: Concurrency and Threads

Locks and Condition Variables Recap. Introducing Monitors. Hoare Monitors: Semantics. Coke Machine Example. Locks. Condition variables

February 23 rd, 2015 Prof. John Kubiatowicz

Main Points. Defini&on. Design pa9ern Example: bounded buffer. Condi&on wait/signal/broadcast

CS 318 Principles of Operating Systems

CSE 153 Design of Operating Systems

Page 1. CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

Concurrent Programming Issues & Readers/Writers

CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

CS162 Operating Systems and Systems Programming Midterm Review"

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

., --t - QJ. ., n. - r-+ r- (v. ::r. !:t. ... c.. c.. V> c.. c.. ::J ::J ::J. ~ ::::s. ::::s \I\ ([) QJ (D -. l/')

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

EECS 482 Introduction to Operating Systems

Lecture #10: Synchronization wrap up

Lecture 4: Threads; weaving control flow

Midterm I February 28 th, 2019 CS162: Operating Systems and Systems Programming

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Lecture 9: Midterm Review

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

CSE 153 Design of Operating Systems Fall 2018

Lecture 5: Synchronization w/locks

CPS 310 midterm exam #1, 2/19/2016

9/17/12! Ion Stoica CS162 UCB Fall 2012!

Concurrency: Deadlock and Starvation

Midterm 1, CSE 451, Winter 2001 (Prof. Steve Gribble)

Advanced Topic: Efficient Synchronization

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

CS Advanced Operating Systems Structures and Implementation Lecture 8. Synchronization Continued. Goals for Today. Synchronization Scheduling

2 Threads vs. Processes

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

EECS 482 Introduction to Operating Systems

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Midterm I October 18 th, 2010 CS162: Operating Systems and Systems Programming

CS 318 Principles of Operating Systems

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

What's wrong with Semaphores?

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

Synchronization 1. Synchronization

Lecture #7: Implementing Mutual Exclusion

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

CS 318 Principles of Operating Systems

Lecture 7: CVs & Scheduling

CSE 153 Design of Operating Systems

Threads and Concurrency

Resource management. Real-Time Systems. Resource management. Resource management

Concurrency and Synchronisation

CS 153 Design of Operating Systems Winter 2016

CPSC/ECE 3220 Summer 2018 Exam 2 No Electronics.

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

Chapter 2 Processes and Threads

Semaphores. Blocking in semaphores. Two types of semaphores. Example: Bounded buffer problem. Binary semaphore usage

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works?

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Today: Synchronization. Recap: Synchronization

Lecture 10: Multi-Object Synchronization

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory

Goals. Processes and Threads. Concurrency Issues. Concurrency. Interlacing Processes. Abstracting a Process

OS Structure. User mode/ kernel mode. System call. Other concepts to know. Memory protection, privileged instructions

Process Scheduling Queues

Monitors in Mesa. Josh Triplett. January 17, Message passing harder to integrate into Mesa type system

High Performance Computing Course Notes Shared Memory Parallel Programming

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

CS 153 Design of Operating Systems Winter 2016

SMD149 - Operating Systems

Operating Systems. Synchronization

Lecture 6 (cont.): Semaphores and Monitors

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani

Operating Systems ECE344

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

Midterm I October 15 th, 2008 CS162: Operating Systems and Systems Programming

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Concurrency: Deadlock and Starvation. Chapter 6

EECS 262a Advanced Topics in Computer Systems Lecture 5. Mesa/Transactions September 17 th, 2014

System Call. Preview. System Call. System Call. System Call 9/7/2018

Processes and Threads Implementation

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Concurrency and Synchronisation

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

Synchronization 1. Synchronization

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

The Kernel Abstraction

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Opera&ng Systems ECE344

Systèmes d Exploitation Avancés

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Concept of a process

Synchronization I. Jo, Heeseung

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write

Transcription:

~ 0 c.. Q.) < < - V> ::J n -c 3 - ::r m ~ 3 m ::J,... Q.)!:t. 0 ::J - N OJ 0!:t. ::J 0 V, - (/') C m V, ::J ~ r ~ 0\ 7 p )7 L v\ \1")

Readers/Writers Lock A common variant for mutual exclusion - One writer at a time, if no readers - Many readers, if no writer How might we implement this? - ReaderAcquire(), ReaderRelease() - WriterAcquire(), WriterRelease() - Need a lock to keep track of shared state - Need condition variables for w aiting if readers/ writers are in progress - Some state variables

Readers/Writers Lock Lock lock= FREE CV oktoread = nil CV oktowrite = nil AW= 0 //active writers AR = 0 // active readers WW= 0 // waiting writers WR= 0 // waiting readers

Lock lock= FREE lock.acquire{); lock.acquire{); while (AW> 0 11 WW> O) { while (AW> 0 11 AR> O) { CV oktoread = nil WR++; CV oktowrite = nil oktoread.wait(&lock); _ WR--, AW=O } AR= 0 AR++; WW= 0 WR=O., lock.release{); Read data WW++; _ I - vv,. 1-Q... okto~wait(&lock); WW--, } AW++; lock.release{); Write data lock.acquire{); AR--, if (AR == 0 && WW > O) oktowrite.signal{); lock.release{); lock.acquire{); AW--, if (WW> 0) oktowrite.signal{); else if (WR > O) oktoread.broadcast{); lock.release{);

Readers/Writers Lock Can readers starve.? - Yes: writers take priority Can writers starve? - Yes: a waiting writer may not be able to proceed, if another writer slips in between signal and wakeup

Writer() { Readers/Writers Lock, w/o Writer Starvation Take 1 lock.acquire(); // check if another thread is already waiting. while ((AW+ AR+ WW)> 0) { } AW++; WW++; oktowrite. Wait( &lock); WW-- I lock. Re lease();

// check in Readers/Writers Lock w/o Writer Starvation Take 2 lock.acquire(); mypos = numwriters++; while ((AW+ AR> 0 11 } mypos > nexttogo) { WW++; oktowrite.wait(&lock); WW-- I AW++; lock.release(); //checkout lock.acquire(); AW-- I nexttogo++; if (WW> O) { okto Write.Sign a I (&lock); } else if (WR > O) oktoread. Beast( &lock); lock.release();

Readers/Writers Lock w/o Writer Starvation Take 3 // check in //checkout lock.acquire(); mypos = numwriters++; mycv = new CV; lock.acquire(); AW--; nexttogo++; writers.append(mycv); if (WW > 0) { while ((AW+ AR> 0 11 } mypos > nexttogo) { cv = writers.removefront(); cv.signal(&lock); WW++; } else if (WR > O) mycv. Wait( &lock); WW--; AW++; delete mycv; lock.release(); oktoread. Broadcast( &lock); lock.release();

Mesa vs. Hoare semantics Mesa - Signal puts waiter on ready list - Signaller keeps lock and processor Hoare - Signal gives processor and lock to waiter - When waiter finishes, processor/lock goes back to signaller All systems you will use are Mesa

get() { lock.acquire(); if (front == tail) { empty. wait( &lock); } item = buf[front % MAX]; front++; fu I I.sign a I( &lock); lock.release(); return item; } FIFO Bounded Buffer (Hoare semantics) put(item) { lock.acquire(); if ((tail - front) == MAX) { full. wait( &lock); } buf[last % MAX] = item; last++; empty.sign a I (&lock); // CAREFUL: someone else ran lock.release(); } Initially: front= tail = O; MAX is buffer capacity empty/full are condition variables

FIFO Bounded Buffer (Mesa semantics) Create a condition variable for every waiter _ Queue condition variables (in FIFO order) Signal picks the front of the queue to wake up CAREFUL if spurious wakeups! Easily extends to case where queue is LIFO, priority, priority donation,..., - With Ho~re semantics, not as easy

FIFO Bounded Buffer (Mesa semantics, put() is similar) get() { lock.acquire(); mypositio,n = numgets++; cv = new CV; nextget.append(cv); while (front< myposition 11 front == tail) { cv.wait(&lock); delete cv; item = buf[front % MAX]; front++; if (next= nextput.remove()) { next->signal( &lock); } lock.release(); return item; } } Initially: front= tail = numgets = O; MAX is buffer capacity nextget, nextput are queues of Condition Variables

lmpl~menting Synchronization Concurrent Applications Semaphores Locks Condition Variables..---- ----- Interrupt Disable Atomic Read/Modify/Write Instructions Multiple Processors Hardware Interrupts

---, r 0 '}O I' I {) --1 ) (\ 0 7 ~ r p _;:- / ) -.) v'\ - ~~ 0 0 C I I ~ I ) ) '-'l' ~ ('I r ~ >,. I 1 ) 1- ~ r d ( ~ ~ 7 - I -+- ' rr- 'v- ;- r r ) r 0 ~ ) r v -1 C V'\ l 7 - r ~ I I I ) I I I I I I I :i 0 ~ ~ --, () ('> f) p ' fl,1 t1 r - > -..t:: C ):. fj7 n 0 r_ ff] ~ fjj c () fv7

Implementing Threads: Roadmap Kernel threads - Thread abstraction only available to kernel - To the kernel, a kernel thread and a single threaded user process look quite similar Multithreaded processes using kernel threads (Linux, MacOS, Windows) - Kernel thread operations available via syscall User-level threads (Windows) - Thread operations without system calls

Multithreaded OS Kernel a 8 Kernel I Kernel Thread 1 Kernel Thread 2 Kernel Thread 3 Process 1 Process 2 s TCB 1 s s TCB 2 TCB 3 PCB 1 PCB 2 a Stack Ld. Stack Stack Stack Stack Ld. LJ. LJ......... User-Level Processes Process 1 Thread s l22j Stack. a a Process 2 Thread s l22j Stack. a a

Voluntary Thread Context Switch - Thread_yield L o C- le. A C. ""\. v 1'r <?._ - (.> c_ ll,5 s U ~ '1 - Thread_join (if child is not done yet) Co- )_:-,.f-,-,- Involuntary - Interrupt or exception - Some other thread is higher priority u <-:-+

Voluntary thread context switch Called by old thread Save registers on old stack Switch to new stack, new thread Restore registers from new stack Return to new thread Exactly the same with kernel threads or user threads

x86 swtch push %rbp push %rbx push %rll push %r12 push %r13 push %r14 push %r15 pop %r15 pop %r14 pop %r13 pop %r12 pop %rll pop %rbx pop %rbp mov %rsp, {%rdi) mov %rsi, %rsp ret // save/restore callee save registers, not caller save

A Subtlety f d, {c_ \ e_ f- Thread_create puts new thread on ready list Some thread calls switch, picks that thread to run next - Saves old thread state to stack - Restores new thread state from stack What does the new thread stack contain so this will work? - Set up thread's stack as if it had saved its state in switch - "returns 11 to PC saved at base of stack to run thread

Thread l's instructions "return" from thread switch into stub call go call thread_yield choose another thread call thread switch save thread 1 state to TCB load thread 2 state return from th read_ switch return from thread_yield call thread_yield choose another thread call th read switch Two Threads Call Yield Thread 2's instructions "return" from thread switch into stub call go call thread_yield choose another thread call thread switch save thread 2 state to TCB load thread 1 state Processor's instructions "return" from thread switch into stub call go call thread_yield choose another thread call thread switch save thread 1 state to TCB load thread 2 state "return" from thread switch into stub call go call thread_yield choose another thread call thread switch save thread 2 state to TCB load thread 1 state return from thread switch return from thread_yield call thread_yield choose another thread call thread switch

IC Q_ ('r- (L. \ -tl~"g I \\ "S \.J'": t-c-l,t-:',-<l..\ f /\-_-t " L-r ------1 hr~ Involuntary Thread/Process Switch (Simple, Slow Version) Timer or 1/0 interrupt µ s-e.., Frnc_~s-s ~~~:J - Tells OS some other thread/process should run 1-tr-tL End of interrupt handler calls switch, before resuming the trapframe I When thread is switched back in, resumes the handler Handler restores the trapframe to resume the user process

Involuntary Thread/Process Sw-itch (Fast Version) Interrupt handler saves state of interrupted thread on trapframe At end of handler, switch to a new thread We don 1 t need.to come back to the interrupt handler I \ Instead: change switch so that it can restore directly from the trapframe On resume, pop trapframe to restore directly to the interrupted thread,.

~ ~ ~~ V) \/\ J) I./) [ t7 ( ~ J\ rt r l r ---re> (Q ' r r (_. ~ ~ - -t- 0-- \../ -- ------ ('-) V" [ r ) t r

Multithreaded User Processes (Take 1) User thread = kernel thread (Linux, MacOS) - System calls for thread fork, join, exit (and lock, unlock,... ) - - Kernel does context switch - Simple, but a lot of transitions between user and kernel mode

Multithreaded User Processes (Take 1) Kernel -I a G Kernel Thread 1 Kernel Thread 2 Kernel Thread 3 Process 1 Process 2 s s s I PCB 1 I I PCB 2 I I TCB 1 I I TCB 2 I I TCB 3 I I TCB 1.A I I TCB 1.B I I TCB 2.A I I TCB 2.B I Stack Stack Stack Stack Stack Stack Stack B 8... LJ LJ LJ LJ.. L=J...... L=J............................,_:"'/ User-Level Proc Process 1 Thread s A Thread s B astack...... a a a a ~~~~~... Process 2 Thread s A Thread s B astack G a a... a ~~~~~......

Multithreaded User Processes (Take 2) Green threads (early Java) - User-level library, within a single-threaded process - Library does thread context switch - Preemption via upcall/unix signal on timer interrupt - Use multiple processes for parallelism Shared memory region mapped into each process

Multithreaded User Processes (Take 3) Scheduler activations (Windows 8) - Kernel allocates processors to user-level library - Thread library implements context switch - Thread library decides what thread to run next Upcall whenever kernel needs a user-level scheduling decision Process assigned a new processor Processor removed from process System call blocks in kernel