Mutual Exclusion and Synchronization

Similar documents
Concurrency: Mutual Exclusion and Synchronization. Concurrency

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Process Synchronization

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

Principles of Operating Systems CS 446/646

CS420: Operating Systems. Process Synchronization

Process Synchronization - I

The concept of concurrency is fundamental to all these areas.

Dept. of CSE, York Univ. 1

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

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

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Process Synchronization

Chapter 6: Process Synchronization

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

Lecture 6. Process Synchronization

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

CS370 Operating Systems

Enforcing Mutual Exclusion Using Monitors

IV. Process Synchronisation

IT 540 Operating Systems ECE519 Advanced Operating Systems

Process Synchronization

CONCURRENCY:MUTUAL EXCLUSION AND SYNCHRONIZATION

Chapter 7: Process Synchronization!

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

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

Lesson 6: Process Synchronization

Concurrency: Mutual Exclusion and

Synchronization Principles

Process Coordination

Chapter 7: Process Synchronization. Background. Illustration

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

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

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

Process Synchronization

Chapter 7: Process Synchronization. Background

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7)

Last class: Today: CPU Scheduling. Start synchronization

Process Synchronization

Chapter 6: Process Synchronization

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

Module 6: Process Synchronization

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

Chapter 6: Process Synchronization

Synchronization for Concurrent Tasks

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

Synchronization Spinlocks - Semaphores

Concurrency: Mutual Exclusion and Synchronization

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

Concurrency. Chapter 5

Concurrency(I) Chapter 5

Chapter 5: Process Synchronization

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

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

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

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

Introduction to Operating Systems

CS604 - Operating System Solved Subjective Midterm Papers For Midterm Exam Preparation

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

Roadmap. Shared Variables: count=0, buffer[] Producer: Background. Consumer: while (1) { Race Condition. Race Condition.

CS370 Operating Systems

Process Synchronization

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

Synchronization I. Jo, Heeseung

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

Process Management And Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Operating Systems CMPSC 473. Synchronization February 21, Lecture 11 Instructor: Trent Jaeger

Dealing with Issues for Interprocess Communication

Process/Thread Synchronization

Chapter 6 Process Synchronization

Interprocess Communication By: Kaushik Vaghani

Lecture 5: Synchronization w/locks

Process Synchronization (Part I)

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

Process Synchronization

1. Motivation (Race Condition)

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

Concurrent Processes Rab Nawaz Jadoon

Concept of a process

Achieving Synchronization or How to Build a Semaphore

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

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

Chapter 6: Process Synchronization

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

UNIX Input/Output Buffering

Chapter 5: Process Synchronization

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

Chapter 6 Synchronization

Chapters 5 and 6 Concurrency

CMSC421: Principles of Operating Systems

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

Concurrency: Mutual Exclusion (Locks)

Chapter 5: Synchronization 1

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Transcription:

Mutual Exclusion and Synchronization

Concurrency Defined Single processor multiprogramming system Interleaving of processes Multiprocessor systems Processes run in parallel on different processors Interleaving and overlapping of processes 2

Why concurrency? Communication among processes Synchronization of multiple processes Allocation of processor time Efficient utilization of processor Sharing resources Better system performance 3

Concurrency arise in these contexts Three different contexts Multiple applications Multiprogramming Structured application Application can be a set of concurrent processes Operating- system structure Operating system is a set of processes or threads 4

Difficul7es of Concurrency Sharing of global resources Operating system managing the allocation of resources optimally Difficult to locate programming errors Relative speed of execution of processes cannot be predicted 5

Race Condi7on Shared (Global) int x = 1; Process A Process B x = x + 1; x = x + 1; CPU <<<<< Process A executing >>>>> 1) Load R1,x <<<<< Interrupt; Process Switch to Process B>>>>> 1) Load R1,x 2) Inc R1 3) Store R1,x <<<<< Interrupt; Process Switch to Process A>>>>> 2) Inc R1 3) Store R1,x 6

Race Condi7on A condition where two or more threads/processes modify a shared resource and the final result depends on the relative timing of their execution. 7

Opera7ng System Concerns Result of processing must be independent of the speed of execution Process Interactions: Processes unaware of each other Compete for resources Process directly aware of each other Cooperation by communication Processes indirectly aware of each other Cooperation by sharing 8

Compe77on for Resources Problem: Execution of one process may affect the behavior of competing processes Solution: Mutual Exclusion a critical section is the fragment of code that accesses shared data when one process is executing in its critical section, no other process is allowed to execute in its critical section Issue: Deadlock and Starvation blocked process will never get access to the resource and never terminate 9

Coopera7on by Communica7on Communication provides a way to synchronize, or coordinate, the various activities no sharing => mutual exclusion not required Possible to have deadlock each process waiting for a message from the other process Possible to have starvation two processes sending message to each other while another process waits for a message 10

Coopera7on by Sharing Processes use and update shared data such as shared variables, files, and data bases Writing must be mutually exclusive Critical sections are used to provide data integrity 11

A simple example void echo() { chin = getchar(); chout = chin; putchar(chout); } Q1 12

A Simple Example Process P1 Process P2.. chin = getchar();.. chin = getchar(); chout = chin; chout = chin; putchar(chout);.. putchar(chout);.. 13

What s causing the problem? Shared variables and shared resources Process or thread behavior must be predictable Process or thread behavior must be independent of the speed of this or other processes or threads 14

How to prevent the problem? Restricted access to shared variables and resources Implementing critical section Requiring mutual execution Q2 15

Mutual Exclusion Requirements Mutually exclusive access to critical section Progress. If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. Bounded Waiting. A bound must exist on the # of times that other processes are allowed to enter their critical sections after a process has requested to enter its critical section and before that request is granted. Assume each process executes at a nonzero speed No assumption concerning relative speed of n processes. 16

Four different approaches Hardware support Software- defined approaches Support from the OS Support from the programming language 17

Enforcing Mutual Exclusion funtion(){ <non- critical section> enter_critical_section(); <critical section> exit_critical_section(); <remainder section> } 18

Hardware Support one void func(){ <non- critical section> /* disable interrupts */ <critical section> /* enable interrupts */ <remainder section> } 19

Hardware Support one Interrupt Disabling A process runs until it invokes an operating system service or until it is terminated Disabling interrupts guarantees mutual exclusion On a uniprocessor system Processor is limited in its ability to interleave programs Multiprocessing disabling interrupts on one processor will not guarantee mutual exclusion 20

Disadvantages If the critical section is long, then control is taken away from the OS for a long period of time. This decreases overall system performance. Will not work on multi- processor systems. 21

Hardware Support two Special Machine Instructions Performed in a single instruction cycle (atomic) Access to the shared memory location is blocked for any other instructions 22

Hardware Support two (2) (Return original value of lock) boolean TestAndSet (int lock) { boolean tmp = lock; lock = 1; return tmp; } TestAndSet(Lock) Semantics If TestAndSet(Lock) = 1, someone else already has the lock If TestAndSet(Lock) = 0, lock is free and is set to 1 by the call TestAndSet(Lock) Usage (Spin- Lock) 23

Mutual Exclusion with Test- and- Set Shared data: int lock = 0; Process P i do { <non- critical section> while (TestAndSet(lock) == 1) continue; // do nothing, spin (busy wait) <critical section> lock = 0; <remainder section> } while (1); Q3 24

Proper7es Of Hardware Support (2) Advantages Applicable to any number of processes and any number of processors Simple and easy to verify Can support multiple critical sections (associate a different variable to each critical section) 25

Proper7es Of Hardware Support (2) Disadvantages Busy waiting wastes processor cycles (just like software- only solutions) Starvation is possible because of arbitrary selection of waiting process Deadlock is possible Need alternative mechanisms 26