Synchronization Mechanisms

Similar documents
Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Locks and semaphores. Johan Montelius KTH

POSIX Threads. Paolo Burgio

real time operating systems course

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

CS 153 Lab6. Kishore Kumar Pusukuri

Synchronization Primitives

Locks and semaphores. Johan Montelius KTH

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

recap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Multithreading Programming II

Real Time Operating Systems and Middleware

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

CSCE 313: Introduction to Computer Systems

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Process Synchronization (Part I)

Resource Access Control (2) Real-Time and Embedded Systems (M) Lecture 14

Posix Threads (Pthreads)

Synchroniza+on II COMS W4118

W4118 Operating Systems. Instructor: Junfeng Yang

Paralleland Distributed Programming. Concurrency

Week 3. Locks & Semaphores

The course that gives CMU its Zip! Concurrency II: Synchronization Nov 14, 2000

Synchronization II. q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

KING FAHD UNIVERSITY OF PETROLEUM & MINERALS. Information and Computer Science Department. ICS 431 Operating Systems. Lab # 9.

Condition Variables. Dongkun Shin, SKKU

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

Process Synchronization(2)

Threads need to synchronize their activities to effectively interact. This includes:

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

CSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010

Lecture 6. Process Synchronization

Final Examination (Fall 2016) (Section 1)

Condition Variables. Dongkun Shin, SKKU

Real Time Operating System Support for Concurrency

Process Synchronization(2)

Process Synchronization. studykorner.org

Synchronization 1. Synchronization

Synchronising Threads

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

ANSI/IEEE POSIX Standard Thread management

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

Introduction to parallel computing

More Types of Synchronization 11/29/16

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Concurrent Server Design Multiple- vs. Single-Thread

Network Programming TDC 561

Sistemi in tempo reale Anno accademico

CMSC421: Principles of Operating Systems

More Shared Memory Programming

Synchronization 1. Synchronization

Process Synchronization(2)

Threads. Jo, Heeseung

Synchronization. Semaphores implementation

Chapter 6: Process Synchronization

Introduction to OS Synchronization MOS 2.3

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Lecture 5 Threads and Pthreads II

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

POSIX PTHREADS PROGRAMMING

Synchronization I. Jo, Heeseung

SWEN-220 Mathematical Models of Software. Process Synchronization Critical Section & Semaphores

C09: Process Synchronization

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2

CS420: Operating Systems. Process Synchronization

Concurrency, Thread. Dongkun Shin, SKKU

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

Synchronization. Dr. Yingwu Zhu

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

[537] Semaphores. Tyler Harter

The mutual-exclusion problem involves making certain that two things don t happen at once. A non-computer example arose in the fighter aircraft of

Locks. Dongkun Shin, SKKU

PROCESS SYNCHRONIZATION

Interprocess Communication and Synchronization

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

Process Synchronization

Operating systems and concurrency (B08)

Solving the Producer Consumer Problem with PThreads

High-level Synchronization

Chapter 6 Process Synchronization

Multi-threaded Programming

PRINCIPLES OF OPERATING SYSTEMS

Operating System Labs. Yuanbin Wu

Synchronization COMPSCI 386

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

Operating Systems (2INC0) 2017/18

Concurrency. Chapter 5

Threads. Jo, Heeseung

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

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

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

Transcription:

Synchronization Mechanisms CSCI 4061 Introduction to Operating Systems Instructor: Abhishek Chandra Mutex Locks Enforce protection and mutual exclusion Condition variables Allow atomic checking of conditions Semaphores Provide more general forms of synchronization 2 Mutex Locks Mutex Lock: Protects access to a shared resource A thread locks a resource before accessing it Another thread will have to wait for the resource to be unlocked The first thread would unlock the resource after accessing it Mutex locks can be used to protect both shared resources and critical sections Mutex States Locked: A single thread holds the mutex Another thread trying to lock mutex will be blocked Queue of blocked threads Unlocked: No thread holds the mutex A thread trying to lock mutex will succeed and get control of the mutex 3 4 1

Mutex Operations Lock: Gain control of the mutex Get lock if free Block if already locked Unlock: Release the mutex Unblock a waiting thread if any Unblocked thread becomes new owner of mutex Trylock: Check for availability Lock if available, otherwise don t block All these operations are atomic Mutex Example Thread A: counter++; un counter=1; Thread B: counter--; un Whichever thread executes lock first gets to execute completely The other thread waits for its turn Ensures consistent value of counter 5 6 Mutex Properties Simple and efficient synchronization mechanism Typically meant to be held for short durations Useful for short critical sections. Examples: Modifying/reading a variable value Modifying pointers in a shared data linked list Provide exactly-one-at-a-time mutual exclusion POSIX Mutex: pthread_mutex_t Initialization: pthread_mutex_init Can also be set to PTHREAD_MUTEX_INITIALIZER Should be done exactly once Destruction: pthread_mutex_destroy Locking/Unlocking Operations: pthread_mutex_lock(pthread_mutex_t *mutex); pthread_mutex_unlock(pthread_mutex_t *mutex); pthread_mutex_trylock(pthread_mutex_t *mutex); 7 8 2

POSIX Mutex: Example Producer-Consumer Problem int counter=1; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; Thread A: pthread_mutex_lock(&mut); counter++; pthread_mutex_unlock(&mut); What s the critical section? How do we protect the critical section? 9 10 Producer-Consumer Synchronization: Using Mutex Locks Handling Empty Buffer: Using Mutex Locks un 11 What happens if buffer is empty? un 12 num_items++; un int num_items=0; while (num_items==0); num_items--; un 3

Conditional Execution Mutex locks: Control access to a shared variable or a code segment Each thread eventually accesses the variable or executes the code segment Typical waiting time is small What if we want to execute a code segment only under certain circumstances? The waiting time could be unbounded Conditional Execution: Example Condition to wait for: x==y Naïve approach: Busy Waiting while (x!=y) /* loop */; 13 14 Conditional Execution: Example Condition to wait for: x==y Naïve approach: Busy Waiting Problems: while (x!=y) /* loop */; Wastes CPU cycles Might prevent other threads from running, and even changing x and y Race conditions Conditional Execution: Another Approach Use a mutex lock Lock a mutex Test the condition x==y If true, unlock the mutex and continue If false, block and unlock the mutex Problem: In what order should we block and unlock the mutex? Block first: Mutex remains locked, some other thread cannot access x and y Unlock first: How to be notified about condition Need a simple way of testing and blocking 15 16 4

Condition Variables Provide atomic way of testing conditions and blocking if required Used in conjunction with a mutex Mutex used to protect access to shared data Condition variable used to signal possible satisfaction of condition, e.g.: x==y Condition Variables: Operations Uses a mutex lock wait: Atomically unlocks mutex and blocks Thread owns mutex when it returns from wait signal: Notifies a waiting thread about a condition signalall/broadcast: Notifies all waiting threads about a condition These operations are atomic 17 18 Condition Variables: Example while (x!=y) wait(cond, mut); do_something(); un cond_var cond; x=y; signal(cond); un Condition Variables: Usage wait operation: A thread must hold mutex when calling wait Blocking and releasing of mutex is an atomic operation done inside wait signal operation: A thread receiving signal may still wait to grab the mutex if not free It must check condition even after being signaled on condition variable 19 20 5

Producer-Consumer Synchronization: Using Mutex Locks Producer- Handling Empty Buffer Using Condition Variables un 21 What happens if buffer is empty? un 22 num_items++; signal(cond); un int num_items=0; cond_var cond; while (num_items==0) wait(cond, mut); num_items--; un POSIX Condition Variable: pthread_cond_t Initialization: pthread_cond_init Can also be set to PTHREAD_COND_INITIALIZER Should be done exactly once Destruction: pthread_cond_destroy Wait/signal Operations: pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); pthread_cond_timedwait(..., struct timespec *time); pthread_cond_signal(pthread_cond_t *cond); pthread_cond_broadcast(pthread_cond_t *cond); POSIX Condition Variable: Example pthread_cond_t cond=pthread_cond_initializer; pthread_mutex_t mut; pthread_mutex_lock(&mut); while (x!=y) pthread_cond_wait(&cond, &mut); do_something(); pthread_mutex_unlock(&mut); pthread_mutex_lock(&mut); x=y; pthread_cond_signal(&cond); pthread_mutex_unlock(&mut); 23 24 6

Semaphores Semaphore Operations 25 Generalization of mutex locks Mutex allows access to exactly one thread Semaphore allows controlled access to multiple threads Semaphore has a non-negative counter The value of the counter determines the access control and synchronization Can be thought of as a C struct struct int counter; thread_list list; 26 Wait Also called P (proberen), down, lock Either decrements counter or blocks if counter is 0 Signal Also called V (verhogen), up, unlock Either increments counter or wakes up a blocked thread if (S->counter > 0) S->counter--; else add to S->list and block; if (S->list!= NULL) remove thread from S->list else S->counter++; Semaphore Properties wait and signal are atomic operations OS implements these as critical sections Can be used for mutual exclusion or signaling Behavior depends on initial value of counter Controlling access to a Critical Section wait(s) Critical section signal(s) Each thread must call wait and signal in correct order How many threads can run in critical section if: Initial value of S->counter is 1 Initial value of S->counter is 10 Initial value of S->counter is 0 27 28 7

Synchronizing Execution Order Producer- Using Semaphores Process A: Statement 1; signal(s); Process B: wait(s); Statement 2; In which order would the threads run if: Initial value of S->counter is 0 Initial value of S->counter is 1 Can we do this kind of synchronization with mutex locks? Semaphore mut=1; wait(mut); num_items++; signal(mut); signal(items); int num_items=0; Semaphore items=0; wait(items); wait(mut); num_items--; signal(mut); 29 30 Do we need num_items? Bounded Buffer Problem Bounded Buffer: Using Semaphores Suppose Buffer is of finite size: capacity of N items What happens if buffer is full? Semaphore mut=1; wait(slots); wait(mut); signal(mut); signal(items); Semaphore items=0, slots=n; wait(items); wait(mut); signal(mut); signal(slots); 31 32 8

POSIX Semaphore: sem_t Initialization: sem_init(sem_t *sem, int pshared, unsigned value); sem: Semaphore variable pshared: 0 => sharing within process Non-zero => shared across processes value: initial value (must be non-negative) POSIX Semaphore: Operations Wait/signal: sem_wait(sem_t *sem); sem_post(sem_t *sem); sem_trywait(sem_t *sem); Destruction: sem_destroy(sem_t *sem); 33 34 POSIX Semaphore: Example sem_t sem; /* Initialize process-local semaphore with initial value=1 */ sem_init(&sem, 0, 1); sem_wait(&sem); /* Critical section */ sem_post(&sem); 35 9