Synchronization I. Jo, Heeseung

Similar documents
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

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

Lecture 5: Synchronization w/locks

Dealing with Issues for Interprocess Communication

CS 153 Design of Operating Systems Winter 2016

CSE 153 Design of Operating Systems

Synchronization Spinlocks - Semaphores

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

CSE 153 Design of Operating Systems Fall 2018

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

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

Dept. of CSE, York Univ. 1

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

Synchronization for Concurrent Tasks

Synchronization Principles

Chapter 6: Process Synchronization

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

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

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

Locks. Dongkun Shin, SKKU

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

Synchronization. Dr. Yingwu Zhu

Chapter 6: Process Synchronization

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

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

Synchronization COMPSCI 386

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

Advance Operating Systems (CS202) Locks Discussion

Process/Thread Synchronization

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

Problem Set 2. CS347: Operating Systems

Interprocess Communication By: Kaushik Vaghani

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

Process/Thread Synchronization

CHAPTER 6: PROCESS SYNCHRONIZATION

Lesson 6: Process Synchronization

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

Process Synchronization

IV. Process Synchronisation

Chapter 6: Process Synchronization

PROCESS SYNCHRONIZATION

Concurrency: Mutual Exclusion (Locks)

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

CS 153 Design of Operating Systems Winter 2016

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

[537] Locks. Tyler Harter

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

CS420: Operating Systems. Process Synchronization

Process Synchronization

Process Coordination

Mutex Implementation

Chapter 6 Process Synchronization

Process Synchronization

Chapter 5 Asynchronous Concurrent Execution

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

CS370 Operating Systems

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

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

CS 537 Lecture 11 Locks

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

Process Synchronization

Synchronization 1. Synchronization

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

CS3733: Operating Systems

Module 6: Process Synchronization

CSE 153 Design of Operating Systems

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

Lecture 9: Midterm Review

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

Operating Systems. Sina Meraji U of T

Concurrency: Mutual Exclusion and

Process Synchronization

Chapter 5: Process Synchronization

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

Systèmes d Exploitation Avancés

Chapter 6: Process Synchronization. Module 6: Process Synchronization

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

CS533 Concepts of Operating Systems. Jonathan Walpole

Operating Systems. Synchronisation Part I

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

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

Concurrent Processes Rab Nawaz Jadoon

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

CSCI [4 6] 730 Operating Systems. Example Execution. Process [& Thread] Synchronization. Why does cooperation require synchronization?

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

Process Synchronization (Part I)

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

Concurrency: Locks. Announcements

Chapter 6: Process Synchronization

Process Synchronization

Multiprocessors and Locking

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Multiprocessor Systems. COMP s1

CMSC421: Principles of Operating Systems

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

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Mutual Exclusion and Synchronization

Transcription:

Synchronization I Jo, Heeseung

Today's Topics Synchronization problem Locks 2

Synchronization Threads cooperate in multithreaded programs To share resources, access shared data structures Also, to coordinate their execution 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 execution (Note) This also applies to processes, not just threads - And it also applies across machines in a distributed system 3

The Classic Example (1) Withdraw money from a bank account Suppose you and your girl(boy) friend share a bank account with a balance of 1,000,000 won What happens if both go to separate ATM machines, and simultaneously withdraw 100,000 won from the account? int withdraw (account, amount) { balance = get_balance (account); balance = balance - amount; put_balance (account, balance); return balance; 4

The Classic Example (2) Interleaved schedules Represent the situation by creating a separate thread for each person to do the withdrawals The execution of the two threads can be interleaved, assuming preemptive scheduling: Execution sequence as seen by CPU balance = get_balance (account); balance = balance - amount; balance = get_balance (account); balance = balance - amount; put_balance (account, balance); put_balance (account, balance); Context switch Context switch 5

Synchronization Problem Problem Two concurrent threads (or processes) access a shared resource without any synchronization Creates a race condition: - The situation where several processes access and manipulate shared data concurrently - The result is non-deterministic and depends on timing Need mechanisms for controlling access to shared resources - So that we can reason about the operation of programs Synchronization is necessary for any shared data structure - buffers, queues, lists, etc. 6

Sharing Resources Between threads 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 static data segment, accessible by any thread Dynamic objects are shared - Stored in the heap, shared through the pointers Between processes Shared-memory objects, files, etc. are shared 7

Critical Sections (1) Critical sections Parts of the program that access shared resources - Shared files, shared memory(variable), etc. Use mutual exclusion to synchronize access to shared resources in critical sections - 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 Otherwise, critical sections can lead to race conditions - The final result depends on the sequence of execution of the processes int withdraw (account, amount) { balance = get_balance (account); balance = balance - amount; put_balance (account, balance); return balance; 8

Critical Sections (2) Requirements Mutual exclusion - At most one thread is in the critical section Progress - If thread T is inside the critical section, T must finish the critical section within reasonable time - 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 Performance - The overhead of entering and exiting the critical section is small with respect to the work being done within it 9

Critical Sections (3) Mechanisms for building critical 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 Messages e.g. Java "synchronized" - Simple model of communication and synchronization based on (atomic) transfer of data across a channel - Direct application to distributed systems 10

Locks Locks A lock is an object (in memory) that provides the following two operations: - acquire(): wait until lock is free, then grab it - release(): unlock, and wake up any thread waiting in acquire() Using locks - Lock is initially free - Call acquire() before entering a critical section, and release() after leaving it - 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 Locks can spin (a spinlock) or block (a mutex) 11

Using Locks A S1 S2 S3 R int withdraw (account, amount) { acquire (lock); balance = get_balance (account); balance = balance - amount; put_balance (account, balance); release (lock); return balance; Critical section Thread T1 Thread T2 A S1 S2 S3 R A S1 S2 S3 R 13

Implementing Locks (1) An initial attempt struct lock { int held = 0; void acquire (struct lock *l) { while (l->held); l->held = 1; void release (struct lock *l) { l->held = 0; Does this work? The caller "busy-waits", or spins for locks to be released, hence spinlocks int withdraw (account, amount) { acquire (lock); balance = get_balance (account); balance = balance - amount; put_balance (account, balance); release (lock); return balance; 14

Implementing Locks (2) Problem Implementation of locks has a critical section, too! - The acquire/release must be atomic - A recursion, huh? Atomic operation - Executes as though it could not be interrupted - Code that executes "all or nothing" 15

Implementing Locks (3) Solutions Software-only algorithms - Dekker's algorithm (1962) - Peterson's algorithm (1981) - Lamport's Bakery algorithm for more than two processes (1974) Hardware atomic instructions - Test-and-set, compare-and-swap, etc. Disable/reenable interrupts - To prevent context switches 16

Software-only Algorithms Wrong algorithm Mutual exclusion? Progress? Thread 0... acquire(0) do something release(0) Thread 1... acquire(1) do something release(1) int interested[2]; interested[0]=false; interested[1]=false; void acquire (int process) { int other = 1 - process; interested[process] = TRUE; while (interested[other]); void release (int process) { interested[process] = FALSE; void acquire (int process) { int other = 1 - process; interested[process] = TRUE; while (interested[other]); void release (int process) { interested[process] = FALSE; 17

Peterson's Algorithm Solves the critical section problem for two processes int turn; int interested[2]; interested[0]=false; interested[1]=false; void acquire (int process) { int other = 1 process; interested[process] = TRUE; turn = other; while (interested[other] && turn == other); void release (int process) { interested[process] = FALSE; void acquire (int process) { int other = 1 process; interested[process] = TRUE; turn = other; while (interested[other] && turn == other); void release (int process) { interested[process] = FALSE; 18

Atomic Instructions (1) Test-and-Set Mostly supported by H/W int TestAndSet (int *v) { int rv = *v; *v = 1; return rv; Atomic Using Test-and-Set instruction void struct lock { int value = 0; void acquire (struct lock *l) { while (TestAndSet (&l->value)); void release (struct lock *l) { l->value = 0; 21

Atomic Instructions (2) Swap Mostly supported by H/W void Swap (int *v1, int *v2) { int temp = *v1; *v1 = *v2; *v2 = temp; Atomic Using Swap instruction void struct lock { int value = 0; void acquire (struct lock *l) { int key = 1; while (key == 1) Swap(&l->value, &key); void release (struct lock *l) { l->value = 0; 22

Problems with Spinlocks Spinlocks Horribly wasteful! - If a thread is spinning on a lock, the thread holding the lock cannot make progress - The longer the critical section, the longer the spin - CPU cycle is wasted - Greater the chances for lock holder to be interrupted through involuntary context switch Only want to use spinlock as primitives to build higherlevel synchronization constructs 24

Disabling Interrupts (1) Implementing locks by disabling interrupts void acquire (struct lock *l) { cli(); // local_irq_disable(), disable interrupts; void release (struct lock *l) { sti(); // local_irq_enable(), enable interrupts; Disabling interrupts - Blocks notification of external events - No context switch (e.g., timer) There is no state associate with the lock Can two threads disable interrupts simultaneously? 25

Disabling Interrupts (2) What's wrong? Only available to kernel - Why not have the OS support these as system calls? Insufficient on a multiprocessor - Back to atomic instructions What if the critical section is long? - Can miss or delay important events (e.g., timer, I/O) Like spinlocks, only use to implement higher-level synchronization primitives 26

Summary Implementing locks Software-only algorithms Hardware atomic instructions Disable/reenable interrupts Spinlocks and disabling interrupts are primitive synchronization mechanisms They are used to build higher-level synchronization constructs 27