Process/Thread Synchronization

Similar documents
Process/Thread Synchronization

Synchronization Spinlocks - Semaphores

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

Chapter 6: Process Synchronization

CS370 Operating Systems

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

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Chapter 7: Process Synchronization!

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

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

CS420: Operating Systems. Process Synchronization

Synchronization Principles I

Dept. of CSE, York Univ. 1

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

CS370 Operating Systems

Synchronization Principles

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

Process Synchronization (Part I)

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

Prof. Hui Jiang Dept of Computer Science and Engineering York University

Process Synchronization(2)

Process Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 7: Process Synchronization. Background

Lesson 6: Process Synchronization

Process Synchronization

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

Process Synchronization

Chapter 6 Process Synchronization

Achieving Synchronization or How to Build a Semaphore

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

Process Synchronization

Synchronization I. Jo, Heeseung

Process Synchronization

Chapter 5: Process Synchronization

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

Synchronization for Concurrent Tasks

Chapter 6: Process Synchronization

Interprocess Communication By: Kaushik Vaghani

CS3733: Operating Systems

Process Synchronization

Process Synchronization(2)

Comp 310 Computer Systems and Organization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Process Synchronization

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

Chapter 5: Process Synchronization

Process Synchronization(2)

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

Chapter 5: Process Synchronization

Process Coordination

Module 6: Process Synchronization

CSE Opera,ng System Principles

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

Lecture 6. Process Synchronization

Process Synchronization

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

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

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

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

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

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

High-level Synchronization

CSE Traditional Operating Systems deal with typical system software designed to be:

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

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

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

1. Motivation (Race Condition)

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

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

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

Part II Process Management Chapter 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

UNIT-II PROCESS SYNCHRONIZATION

UNIT II PROCESS MANAGEMENT 9

Process Synchronization

Concept of a process

Introduction to OS Synchronization MOS 2.3

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Synchronization COMPSCI 386

Chapter 6 Synchronization

CS370 Operating Systems

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

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Process Synchronization Examples

Locks. Dongkun Shin, SKKU

Chapter 6: Process Synchronization

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

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Lecture 3: Synchronization & Deadlocks

Dealing with Issues for Interprocess Communication

OS Process Synchronization!

Process Co-ordination OPERATING SYSTEMS

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

Concurrency: Mutual Exclusion and

Chapter 6: Process Synchronization

UNIT 2 Basic Concepts of CPU Scheduling. UNIT -02/Lecture 01

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Lecture 5: Synchronization w/locks

Transcription:

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, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again. Critical Sections shared double balance; Code for P 1 Code for P 2...... balance = balance + amount; balance = balance - amount;...... balance+=amount balance-=amount balance Is there any problem? 3/3/11 CSE325: Synchronization 2

The Critical Section Problem Consider a system of n processes {P 0, P 1,, P n-1 } Each P i has a segment of code called critical section Change common variables Update table Write file No two processes execute in critical section at same time 3/3/11 CSE325: Synchronization 3

The Critical Section Problem (Cont.) Problem is to design a protocol to allow cooperation Processes must request permission to execute The structure consists of four sections Entry section Critical section Exit section Remainder section 3/3/11 CSE325: Synchronization 4

General Structure of a Process, P i do { entry section critical section exit section remainder section } while (TRUE); 3/3/11 CSE325: Synchronization 5

Critical Section Problems Mutual exclusion: Only one process can be in the critical section at a time There is a race to execute critical sections The sections may be defined by different code in different processes cannot easily detect with static analysis Without mutual exclusion, results of multiple execution are not determinate Need an OS mechanism so programmer can resolve race conditions 3/3/11 CSE325: Synchronization 6

Critical Section Requirements Mutual exclusion If process Pi executing in CS, no other processes can be executing in CS Progress Only allow processes needing to execute in CS to participate in selection of process to allow Bounded waiting There exists a limit on the # of times other processes enter CS after a process has made request to enter CS 3/3/11 CSE325: Synchronization 7

General Approaches In Operating Systems Preemptive kernels Nonpreemptive kernels Issues? Preemptive kernels Allows race conditions Allows system to be more responsive Nonpreemptive kernels Race free on kernel data structures Only when on single processor 3/3/11 CSE325: Synchronization 8

Peterson s Solution Software-based Two processes, P 0 & P 1, alternating execution of CS s Must share: int turn; boolean flag[2]; Variable turn indicates which process is next for CS Array flag indicates if a process is ready to enter CS 3/3/11 CSE325: Synchronization 9

Synchronization Hardware Need support from Hardware Mechanism called lock needed Processes must acquire the lock before entering CS 3/3/11 CSE325: Synchronization 10

Solution using locks do { acquire lock critical section release lock remainder section } while (TRUE); 3/3/11 CSE325: Synchronization 11

Disabling Interrupts /* shared double balance */ Code for P 1 Code for P 2 disableinterrupts(); disableinterrupts(); balance = balance + amount; balance = balance - amount; enableinterrupts(); enableinterrupts(); Interrupts could be disabled arbitrarily long Really only want to prevent P 1 and P 2 from interfering with one another; this blocks all other P i 3/3/11 CSE325: Synchronization 12

Test and Set Instruction Atomic operation that will test a variable and set it without being interrupted boolean TestAndSet(boolean *target) { boolean rv = *target; *target = TRUE; return rv; } 3/3/11 CSE325: Synchronization 13

Use of Test and Set Instruction do { while (TestAndSet(&lock)) ; // just wait and do nothing // run critical section code lock = FALSE; // run remainder section code } while (TRUE); 3/3/11 CSE325: Synchronization 14

Swap Instruction Atomic operation that will swap the values of two variables without being interrupted boolean Swap(boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp; } 3/3/11 CSE325: Synchronization 15

Use of Swap Instruction do { key = TRUE; while (key == TRUE) Swap(&lock, &key); // run critical section code lock = FALSE; // run remainder section code } while (TRUE); 3/3/11 CSE325: Synchronization 16

Discussion Are these viable solutions to the Critical-Section Problem? Mutual exclusion? Yes Progress? Yes Bounded waiting? No 3/3/11 CSE325: Synchronization 17

A Semaphore 3/3/11 CSE325: Synchronization 18

Semaphore Invented by Dijkstra in the 1960s Conceptual OS mechanism, with no specific implementation defined (could be enter()/ exit(), was called P() and V(), or wait () and signal()) Basis of all contemporary OS synchronization mechanisms 3/3/11 CSE325: Synchronization 19

Semaphore (Cont.) wait(s) { while S <=0 ; //no-op S--; } signal(s) { S++; } 3/3/11 CSE325: Synchronization 20

Use of Semophores Usually two types of semaphores Counting semaphore Binary semaphore (also called mutex locks) Binary semaphores can be used for: Critical section problems for multiple processes Counting semaphores can be used for: Controlling access to multiple, but finite set of resources Ordering the running of critical sections 3/3/11 CSE325: Synchronization 21

Semaphore Implementation Issues Is there a problem? Busy waiting wastes resources Redefine the semaphore implementation to allow blocking instead 3/3/11 CSE325: Synchronization 22

Semaphore Implementation Processes are only blocked on their own critical sections (not critical sections that they should not care about) If disabling interrupts, be sure to bound the time they are disabled 3/3/11 CSE325: Synchronization 23

Semaphore Implementation Define a semaphore as a record typedef struct { int value; struct process *L; } semaphore; Assume two simple operations: block suspends the process that invokes it. wakeup(p) resumes the execution of a blocked process P. 3/3/11 CSE325: Synchronization 24

Implementation Semaphore operations now defined as enter(s): S.value--; if (S.value < 0) { } add this process to S.L; block; exit(s): S.value++; if (S.value <= 0) { } remove a process P from S.L; wakeup(p); 3/3/11 CSE325: Synchronization 25