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

Similar documents
Synchronization I. Jo, Heeseung

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

Dept. of CSE, York Univ. 1

CS 153 Design of Operating Systems Winter 2016

CSE 153 Design of Operating Systems

Synchronization for Concurrent Tasks

Synchronization Spinlocks - Semaphores

CSE 153 Design of Operating Systems Fall 2018

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

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

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

Chapter 6: Process Synchronization

Process Coordination

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

Chapter 6: Process Synchronization

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

Process Synchronization

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

Synchronization. Dr. Yingwu Zhu

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

Synchronization Principles

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

Locks. Dongkun Shin, SKKU

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Process Synchronization

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 7: Process Synchronization. Background. Illustration

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

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

Synchronization COMPSCI 386

Advance Operating Systems (CS202) Locks Discussion

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

Chapter 7: Process Synchronization. Background

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

Process/Thread Synchronization

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

Lesson 6: Process Synchronization

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

Concurrency: Mutual Exclusion and

Problem Set 2. CS347: Operating Systems

Chapter 6: Process Synchronization

Process/Thread Synchronization

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Process Synchronization

IV. Process Synchronisation

Interprocess Communication By: Kaushik Vaghani

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

CS420: Operating Systems. Process Synchronization

Mutex Implementation

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

CS370 Operating Systems

PROCESS SYNCHRONIZATION

CS 153 Design of Operating Systems Winter 2016

Chapter 6 Process Synchronization

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Concurrency: Mutual Exclusion (Locks)

Process Synchronization

Concurrent Processes Rab Nawaz Jadoon

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter 5 Asynchronous Concurrent Execution

[537] Locks. Tyler Harter

CS3733: Operating Systems

Process Synchronization

Last class: Today: CPU Scheduling. Start synchronization

Module 6: Process Synchronization

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

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Lecture 9: Midterm Review

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

Chapter 5: Process Synchronization

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

Chapter 7: Process Synchronization!

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

Synchronization 1. Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

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

Concurrency: Mutual Exclusion and Synchronization

Mutual Exclusion and Synchronization

CSE 153 Design of Operating Systems

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

Process Synchronization

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7)

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

Chapter 6: Process Synchronization

C09: Process Synchronization

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Process Synchronization(2)

CS370 Operating Systems

Process Synchronization

Systèmes d Exploitation Avancés

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

Models of concurrency & synchronization algorithms

Lecture #7: Shared objects and locks

CS533 Concepts of Operating Systems. Jonathan Walpole

Transcription:

Synchronization I Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

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,000won. What happens if both go to separate ATM machines, and simultaneously withdraw 100,000won 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. We need mechanisms for controlling access to shared resources in the face of concurrency. 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 Critical sections are parts of the program that access shared memory or shared files or other shared resources. We want to 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. 8

Critical Sections (2) 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. 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: Java synchronized Messages 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 12

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; The caller busy-waits, or spins for locks to be released, hence spinlocks. Does this work? 13

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. 14

Implementing Locks (3) Solutions Software-only algorithms Dekker s algorithm (1962) (cf. Exercises 6.1) 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 15

Software-only Algorithms Wrong algorithm Mutual exclusion? Progress? int interested[2]; void acquire (int process) { int other = 1 process; interested[process] = TRUE; while (interested[other]); void release (int process) { interested[process] = FALSE; 16

Peterson s Algorithm Solves the critical section problem for two processes int turn; int interested[2]; 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; 17

Bakery Algorithm (1) Multiple-process solution Before entering its critical section, process receives a sequence number. Holder of the smallest number enters the critical section If processes P i and P j receive the same number, if i < j, then P i is served first; else P j is served first. The numbering scheme always generates numbers in increasing order of enumeration; i.e. 1,2,3,3,3,4,4,5 18

Bakery Algorithm (2) int number[n]; int choosing[n]; #define EARLIER(a,b) \\ ((number[a] < number[b]) \\ (number[a] == number[b] && \\ (a) < (b))) int Findmax () { int i; int max = number[0]; for (i = 1; i < N; i++) if (number[i] > max) max = number[i]; return max; void acquire (int me) { int other; choosing[me] = TRUE; number[me] = Findmax() + 1; choosing[me] = FALSE; for (other=0; other<n; other++) { while (choosing[other]); while (number[other] && EARLIER(other, me)); void release (int me) { number[me] = 0; 19

Atomic Instructions (1) Test-and-Set int TestAndSet (int *v) { int rv = *v; *v = 1; return rv; 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; 20

Atomic Instructions (2) Swap void Swap (int *v1, int *v2) { int temp = *v1; *v1 = *v2; *v2 = temp; 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; 21

Atomic Instructions (3) Locks using Test-and-Set with boundedwaiting struct lock { int value = 0; int waiting[n]; void acquire (struct lock *l, int me) { int key; waiting[me] = 1; key = 1; while (waiting[me] && key) key = TestAndSet (&l->value); waiting[me] = 0; void release (struct lock *l, int me) { int next = (me + 1) % N; while ((next!= me) &&!waiting[next]) next = (next + 1) % N; if (next == me) l->value = 0; else waiting[next] = 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 higher-level synchronization constructs. 23

Disabling Interrupts (1) Implementing locks by disabling interrupts void acquire (struct lock *l) { cli(); // disable interrupts; void release (struct lock *l) { sti(); // enable interrupts; Disabling interrupts blocks notification of external events that could trigger a context switch (e.g., timer) There is no state associate with the lock. Can two threads disable interrupts simultaneously? 24

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. 25

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. 26