More Threads and Synchronization

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Midterm review Ding Yuan
Advertisements

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.
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.
Critical Sections and Race Conditions. Administrivia Install and build Nachos if you haven’t already.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
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.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Welcome to the World of Nachos CPS 110 Spring 2004 Discussion Session 1.
Threads and Concurrency. A First Look at Some Key Concepts kernel The software component that controls the hardware directly, and implements the core.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Race Conditions Defined 1. Every data structure defines invariant conditions. defines the space of possible legal states of the structure defines what.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSE 451: Operating Systems Section 5 Midterm review.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
1 Review of Process Mechanisms. 2 Scheduling: Policy and Mechanism Scheduling policy answers the question: Which process/thread, among all those ready.
Introduction to Operating Systems and Concurrency.
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.
CPS110: Threads review and wrap up Landon Cox February 11, 2009.
Administrivia Nachos guide and Lab #1 are on the web. Form project teams of 2-3. Lab #1 due February 5. Synchronization.
CSE 153 Design of Operating Systems Winter 2015 Lecture 5: Synchronization.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
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.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
Tutorial 2: Homework 1 and Project 1
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Processes and threads.
Process concept.
CSE 120 Principles of Operating
CPS110: Threads review and wrap up
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Synchronization.
Semaphores and Condition Variables
Chapter 5: Process Synchronization
COT 5611 Operating Systems Design Principles Spring 2014
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Jonathan Walpole Computer Science Portland State University
Thread Implementation Issues
COT 5611 Operating Systems Design Principles Spring 2012
Architectural Support for OS
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Implementing Mutual Exclusion
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
CS510 Operating System Foundations
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Architectural Support for OS
Chapter 6: Synchronization Tools
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
CSE 542: Operating Systems
CSE 542: Operating Systems
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

More Threads and Synchronization

Administrivia Lab #1 is due at midnight. Carefully follow submission instructions in the Project Guide. E-mail me your project writeup and group info before midnight. You may continue to work on projects after the deadline for less-than-full credit (tell us, and put limitations in your writeup). Sign up for Lab #1 demos on the Web. Sign up before you submit, and cc: your demoee on writeup. All demos meet (by default) in D-LSRC in third-floor fishbowl. Reading: Nutt: chapter on synchronization. Birrell: “An Introduction to Programming with Threads”

Implementing Threads in a Library The Nachos library implements user-level threads. coroutines no special support needed from the kernel (use any Unix) thread creation and context switch are fast (no syscall) defines its own thread model and scheduling policies readyList data while(1) { t = get next ready thread; scheduler->Run(t); }

Thread Context Switch program switch in switch out address space common runtime x program code library data R0 1. save registers CPU Rn y PC x stack SP y registers 2. load registers stack high “memory”

A Nachos Context Switch Save current stack pointer and caller’s return address in old thread object. /* * Save context of the calling thread (old), restore registers of * the next thread to run (new), and return in context of new. */ switch/MIPS (old, new) { old->stackTop = SP; save RA in old->MachineState[PC]; save callee registers in old->MachineState restore callee registers from new->MachineState RA = new->MachineState[PC]; SP = new->stackTop; return (to RA) } Caller-saved registers (if needed) are already saved on the thread’s stack. Caller-saved regs restored automatically on return. Switch off of old stack and back to new stack. Return to procedure that called switch in new thread.

Blocking in Sleep An executing thread may request some resource or action that causes it to block or sleep awaiting some event. passage of a specific amount of time (a pause request) completion of I/O to a slow device (e.g., keyboard or disk) release of some needed resource (e.g., memory) In Nachos, threads block by calling Thread::Sleep. A sleeping thread cannot run until the event occurs. The blocked thread is awakened when the event occurs. E.g., Wakeup or Nachos Scheduler::ReadyToRun(Thread* t) In an OS, threads or processes may sleep while executing in the kernel to handle a system call or fault.

Thread States and Transitions running Thread::Yield (voluntary or involuntary) Thread::Sleep (voluntary) Scheduler::Run blocked ready Scheduler::ReadyToRun (“wakeup”)

CPU Scheduling 101 The CPU scheduler makes a sequence of “moves” that determines the interleaving of threads. Programs use synchronization to prevent “bad moves”. …but otherwise scheduling choices appear (to the program) to be nondeterministic. The scheduler’s moves are dictated by a scheduling policy. interrupt current thread or wait for it to block/yield/terminate blocked threads readyList Wakeup or ReadyToRun GetNextToRun() SWITCH()

Questions about Nachos Context Switches Can I trace the stack of a thread that has switched out and is not active? What will I see? (a thread in the READY or BLOCKED state) What happens on the first switch into a newly forked thread? Thread::Fork (actually StackAllocate) sets up for first switch. What happens when the thread’s main procedure returns? When do we delete a dying thread’s stack? How does Nachos know what the current thread is?

Debugging with Threads Lab 1: demonstrate races with the Nachos List class. Some will result in a crash; know how to analyze them. > gdb nachos [or ddd] (gdb) run program_arguments Program received signal SIGSEGV, Segmentation Fault. 0x10954 in function_name(function_args) at file.c:line_number (gdb) where Caution: gdb [ddd] is not Nachos-thread aware! A context switch will change the stack pointer. Before stepping: (gdb) break SWITCH

Avoiding Races #1 1. Identify critical sections, code sequences that: rely on an invariant condition being true; temporarily violate the invariant; transform the data structure from one legal state to another; or make a sequence of actions that assume the data structure will not “change underneath them”. 2. Never sleep or yield in a critical section. Voluntarily relinquishing control may allow another thread to run and “trip over your mess” or modify the structure while the operation is in progress.

Critical Sections in the Color Stack InitColorStack() { push(blue); push(purple); } PushColor() { if (s[top] == purple) { ASSERT(s[top-1] == blue); } else { ASSERT(s[top] == blue); ASSERT(s[top-1] == purple);

Avoiding Races #2 Is caution with yield and sleep sufficient to prevent races? No! Concurrency races may also result from: involuntary context switches (timeslicing) e.g., caused by the Nachos thread scheduler with -rs flag external events that asynchronously change the flow of control interrupts (inside the kernel) or signals/APCs (outside the kernel) physical concurrency (on a multiprocessor) How to ensure atomicity of critical sections in these cases? Synchronization primitives!

Synchronization 101 Synchronization constrains the set of possible interleavings: Threads can’t prevent the scheduler from switching them out, but they can “agree” to stay out of each other’s way. voluntary blocking or spin-waiting on entrance to critical sections notify blocked or spinning peers on exit from the critical section If we’re “inside the kernel” (e.g., the Nachos kernel), we can temporarily disable interrupts. no races from interrupt handlers or involuntary context switches a blunt instrument to use as a last resort Disabling interrupts is not an accepted synchronization mechanism! insufficient on a multiprocessor

Resource Trajectory Graphs Resource trajectory graphs (RTG) depict the thread scheduler’s “random walk” through the space of possible system states. Sm Sn So RTG for N threads is N-dimensional. Thread i advances along axis I. Each point represents one state in the set of all possible system states. cross-product of the possible states of all threads in the system (But not all states in the cross-product are legally reachable.)

Mutual Exclusion Race conditions can be avoiding by ensuring mutual exclusion in critical sections. Critical sections are code sequences that contribute to races. Every race (possible incorrect interleaving) involves two or more threads executing related critical sections concurrently. To avoid races, we must serialize related critical sections. Never allow more than one thread in a critical section at a time. 1. BAD 2. interrupted critsec BAD 3. GOOD

Locks Locks can be used to ensure mutual exclusion in conflicting critical sections. A lock is an object, a data item in memory. Methods: Lock::Acquire and Lock::Release. Threads pair calls to Acquire and Release. Acquire before entering a critical section. Release after leaving a critical section. Between Acquire/Release, the lock is held. Acquire does not return until any previous holder releases. Waiting locks can spin (a spinlock) or block (a mutex). A A R R

Portrait of a Lock in Motion

Example: Per-Thread Counts and Total /* shared by all threads */ int counters[N]; int total; /* * Increment a counter by a specified value, and keep a running sum. * This is called repeatedly by each of N threads. * tid is an integer thread identifier for the current thread. * value is just some arbitrary number. */ void TouchCount(int tid, int value) { counters[tid] += value; total += value; }

Using Locks: An Example int counters[N]; int total; Lock *lock; /* * Increment a counter by a specified value, and keep a running sum. */ void TouchCount(int tid, int value) { lock->Acquire(); counters[tid] += value; /* critical section code is atomic...*/ total += value; /* …as long as the lock is held */ lock->Release(); }

Reading Between the Lines of C /* counters[tid] += value; total += value; */ load counters, R1 ; load counters base load 8(SP), R2 ; load tid index shl R2, #2, R2 ; index = index * sizeof(int) add R1, R2, R1 ; compute index to array load 4(SP), R3 ; load value load (R1), R2 ; load counters[tid] add R2, R3, R2 ; counters[tid] += value store R2, (R1) ; store back to counters[tid] load total, R2 ; load total add R2, R3, R2 ; total += value store R2, total ; store total load add store load add store vulnerable between load and store of counters[tid]...but it’s non-shared. vulnerable between load and store of total, which is shared.

Relativity of Critical Sections 1. If a thread is executing a critical section, never permit another thread to enter the same critical section. Two executions of the same critical section on the same data are always “mutually conflicting” (assuming it modifies the data). 2. If a thread is executing a critical section, never permit another thread to enter a related critical section. Two different critical sections may be mutually conflicting. E.g., if they access the same data, and at least one is a writer. E.g., List::Add and List::Remove on the same list. 3. Two threads may safely enter unrelated critical sections. If they access different data or are reader-only.