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

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

Recap. Mutual exclusion. Peterson s algorithm. Synchronization instructions. s/w solution Assume program order execution. Test&set

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

OS Structure. User mode/ kernel mode. System call. Other concepts to know. Memory protection, privileged instructions

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works?

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

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

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani

Synchronization Spinlocks - Semaphores

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

Synchronization I. Jo, Heeseung

Process Synchronization

Synchronization for Concurrent Tasks

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

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

Chapter 6: Process Synchronization

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

Review: Easy Piece 1

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

Process Synchronization

CHAPTER 6: PROCESS SYNCHRONIZATION

Systèmes d Exploitation Avancés

CSCI 447 Operating Systems Filip Jagodzinski

Lesson 6: Process Synchronization

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Process Synchronization (Part I)

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

CS420: Operating Systems. Process Synchronization

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

CS370 Operating Systems

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

Synchronization Principles

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

Chapter 6: Process Synchronization

Dept. of CSE, York Univ. 1

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

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Synchronization Principles I

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

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

Review: Thread package API

Process Synchronization

Process/Thread Synchronization

Today: Synchronization. Recap: Synchronization

Process/Thread Synchronization

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

Review: Thread package API

Chapter 6: Process Synchronization

CS3733: Operating Systems

Review: Thread package API

Synchronization for Concurrent Tasks

Chapter 5: Process Synchronization

Process Synchronization

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

Process & Thread Management II CIS 657

Process Synchronization

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

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS5460: Operating Systems

Administrivia. Review: Thread package API. Program B. Program A. Program C. Correct answers. Please ask questions on Google group

C09: Process Synchronization

COMP 3430 Robert Guderian

Synchronization COMPSCI 386

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

Process Coordination

Chapter 5: Process Synchronization

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

Chapter 5: Process Synchronization

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

Last Class: Synchronization

Interprocess Communication By: Kaushik Vaghani

Dealing with Issues for Interprocess Communication

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

CS370 Operating Systems

CS 153 Design of Operating Systems Winter 2016

Program A. Review: Thread package API. Program B. Program C. Correct answers. Correct answers. Q: Can both critical sections run?

Synchronising Threads

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018

Process Synchronization

IV. Process Synchronisation

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Process Synchronization

Chapter 6 Process Synchronization

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

Locks and semaphores. Johan Montelius KTH

[537] Locks. Tyler Harter

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

PROCESS SYNCHRONIZATION

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

CS 537 Lecture 11 Locks

Concurrency: Mutual Exclusion (Locks)

Concurrency: Locks. Announcements

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Transcription:

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

What is it? Recap: Thread Independent flow of control What does it need (thread private)? Stack What for? Lightweight programming construct for concurrent activities How to implement? Kernel thread vs. user thread 2

Recap: Process vs. Thread Figure source: https://computing.llnl.gov/tutorials/pthreads/ 3

Recap: Multi-threads vs. Multiprocesses Multi-processes (+) protection (-) performance (?) Process-per-tab Multi-threads (+) performance (-) protection Single-process multi-threads 4

Mutual exclusion Agenda Peterson s algorithm (Software) Synchronization instructions (Hardware) Spinlock High-level synchronization mechanisms Mutex Semaphore Monitor 5

Producer/Consumer Producer Thread Buffer[10] Consumer Thread 6

Producer/Consumer Producer while (true) /* wait if buffer full */ while (counter == 10); /* produce data */ buffer[in] = sdata; in = (in + 1) % 10; Consumer while (true) /* wait if buffer empty */ while (counter == 0); /* consume data */ sdata = buffer[out]; out = (out + 1) % 10; /* update number of items in buffer */ counter++; /* update number of items in buffer */ counter--; 7

Producer/Consumer Producer while (true) /* wait if buffer full */ while (counter == 10); /* produce data */ buffer[in] = sdata; in = (in + 1) % 10; Consumer while (true) /* wait if buffer empty */ while (counter == 0); /* consume data */ sdata = buffer[out]; out = (out + 1) % 10; /* update number of items in buffer */ R1 = load (counter); R1 = R1 + 1; counter = store (R1); /* update number of items in buffer */ R2 = load (counter); R2 = R2 1; counter = store (R2); 8

Check Yourself int count = 0; int main() count = count + 1; return count; $ gcc O2 S sync.c movl addl movl count(%rip), %eax $1, %eax %eax, count(%rip)

Race Condition Initial condition: counter = 5 Thread 1 Thread 2 R1 = load (counter); R1 = R1 + 1; counter = store (R1); R2 = load (counter); R2 = R2 1; counter = store (R2); What are the possible outcome? 10

Race Condition Initial condition: counter = 5 R1 = load (counter); R1 = R1 + 1; counter = store (R1); R2 = load (counter); R2 = R2 1; counter = store (R2); R1 = load (counter); R1 = R1 + 1; R2 = load (counter); R2 = R2 1; counter = store (R1); counter = store (R2); R1 = load (counter); R1 = R1 + 1; R2 = load (counter); R2 = R2 1; counter = store (R2); counter = store (R1); counter = 5 counter = 4 counter = 6 Why this happens? 11

Race Condition A situation when two or more threads read and write shared data at the same time Correctness depends on the execution order Thread 1 Thread 2 R1 = load (counter); R1 = R1 + 1; counter = store (R1); read write R2 = load (counter); R2 = R2 1; counter = store (R2); How to prevent race conditions? 12

Critical Section Code sections of potential race conditions Thread 1 Thread 2 Do something.. R1 = load (counter); R1 = R1 + 1; counter = store (R1);... Do something Do something.. R2 = load (counter); R2 = R2 1; counter = store (R2);.. Do something Critical sections 13

Solution Requirements Mutual Exclusion If a thread executes its critical section, no other threads can enter their critical sections Progress If no one executes a critical section, someone can enter its critical section Bounded waiting Waiting (time/number) must be bounded 14

Simple Solution (?): Use a Flag // wait while (in_cs) ; // enter critical section in_cs = true; Do something // exit critical section in_cs = false; T1 while(in_cs); in_cs = true; //enter T2 while(in_cs); in_cs = true; //enter Mutual exclusion is not guaranteed 15

Peterson s Solution Software solution (no h/w support) Two process solution Multi-process extension exists The two processes share two variables: int turn; The variable turn indicates whose turn it is to enter the critical section Boolean flag[2] The flag array is used to indicate if a process is ready to enter the critical section. 16

Peterson s Solution Thread 1 Thread 2 do flag[0] = TRUE; turn = 1; while (flag[1] && turn==1) ; // critical section flag[0] = FALSE; // remainder section while (TRUE) do flag[1] = TRUE; turn = 0; while (flag[0] && turn==0) ; // critical section flag[1] = FALSE; // remainder section while (TRUE) Solution meets all three requirements Mutual exclusion: P0 and P1 cannot be in the critical section at the same time Progress: if P0 does not want to enter critical region, P1 does no waiting Bounded waiting: process waits for at most one turn 17

Peterson s Solution Limitations Only supports two processes generalizing for more than two processes has been achieved, but not very efficient Assumes LOAD and STORE instructions are atomic In reality, no guarantees Assumes memory accesses are not reordered compiler re-orders instructions (gcc O2, -O3, ) Out-of-order processor re-orders instructions 18

Reordering by the CPU Initially X = Y = 0 Thread 0 Thread 1 Thread 0 Thread 1 Possible values of R1 and R2? 0,1 1,0 1,1 X = 1 R1 = Y Y = 1 R2 = X 0,0 possible on PC R1 = Y X = 1 R2 = X Y = 1 19

Summary Peterson s algorithm Software-only solution Pros Turn based No hardware support Satisfy all requirements Cons Mutual exclusion, progress, bounded waiting Complicated Assume program order May not work on out-of-order processors 20

Race condition Recap A situation when two or more threads read and write shared data at the same time Correctness depends on the execution order Critical section Code sections of potential race conditions Mutual exclusion If a thread executes its critical section, no other threads can enter their critical sections 21

Recap Peterson s algorithm Software-only solution Pros Turn based No hardware support Satisfy all requirements Cons Mutual exclusion, progress, bounded waiting Complicated Assume program order May not work on out-of-order processors 22

Today Hardware support Synchronization instructions Lock Spinlock Mutex 23

General solution Lock Protect critical section via a lock Acquire on enter, release on exit do acquire lock; critical section release lock; remainder section while(true); 24

How to Implement a Lock? Unicore processor No true concurrency one thread at a time Threads are interrupted by the OS scheduling events: timer interrupt, device interrupts Disabling interrupt Threads can t be interrupted do disable interrupts; critical section enable interrupts; remainder section while(true); 25

How to Implement a Lock? Multicore processor True concurrency More than one active threads sharing memory Disabling interrupts don t solve the problem More than one threads are executing at a time Hardware support Synchronization instructions Atomic test&set instruction Atomic compare&swap instruction What do we mean by atomic? All or nothing 26

Pseudo code TestAndSet Instruction boolean TestAndSet (boolean *target) boolean rv = *target; *target = TRUE; return rv: 27

Spinlock using TestAndSet int mutex; init_lock (&mutex); do lock (&mutex); critical section unlock (&mutex); remainder section while(true); void init_lock (int *mutex) *mutex = 0; void lock (int *mutex) while(testandset(mutex)) ; void unlock (int *mutex) *mutex = 0; 28

CAS (Compare & Swap) Instruction Pseudo code int CAS(int *value, int oldval, int newval) int temp = *value; if (*value == oldval) *value = newval; return temp; 29

Spinlock using CAS int mutex; init_lock (&mutex); do lock (&mutex); critical section unlock (&mutex); remainder section while(true); void init_lock (int *mutex) *mutex = 0; void lock (int *mutex) while(cas(&mutex, 0, 1)!= 0); void unlock (int *mutex) *mutex = 0; 30

What s Wrong With Spinlocks? Very wasteful Waiting thread continues to use CPU cycles While doing absolutely nothing but wait 100% CPU utilization, but no useful work done Power consumption, fan noise, Useful when You hold the lock only briefly Otherwise A better solution is needed 31

Mutex Blocking Lock Instead of spinning Let the thread sleep There can be multiple waiting threads In the meantime, let other threads use the CPU When the lock is released, wake-up one thread Pick one if there multiple threads were waiting 32

void mutex_init (mutex_t *lock) lock->value = 0; list_init(&lock->wait_list); spin_lock_init(&lock->wait_lock); More reading: mutex.c in Linux Thread waiting list To protect waiting list void mutex_lock (mutex_t *lock) while(testandset(&lock->value)) void mutex_unlock (mutex_t *lock) lock->value = 0; 33

void mutex_init (mutex_t *lock) lock->value = 0; list_init(&lock->wait_list); spin_lock_init(&lock->wait_lock); void mutex_lock (mutex_t *lock) while(testandset(&lock->value)) current->state = WAITING; list_add(&lock->wait_list, current); schedule(); void mutex_unlock (mutex_t *lock) lock->value = 0; More reading: mutex.c in Linux Thread waiting list To protect waiting list Thread state change Add the current thread to the waiting list Sleep or schedule another thread 34

void mutex_init (mutex_t *lock) lock->value = 0; list_init(&lock->wait_list); spin_lock_init(&lock->wait_lock); void mutex_lock (mutex_t *lock) spin_lock(&lock->wait_lock); while(testandset(&lock->value)) current->state = WAITING; list_add(&lock->wait_list, current); schedule(); spin_unlock(&lock->wait_lock); void mutex_unlock (mutex_t *lock) lock->value = 0; More reading: mutex.c in Linux Thread waiting list To protect waiting list Thread state change Add the current thread to the waiting list Sleep or schedule another thread 35

void mutex_init (mutex_t *lock) lock->value = 0; list_init(&lock->wait_list); spin_lock_init(&lock->wait_lock); void mutex_lock (mutex_t *lock) spin_lock(&lock->wait_lock); while(testandset(&lock->value)) current->state = WAITING; list_add(&lock->wait_list, current); spin_unlock(&lock->wait_lock); schedule(); spin_lock(&lock->wait_lock); spin_unlock(&lock->wait_lock); void mutex_unlock (mutex_t *lock) lock->value = 0; More reading: mutex.c in Linux Thread waiting list To protect waiting list Thread state change Add the current thread to the waiting list Sleep or schedule another thread 36

void mutex_init (mutex_t *lock) lock->value = 0; list_init(&lock->wait_list); spin_lock_init(&lock->wait_lock); void mutex_lock (mutex_t *lock) spin_lock(&lock->wait_lock); while(testandset(&lock->value)) current->state = WAITING; list_add(&lock->wait_list, current); spin_unlock(&lock->wait_lock); schedule(); spin_lock(&lock->wait_lock); spin_unlock(&lock->wait_lock); More reading: mutex.c in Linux Thread waiting list To protect waiting list Thread state change Add the current thread to the waiting list Sleep or schedule another thread void mutex_unlock (mutex_t *lock) lock->value = 0; if (!list_empty(&lock->wait_list)) wake_up_process(&lock->wait_list) Someone is waiting for the lock Wake-up a waiting thread 37

void mutex_init (mutex_t *lock) lock->value = 0; list_init(&lock->wait_list); spin_lock_init(&lock->wait_lock); void mutex_lock (mutex_t *lock) spin_lock(&lock->wait_lock); while(testandset(&lock->value)) current->state = WAITING; list_add(&lock->wait_list, current); spin_unlock(&lock->wait_lock); schedule(); spin_lock(&lock->wait_lock); spin_unlock(&lock->wait_lock); More reading: mutex.c in Linux Thread waiting list To protect waiting list Thread state change Add the current thread to the waiting list Sleep or schedule another thread void mutex_unlock (mutex_t *lock) spin_lock(&lock->wait_lock); lock->value = 0; if (!list_empty(&lock->wait_list)) wake_up_process(&lock->wait_list) spin_unlock(&lock->wait_lock); Someone is waiting for the lock Wake-up a waiting thread 38

void mutex_init (mutex_t *lock) lock->value = 0; list_init(&lock->wait_list); spin_lock_init(&lock->wait_lock); T1 More reading: mutex.c in Linux T2 void mutex_lock (mutex_t *lock) while(testandset(&lock->value)) current->state = WAITING; spin_lock(&lock->wait_lock); list_add(&lock->wait_list, current); spin_unlock(&lock->wait_lock); schedule(); Correct? void mutex_unlock (mutex_t *lock) spin_lock(&lock->wait_lock); lock->value = 0; if (!list_empty(&lock->wait_list)) wake_up_process(&lock->wait_list) spin_unlock(&lock->wait_lock); mutex_unlock spin_lock() lock->value = 0 // list is empty // do nothing spin_unlock() mutex_lock lock->value = 1 spin_lock() list_add() spin_unlock() schedule() Q. Who will wake-up T2? A. Nobody!!!

Summary Synchronization instructions test&set, compare&swap All or nothing Spinlock Spin on wait Good for short critical section but can be wasteful Mutex Block (sleep) on wait Good for long critical section but bad for short one 40