Chapter 6: Process Synchronization

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

Chapter 6: Process Synchronization

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

Synchronization Principles

CHAPTER 6: PROCESS SYNCHRONIZATION

Process Synchronization

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

Process Synchronization

Chapter 7: Process Synchronization!

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

Chapter 6: Process Synchronization

Lesson 6: Process Synchronization

Module 6: Process Synchronization

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

Process Co-ordination OPERATING SYSTEMS

Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

Chapter 5: Process Synchronization

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

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

Chapter 7: Process Synchronization. Background

Chapter 5: Process Synchronization

CS370 Operating Systems

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

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

Interprocess Communication By: Kaushik Vaghani

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Chapter 6: Process Synchronization

CS370 Operating Systems

Introduction to Operating Systems

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

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Chapter 6: Process Synchronization. Module 6: Process Synchronization

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

Process Synchronization(2)

Process Synchronization

Process Synchronization

Process Synchronization

Chapter 5: Process Synchronization

CS420: Operating Systems. Process Synchronization

Process Coordination

Chapter 6 Synchronization

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

Lecture 3: Synchronization & Deadlocks

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

Process Synchronization(2)

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

Synchronization Principles II

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

Dept. of CSE, York Univ. 1

CS3502 OPERATING SYSTEMS

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Synchronization for Concurrent Tasks

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

1. Motivation (Race Condition)

Process Synchronization(2)

Chapter 6 Process Synchronization

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 Management And Synchronization

CS370 Operating Systems

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

CSE Opera,ng System Principles

CSE 4/521 Introduction to Operating Systems

Module 6: Process Synchronization

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

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

UNIT II PROCESS MANAGEMENT 9

Chapter 6: Process Synchronization

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

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

Process Synchronization

Dealing with Issues for Interprocess Communication

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

CSC501 Operating Systems Principles. Process Synchronization

Real-Time Operating Systems M. 5. Process Synchronization

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

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

Chapter 6: Synchronization

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Multitasking / Multithreading system Supports multiple tasks

$ %! 0,-./ + %/ 0"/ C (" &() + A &B' 7! .+ N!! O8K + 8 N. (Monitors) 3+!

Process/Thread Synchronization

High-level Synchronization

CS3733: Operating Systems

Silberschatz and Galvin Chapter 6

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

PROCESS SYNCHRONIZATION

Lecture 5: Inter-process Communication and Synchronization

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

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Process/Thread Synchronization

Synchronization Principles I

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

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

PESIT Bangalore South Campus

Process Synchronization (Part I)

Concurrency. Chapter 5

Process Synchronization. studykorner.org

Transcription:

Chapter 6: Process Synchronization

Objectives Introduce Concept of Critical-Section Problem Hardware and Software Solutions of Critical-Section Problem Concept of Atomic Transaction Operating Systems CS 33211 2

Background Producer-Consumer Problem Revisited Model illustrates: Asynchronous Processes and/or Threads (lightweight processes) share data memory Producer code: while (true) { /* Produce an item in next Produced */ while (counter == BUFFER_SIZE) ; /* do nothing */ buffer [in] = nextproduced; in = (in + 1) % BUFFER_SIZE; counter++; Shared memory: bounded circular buffer 0 7 x x x x Consumer code: while (true) { while (counter == 0) ; /* do nothing */ nextconsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; /* Consume item in nextcomsumed */ BUFFER_SIZE = 8 out in Operating Systems CS 33211 3

Background Producer-Consumer Problem Revisited Concurrent executions of Producer and Consumer Routines yields Possible inconsistency in value of counter Let s examine scenarios when current value is 4 counter = 3-4-5? Root cause: Both processes allowed to manipulate counter variable concurrently and outcome depended on the order of executions (Race Condition) Solution Approach Ensure only one process at time can manipulate the counter variable We will talk about this shortly Operating Systems CS 33211 4

Critical Section Problem General Structure of Typical Process Process : do { entry selection <CRITICAL SECTION> exit section <Remainder section> while (TRUE); Operating Systems CS 33211 5

Mutual Exclusion Concepts Race Condition Several processes access and manipulate the same data concurrently, and the outcome of execution depends on the order in which access takes place Critical section A segment of code in which a process has exclusive access to manipulate one or more shared resources Write to a file, update a table, change a common variables The general structure of a typical process: <entry section> CRITICAL SECTION <exit section> Remainder section Mutual Exclusion When several processes try to use the same set of resources, how do we ensure that they gain access to the resources only one at a time Operating Systems CS 33211 6

Critical Section Problem Solution Approach Requirements Mutual Exclusion Allow one and only one process at a time to execute in the CRITICAL SECTION of the code Progress A process that is not requesting entry to CRITICAL SECTION of its code should not block the process (or processes) that are requesting entry into theirs Bounded waiting A process should not wait indefinitely before it can enter the CRITICAL SECTION Operating Systems CS 33211 7

Critical Section Problem Peterson s Solution Restricted to two Processes (P 0,P 1 ) Processes alternate executions between Critical sections and Remainder section Shared data items: int turn ; /* turn = 1 P 1 is allowed to execute critical section */ boolean flag[2]; /* flag[0] = true P 0 is ready to enter critical section */ Process 0: do { flag[0] = TRUE; turn = 1 while (flag[1] && turn == 1); <CRITICAL SECTION> flag[0] = FALSE; <Remainder section> while (TRUE); Process 1: do { flag[1] = TRUE; turn = 0 while (flag[0] && turn == 0); <CRITICAL SECTION> flag[1] = FALSE; <Remainder section> while (TRUE); Operating Systems CS 33211 8

Critical Section Problem Solution To Avoid race conditions: Process must acquire a lock before entering the critical section Process : do { Acquire lock <CRITICAL SECTION> Release lock <Remainder section> while (TRUE); Operating Systems CS 33211 9

Semaphores A special Synchronization variable S Defines an event, which is announced to the OS Process wishes to wait for the event Calls wait(s) Process executing the event, after event completes Calls signal(s) OS ensures that only one process can modify S at a time Operating Systems CS 33211 10

Semaphore Implementation Semaphore s is initially set to 1 Before entering the critical section, process calls wait (s) s = s -1 if (s < 0) Block the process that called wait (s) on a queue associated with a semaphore s Else Allow the process that called wait(s) continue into critical section After exiting critical section, process calls signal (s) s = s + 1 if ( s <= 0)then Wake up one of the threads that called wait (s) and run it so it continues into critical section Operating Systems CS 33211 11

Semaphore Implementation Sample code Process P 0 : t(){ while (true){ wait (s); <CRITICAL SECTION> signal (s); <Remainder section> Process P 1 : p(){ while (true){ wait (s); <CRITICAL SECTION> signal (s); <Remainder section> OS ensures that no two processes can execute wait() and signal() on the same semaphore at the same time Operating Systems CS 33211 12

Semaphore Implementation So far Busy Waiting Process P 0 : Recall: t(){ while (true){ wait (s); <CRITICAL SECTION> signal (s); <Remainder section> Process P 1 spins: While Process 0 is in CRITICAL SECTION: Process 1 loops continuously in ENTRY SECTION of its code (s <= 0) Wastes CPU cycles Semaphore Type: SPINLOCK Counting Semaphore and Binary Semaphore may cause Spinlock How do we avoid Spinlock? Operating Systems CS 33211 13

Semaphore Implementation Avoiding Spinlock 1. Define semaphore type: typedef struct { int value; struct process *list; semaphore; List of waiting processes struct contains link field to PCB Pointer to list of PCBs E.g., Use FIFO queue 2. Modify wait ( ) semaphore operation: If Process P 1 s semaphore value in entry section is not positive Process P 1 puts itself in a waiting queue by invoking system call block ( ) 3. Modify signal ( ) semaphore operation: When P 0 executes signal ( ) Restart a process in waiting queue by invoking system call wakeup( ) Operating Systems CS 33211 14

Semaphore Implementation Avoiding Spinlock wait ( ) semaphore operation wait (semaphore *S { S->value --; if S->value < 0 { add process that called wait ( ) to S->list; block ( ); Suspend the process that called wait Operating Systems CS 33211 15

Semaphore Implementation Avoiding Spinlock signal ( ) semaphore operation signal (semaphore *S { S->value ++; if S->value <= 0 { remove a process P from S->list; wakeup (P ); Which process? P resumes execution of its critical section Operating Systems CS 33211 16

Semaphore Implementation Process P 0 : t(){ while (true){ wait (*s); <CRITICAL SECTION> signal (*s); <Remainder section> Process P 1 : p(){ while (true){ wait (*s); <CRITICAL SECTION> signal (*s); <Remainder section> Operating Systems CS 33211 17

Deadlocks and Starvation Waiting Queue Deadlock Two or more processes wait for an event, signal op, that can be triggered by only one of the waiting processes P 0 wait(s); wait (Q); P 1 wait(q); wait (S); signal (S); signal (Q); signal (Q); signal (S); Starvation: When one processes wait indefinitely within the same semaphore Operating Systems CS 33211 18

Classic Problems of Synchronization Case #1: The Bounded-Buffer Problem Why examine Classic Synch Problems? The Bounded-Buffer Problem: (Producer/Consumer Processes) Assumptions n-buffers, each holds one item Semaphore (mutex: 0/1) provides mutual exclusion access to n-buffers Semaphore (empty) ~ count of empty buffers: [empty n] Semaphore (full) ~ count of full buffers: [full 0] Producer Process: do { /* Produce an item in next nextp */ wait (empty) wait (mutex). /* add nextp to buffer */ signal (mutex); signal (full); while (TRUE); Consumer Process: do { /* Produce an item in next nextp */ wait (full) wait (mutex). /* remove item from buffer to nextc */ signal (mutex);; signal (empty); while (TRUE); Operating Systems CS 33211 19

Classic Problems of Synchronization Case #2: Readers-Writers Problem (db) Assumptions Assume db is shared by n Concurrent Processes R i, i = 1, j; W i, i = 1, k; where n = j + n Processes R perform a read operation on db Processes W perform db update (read followed by write) What happens to data item if Processes R access the shared data simultaneously? What could happen to a shared db object if a W Process and another Processes R or W access the shared data simultaneously? Assume interrupts are not inhibited Basic Solution Requirements 1. Writers (W) will have exclusive access to db 2. A Reader cannot be blocked unless Writer has permission to shared object Operating Systems CS 33211 20

Classic Problems of Synchronization Case #2: Readers-Writers Problem (db) The Basic Solution: Assumptions Semaphore (wrt: 0/1) provides mutual exclusion for writers (used by readers) wrt 1 Semaphore (readcount) ~ count of process reading shared object: [readcount 0] Semaphore (mutex) ~ provides mutual exclusion after updating readcount: [mutex 1] Reader Processes: Writer Processes: semaphore wrt do { wait (wrt) /* write to db */ signal (wrt); while (TRUE); semaphore mutex, wrt int readcount; do { wait (mutex) readcount++; if (readcount == 1) wait (wrt); signal (mutex); /* read db */ readcount--; if (readcount == 0) signal (wrt); signal (mutex); while (TRUE); Operating Systems CS 33211 21

Classic Problems of Synchronization Case #3: Dinning-Philosophers Problem Consider 5 Philosophers To Think and Eat (fried rice?) Share a round table with 5 chairs Each Philosopher has assigned chair and plate Fried rice located at center of table One chopstick between neighbor plates To eat Philosopher uses assigned chair, plate and grabs left chopstick followed by right chopstick on either side of his/her plate When Philosopher is not eating, s/he is thinking Problem: Develop an algorithm to allow Philosophers to eat. Algorithm must satisfy: Mutual Exclusion (One and only one Philosopher uses a chopstick at a time) Avoid deadlock (Where everyone holds left chopstick and waits for right chopstick) Avoid starvation ( Everyone must eat eventually) Operating Systems CS 33211 22

Synchronization Mechanism Condition Construct Condition variables coordinate events: Define one or more variables of type condition condition x, y; Supports two atomic operations x.wait () /* Suspend the process invokes operation until x. signal( ) is invoked by another process*/ x. signal( ) /*resume one suspended process */ Now associate a semaphore and integer variable with each condition variable Operating Systems CS 33211 23

Synchronization Mechanism Condition Variables Implementation x.wait( ) x.signal( ) semaphore x_sem 0 integer x_count 0 x_count++{ if (next_count > 0) signal (next) else signal (mutex) wait(x_sem) xcount--; semaphore x_sem 0 integer x_count 0 if (next_count > 0) { next_count++; signal (x_sem); wait (next); next_count--; Operating Systems CS 33211 24