Lecture 11: Mutual Exclusion

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Interprocess Communication
Cpr E 308 Spring 2004 Recap for Midterm Introductory Material What belongs in the OS, what doesn’t? Basic Understanding of Hardware, Memory Hierarchy.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
Concurrency.
EEE 435 Principles of Operating Systems Interprocess Communication Pt I (Modern Operating Systems 2.3)
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Concurrency, Mutual Exclusion and Synchronization.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
Cpr E 308 Spring 2004 Real-time Scheduling Provide time guarantees Upper bound on response times –Programmer’s job! –Every level of the system Soft versus.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Interprocess Communication Race Conditions
Sarah Diesburg Operating Systems COP 4610
Background on the need for Synchronization
Concurrency.
Chapter 5: Process Synchronization
143a discussion session week 3
Lecture 12: Peterson’s Solution and Hardware Support
Lecture 13: Producer-Consumer and Semaphores
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Module 7a: Classic Synchronization
Jonathan Walpole Computer Science Portland State University
Mutual Exclusion.
Lecture 2 Part 2 Process Synchronization
Coordination Lecture 5.
Grades.
Implementing Mutual Exclusion
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Implementing Mutual Exclusion
Lecture 12: Peterson’s Solution and Hardware Support
Lecture 13: Producer-Consumer and Semaphores
Lecture 11: Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Atomicity, Mutex, and Locks
Process/Thread Synchronization (Part 2)
Sarah Diesburg Operating Systems CS 3430
Presentation transcript:

Lecture 11: Mutual Exclusion

Review: POSIX Standards pthread_create() pthread_exit() pthread_join() pthread_yield()

Review: Threads Implementation User Level Threads Kernel Level Threads Hybrid Implementations

In this lecture Mutual exclusion Race conditions Critical regions Attempts for achieving mutual exclusion

Threads and Race Conditions x = x+1; /* ld r1,x add r1,1 st r1,x */ Thread 2: x = x+1; /* ld r1,x add r1,1 st r1,x */ Here we have two threads that are reading and modifying the same variable: both are adding one to x. Although the operation is written as a single step in terms of C code, it generally takes three machine instructions, as shown in the slide. If the initial value of x is 0 and the two threads execute the code shown in the slide, we might expect that the final value of x is 2. However, suppose the two threads execute the machine code at roughly the same time: each loads the value of x into its register, each adds one to the contents of the register, and each stores the result into x. The final result, of course, is that x is 1, not 2. Final value of x could be 1 or depending on the interleaving.

Mutual exclusion Two processes/threads cannot operate on the same shared variable simultaneously

Critical Region Shared variables among multiple processes/threads Protect Shared Variables Operation should be “atomic” Ensure other processes/threads don’t interfere with a sequence of instructions Critical region

Mutual exclusion via Critical Region Expected behavior

Protect Shared Variables Thread 1: lock(mutex); x = x+1; /* ld r1,x add r1,1 st r1,x */ unlock(mutex); Thread 2: lock(mutex); x = x+1; /* ld r1,x add r1,1 st r1,x */ unlock(mutex); Here we have two threads that are reading and modifying the same variable: both are adding one to x. Although the operation is written as a single step in terms of C code, it generally takes three machine instructions, as shown in the slide. If the initial value of x is 0 and the two threads execute the code shown in the slide, we might expect that the final value of x is 2. However, suppose the two threads execute the machine code at roughly the same time: each loads the value of x into its register, each adds one to the contents of the register, and each stores the result into x. The final result, of course, is that x is 1, not 2.

Does this need mutual exclusion? Thread 1: My_balance = My_balance +1; Your_balance = Your_balance - 1; Thread 2: Total = My_balance + Your_balance; Yes. Interleaving may cause thread 2 to see an inconsistent state.

Do we need mutual exclusion? Thread 1: x = x+1; Thread 2: y = x; No. Since the reads and writes are atomic.

Code with the mutex Thread 1: Lock(b_mutex); Unlock(b_mutex); Mutex “b_mutex” protects My_balance and Your_balance Thread 1: Lock(b_mutex); My_balance = My_balance +1; Your_balance = Your_balance - 1; Unlock(b_mutex); Thread 2: Lock(b_mutex); Total = My_balance + Your_balance; Unlock(b_mutex);

Mutex and Critical Section Only one thread can be in the critical section at a time. Thread 2: Enter_Mutex(); Critical_Section(); Exit_Mutex(); Thread 1: Enter_Mutex(); Critical_Section(); Exit_Mutex();

Our Problem How to implement enter_mutex() and exit_mutex()? User space (no OS support) Inside the kernel Solution should satisfy: No two threads in the critical region at the same time

Software (user space) Solution 1 Enter Mutex: while (busy == 1); busy = 1; Critical Section: account_balance ++ ; Exit Mutex: busy = 0; Enter Mutex: while (busy == 1); busy = 1; Critical Section: account_balance ++ ; Exit Mutex: busy = 0; Thread 1 Thread 2 No good (why??) “busy” is a (shared) global variable.

Software Solution 2 Enter Mutex: while (turn == 2); Critical Section Exit Mutex: turn = 2; Enter Mutex: while (turn == 1); Critical Section Exit Mutex: turn = 1; Thread 1 Thread 2 “turn” is a (shared) global variable.

Problems with Solution 2 Threads have to strictly alternate One thread wanting to enter the critical section might have to wait for another which does not want to

Problem Statement Refined A solution to mutual exclusion should satisfy four conditions: No two processes simultaneously in critical region No assumptions made about speeds of CPUs No process running outside its critical region may block another process No process must wait forever to enter its critical region

Possible solutions for mutual exclusion Disabling interrupts Peterson’s solution Hardware support

Disable Interrupts Disable Interrupts during critical section Prevent context switch Enable Interrupts after critical section Good: No busy waiting Problems?

Problem Critical Section must be short No multiprogramming possible during critical section Cannot trust users to have a short critical section Used inside the kernel for mutual exclusion

Multiprocessors Disabling Interrupts doesn’t work Preventing a context switch doesn’t ensure that only one process is running

Disabling Interrupts: Use in mutex_lock (uniprocessor only) mutex is a data structure inside the kernel mutex_lock() traps into the kernel Disable interrupts Set Lock If (unsuccessful) then Enable interrupts Thread_yield() Try again Enable Interrupts