3.1. Concurrency, Critical Sections, Semaphores

Slides:



Advertisements
Similar presentations
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Advertisements

Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
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.
China’s Software Industry August 2006 Instructor: Hengming Zou, Ph.D.
Dr. Kalpakis CMSC 421, Operating Systems. Fall Process Synchronization.
Silberschatz, Galvin and Gagne  Operating System Concepts Background Shared-memory solution to bounded-buffer problem allows at most n – 1 items.
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
The Critical-Section Problem
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Chapter 7: Process Synchronization
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Synchronization Solutions
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Concurrency, Mutual Exclusion and Synchronization.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 7 Process Synchronization Slide 1 Chapter 7 Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
7.1 Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
崑山科技大學資管系 Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Chapter 5: Process Synchronization
Chapter 7: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
The Critical-Section Problem
Lecture 20 Syed Mansoor Sarwar
Lecture 2 Part 2 Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
Presentation transcript:

3.1. Concurrency, Critical Sections, Semaphores Unit OS3: Concurrency 3.1. Concurrency, Critical Sections, Semaphores Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze

Copyright Notice © 2000-2005 David A. Solomon and Mark Russinovich These materials are part of the Windows Operating System Internals Curriculum Development Kit, developed by David A. Solomon and Mark E. Russinovich with Andreas Polze Microsoft has licensed these materials from David Solomon Expert Seminars, Inc. for distribution to academic organizations solely for use in academic environments (and not for commercial use)

Roadmap for Section 3.1. The Critical-Section Problem Software Solutions Synchronization Hardware Semaphores Synchronization in Windows & Linux With its notions of processes, threads, fibers, and jobs, Windows supports multiprogramming and distributed processing. Mutual exclusion and synchronization are the two aspects of concurrency control that are most fundamental to operating system design. Mutual exclusion denotes the ability of multiple units of concurrent execution (processes or threads) to share code, data, or resources in such a way that only one unit has access to the shared object at a time. Synchronization denotes the ability of multiple units of concurrent execution to coordinate their activities by exchange of information. Critical sections, semaphores, monitors, and message passing (interprocess communication through pipes, mailslots, shared memory, or window messages) are the most important concepts to support concurrency.

The Critical-Section Problem n threads all competing to use a shared resource (i.e.; shared data) Each thread has a code segment, called critical section, in which the shared data is accessed Problem: Ensure that when one thread is executing in its critical section, no other thread is allowed to execute in its critical section For simplicity, we generally refer to the concurrent execution of threads (as typical for Windows). However, in some systems the fundamental unit of concurrency might be a process rather than a thread.

Solution to Critical-Section Problem Mutual Exclusion Only one thread at a time is allowed into its critical section, among all threads that have critical sections for the same resource or shared data. A thread halted in its non-critical section must not interfere with other threads. Progress A thread remains inside its critical section for a finite time only. No assumptions concerning relative speed of the threads. Bounded Waiting It must no be possible for a thread requiring access to a critical section to be delayed indefinitely. When no thread is in a critical section, any thread that requests entry must be permitted to enter without delay.

Initial Attempts to Solve Problem Only 2 threads, T0 and T1 General structure of thread Ti (other thread Tj) do { enter section critical section exit section reminder section } while (1); Threads may share some common variables to synchronize their actions.

First Attempt: Algorithm 1 Shared variables - initialization int turn = 0; turn == i  Ti can enter its critical section Thread Ti do { while (turn != i) ; critical section turn = j; reminder section } while (1); Satisfies mutual exclusion, but not progress

Second Attempt: Algorithm 2 Shared variables - initialization int flag[2]; flag[0] = flag[1] = 0; flag[i] == 1  Ti can enter its critical section Thread Ti do { flag[i] = 1; while (flag[j] == 1) ; critical section flag[i] = 0; remainder section } while(1); Satisfies mutual exclusion, but not progress requirement.

Third Attempt: Algorithm 3 (Peterson’s Algorithm - 1981) Shared variables of algorithms 1 and 2 - initialization: int flag[2]; flag[0] = flag[1] = 0; int turn = 0; Thread Ti do { flag[i] = 1; turn = j; while ((flag[j] == 1) && turn == j) ; critical section flag[i] = 0; remainder section } while (1); Solves the critical-section problem for two threads.

Dekker’s Algorithm (1965) This is the first correct solution proposed for the two-thread (two-process) case. Originally developed by Dekker in a different context, it was applied to the critical section problem by Dijkstra. Dekker adds the idea of a favored thread and allows access to either thread when the request is uncontested. When there is a conflict, one thread is favored, and the priority reverses after successful execution of the critical section.

Dekker’s Algorithm (contd.) Shared variables - initialization: int flag[2]; flag[0] = flag[1] = 0; int turn = 0; Thread Ti do { flag[i] = 1; while (flag[j] ) if (turn == j) { flag[i] = 0; while (turn == j); flag[i] = 1; } critical section turn = j; flag[I] = 0;; remainder section } while (1);

Bakery Algorithm (Lamport 1979) A Solution to the Critical Section problem for n threads Before entering its critical section, a thread receives a number. Holder of the smallest number enters the critical section. If threads Ti and Tj receive the same number, if i < j, then Ti is served first; else Tj is served first. The numbering scheme generates numbers in monotonically non-decreasing order; i.e., 1,1,1,2,3,3,3,4,4,5...

Bakery Algorithm Notation “<“ establishes lexicographical order among 2-tuples (ticket #, thread id #) (a,b) < (c,d) if a < c or if a == c and b < d max (a0,…, an-1) = { k | k  ai for i = 0,…, n – 1 } Shared data int choosing[n]; int number[n]; - the ticket Data structures are initialized to 0

Bakery Algorithm do { choosing[i] = 1; number[i] = max(number[0],number[1] ...,number[n-1]) + 1; choosing[i] = 0; for (j = 0; j < n; j++) { while (choosing[j] == 1) ; while ((number[j] != 0) && ((number[j],j) ‘’<‘’ (number[i],i))); } critical section number[i] = 0; remainder section } while (1);

Mutual Exclusion - Hardware Support Interrupt Disabling Concurrent threads cannot overlap on a uniprocessor Thread will run until performing a system call or interrupt happens Special Atomic Machine Instructions Test and Set Instruction - read & write a memory location Exchange Instruction - swap register and memory location Problems with Machine-Instruction Approach Busy waiting Starvation is possible Deadlock is possible

Synchronization Hardware Test and modify the content of a word atomically boolean TestAndSet(boolean &target) { boolean rv = target; target = true; return rv; }

Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Thread Ti do { while (TestAndSet(lock)) ; critical section lock = false; remainder section }

Synchronization Hardware Atomically swap two variables. void Swap(boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; }

Mutual Exclusion with Swap Shared data (initialized to 0): int lock = 0; Thread Ti int key; do { key = 1; while (key == 1) Swap(lock,key); critical section lock = 0; remainder section }

Semaphores Semaphore S – integer variable can only be accessed via two atomic operations wait (S): while (S <= 0); S--; signal (S): S++;

Critical Section of n Threads Shared data: semaphore mutex; //initially mutex = 1 Thread Ti: do { wait(mutex); critical section signal(mutex); remainder section } while (1);

Semaphore Implementation Semaphores may suspend/resume threads Avoid busy waiting Define a semaphore as a record typedef struct { int value; struct thread *L; } semaphore; Assume two simple operations: suspend() suspends the thread that invokes it. resume(T) resumes the execution of a blocked thread T.

Implementation Semaphore operations now defined as wait(S): S.value--; if (S.value < 0) { add this thread to S.L; suspend(); } signal(S): S.value++; if (S.value <= 0) { remove a thread T from S.L; resume(T);

Semaphore as a General Synchronization Tool Execute B in Tj only after A executed in Ti Use semaphore flag initialized to 0 Code: Ti Tj … … A wait(flag) signal(flag) B

Two Types of Semaphores Counting semaphore integer value can range over an unrestricted domain. Binary semaphore integer value can range only between 0 and 1; can be simpler to implement. Counting semaphore S can be implemented as a binary semaphore.

Deadlock and Starvation Deadlock – two or more threads are waiting indefinitely for an event that can be caused by only one of the waiting threads. Let S and Q be two semaphores initialized to 1 T0 T1 wait(S); wait(Q); wait(Q); wait(S); … … signal(S); signal(Q); signal(Q) signal(S); Starvation – indefinite blocking. A thread may never be removed from the semaphore queue in which it is suspended. Solution - all code should acquire/release semaphores in same order

Windows Synchronization Uses interrupt masks to protect access to global resources on uniprocessor systems. Uses spinlocks on multiprocessor systems. Provides dispatcher objects which may act as mutexes and semaphores. Dispatcher objects may also provide events. An event acts much like a condition variable.

Linux Synchronization Kernel disables interrupts for synchronizing access to global data on uniprocessor systems. Uses spinlocks for multiprocessor synchronization. Uses semaphores and readers-writers locks when longer sections of code need access to data. Implements POSIX synchronization primitives to support multitasking, multithreading (including real-time threads), and multiprocessing.

Further Reading Ben-Ari, M., Principles of Concurrent Programming, Prentice Hall, 1982 Lamport, L., The Mutual Exclusion Problem, Journal of the ACM, April 1986 Abraham Silberschatz, Peter B. Galvin, Operating System Concepts, John Wiley & Sons, 6th Ed., 2003; Chapter 7 - Process Synchronization Chapter 8 - Deadlocks