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

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

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

Operating Systems. Synchronization

Synchronization I. Jo, Heeseung

Dealing with Issues for Interprocess Communication

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

Lecture 5: Synchronization w/locks

CS 153 Design of Operating Systems Winter 2016

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

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

CSE 153 Design of Operating Systems

CSE 153 Design of Operating Systems Fall 2018

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

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

CS 153 Design of Operating Systems Winter 2016

Advance Operating Systems (CS202) Locks Discussion

Synchronization COMPSCI 386

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

Synchronization. Dr. Yingwu Zhu

Problem Set 2. CS347: Operating Systems

Operating Systems. Sina Meraji U of T

CSE 153 Design of Operating Systems

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Locks. Dongkun Shin, SKKU

Concurrency: Mutual Exclusion (Locks)

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Dept. of CSE, York Univ. 1

Chapter 6: Process Synchronization

Synchronization Spinlocks - Semaphores

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

Multiprocessors and Locking

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

CS3733: Operating Systems

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

Chapter 6: Process Synchronization

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

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

Process & Thread Management II CIS 657

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

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

Synchronization Principles

Synchronization for Concurrent Tasks

Process/Thread Synchronization

IV. Process Synchronisation

Lesson 6: Process Synchronization

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

CS533 Concepts of Operating Systems. Jonathan Walpole

Lecture 9: Midterm Review

[537] Locks. Tyler Harter

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Process/Thread Synchronization

Interprocess Communication By: Kaushik Vaghani

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

CS420: Operating Systems. Process Synchronization

Systèmes d Exploitation Avancés

Threading and Synchronization. Fahd Albinali

CSE 410 Final Exam 6/09/09. Suppose we have a memory and a direct-mapped cache with the following characteristics.

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

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

CS 537 Lecture 11 Locks

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

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

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

CSE 153 Design of Operating Systems

CHAPTER 6: PROCESS SYNCHRONIZATION

CS 318 Principles of Operating Systems

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

What are they? How do we represent them? Scheduling Something smaller than a process? Threads Synchronizing and Communicating Classic IPC problems

Chapter 6: Process Synchronization

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

Last Class: Synchronization

Midterm Exam. October 20th, Thursday NSC

Chapter 6 Process Synchronization

CS370 Operating Systems

Concept of a process

Multiprocessor System. Multiprocessor Systems. Bus Based UMA. Types of Multiprocessors (MPs) Cache Consistency. Bus Based UMA. Chapter 8, 8.

Part II Process Management Chapter 6: Process Synchronization

Process Synchronization Mechanisms

Multiprocessor Systems. COMP s1

Synchronization 1. Synchronization

PROCESS SYNCHRONIZATION

Mutex Implementation

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Advanced Operating Systems (CS 202) Memory Consistency, Cache Coherence and Synchronization

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

Concurrency: Locks. Announcements

CS 153 Design of Operating Systems Spring 18

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

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

Process Synchronization

Programmazione di sistemi multicore

Concurrency: Signaling and Condition Variables

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

CSE 120 Principles of Operating Systems

Process Synchronization

C09: Process Synchronization

Synchronization

Transcription:

CSE 451: Operating Systems Winter 2005 Lecture 7 Synchronization Steve Gribble Synchronization Threads cooperate in multithreaded programs to share resources, access shared data structures e.g., threads accessing a memory cache in a web server also, to coordinate their execution e.g., a disk reader thread hands off a block to a network writer For correctness, we have to control this cooperation must assume threads interleave executions arbitrarily and at different rates scheduling is not under application writers control we control cooperation using synchronization enables us to restrict the interleaving of executions Note: this also applies to processes, not just threads and it also applies across machines in a distributed system 1/24/05 2005 Steve Gribble 2 1

Shared Resources We ll focus on coordinating access to shared resources basic problem: two concurrent threads are accessing a shared variable if the variable is read/modified/written by both threads, then access to the variable must be controlled otherwise, unexpected results may occur Over the next two lectures, we ll look at: mechanisms to control access to shared resources low level mechanisms like locks higher level mechanisms like mutexes, semaphores, monitors, and condition variables patterns for coordinating access to shared resources bounded buffer, producer-consumer, 1/24/05 2005 Steve Gribble 3 The classic example Suppose we have to implement a function to withdraw money from a bank account: int withdraw(account, amount) { return balance; Now suppose that you and your S.O. share a bank account with a balance of $100.00 what happens if you both go to separate ATM machines, and simultaneously withdraw $10.00 from the account? 1/24/05 2005 Steve Gribble 4 2

Example continued Represent the situation by creating a separate thread for each person to do the withdrawals have both threads run on the same bank mainframe: int withdraw(account, amount) { return balance; int withdraw(account, amount) { return balance; What s the problem with this? what are the possible balance values after this runs? 1/24/05 2005 Steve Gribble 5 Interleaved Schedules The problem is that the execution of the two threads can be interleaved, assuming preemptive scheduling: Execution sequence as seen by CPU context switch context switch What s the account balance after this sequence? who s happy, the bank or you? ;) 1/24/05 2005 Steve Gribble 6 3

The crux of the matter The problem is that two concurrent threads (or processes) access a shared resource (account) without any synchronization creates a race condition output is non-deterministic, depends on timing We need mechanisms for controlling access to shared resources in the face of concurrency so we can reason about the operation of programs essentially, re-introducing determinism Synchronization is necessary for any shared data structure buffers, queues, lists, hash tables, 1/24/05 2005 Steve Gribble 7 When are Resources Shared? Local variables are not shared refer to data on the stack, each thread has its own stack never pass/share/store a pointer to a local variable on another thread s stack Global variables are shared stored in the static data segment, accessible by any thread Dynamic objects are shared stored in the heap, shared if you can name it in C, can conjure up the pointer e.g. void *x = (void *) 0xDEADBEEF in Java, strong typing prevents this must pass references explicitly 1/24/05 2005 Steve Gribble 8 4

Mutual Exclusion We want to use mutual exclusion to synchronize access to shared resources Code that uses mutual exclusion to synchronize its execution is called a critical section only one thread at a time can execute in the critical section all other threads are forced to wait on entry when a thread leaves a critical section, another can enter 1/24/05 2005 Steve Gribble 9 Critical Section Requirements Critical sections have the following requirements mutual exclusion at most one thread is in the critical section progress if thread T is outside the critical section, then T cannot prevent thread S from entering the critical section bounded waiting (no starvation) if thread T is waiting on the critical section, then T will eventually enter the critical section assumes threads eventually leave critical sections performance the overhead of entering and exiting the critical section is small with respect to the work being done within it 1/24/05 2005 Steve Gribble 10 5

Mechanisms for Building Crit. Sections Locks very primitive, minimal semantics; used to build others Semaphores basic, easy to get the hang of, hard to program with Monitors high level, requires language support, implicit operations easy to program with; Java synchronized() as example Messages simple model of communication and synchronization based on (atomic) transfer of data across a channel direct application to distributed systems 1/24/05 2005 Steve Gribble 11 Locks A lock is a object (in memory) that provides the following two operations: acquire( ): a thread calls this before entering a critical section release( ): a thread calls this after leaving a critical section Threads pair up calls to acquire( ) and release( ) between acquire( ) and release( ), the thread holds the lock acquire( ) does not return until the caller holds the lock at most one thread can hold a lock at a time (usually) so: what can happen if the calls aren t paired? Two basic flavors of locks spinlock blocking (a.k.a. mutex ) 1/24/05 2005 Steve Gribble 12 6

Using Locks int withdraw(account, amount) { acquire(lock); release(lock); return balance; critical section acquire(lock) acquire(lock) release(lock); release(lock); What happens when green tries to acquire the lock? Why is the return outside the critical section? is this ok? 1/24/05 2005 Steve Gribble 13 Spinlocks How do we implement locks? Here s one attempt: struct lock { int held = 0; void acquire(lock) { while (lock->held); lock->held = 1; void release(lock) { lock->held = 0; the caller busy-waits, or spins for lock to be released, hence spinlock Why doesn t this work? where is the race condition? 1/24/05 2005 Steve Gribble 14 7

Implementing locks (continued) Problem is that implementation of locks has critical sections, too! the acquire/release must be atomic atomic == executes as though it could not be interrupted code that executes all or nothing Need help from the hardware atomic instructions test-and-set, compare-and-swap, disable/reenable interrupts to prevent context switches 1/24/05 2005 Steve Gribble 15 Spinlocks redux: Test-and-Set CPU provides the following as one atomic instruction: bool test_and_set(bool *flag) { bool old = *flag; *flag = True; return old; So, to fix our broken spinlocks, do: struct lock { int held = 0; void acquire(lock) { while(test_and_set(&lock->held)); void release(lock) { lock->held = 0; 1/24/05 2005 Steve Gribble 16 8

Problems with spinlocks Horribly wasteful! if a thread is spinning on a lock, the thread holding the lock cannot make process How did lock holder yield the CPU in the first place? calls yield( ) or sleep( ) involuntary context switch Only want spinlocks as primitives to build higherlevel synchronization constructs 1/24/05 2005 Steve Gribble 17 Disabling Interrupts An alternative: struct lock { void acquire(lock) { cli(); // disable interrupts void release(lock) { sti(); // reenable interupts Can two threads disable interrupts simultaneously? What s wrong with interrupts? only available to kernel (why? how can user-level use?) insufficient on a multiprocessor back to atomic instructions Like spinlocks, only use to implement higher-level synchronization primitives 1/24/05 2005 Steve Gribble 18 9