Week 3. Locks & Semaphores

Similar documents
Synchronization Primitives

Multithreading Programming II

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

Process Synchronization. studykorner.org

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

CS 550 Operating Systems Spring Concurrency Semaphores, Condition Variables, Producer Consumer Problem

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

POSIX Threads. Paolo Burgio

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

W4118 Operating Systems. Instructor: Junfeng Yang

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

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

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

real time operating systems course

Locks and semaphores. Johan Montelius KTH

POSIX Semaphores. Operations on semaphores (taken from the Linux man page)

Synchroniza+on II COMS W4118

CS 153 Design of Operating Systems Winter 2016

Synchronization. Semaphores implementation

Lecture 18. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture18/ $ cp r /home/hwang/cs375/lecture18/*.

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

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

CS 153 Lab6. Kishore Kumar Pusukuri

Dining Philosophers, Semaphores

Synchronization Mechanisms

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

Locks and semaphores. Johan Montelius KTH

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

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Synchronization. Dr. Yingwu Zhu

CMSC421: Principles of Operating Systems

Synchronization for Concurrent Tasks

Process Synchronization

Concurrency: a crash course

CSE 120 Principles of Operating Systems Spring 2016

Dealing with Issues for Interprocess Communication

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

CS 153 Design of Operating Systems Winter 2016

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

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

The University of Texas at Arlington

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

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

IV. Process Synchronisation

Process Synchronization(2)

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

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

Sistemi in tempo reale Anno accademico

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

Concurrent Processes Rab Nawaz Jadoon

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

IT 540 Operating Systems ECE519 Advanced Operating Systems

POSIX / System Programming

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

Process Synchronization(2)

The University of Texas at Arlington

UNIX Input/Output Buffering

EE458 - Embedded Systems Lecture 8 Semaphores

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

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

Announcements. Class feedback for mid-course evaluations Receive about survey to fill out until this Friday

Process Synchronization(2)

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

Lecture 8: September 30

POSIX Threads. HUJI Spring 2011

Lecture 6 (cont.): Semaphores and Monitors

Operating Systems. Synchronization

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

Processor speed. Concurrency Structure and Interpretation of Computer Programs. Multiple processors. Processor speed. Mike Phillips <mpp>

Concurrent Server Design Multiple- vs. Single-Thread

Chapter 6 Concurrency: Deadlock and Starvation

[537] Semaphores. Tyler Harter

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

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

Real Time Operating Systems and Middleware

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Synchronization. Before We Begin. Synchronization. Example of a Race Condition. CSE 120: Principles of Operating Systems. Lecture 4.

Lecture 6. Process Synchronization

Synchronization: Advanced

Concept of a process

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2

Concurrency and Synchronisation

Threading and Synchronization. Fahd Albinali

COMP346 Winter Tutorial 4 Synchronization Semaphores

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 120: Principles of Operating Systems. Lecture 4. Synchronization. October 7, Prof. Joe Pasquale

Concurrency. Chapter 5

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

Semaphores. Otto J. Anshus University of {Tromsø, Oslo}

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

Synchronising Threads

Today. Threads review Sharing Mutual exclusion Semaphores

Process Synchronization Mechanisms

Synchronization: Basics

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

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

Concurrency: Deadlock and Starvation

Interprocess Communication and Synchronization

Transcription:

Week 3 Locks & Semaphores

Synchronization Mechanisms Locks Very primitive constructs with minimal semantics Semaphores A generalization of locks Easy to understand, hard to program with Condition Variables Constructs used in implementing monitors (more on this later )

Locks Synchronization mechanisms with 2 operations: acquire(), and release() In simplest terms: an object associated with a particular critical section that you need to own if you wish to execute in that region Simple semantics to provide mutual exclusion: acquire(lock); //CRITICAL SECTION release(lock); Downsides: Can cause deadlock if not careful Cannot allow multiple concurrent accesses to a resource

POSIX Locks POSIX locks are called mutexes (since locks provide mutual exclusion ) A few calls associated with POSIX mutexes: pthread_mutex_init(mutex, attr) Initialize a mutex pthread_mutex_destroy(mutex) Destroy a mutex pthread_mutex_lock(mutex) Acquire the lock pthread_mutex_trylock(mutex) Try to acquire the lock (more on this later ) pthread_mutex_unlock(mutex) Release the lock

Banking Example Bank account balance maintained in one variable int balance Transactions: deposit or withdraw some amount from the account (+/- balance) Unprotected, concurrentedaccesses to your balance could create race conditions

Banking Example - Transactions void *deposit(void *amt){ pthread_mutex_lock(&bal_mutex); //CRITICAL SECTION int amount = (int)amt; int new_balance = balance + amount; balance = new_balance; pthread_mutex_unlock(&bal_mutex) ; void *widthdraw(void *amt){ pthread_mutex_lock(&bal_mutex); //CRITICAL SECTION int amount = (int)amt; int new_balance = balance - amount; balance = new_balance; pthread_mutex_unlock(&bal_mutex) ; } pthread_exit((void *)0); } pthread_exit((void *)0);

Semaphore Synchronization mechanism that generalizes locks to more than just acquired and free (or released ) A semaphore provides you with: An integer count accessed through 2 atomic operations Wait - aka: down, decrement, P (for proberen) Block until semaphore is free, then decrement the variable Signal - aka: up, post, increment, V (for verhogen) Increment the variable and unblock a waiting thread (if there are any) A mutex was just a binary semaphore (remember pthread_mutex_lock blocked if another thread was holding the lock) A queue of waiting threads

POSIX Semaphores Declared in semaphore.h A few calls associated with POSIX semaphores: sem_init Initialize the semaphore sem_wait Wait on the semaphore (decrement value) sem_post Signal (post) on the semaphore (increment value) sem_getvalue Get the current value of the semaphore sem_destroy Destroy the semaphore

Initializing & Destroying POSIX Semaphores Initialize semaphores using sem_init int sem_init(sem_t *sem, int pshared, unsigned int value); sem: the semaphore to initialize pshared: non-zero to share between processes value: initial count value of the semaphore Destroy semaphores using sem_destroy int sem_destroy(sem_t *sem); sem: semaphore to destroy Semaphore must have been created using sem_init Destroying a semaphore that has threads blocked on it is undefined.

Decrementing & Incrementing POSIX Semaphores Decrement semaphores using sem_wait intsem_wait(sem_t *sem); sem: the semaphore to decrement (wait on) Increment semaphores using sem_post int sem_post(sem_t *sem); sem: semaphore to increment Let s look at an example of a very simple server simulation

Barrier Example Rendezvous problem: two threads meet (stop) at a point of execution before they re allowed to proceed Generalization: any number of threads all have to reach a barrier before allowed to proceed

Barrier Example 1 Rendezvous 2 Critical Poi N threads have the following structure 1 rendezvous 2 critical point No thread can execute critical point until all threads executed rendezvous i.e. N-1 threads block after line 1, until thread N arrives, then all threads are allowed to execute Note that critical point is not a critical section and threads may proceed in any order

Barrier Example We need a variable to keep track of the number of threads that have arrived Need to lock this variable so it s thread safe! We need to make the first N-1 threads wait, and the Nth thread sends the signal to proceed A semaphore, we ll call barrier, which is initialized to 0 (will immediately block when waited on)

Barrier Example rendezvous count_lock.lock() count = count + 1 count_lock.unlock() if (count == N) barrier.post() barrier.wait() Counter and lock to protect it All threads wait, the last thread to arrive does a signal() critical point Are we done? Not quite There s been N wait() but only 1 signal()!

Barrier Example rendezvous count_lock.lock() count = count + 1 count_lock.unlock() if (count == N) barrier.post() barrier.wait() barrier.post() critical point Every thread that s woken up does an additional post() to tag some other thread