Synchronization for Concurrent Tasks

Similar documents
Dept. of CSE, York Univ. 1

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

Synchronization for Concurrent Tasks

Process Coordination

Chapter 7: Process Synchronization. Background. Illustration

CS420: Operating Systems. Process Synchronization

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

Process Synchronization

Chapter 7: Process Synchronization. Background

Process Synchronization (Part I)

Process Synchronization

Synchronization Spinlocks - Semaphores

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

Chapter 6: Process Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Process Synchronization

Chapter 6: Process Synchronization

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

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

Synchronization Principles

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

Lesson 6: Process Synchronization

CS370 Operating Systems

Chapter 7: Process Synchronization!

CHAPTER 6: PROCESS SYNCHRONIZATION

Process Synchronization

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

Synchronization I. Jo, Heeseung

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

Concurrency: a crash course

Chapter 5: Process Synchronization

Synchronization Principles I

Chapter 5: Process Synchronization

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

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

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

Module 6: Process Synchronization

IV. Process Synchronisation

Process/Thread Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Synchronization

Concurrency: Mutual Exclusion and

Dealing with Issues for Interprocess Communication

Chapter 6: Process Synchronization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

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

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

Process Synchronization

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter 5: Process Synchronization

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

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

Process Synchronization

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

Process Synchronization

CSC501 Operating Systems Principles. Process Synchronization

Chapter 6: Process Synchronization

Chapter 6 Synchronization

Process/Thread Synchronization

Introduction to Multicore Programming

Synchronization COMPSCI 386

CS370 Operating Systems

Chapter 6: Synchronization

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

CSE 4/521 Introduction to Operating Systems

Resource management. Real-Time Systems. Resource management. Resource management

Concurrency: Mutual Exclusion and Synchronization

Process Synchronization(2)

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

Chapter 5: Synchronization 1

CS370 Operating Systems Midterm Review

UNIT II PROCESS MANAGEMENT 9

Introduction to Operating Systems

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

Comp 310 Computer Systems and Organization

1. Motivation (Race Condition)

Process Synchronization

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

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Lecture 3: Synchronization & Deadlocks

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

The University of Texas at Arlington

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

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

Chapter 6 Synchronization

10/17/2011. Cooperating Processes. Synchronization 1. Example: Producer Consumer (3) Example

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Chapter 6: Process Synchronization

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Process Synchronization

Chapter 6 Process Synchronization

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

CS370 Operating Systems

Lecture 5: Inter-process Communication and Synchronization

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Module 6: Process Synchronization

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

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

Interprocess Communication By: Kaushik Vaghani

Transcription:

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 OS Primitives Page X 5 Q & A Page X 2

3 Race Condition The situation where several processes access and manipulate shared data concurrently The final value of the shared data depends upon which process finishes last Thread A item nextproduced; Thread B item nextconsumed; if (user_wants_to_write == 1) { while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = nextproduced; in = (in + 1) % BUFFER_SIZE; counter++; } If (user_wants_to_read == 1) { while (counter == 0) ; /* do nothing */ nextconsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; } 3

4 General Solution Structure General structure of process P i (other process P j ) do { entry section critical section exit section remainder section } while (1); Processes may share some common variables to synchronize their actions 4

5 Example Thread A item nextproduced; Thread B item nextconsumed; if (user_wants_to_write == 1) { while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = nextproduced; in = (in + 1) % BUFFER_SIZE; pthread_mutex_lock(&mutex); counter++; pthread_mutex_lock(&mutex); } If (user_wants_to_read == 1) { while (counter == 0) ; /* do nothing */ nextconsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; pthread_mutex_lock(&mutex); counter--; pthread_mutex_lock(&mutex); } 5

6 Three Approaches to Critical Sections Algorithmic approaches Algorithmically solves the critical section problem without using any special HW and OS support Hardware support Use special hardware support to achieve atomicity Interrupt disabling, Test and Set instruction, Swap instruction OS primitives Use OS primitives Semaphore, mutex, spin lock, reader-writer lock, 6

Algorithmic Approach: Bakery Algorithm by Lamport Before entering its critical section, process receives a number Holder of the smallest number enters the critical section 7 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,3,4,5... 7

8 Bakery Algorithm by Lamport Process P i Initially, number[i] = 0 do { choosing[i] = true; number[i] = max (number[0], number[1],, number [n 1]) + 1; choosing[i] = false; for (j = 0; j < n; j++) { while (choosing[ j ]) ; while ((number[ j ]!= 0) && ((number[ j ], j) < (number[i], i))) ; } critical section (a,b) < (c,d) if a < c or if a = c and b < d number[i] = 0; remainder section } while (1); 8

Hardware Approach: Synchronization by Interrupt Disabling Guarantee atomicity for accessing a critical section Forbid interrupts to occur in a critical section The current sequence of instructions would be allowed to execute in order without preemption (atomicity) No unexpected modifications could be made 9 Unfortunately, this solution is not feasible in a multiprocessor environment Disabling interrupts on a multiprocessor requires significant time to propagate the message to all the processors 9

10 Synchronization by Test-and-Set Many machines provide special HW instructions To allow us to test and modify the content of a word, or To swap the contents of two words, atomically Test and set instruction boolean TestAndSet(boolean &target) { boolean rv = target; target = true; } return rv; 10

11 Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Process P i do { while (TestAndSet(lock)) ; critical section lock = false; remainder section } while(1) 11

12 OS Primitives for Critical Sections OS provide several primitives for critical sections Mutual exclusion, progress, and bounded waiting are guaranteed by OS Most popular primitives are semaphores and mutexes 12

13 Semaphore Semaphores vs. Mutexes A semaphore is a counter that can be either incremented or decremented Two types of semaphores Counting semaphores (integer numbers) Binary semaphores (0 or 1) Semaphores can be shared by different processes Mutex A binary semaphore Mutex has a sense of ownership Only the task that has lock the mutex can unlock it By default, a mutex can be shared between threads 13

14 Deadlock Semaphores or mutexes must be used carefully Otherwise, a deadlock or a livelock can happen 14

15 Livelock Threads can be trapped in a livelock of constantly acquiring and releasing mutexes 15

16 Spin Locks Spin locks are essentially mutex locks Tasks waiting for mutex locks can sleep or spin Tasks using a spin lock keeps trying to acquire the lock without sleeping Advantage of spin locks Tasks will acquire the lock as soon as it is released For a mutex lock that sleeps, the task needs to be woken by the operating system before it can get the lock Disadvantage of spin locks A spin lock will be monopolizing the CPU 16

17 POSIX Spinlocks 17

18 Synchronization in Real World OS primitives often serve as a powerful tool for many applications However, there are many real world problems that are hard to solve only with OS primitives We often need a more complex approach One difficult problem is the Readers-Writers Synchronization 18

19 19