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

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

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

Process Synchronization

Chapter 7: Process Synchronization!

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

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Chapter 6: Process Synchronization

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

Process Synchronization

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

CS370 Operating Systems

Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

Chapter 7: Process Synchronization. Background

Chapter 6: Process Synchronization

Synchronization Principles

CHAPTER 6: PROCESS SYNCHRONIZATION

CS370 Operating Systems

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

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

Module 6: Process Synchronization

Lesson 6: Process Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Process Synchronization(2)

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

Module 6: Process Synchronization

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

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

Interprocess Communication By: Kaushik Vaghani

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

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

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

Introduction to Operating Systems

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

Process Synchronization(2)

IV. Process Synchronisation

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Process/Thread Synchronization

Process Coordination

Process Synchronization. studykorner.org

Process/Thread Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 6: Process Synchronization

CS370 Operating Systems

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Process Synchronization

Process Synchronization

CS420: Operating Systems. Process Synchronization

Chapter 6: Process Synchronization

Process Synchronization(2)

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

CSC501 Operating Systems Principles. Process Synchronization

OS Process Synchronization!

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization

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

Lecture 3: Synchronization & Deadlocks

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

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

CSE Opera,ng System Principles

CS3502 OPERATING SYSTEMS

Process Synchronization

High-level Synchronization

Process Synchronization

Chapter 5: Process Synchronization

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

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

Concurrency pros and cons. Concurrent Programming Problems. Mutual Exclusion. Concurrency is good for users

Process Co-ordination OPERATING SYSTEMS

Process Management And Synchronization

Concurrency pros and cons. Concurrent Programming Problems. Linked list example. Linked list example. Mutual Exclusion. Concurrency is good for users

Chapter 6 Synchronization

Concurrency: Mutual Exclusion and Synchronization

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Synchronization Principles I

CS3733: Operating Systems

Dept. of CSE, York Univ. 1

Dealing with Issues for Interprocess Communication

Concurrency. Chapter 5

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

1. Motivation (Race Condition)

Process Synchronization

Achieving Synchronization or How to Build a Semaphore

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Comp 310 Computer Systems and Organization

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

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Chapter 6 Process Synchronization

Concurrency: Mutual Exclusion and

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

CS370 Operating Systems

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

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

PROCESS SYNCHRONIZATION

Enforcing Mutual Exclusion Using Monitors

Silberschatz and Galvin Chapter 6

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

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Transcription:

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

Critical-section problem solution 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections 3. 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 3.Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes

Structure of a typical process Pi do { entry section critical section exit section remainder section } while (TRUE);

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

Semaphore Synchronization tool that does not require busy waiting Semaphore S integer variable Two standard operations modify S: wait() and signal() Less complicated than using hardware instructions Can only be accessed via two indivisible (atomic) operations wait (S) { } } while S <= 0 ; // no-op S--; signal (S) { S++;

Semaphore as General Synchronization Tool 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 Also known as mutex locks Provides mutual exclusion Semaphore mutex; // initialized to 1 do { wait (mutex); // Critical Section signal (mutex); // remainder section } while (TRUE);

Semaphore implementation Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section. Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied Note that applications may spend lots of time in critical sections and therefore this is not a good solution.

Semaphore Implementation with no busy wait Define a semaphore as a struct typedef struct { int value; struct process *queue; } semaphore; Assume same simple operations: wait(s) may block the process that invokes it signal(s) resumes the execution of a blocked process P

Implementation wait(s): S.value--; if (S.value < 0) { add this process P to S.queue; block(p); } signal(s): S.value++; if (S.value <= 0) { remove a process P from S.queue; wakeup(p); }

Allowing multiple Critical Sections Counting semaphores s.value >= 0 - # of processes that can execute wait(s) without blocking Assumption: no signal(s) executed Each can enter its critical section. s.value < 0 - # of processes blocked on s These blocked processes are in s.queue

Deadlock and Starvation Deadlock two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P0 wait (S); wait (Q);...... signal (S); signal (Q); P1 wait (Q); wait (S); signal (Q); signal (S); Starvation indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-priority process

Classical Synchronization Problems Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

Bounded-Buffer Problem N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N

Bounded Buffer Problem (Cont.) The structure of the producer process do { // produce an item in nextp wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); } while (TRUE);

Bounded Buffer Problem (Cont.) The structure of the consumer process do { wait (full); wait (mutex); // remove an item from buffer in nextc signal (mutex); signal (empty); // consume the item in nextc } while (TRUE);