Mutex Implementation

Similar documents
Preemptive Scheduling and Mutual Exclusion with Hardware Support

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

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

Lecture #7: Implementing Mutual Exclusion

Synchronization I. Jo, Heeseung

Semaphores (and Eventcounts) Otto J. Anshus University of {Tromsø, Oslo}

Lecture 5: Synchronization w/locks

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Semaphores. Otto J. Anshus University of {Tromsø, Oslo}

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

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

CS5460: Operating Systems

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

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

CS 153 Design of Operating Systems Winter 2016

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Locks. Dongkun Shin, SKKU

Last Class: Synchronization

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

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

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

Dealing with Issues for Interprocess Communication

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Today: Synchronization. Recap: Synchronization

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Introduction to OS Synchronization MOS 2.3

Concurrency and Synchronisation. Leonid Ryzhyk

Introduction to Real-Time Operating Systems

EECS 482 Introduction to Operating Systems

Lecture #7: Shared objects and locks

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

PROCESS SYNCHRONIZATION

Concurrency and Synchronisation

CSE Traditional Operating Systems deal with typical system software designed to be:

Threading and Synchronization. Fahd Albinali

Timers 1 / 46. Jiffies. Potent and Evil Magic

Synchronization 1. Synchronization

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

CS162 Operating Systems and Systems Programming Lecture 7. Mutual Exclusion, Semaphores, Monitors, and Condition Variables

C09: Process Synchronization

Page 1. Another Concurrent Program Example" Goals for Today" CS162 Operating Systems and Systems Programming Lecture 4

Symmetric Multiprocessors: Synchronization and Sequential Consistency

Concurrency and Synchronisation

Other consistency models

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

Synchronization. Coherency protocols guarantee that a reading processor (thread) sees the most current update to shared data.

Process Management And Synchronization

Threads and Synchronization. Kevin Webb Swarthmore College February 15, 2018

CS 550 Operating Systems Spring Interrupt

Last Class: Deadlocks. Today

COS 318: Operating Systems. Deadlocks. Jaswinder Pal Singh Computer Science Department Princeton University

Process Coordination and Shared Data

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

M4 Parallelism. Implementation of Locks Cache Coherence

Operating Systems. Synchronization

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Part II Process Management Chapter 6: Process Synchronization

CS-537: Midterm Exam (Spring 2001)

Synchronization. CISC3595/5595 Fall 2015 Fordham Univ.

CS 152 Computer Architecture and Engineering. Lecture 19: Synchronization and Sequential Consistency

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Review: Easy Piece 1

Synchronization COMPSCI 386

CS3733: Operating Systems

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Synchronization. Before We Begin. Synchronization. Example of a Race Condition. CSE 120: Principles of Operating Systems. Lecture 4.

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Intro to Threads. Two approaches to concurrency. Threaded multifinger: - Asynchronous I/O (lab) - Threads (today s lecture)

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

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

Last class: Today: Course administration OS definition, some history. Background on Computer Architecture

Cache Coherence and Atomic Operations in Hardware

Concept of a process

Synchronization for Concurrent Tasks

CSE 120: Principles of Operating Systems. Lecture 4. Synchronization. October 7, Prof. Joe Pasquale

CSE 153 Design of Operating Systems Fall 2018

Implementing Locks. Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau)

Multiprocessors II: CC-NUMA DSM. CC-NUMA for Large Systems

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

HY345 - Operating Systems

CPS 110 Midterm. Spring 2011

EECS 482 Introduction to Operating Systems

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Synchronization 1. Synchronization

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Administrivia. p. 1/20

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

CS 153 Design of Operating Systems Winter 2016

Concurrency: Mutual Exclusion (Locks)

IT 540 Operating Systems ECE519 Advanced Operating Systems

Transcription:

COS 318: Operating Systems Mutex Implementation Jaswinder Pal Singh Computer Science Department Princeton University (http://www.cs.princeton.edu/courses/cos318/)

Revisit Mutual Exclusion (Mutex) u Critical section Acquire(lock); if (nocookies) buy cookies; Release(lock); Critical section u Requirements Only one process/thread inside a critical section No assumption about CPU speeds A process/thread inside a critical section should not be blocked by any processes/threads outside the critical section No one waits forever Works for multiprocessors Same code for all processes/threads 2

Today s Topics u Mutex problem u Ways of implementing mutual exclusion 3

Simple Lock Variables Acquire(lock) { while (lock.value == 1) ; lock.value = 1; Release(lock) { lock.value = 0; Thread 1: Acquire(lock) { while (lock.value == 1) ; {context switch) lock.value = 1; {context switch) Thread 2: Acquire(lock) { while (lock.value == 1) ; {context switch) lock.value = 1; 4

Interrupt in A Simplified System Central Processing Unit Memory Interrupt I/O bus I/O devices can raise interrupts to CPU 5

Instruction Execution with Interrupt User Program ld Fetch IP add st mul ld sub bne add jmp Decode Execute Advance IP Interrupt? yes Save context Get INTR ID Lookup ISR Execute ISR Interrupt handler {... no iret Most instructions can be interrupted in the middle

Exceptions u Interrupts are asynchronous From external sources Examples: alarm clock, I/O bus signals from devices u Other exceptions are synchronous (more later) Processor-detected or programmed exceptions Faults correctable; offending instruction is retried Traps often for debugging; instruction is not retried Aborts errors when executing instructions

Why Enable or Disable Interrupts u Enable interrupts Process I/O requests (e.g. keyboard) Implement preemptive CPU scheduling u Disable interrupts Introduce uninterruptible code regions Think sequentially most of the time Delay handling of external events Uninterruptible region DisableInt(). EnableInt() 8

Disabling Interrupts for Critical Section? Acquire(): disable interrupts Release(): enable interrupts Acquire() critical section? Release() Issues: Kernel cannot let users disable interrupts Critical sections can be arbitrarily long Used on uniprocessors, but does not work on multiprocessors 9

Disable Interrupts to Implement Mutex Acquire(lock) { disable interrupts; while (lock.value!= 0) ; lock.value = 1; enable interrupts; Release(lock) { disable interrupts; lock.value = 0; enable interrupts; u Issues: May disable interrupts forever Not designed for user code to use 10

Fix Disable Forever problem? Acquire(lock) { disable interrupts; while (lock.value!= 0){ enable interrupts; disable interrupts; lock.value = 1; enable interrupts; Release(lock) { disable interrupts; lock.value = 0; enable interrupts; Issues Consume CPU cycles Won t work with multiprocessors 11

Another Implementation Acquire(lock) { disable interrupts; while (lock.value == 1) { Enqueue me for lock; Yield(); lock.value = 1; enable interrupts; Issues Working for multiprocessors Release(lock) { disable interrupts; if (anyone in queue) { Dequeue a thread; make it ready; lock.value = 0; enable interrupts; 12

Peterson s Algorithm u See textbook int turn; int interested[n]; void enter_region(int process) { int other; other = 1 process; interested[process] = TRUE; turn = process; while(turn == process && interested[other] == TRUE); u L. Lamport, A Fast Mutual Exclusion Algorithm, ACM Trans. on Computer Systems, 5(1):1-11, Feb 1987. 5 writes and 2 reads 13

Atomic Read-Modify-Write Instructions u LOCK prefix in x86 Make a specific set instructions atomic Together with BTS to implement Test&Set u Exchange (xchg, x86 architecture) Swap register and memory Atomic (even without LOCK) u Fetch&Add or Fetch&Op Atomic instructions for large shared memory multiprocessor systems u Load linked and store conditional (LL-SC) Read value in one instruction (load linked) Do some operations; When store, check if value has been modified. If not, ok; otherwise, jump back to start 14

A Simple Solution with Test&Set u Define TAS(lock) If successfully set, return 1; Otherwise, return 0; u Any issues with the following solution? Acquire(lock) { while (!TAS(lock.value)) ; Release(lock.value) { lock.value = 0; 15

Mutex with Less Waiting? 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; u How long does the busy wait take? 16

Example: Protect a Shared Variable Acquire(lock); /* system call */ count++; Release(lock) /* system call */ u Acquire(mutex) system call Pushing parameter, sys call # onto stack Generating trap/interrupt to enter kernel Jump to appropriate function in kernel Verify process passed in valid pointer to mutex Minimal spinning Block and unblock process if needed Get the lock u Execute count++; u Release(mutex) system call 17

Available Primitives and Operations u Test-and-set Works at either user or kernel u System calls for block/unblock Block takes some token and goes to sleep Unblock wakes up a waiter on token 18

Block and Unblock System Calls Block( lock ) Spin on lock.guard Save the context to TCB Enqueue TCB to lock.q Clear lock.guard Call scheduler Unblock( lock ) Spin on lock.guard Dequeue a TCB from lock.q Put TCB in ready queue Clear lock.guard 19

Always Block Acquire(lock) { while (!TAS(lock.value)) Block( lock ); Release(lock) { lock.value = 0; Unblock( lock ); u Good u Bad Acquire won t make a system call if TAS succeeds TAS instruction locks the memory bus Block/Unblock still has substantial overhead

Always Spin Acquire(lock) { while (!TAS(lock.value)) while (lock.value) ; u Two spinning loops in Acquire()? Release(lock) { lock.value = 0; CPU CPU L1 $ L1 $ CPU L1 $ CPU L1 $ TAS L2 $ L2 $ TAS L2 $ Multicore Memory SMP 21

Optimal Algorithms u What is the optimal solution to spin vs. block? Know the future Exactly when to spin and when to block u But, we don t know the future There is no online optimal algorithm u Offline optimal algorithm Afterwards, derive exactly when to block or spin ( what if ) Useful to compare against online algorithms 22

Competitive Algorithms u An algorithm is c-competitive if for every input sequence σ C A (σ) c C opt (σ) + k c is a constant C A (σ) is the cost incurred by algorithm A in processing σ C opt (σ) is the cost incurred by the optimal algorithm in processing σ u What we want is to have c as small as possible Deterministic Randomized 23

Constant Competitive Algorithms Acquire(lock, N) { int i; while (!TAS(lock.value)) { i = N; while (!lock.value && i) i--; if (!i) Block(lock); u Spin up to N times if the lock is held by another thread u If the lock is still held after spinning N times, block u If spinning N times is equal to the context-switch time, what is the competitive factor of the algorithm?

Approximate Optimal Online Algorithms u Main idea Use past to predict future u Approach Random walk Decrement N by a unit if the last Acquire() blocked Increment N by a unit if the last Acquire() didn t block Recompute N each time for each Acquire() based on some lock-waiting distribution for each lock u Theoretical results E C A (σ (P)) (e/(e-1)) E C opt (σ(p)) The competitive factor is about 1.58. 25

Empirical Results A. Karlin, K. Li, M. Manasse, and S. Owicki, Empirical Studies of Competitive Spinning for a Shared-Memory Multiprocessor, Proceedings of the 13 th ACM Symposium on Operating Systems Principle, 1991. 26

The Big Picture OS codes and concurrent applications High-Level Atomic API Mutex Semaphores Monitors Send/Recv Low-Level Atomic Ops Load/store Interrupt disable/enable Test&Set Other atomic instructions Interrupts (I/O, timer) Multiprocessors CPU scheduling 27

Summary u Disabling interrupts for mutex There are many issues When making it work, it works for only uniprocessors u Atomic instruction support for mutex Atomic load and stores are not good enough Test&set and other instructions are the way to go u Competitive spinning Spin at the user level most of the time Make no system calls in the absence of contention Have more threads than processors 28