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

Size: px
Start display at page:

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

Transcription

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

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

3 Mutual Exclusion 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; 3

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

5 Mutual Exclusion 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; 5

6 Roadmap Solutions for mutual exclusion Peterson s algorithm (Software) Synchronization instructions (Hardware) High-level synchronization mechanisms Mutex Semaphore Monitor 6

7 Spinlock using TestAndSet void init_lock (int *mutex) *mutex = 0; void lock (int *mutex) while(testandset(mutex)) ; void unlock (int *mutex) *mutex = 0; 7

8 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 8

9 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 9

10 void mutex_init (mutex_t *lock) 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) 10

11 void mutex_init (mutex_t *lock) 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) 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 11

12 void mutex_init (mutex_t *lock) 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) 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 12

13 void mutex_init (mutex_t *lock) 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) 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 13

14 void mutex_init (mutex_t *lock) 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) if (!list_empty(&lock->wait_list)) wake_up_process(&lock->wait_list) Someone is waiting for the lock Wake-up a waiting thread 14

15 void mutex_init (mutex_t *lock) 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); 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 15

16 void mutex_init (mutex_t *lock) 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); 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!!!

17 Agenda High-level synchronization mechanisms Mutex Semaphore Monitor 17

18 High-level Synchronization Primitives Lock (mutex) is great, but Too low-level primitive Sometimes we need more powerful primitives Semaphore Binary/integer semaphore Monitor Condition variable 18

19 Semaphore High-level synchronization primitive Designed by Dijkstra in 1960 Definition Semaphore is an integer variable Only two operations are possible: P() or wait() or down() V() or signal() or up() 19

20 Simple Semaphore Implementation P() operation P(semaphore *S) while(s >value == 0) S->list->addQ(P); schedule( ); S >value ; schedule ( ) schedule another thread V() operation V(semaphore *S) if(!isempty(&s->list)) P = delq(&s->list); wakeup(p); S >value++ ; wakeup( ) wake up a thread What s wrong with the code? 20

21 Simple Semaphore Implementation P() operation P(semaphore *S) S->lock->Acquire(); while(s >value == 0) addq(&s->list, P); S->lock->Release(); schedule( ); S->lock->Acquire(); S >value ; S->lock->Release(); V() operation V(semaphore *S) S->lock->Acquire(); if(!isempty(&s->list)) P = delq(&s->list); wakeup(p); S >value++ ; S->lock->Release(); 21

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

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1 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?

More information

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

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack 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.

More information

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 Disclaimer: some slides are adopted from the book authors slides 1 Recap Synchronization instructions test&set, compare&swap All or nothing Spinlock Spin on wait Good for short critical

More information

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 Disclaimer: some slides are adopted from the book authors slides 1 Recap Synchronization instructions test&set, compare&swap All or nothing Spinlock Spin on wait Good for short critical

More information

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

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani Synchronization Heechul Yun Disclaimer: some slides are adopted from the book authors and Dr. Kulkani 1 Synchronization Spinlock Recap Implement using h/w instructions (e.g., test-and-set) Mutex Sleep

More information

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

OS Structure. User mode/ kernel mode. System call. Other concepts to know. Memory protection, privileged instructions Midterm Review OS Structure User mode/ kernel mode Memory protection, privileged instructions System call Definition, examples, how it works? Other concepts to know Monolithic kernel vs. Micro kernel 2

More information

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

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works? Midterm Review OS Structure User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions System call Definition, examples, how it works? Other concepts to know Monolithic kernel vs. Micro

More information

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

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1 Thread Disclaimer: some slides are adopted from the book authors slides with permission 1 IPC Shared memory Recap share a memory region between processes read or write to the shared memory region fast

More information

Synchronization Spinlocks - Semaphores

Synchronization Spinlocks - Semaphores CS 4410 Operating Systems Synchronization Spinlocks - Semaphores Summer 2013 Cornell University 1 Today How can I synchronize the execution of multiple threads of the same process? Example Race condition

More information

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

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10) Synchronization CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10) 1 Outline Critical region and mutual exclusion Mutual exclusion using busy waiting Sleep and

More information

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

Chapter 6: Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Synchronization, Silberschatz, Galvin and Gagne 2009 Outline Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

Synchronization I. Jo, Heeseung

Synchronization I. Jo, Heeseung 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

More information

Last Class: Synchronization

Last Class: Synchronization Last Class: Synchronization Synchronization primitives are required to ensure that only one thread executes in a critical section at a time. Concurrent programs Low-level atomic operations (hardware) load/store

More information

CS 153 Design of Operating Systems Winter 2016

CS 153 Design of Operating Systems Winter 2016 CS 153 Design of Operating Systems Winter 2016 Lecture 9: Semaphores and Monitors Some slides from Matt Welsh Summarize Where We Are Goal: Use mutual exclusion to protect critical sections of code that

More information

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

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling. Background The Critical-Section Problem Background Race Conditions Solution Criteria to Critical-Section Problem Peterson s (Software) Solution Concurrent access to shared data may result in data inconsistency

More information

Synchronization Principles

Synchronization Principles Synchronization Principles Gordon College Stephen Brinton The Problem with Concurrency Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms

More information

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

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code Old Producer Process Code Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Our

More information

CS420: Operating Systems. Process Synchronization

CS420: Operating Systems. Process Synchronization Process Synchronization James Moscola Department of Engineering & Computer Science York College of Pennsylvania Based on Operating System Concepts, 9th Edition by Silberschatz, Galvin, Gagne Background

More information

Process/Thread Synchronization

Process/Thread Synchronization CSE325 Principles of Operating Systems Process/Thread Synchronization David Duggan dduggan@sandia.gov February 14, 2013 Reading Assignment 7 Chapter 7 Deadlocks, due 2/21 2/14/13 CSE325: Synchronization

More information

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

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657 Process & Thread Management II CIS 657 Queues Run queues: hold threads ready to execute Not a single ready queue; 64 queues All threads in same queue are treated as same priority Sleep queues: hold threads

More information

Process & Thread Management II CIS 657

Process & Thread Management II CIS 657 Process & Thread Management II CIS 657 Queues Run queues: hold threads ready to execute Not a single ready queue; 64 queues All threads in same queue are treated as same priority Sleep queues: hold threads

More information

Systèmes d Exploitation Avancés

Systèmes d Exploitation Avancés Systèmes d Exploitation Avancés Instructor: Pablo Oliveira ISTY Instructor: Pablo Oliveira (ISTY) Systèmes d Exploitation Avancés 1 / 32 Review : Thread package API tid thread create (void (*fn) (void

More information

IV. Process Synchronisation

IV. Process Synchronisation IV. Process Synchronisation Operating Systems Stefan Klinger Database & Information Systems Group University of Konstanz Summer Term 2009 Background Multiprogramming Multiple processes are executed asynchronously.

More information

Synchronization for Concurrent Tasks

Synchronization for Concurrent Tasks Synchronization for Concurrent Tasks Minsoo Ryu Department of Computer Science and Engineering 2 1 Race Condition and Critical Section Page X 2 Algorithmic Approaches Page X 3 Hardware Support Page X 4

More information

Process Synchronization

Process Synchronization Process Synchronization Concurrent access to shared data may result in data inconsistency Multiple threads in a single process Maintaining data consistency requires mechanisms to ensure the orderly execution

More information

Process/Thread Synchronization

Process/Thread Synchronization CSE325 Principles of Operating Systems Process/Thread Synchronization David Duggan dduggan@sandia.gov March 1, 2011 The image cannot be displayed. Your computer may not have enough memory to open the image,

More information

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang COP 4225 Advanced Unix Programming Synchronization Chi Zhang czhang@cs.fiu.edu 1 Cooperating Processes Independent process cannot affect or be affected by the execution of another process. Cooperating

More information

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

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University 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

More information

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

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives. CS 5523 Operating Systems: Concurrency and Synchronization Thank Dr. Dakai Zhu and Dr. Palden Lama for providing their slides. Lecture Outline Problems with concurrent access to shared data Ø Race condition

More information

Today: Synchronization. Recap: Synchronization

Today: Synchronization. Recap: Synchronization Today: Synchronization Synchronization Mutual exclusion Critical sections Example: Too Much Milk Locks Synchronization primitives are required to ensure that only one thread executes in a critical section

More information

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

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6. Part Three - Process Coordination Chapter 6: Synchronization 6.1 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure

More information

EEE3052: Introduction to Operating Systems. Fall Project #3

EEE3052: Introduction to Operating Systems. Fall Project #3 EEE3052: Introduction to Operating Systems Fall 2017 Project #3 Project Plan 4 projects 0) Install Xv6 1) Process management 2) Virtual memory 3) Synchronization - Thread (11/13 ~ 11/21) - Mutex & Condition

More information

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

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Process Synchronization, Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Synchronization 6.1 Background 6.2 The Critical-Section Problem 6.3 Peterson s Solution 6.4 Synchronization Hardware 6.5 Mutex Locks 6.6 Semaphores 6.7 Classic

More information

Synchronization for Concurrent Tasks

Synchronization for Concurrent Tasks Synchronization for Concurrent Tasks Minsoo Ryu Department of Computer Science and Engineering 2 1 Race Condition and Critical Section Page X 2 Synchronization in the Linux Kernel Page X 3 Other Synchronization

More information

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Review: Processes. A process is an instance of a running program. POSIX Thread APIs:

Review: Processes. A process is an instance of a running program. POSIX Thread APIs: Review: Processes A process is an instance of a running program - A thread is an execution context - Process can have one or more threads - Threads share address space (code, data, heap), open files -

More information

Chapter 6 Process Synchronization

Chapter 6 Process Synchronization Chapter 6 Process Synchronization Cooperating Process process that can affect or be affected by other processes directly share a logical address space (threads) be allowed to share data via files or messages

More information

Introduction to Operating Systems

Introduction to Operating Systems Introduction to Operating Systems Lecture 4: Process Synchronization MING GAO SE@ecnu (for course related communications) mgao@sei.ecnu.edu.cn Mar. 18, 2015 Outline 1 The synchronization problem 2 A roadmap

More information

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

PROCESS SYNCHRONIZATION

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

C09: Process Synchronization

C09: Process Synchronization CISC 7310X C09: Process Synchronization Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/29/2018 CUNY Brooklyn College 1 Outline Race condition and critical regions The bounded

More information

1) If a location is initialized to 0, what will the first invocation of TestAndSet on that location return?

1) If a location is initialized to 0, what will the first invocation of TestAndSet on that location return? Synchronization Part 1: Synchronization - Locks Dekker s Algorithm and the Bakery Algorithm provide software-only synchronization. Thanks to advancements in hardware, synchronization approaches have been

More information

Synchronising Threads

Synchronising Threads Synchronising Threads David Chisnall March 1, 2011 First Rule for Maintainable Concurrent Code No data may be both mutable and aliased Harder Problems Data is shared and mutable Access to it must be protected

More information

CS3733: Operating Systems

CS3733: Operating Systems Outline CS3733: Operating Systems Topics: Synchronization, Critical Sections and Semaphores (SGG Chapter 6) Instructor: Dr. Tongping Liu 1 Memory Model of Multithreaded Programs Synchronization for coordinated

More information

Process Synchronization

Process Synchronization TDDI04 Concurrent Programming, Operating Systems, and Real-time Operating Systems Process Synchronization [SGG7] Chapter 6 Copyright Notice: The lecture notes are mainly based on Silberschatz s, Galvin

More information

Synchronization. Heechul Yun

Synchronization. Heechul Yun Synchronization Heechul Yun 1 Recap: Semaphore High-level synchronization primitive Designed by Dijkstra in 1960 Definition Semaphore is an integer variable Only two operations are possible: P() or wait()

More information

Process Synchronization

Process Synchronization CSC 4103 - Operating Systems Spring 2007 Lecture - VI Process Synchronization Tevfik Koşar Louisiana State University February 6 th, 2007 1 Roadmap Process Synchronization The Critical-Section Problem

More information

Lesson 6: Process Synchronization

Lesson 6: Process Synchronization Lesson 6: Process Synchronization Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Objectives Introduce Concept of Critical-Section Problem Hardware and Software Solutions of Critical-Section Problem Concept of Atomic Transaction Operating Systems CS

More information

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs CSE 451: Operating Systems Winter 2005 Lecture 7 Synchronization Steve Gribble Synchronization Threads cooperate in multithreaded programs to share resources, access shared data structures e.g., threads

More information

Interprocess Communication By: Kaushik Vaghani

Interprocess Communication By: Kaushik Vaghani Interprocess Communication By: Kaushik Vaghani Background Race Condition: A situation where several processes access and manipulate the same data concurrently and the outcome of execution depends on the

More information

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

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

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

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 Concurrent programming Assumptions:

More information

CHAPTER 6: PROCESS SYNCHRONIZATION

CHAPTER 6: PROCESS SYNCHRONIZATION CHAPTER 6: PROCESS SYNCHRONIZATION The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. TOPICS Background

More information

Process Synchronization

Process Synchronization Process Synchronization Chapter 6 2015 Prof. Amr El-Kadi Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 11 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Multilevel Feedback Queue: Q0, Q1,

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - XXVI April 27 th, 2006 1 Roadmap Shared Memory Synchronization Spin Locks Barriers Semaphores Monitors 2 1 Memory Architectures Distributed Memory Shared Memory

More information

Synchronization 1. Synchronization

Synchronization 1. Synchronization Synchronization 1 Synchronization key concepts critical sections, mutual exclusion, test-and-set, spinlocks, blocking and blocking locks, semaphores, condition variables, deadlocks reading Three Easy Pieces:

More information

EECS 482 Introduction to Operating Systems

EECS 482 Introduction to Operating Systems EECS 482 Introduction to Operating Systems Fall 2017 Manos Kapritsos Slides by: Harsha V. Madhyastha Implementing reader-writer locks with monitors Shared data needed to implement readerstart, readerfinish,

More information

Review: Thread package API

Review: Thread package API 1/39 Review: Thread package API tid thread create (void (*fn) (void *), void *arg); - Create a new thread that callsfn witharg void thread exit (); void thread join (tid thread); The execution of multiple

More information

Review: Thread package API

Review: Thread package API Review: Thread package API tid thread create (void (*fn) (void *), void *arg); - Create a new thread that calls fn with arg void thread exit (); void thread join (tid thread); The execution of multiple

More information

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430 Implementing Mutual Exclusion Sarah Diesburg Operating Systems CS 3430 From the Previous Lecture The too much milk example shows that writing concurrent programs directly with load and store instructions

More information

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization Hank Levy Levy@cs.washington.edu 412 Sieg Hall Synchronization Threads cooperate in multithreaded programs to share resources, access shared

More information

Locks. Dongkun Shin, SKKU

Locks. Dongkun Shin, SKKU Locks 1 Locks: The Basic Idea To implement a critical section A lock variable must be declared A lock variable holds the state of the lock Available (unlocked, free) Acquired (locked, held) Exactly one

More information

Operating Systems. Lab. Class Week 14

Operating Systems. Lab. Class Week 14 Operating Systems Lab. Class Week 14 Project Plan 6 projects 0. Install xv6 1. System call 2. Scheduling 3. Virtual memory 1 4. Virtual memory 2 5. Concurrency 1 6. Concurrency 2 Individual projects 2016-06-01

More information

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

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Operating Systems Lecture 4 - Concurrency and Synchronization Adrien Krähenbühl Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Mutual exclusion Hardware solutions Semaphores IPC: Message passing

More information

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set? Last Class: Synchronization Review Synchronization Mutual exclusion Critical sections Example: Too Much Milk Locks Synchronization primitives are required to ensure that only one thread executes in a critical

More information

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization Chapter 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

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

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

More information

CMSC421: Principles of Operating Systems

CMSC421: Principles of Operating Systems CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1018 L11 Synchronization Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Multilevel feedback queue:

More information

Interprocess Communication and Synchronization

Interprocess Communication and Synchronization Chapter 2 (Second Part) Interprocess Communication and Synchronization Slide Credits: Jonathan Walpole Andrew Tanenbaum 1 Outline Race Conditions Mutual Exclusion and Critical Regions Mutex s Test-And-Set

More information

Process Synchronization(2)

Process Synchronization(2) EECS 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Semaphores Problems with the software solutions.

More information

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

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 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 2. 3. 8 A N D 2. 3. 1 0 S P R I N G 2018 INTER-PROCESS COMMUNICATION 1. How a process pass information to another process

More information

Synchronization. CISC3595/5595 Fall 2015 Fordham Univ.

Synchronization. CISC3595/5595 Fall 2015 Fordham Univ. Synchronization CISC3595/5595 Fall 2015 Fordham Univ. Synchronization Motivation When threads concurrently read/write shared memory, program behavior is undefined Two threads write to the same variable;

More information

9/28/2014. CS341: Operating System. Synchronization. High Level Construct: Monitor Classical Problems of Synchronizations FAQ: Mid Semester

9/28/2014. CS341: Operating System. Synchronization. High Level Construct: Monitor Classical Problems of Synchronizations FAQ: Mid Semester CS341: Operating System Lect22: 18 th Sept 2014 Dr. A. Sahu Dept of Comp. Sc. & Engg. Indian Institute of Technology Guwahati Synchronization Hardware Support TAS, CAS, TTAS, LL LC, XCHG, Compare, FAI

More information

Process Synchronization(2)

Process Synchronization(2) EECS 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Semaphores Problems with the software solutions.

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

Achieving Synchronization or How to Build a Semaphore

Achieving Synchronization or How to Build a Semaphore Achieving Synchronization or How to Build a Semaphore CS 241 March 12, 2012 Copyright University of Illinois CS 241 Staff 1 Announcements MP5 due tomorrow Jelly beans... Today Building a Semaphore If time:

More information

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University Semaphores Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) Synchronization

More information

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; } Semaphore Semaphore S integer variable Two standard operations modify S: wait() and signal() Originally called P() and V() Can only be accessed via two indivisible (atomic) operations wait (S) { while

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst What is a Monitor? Ties data and the synchronization operations together Monitors guarantee mutual exclusion, i.e.,

More information

Student: Yu Cheng (Jade) ICS 412 Homework #3 October 07, Explain why a spin-lock is not a good idea on a single processor, single machine.

Student: Yu Cheng (Jade) ICS 412 Homework #3 October 07, Explain why a spin-lock is not a good idea on a single processor, single machine. Student: Yu Cheng (Jade) ICS 412 Homework #3 October 07, 2009 Homework #3 Exercise 1: Explain why a spin-lock is not a good idea on a single processor, single machine. There is a bad side and a good side

More information

Operating Systems. Synchronization Based on Ch. 5 of OS Concepts by SGG

Operating Systems. Synchronization Based on Ch. 5 of OS Concepts by SGG Operating Systems Synchronization Based on Ch. 5 of OS Concepts by SGG Need to Synchronize We already saw that if we write on a shared data structure w/o synchronization bad things can happen. The parts

More information

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

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018 Lecture In the lecture on March 13 we will mainly discuss Chapter 6 (Process Scheduling). Examples will be be shown for the simulation of the Dining Philosopher problem, a solution with monitors will also

More information

Module 6: Process Synchronization

Module 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic

More information

Synchronization Principles I

Synchronization Principles I CSC 256/456: Operating Systems Synchronization Principles I John Criswell University of Rochester 1 Synchronization Principles Background Concurrent access to shared data may result in data inconsistency.

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Sections 2.3 & 2.4 Textbook 2 Making Single-Threaded Code Multithreaded Conflicts between threads over the use of a global variable 3 Inter- Thread and Process Communication

More information

Concurrency and Synchronisation. Leonid Ryzhyk

Concurrency and Synchronisation. Leonid Ryzhyk Concurrency and Synchronisation Leonid Ryzhyk Textbook Sections 2.3 & 2.5 2 Concurrency in operating systems Inter-process communication web server SQL request DB Intra-process communication worker thread

More information

Operating System. Project #

Operating System. Project # Operating System Project #5 16. 11. 28 Project Plan 5 projects Install Xv6 System call + scheduling Virtual memory (stack growth + COW) Thread-support Concurrency Single-handed project Syncronization on

More information

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

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Process Synchronization, Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Learning Outcomes Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ! Last Class: CPU Scheduling! Scheduling Algorithms: FCFS Round Robin SJF Multilevel Feedback Queues Lottery Scheduling Review questions: How does each work? Advantages? Disadvantages? Lecture 7, page 1

More information

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

Program A. Review: Thread package API. Program B. Program C. Correct answers. Correct answers. Q: Can both critical sections run? Review: Thread package API tid thread_create (void (*fn) (void *), void *arg); - Create a new thread that calls fn with arg void thread_exit (); void thread_join (tid thread); The execution of multiple

More information

Readers-Writers Problem. Implementing shared locks. shared locks (continued) Review: Test-and-set spinlock. Review: Test-and-set on alpha

Readers-Writers Problem. Implementing shared locks. shared locks (continued) Review: Test-and-set spinlock. Review: Test-and-set on alpha Readers-Writers Problem Implementing shared locks Multiple threads may access data - Readers will only observe, not modify data - Writers will change the data Goal: allow multiple readers or one single

More information

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Operating System Concepts 9th Edition Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution

More information

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Synchronization 1. Synchronization

Synchronization 1. Synchronization Synchronization 1 Synchronization key concepts critical sections, mutual exclusion, test-and-set, spinlocks, blocking and blocking locks, semaphores, condition variables, deadlocks reading Three Easy Pieces:

More information