Process Synchronization Mechanisms

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

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

CS420: Operating Systems. Process Synchronization

CS370 Operating Systems

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Lesson 6: Process Synchronization

CS3733: Operating Systems

CS370 Operating Systems

Comp 310 Computer Systems and Organization

Chapter 7: Process Synchronization!

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

Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

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

IV. Process Synchronisation

Process Synchronization

Dept. of CSE, York Univ. 1

Chapter 7: Process Synchronization. Background

CHAPTER 6: PROCESS SYNCHRONIZATION

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

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

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

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

CS 153 Design of Operating Systems Winter 2016

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

Concurrency and Synchronisation

Process Coordination

Process Synchronization

Process Synchronization

Introduction to Operating Systems

Concurrency and Synchronisation

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

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

Process Synchronization

Lecture 6. Process Synchronization

Chapter 6: Process Synchronization

Concurrency. Chapter 5

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Process Synchronization

Concept of a process

Process Synchronization

Synchronization Principles

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

Chapter 5: Process Synchronization

Dealing with Issues for Interprocess Communication

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

Concurrency: Mutual Exclusion and

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

Chapter 5: Process Synchronization

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

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

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

CSC501 Operating Systems Principles. Process Synchronization

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

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

CS370 Operating Systems

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Chapter 5: Process Synchronization

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

Chapter 5 Asynchronous Concurrent Execution

Lecture 6 (cont.): Semaphores and Monitors

CS370 Operating Systems Midterm Review

Introduction to OS Synchronization MOS 2.3

Concurrency: Mutual Exclusion and Synchronization

Interprocess Communication By: Kaushik Vaghani

Process Synchronization

Synchronization I. Jo, Heeseung

Process Synchronization. studykorner.org

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

Operating Systems ECE344

Dekker s algorithms for Semaphores Implementation. ALI TARIQ AL-KHAYYAT ( ) Submitted to : Prof. Dr. Huseyin Balik

More Types of Synchronization 11/29/16

CS 153 Design of Operating Systems Winter 2016

Multitasking / Multithreading system Supports multiple tasks

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

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

Process Synchronization

Process Synchronization(2)

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Systèmes d Exploitation Avancés

Process Co-ordination OPERATING SYSTEMS

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

Chapter 6 Process Synchronization

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

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

Synchronization. Dr. Yingwu Zhu

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Synchronization for Concurrent Tasks

Chapter 6: Process Synchronization

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Transcription:

Process Synchronization Mechanisms Tom Kelliher, CS 311 Mar 19, 2012 Announcements: From last time: 1. CPU scheduling. Outline: 1. Critical sections and cooperating processes. 2. Cooperating processes (review). 3. A hardware solution to the C. S. problem. 4. Semaphores. Assignment: Read Chapter 6. 1 Critical Sections and Cooperating Processes What is a critical section? The overlapping portion of cooperating processes, where shared variables are being accessed. 1

Not all processes share variables: independent processes. Cooperating/independent processes. Necessary conditions for a solution to the c.s. problem: 1. Mutual Exclusion if P i is executing in one of its critical sections, no P j, j i, is executing in its critical sections. 2. Progress a process operating outside of its critical section cannot prevent other processes from entering theirs; processes attempting to enter their critical sections simultaneously must decide which process enters eventually. 3. Bounded Waiting a process attempting to enter its critical region will be able to do so eventually. Assumptions: 1. No assumptions made about relative speed of processes 2. No process may remain in its critical section indefinitely (may not terminate in its critical section) 3. A memory operation (read or write) is atomic cannot be interrupted. For now, we do not assume indivisible RMW cycles. Classic example: the producer/consumer problem (aka bounded buffer): Global data: const int N = 10; int buffer[n]; int in = 0; int out = 0; int full = 0; int empty = N; Producer: 2

while (1) while (empty == 0) ; buffer[in] = indata; in = ++in % N; --empty; ++full; Consumer: while (1) while (full == 0) ; outdata = buffer[out]; out = ++out % N; --full; ++empty; Is there potential for trouble here? 1.1 Critical Section Usage Model (for n processes, 1 i n) Pi: do mutexbegin(); /* CS entry */ CSi; mutexend(); /* CS exit */ non-cs while (!done); 3

1.2 A Simple, Primitive Hardware Solution 1. Just disable interrupts. 2. Umm, what about user processes? 3. Why this doesn t work with multiprocessors. 4. This is dangerous. 2 Cooperating Processes 1. Must cooperating processes synchronize under all conditions? (Don t forget single writer performing atomic writes/multiple readers.) 2. What does atomic mean? 3. Recall necessary and sufficient conditions: Mutual exclusion, progress, and bounded waiting. 3 A Hardware Solution: TAS Instruction TAS: Test And Set. Semantics: int TAS(int& val) int temp; temp = val; val = 1; return temp; // Body performed atomically. A partial solution to the critical section problem for n processes: 4

// Initialization int lock = 0; void MutexBegin() while (TAS(lock)) // Ugh. A spin lock. ; void MutexEnd() lock = 0; Prove that this is a solution to the C. S. problem. 4 Semaphores 1. Created by Dijkstra (Dutch) 2. A semaphore is an integer flag, indicating that it is safe to proceed. 3. Two operations: (a) Wait (p) proberen, test: wait(s) while (s == 0) ; s--; Test and (possible) decrement executed atomically (usually achieved through hardware means). (b) Signal (v) verhogen, increment: 5

signal(s) s++; (c) Why not resort to hardware methods? 4. These are operations provided by the kernel. Wait and signal are atomic operations. 4.1 Critical Section Problem Solution 1. Critical section solution: semaphore mutex = 1; mutexbegin: wait(mutex); mutexend: signal(mutex); (a) Mutual exclusion is achieved: consider a contradiction. (b) Progress is achieved: someone got the semaphore. (c) Bounded waiting depends on how the wait queue is implemented (if at all). 4.2 Usage Examples 1. Interrupt signalling: semaphore sig = 0; int_hndl: signal(sig); driver: startread(); wait(sig); 6

2. Process synchronization: semaphore flag = 0; process1() p1part1(); // This will complete before p2part2() begins. signal(flag); p1part2(); process2() p2part1(); wait(flag); p2part2(); 3. Resource management (pool of buffers) Producer/Consumer problem: semaphore count = N; semaphore mutex = 1; getbuf: wait(count); /* order important here */ wait(mutex); <grab unallocated buffer> signal(mutex); return(buffer); relbuf: wait(mutex); <release buffer> signal(mutex); signal(count); 4.3 A Better Semaphore 1. Above semaphores inefficient spinlocks. Let waits which cause busy waits actually block the process: 7

Ready Dispatched Running Possibly call scheduler Pre-empted signal(s) Blocked wait(s<=0) Associate a blocked queue with each semaphore. typedef struct semaphore int value; pcb *head; Semaphore creation: semaphore *createsem(int value) semaphore *sem; sem = get_next_sem(); sem->value = value; sem->head = NULL; return (sem); void wait(semaphore *sem) /* need mutex goo here */ if (--sem->value < 0) <update status of current process> insqu(sem->head->prev, current); scheduler(); void signal(semaphore *sem) /* mutex */ pcb *proc; 8

if (++sem->value <= 0) proc = remqu(sem->head->next); <update status of proc> ordinsqu(ready, proc); if (proc->prio > current->prio) scheduler(); 9