Preemptive Scheduling and Mutual Exclusion with Hardware Support Thomas Plagemann With slides from Otto J. Anshus & Tore Larsen (University of Tromsø)

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Mutual Exclusion.
Preemptive Scheduling and Mutual Exclusion with Hardware Support Thomas Plagemann With slides from Otto J. Anshus & Tore Larsen (University of Tromsø)
Process Description and Control
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
1 Last Class: Introduction Operating system = interface between user & architecture Importance of OS OS history: Change is only constant User-level Applications.
Computer Organization and Architecture
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
1 OS & Computer Architecture Modern OS Functionality (brief review) Architecture Basics Hardware Support for OS Features.
Threads and Critical Sections Vivek Pai / Kai Li Princeton University.
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.
Introduction to Embedded Systems
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
CHAPTER 7 CONCURRENT SOFTWARE Copyright © 2000, Daniel W. Lewis. All Rights Reserved.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
Slides on threads borrowed by Chase Landon Cox. Thread states BlockedReady Running Thread is scheduled Thread is Pre-empted (or yields) Thread calls Lock.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Overview: Using Hardware.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Introduction to Operating Systems
Processes and threads.
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Structure of Processes
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Processes and Non-Preemptive Scheduling
Jonathan Walpole Computer Science Portland State University
Process Description and Control
Lecture 2 Part 2 Process Synchronization
Architectural Support for OS
Process Description and Control
Implementing Mutual Exclusion
Process Description and Control
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
Process Description and Control
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Architectural Support for OS
Chapter 6: Synchronization Tools
CSE 542: Operating Systems
CSE 542: Operating Systems
Chapter 3: Process Management
Presentation transcript:

Preemptive Scheduling and Mutual Exclusion with Hardware Support Thomas Plagemann With slides from Otto J. Anshus & Tore Larsen (University of Tromsø) and Kai Li (Princeton University)

Preemptive Scheduling Scheduler select a READY process and sets it up to run for a maximum of some fixed time (time-slice) Scheduled process computes happily, oblivious to the fact that a maximum time-slice was set by the scheduler Whenever a running process exhausts its time-slice, the scheduler needs to suspend the process and select another process to run (assuming one exists) To do this, the scheduler needs to be running! To make sure that no process computes beyond its time-slice, the scheduler needs a mechanism that guarantees that the scheduler itself is not suspended beyond the duration of one time-slice. A “wake- up” call is needed

Interrupts and Exceptions Interrupts and exceptions suspend the execution of the running thread of control, and activates some kernel routine Three categories of interrupts: –Software interrupts –Hardware interrupts –Exceptions

Real Mode Interrupt/Exception Handling High level descriptionProcessor actions

Interrupt Handling in Real vs. Protected Mode Real mode –Each entry in interrupt table is 4 bytes long –Start address of interrupt handler in segment:offset format –Any program can use INT nn Protected mode –OS must restrict access to some routines that may be called through INT nn –Handle some interrupts or exceptions by suspending current task and switching to another task –Each entry in interrupt table is 8 bytes –Calling program must have sufficient privilege to trigger a routine and must meet privilege level specified in Eflags[IOPL] to execute CLI and STI

Software Interrupts INT instruction Explicitly issued by program Synchronous to program execution Example: INT 10h

Hardware Interrupts Set by hardware components (for example timer), and peripheral devices (for example disk) –Timer component, set to generate timer-interrupt at any specified frequency! Separate unit or integral part of interrupt controller Asynchronous to program execution Non-maskable (NMI), and maskable interrupts. –NMI are processed immediately once current instruction is finished. –Maskable interrupts may be permanently or temporarily masked

Maskable Interrupt Request Some IO devices generate an interrupt request to signal that: –An action is required on the part of the program in order to continue operation –A previously-initiated operation has been completed with no errors encountered –A previously-initiated operation has encountered an error condition and cannot continue

Non-maskable Interrupt Requests In the PC-compatible world, the processor’s non- maskable interrupt request input (NMI) is used to report catastrophic HW failures to the OS

Exceptions Initiated by processor Three types: –Fault: Faulting instruction causes exception without completing. When thread resumes (after IRET), the faulting instruction is re-issued. For example page-fault –Trap: Exception is issued after instruction completes. When thread resumes (after IRET), the immediately following instruction is issued. May be used for debugging –Abort: Serious failure. May not indicate address of offending instruction Have used Intel terminology in this presentation. Classification, terminology, and functionality varies among manufacturers and authors

I/O and Timer Interrupts Overlapping computation and I/O: –Within single thread: Non-blocking I/O –Among multiple threads: Also blocking I/O with scheduling Sharing CPU among multiple threads –Set timer interrupt to enforce maximum time-slice –Ensures even and fair progression of concurrent threads Maintaining consistent kernel structures –Disable/enable interrupts cautiously in kernel CPU Memory Interrupt

When to Schedule? Process created Process exits Process blocks I/O interrupt Timer

Process State Transitions Running Blocked Ready I/O completion interrupt (move to ready queue) Create Terminate (call scheduler) Yield, Timer Interrupt (call scheduler) Block for resource (call scheduler) Scheduler dispatch

Process State Transitions (cont.) P4P3P2P1 P2 P1 ReadyQueue P4P3 BlockedQueue Scheduler Dispatcher Trap Handler Service Current Trap Return Handler U s e r L e v e l P r o c e s s e s KERNEL MULTIPROGRAMMING Uniprocessor: Interleaving (“pseudoparallelism”) Multiprocessor: Overlapping (“true paralellism”) PC PCB’s Memory resident part (Process Table) Running Blocked Ready I/O completion interrupt (move to ready queue) Create Terminate (call scheduler) Yield, Timer Interrupt (call scheduler) Block for resource (call scheduler) Scheduler dispatch Syscall

Transparent vs. Non-transparent Interleaving and Overlapping Non-preemptive scheduling (“Yield”) –Current process or thread has control, no other process or thread will execute before current says Yield Access to shared resources simplified Preemptive scheduling (timer and I/O interrupts) –Current process or thread will loose control at any time without even discovering this, and another will start executing Access to shared resources must be synchronized

Implementation of Synchronization Mechanisms Concurrent Applications Locks Semaphores Monitors Load/Store Interrupt disable Test&Set High-Level Atomic API Low-Level Atomic Ops Interrupt (timer or I/O completion), Scheduling, Multiprocessor Send/Receive Shared VariablesMessage Passing

Hardware Support for Mutex Atomic memory load and store –Assumed by Dijkstra (CACM 1965): Shared memory w/atomic R and W operations –L. Lamport, “A Fast Mutual Exclusion Algorithm,” ACM Trans. on Computer Systems, 5(1):1-11, Feb Disable Interrupts Atomic read-modify-write –IBM/360: Test And Set proposed by Dirac (1963) –IBM/370: Generalized Compare And Swap (1970)

A Fast Mutual Exclusion Algorithm (Fischer) Repeat await ; ; ; until ; use shared resource ; Entry: Exit Critical Region ”While x ≠ 0 do skip;” Or could block? How? Executed by process no. i. X is shared memory. is an Atomic Operation. We are assuming that COMMON CASE will be fast and that all processes will get through eventually

Disable Interrupts CPU scheduling –Internal events Threads do something to relinquish the CPU –External events Interrupts cause rescheduling of the CPU Disabling interrupts –Delay handling of external events and make sure we have a safe ENTRY or EXIT

Does This Work? Kernel cannot let users disable interrupts Kernel can provide two system calls, Acquire and Release, but need ID of critical region Remember: Critical sections can be arbitrary long (no preemption!) Used on uni-processors, but won’t work on multiprocessors Acquire() { disable interrupts; } Release() { enable interrupts; } User Level

Disabling Interrupts with Busy Wait We are at Kernel Level!: So why do we need to disable interrupts at all? Why do we need to enable interrupts inside the loop in Acquire ? Would this work for multiprocessors? Why not have a “disabled” Kernel? Acquire(lock) { disable interrupts; while (lock != FREE){ enable interrupts; disable interrupts; } lock = BUSY; enable interrupts; } Release(lock) { disable interrupts; lock = FREE; enable interrupts; }

Using Disabling Interrupts with Blocking When must Acquire re-enable interrupts in going to sleep? –Before insert()? –After insert(), but before block? Would this work on multiprocessors? Acquire(lock) { disable interrupts; while (lock == BUSY) { insert(caller, lock_queue); BLOCK; } else lock = BUSY; enable interrupts; } Release(lock) { disable interrupts; if (nonempty(lock_queue)) { out(tid, lock_queue); READY(tid); } lock = FREE; enable interrupts; }

Atomic Read-Modify-Write Instructions What we want: Test&Set(lock): –Returns TRUE if lock is TRUE (closed), else returns FALSE and closes lock. Exchange ( xchg, x86 architecture) –Swap register and memory Compare and Exchange ( cmpxchg, 486 or Pentium) –cmpxchg d,s: 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 The Butterfly multiprocessor –atomicadd: one processor can read and increment a memory location while preventing other processors from accessing the location simultaneously

Figure Entering and leaving a critical region using the TSL instruction. The TSL Instruction (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

Figure Entering and leaving a critical region using the XCHG instruction. The XCHG Instruction Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

The LOCK# Signal and Prefix in x86 causes the CPUs to assert the LOCK# signal practically ensures exclusive use of the memory address in multiprocessors / multi-thread environments Works with the following instructions: Works only if all follow the rule: BT, BTS, BTR, BTC (mem, reg/imm) XCHG, XADD (reg, mem / mem, reg) ADD, OR, ADC, SBB (mem, reg/imm) AND, SUB, XOR (mem, reg/imm) NOT, NEG, INC, DEC (mem) Thread 1: lock inc ptr [edx] Thread 2: inc ptr [edx]

A Simple Solution with Test&Set Waste CPU time (busy waiting by all threads) Low priority threads may never get a chance to run (starvation possible because other threads always grabs the lock, but can be lucky…): No Bounded Waiting ( a MUTEX criteria) No fairness, no order, random who gets access Acquire(lock) { while (TAS(lock)) ; } Release(lock) { lock = FALSE; } {TAS := lock; lock := TRUE;} INITIALLY: Lock := FALSE; /* OPEN */ Spin until lock = open TAS (lock):

Test&Set with Minimal Busy Waiting Two levels: Get inside a mutex, then check resource availability (and block (remember to open mutex!) or not). Still busy wait, but only for a short time Works with multiprocessors Acquire(lock) { while (TAS(lock.guard)) ; if (lock.value) { enqueue the thread; block and lock.guard:=OPEN; %Starts here after a Release() } lock.value:=CLOSED; lock.guard:=OPEN; } Release(lock) { while (TAS(lock.guard)) ; if (anyone in queue) { dequeue a thread; make it ready; } else lock.value:=OPEN; lock.guard:=OPEN; } CLOSED = TRUE OPEN = FALSE

A Solution without Busy Waiting? BUT: No mutual exclusion on the thread queue for each lock: queue is shared resource Need to solve another mutual exclusion problem Is there anything wrong with using this at the user level? Performance “Block”?? Acquire(lock) { while (TAS(lock)) { enqueue the thread; block; } Release(lock) { if (anyone in queue) { dequeue a thread; make it ready; } else lock:=OPEN; }

Different Ways of Spinning Perform TAS only when lock.guard is likely to be cleared –TAS is expensive while (TAS(lock.guard)) ; while (TAS(lock.guard)) { while (lock.guard) ; } Always execute TAS

Using System Call Block/Unblock Block/Unblock are implemented as system calls How would you implement them? –Minimal waiting solution Acquire(lock) { while (TAS(lock)) Block( lock ); } Release(lock) { lock = 0; Unblock( lock ); }

Block and Unblock Block (lock) { insert (current, lock_queue, last); goto scheduler (; } Unblock (lock) { insert (out (lock_queue, first), Ready_Queue, last); goto scheduler; } Ready_Queue lock_queue Current