Threads and Critical Sections Vivek Pai / Kai Li Princeton University.

Slides:



Advertisements
Similar presentations
Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Advertisements

Threads and Critical Sections Thomas Plagemann Slides from Otto J. Anshus, Tore Larsen (University of Tromsø), Kai Li (Princeton University)
Preemptive Scheduling and Mutual Exclusion with Hardware Support Thomas Plagemann With slides from Otto J. Anshus & Tore Larsen (University of Tromsø)
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
PROCESS SYNCHRONIZATION READINGS: CHAPTER 5. ISSUES IN COOPERING PROCESSES AND THREADS – DATA SHARING Shared Memory Two or more processes share a part.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Mutual Exclusion.
Preemptive Scheduling and Mutual Exclusion with Hardware Support Thomas Plagemann With slides from Otto J. Anshus & Tore Larsen (University of Tromsø)
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
Computer Systems/Operating Systems - Class 8
Review: Chapters 1 – Chapter 1: OS is a layer between user and hardware to make life easier for user and use hardware efficiently Control program.
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
Synchronization CSCI 444/544 Operating Systems Fall 2008.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Cpr E 308 Spring 2004 Real-time Scheduling Provide time guarantees Upper bound on response times –Programmer’s job! –Every level of the system Soft versus.
Chapter 1 Computer System Overview Sections 1.1 to 1.6 Instruction exe cution Interrupt Memory hierarchy Cache memory Locality: spatial and temporal Problem.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Operating Systems CSE 411 Multi-processor Operating Systems Multi-processor Operating Systems Dec Lecture 30 Instructor: Bhuvan Urgaonkar.
COSC 3407: Operating Systems Lecture 7: Implementing Mutual Exclusion.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
CSE 451: Operating Systems Section 5 Midterm review.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Processes & Threads Introduction to Operating Systems: Module 5.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
What is an Operating System? Various systems and their pros and cons –E.g. multi-tasking vs. Batch OS definitions –Resource allocator –Control program.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
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.
Transmitter Interrupts Review of Receiver Interrupts How to Handle Transmitter Interrupts? Critical Regions Text: Tanenbaum
Processes and threads.
CSE 120 Principles of Operating
Process Management Process Concept Why only the global variables?
CS703 – Advanced Operating Systems
Background on the need for Synchronization
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Jonathan Walpole Computer Science Portland State University
Implementing Mutual Exclusion
Prof. Leonardo Mostarda University of Camerino
Implementing Mutual Exclusion
February 5, 2004 Adrienne Noble
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
Lecture 20: Synchronization
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Threads and Critical Sections Vivek Pai / Kai Li Princeton University

2 Gedankenthreads What happens during fork? We need particular mechanisms, but do we have options about what to do?

3 Mechanics Midterm grading finish 26-30: : : : : : : : : : : 1 Quiz still not graded

4 Thread and Address Space Thread A sequential execution stream within a process (also called lightweight process) Address space All the state needed to run a program Provide illusion that program is running on its own machine (protection) There can be more than one thread per address space

5 Concurrency and Threads I/O devices Overlap I/Os with I/Os and computation (modern OS approach) Human users Doing multiple things to the machine: Web browser Distributed systems Client/server computing: NFS file server Multiprocessors Multiple CPUs sharing the same memory: parallel program

6 Typical Thread API Creation Fork, Join Mutual exclusion Acquire (lock), Release (unlock) Condition variables Wait, Signal, Broadcast Alert Alert, AlertWait, TestAlert

7 User vs. Kernel-Level Threads Question What is the difference between user-level and kernel-level threads? Discussions When a user-level thread is blocked on an I/O event, the whole process is blocked A context switch of kernel-threads is expensive A smart scheduler (two-level) can avoid both drawbacks

8 Thread Control Block Shared information Processor info: parent process, time, etc Memory: segments, page table, and stats, etc I/O and file: comm ports, directories and file descriptors, etc Private state State (ready, running and blocked) Registers Program counter Execution stack

9 Threads Backed By Kernel Threads Each thread has a user stack a private kernel stack Pros concurrent accesses to system services works on a multiprocessor Cons More memory Each thread has a user stack a shared kernel stack with other threads in the same address space Pros less memory Does not work on a multiprocessor Cons serial access to system services

10 “Too Much Milk” Problem Don’t buy too much milk Any person can be distracted at any point Person APerson B Look in fridge: out of milk Leave for Wawa Arrive at Wawa Buy milk Arrive home Look in fridge: out of milk Leave for Wawa Arrive at Wawa Buy milk Arrive home

11 A Possible Solution? Thread can get context switched after checking milk and note, but before buying milk if ( noMilk ) { if (noNote) { leave note; buy milk; remove note; } if ( noMilk ) { if (noNote) { leave note; buy milk; remove note; }

12 Another Possible Solution? Thread A switched out right after leaving a note Thread A leave noteA if (noNoteB) { if (noMilk) { buy milk } remove noteA Thread B leave noteB if (noNoteA) { if (noMilk) { buy milk } remove noteB

13 Yet Another Possible Solution? Safe to buy If the other buys, quit Thread A leave noteA while (noteB) do nothing; if (noMilk) buy milk; remove noteA Thread B leave noteB if (noNoteA) { if (noMilk) { buy milk } remove noteB

14 Remarks The last solution works, but Life is too complicated A’s code is different from B’s Busy waiting is a waste Peterson’s solution is also complex What we want is: Acquire(lock); if (noMilk) buy milk; Release(lock); } Critical section

15 What Is A Good Solution Only one process inside a critical section No assumption about CPU speeds Processes outside of critical section should not block other processes No one waits forever Works for multiprocessors

16 Primitives We want to avoid thinking (repeatedly) So, we want some “contract” that provides certain behavior Low-level behavior encapsulated in “primitives” Application uses primitives to construct more complex behavior

17 The Simplistic Acquire/Release Kernel cannot let users disable interrupts Critical sections can be arbitrarily long Used on uniprocessors, but won’t work on multiprocessors Acquire() { disable interrupts; } Release() { enable interrupts; }

18 Disabling Interrupts Done right, serializes activity People think sequentially – easier to reason Guarantees code executes without interruption Delays handling of external events Used throughout the kernel

19 Using Disabling Interrupts Why do we need to disable interrupts at all? Why do we need to enable interrupts inside the loop in Acquire ? Acquire(lock) { disable interrupts; while (lock != FREE){ enable interrupts; disable interrupts; } lock = BUSY; enable interrupts; } Release(lock) { disable interrupts; lock = FREE; enable interrupts; }

20 Using Disabling Interrupts When does Acquire re-enable interrupts in going to sleep? Before enqueue? After enqueue but before block? Acquire(lock) { disable interrupts; while (lock == BUSY) { enqueue me for lock; block; } else lock = BUSY; enable interrupts; } Release(lock) { disable interrupts; if (anyone in queue) { dequeue a thread; make it ready; } lock = FREE; enable interrupts; }

21 Hardware Support for Mutex Mutex = mutual exclusion Early software-only approaches limited Hardware support became common Various approaches: Disabling interrupts Atomic memory load and store Atomic read-modify-write L. Lamport, “A Fast Mutual Exclusion Algorithm,” ACM Trans. on Computer Systems, 5(1):1-11, Feb – use Google to find

22 The Big Picture Concurrent Applications Locks Semaphores Monitors Send/Receive Load/Store Interrupt disable Test&Set High-Level Atomic API Low-Level Atomic Ops Interrupt (timer or I/O completion), Scheduling, Multiprocessor

23 Atomic Read-Modify-Write Instructions Test&Set: Read value and write 1 back to memory Exchange ( xchg, x86 architecture) Swap register and memory Compare and Exchange ( cmpxchg, 486+) If Dest = ( al,ax,eax ), Dest = SRC; else ( al,ax,eax ) = Dest LOCK prefix in x86 Load link and conditional store (MIPS, Alpha) Read value in one instruction, do some operations When store, check if value has been modified. If not, ok; otherwise, jump back to start

24 A Simple Solution with Test&Set Waste CPU time Low priority threads may never get a chance to run Acquire(lock) { while (!TAS(lock)) ; } Release(lock) { lock = 0; }

25 Test&Set, Minimal Busy Waiting Why does this work? Acquire(lock) { while (!TAS(lock.guard)) ; if (lock.value) { enqueue the thread; block and lock.guard = 0; } else { lock.value = 1; lock.guard = 0; } Release(lock) { while (!TAS(lock.guard)) ; if (anyone in queue) { dequeue a thread; make it ready; } else lock.value = 0; lock.guard = 0; }