Process Synchronization

Similar documents
Process Coordination

Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

Chapter 7: Process Synchronization. Background

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

Lesson 6: Process Synchronization

CS370 Operating Systems

Process Synchronization(2)

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

Chapter 7: Process Synchronization!

Dept. of CSE, York Univ. 1

CS370 Operating Systems

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Process Synchronization(2)

Chapter 6: Process Synchronization

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

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

Chapter 6: Process Synchronization

CS420: Operating Systems. Process Synchronization

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

1. Motivation (Race Condition)

Chapter 5: Process Synchronization

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

Module 6: Process Synchronization

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Process Synchronization Examples

Chapter 5: Process Synchronization

Chapter 7 Process Synchronization

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

Chapter 5: Process Synchronization

Process Synchronization(2)

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Process Synchronization

Process Synchronization

Synchronization Principles

CSE 4/521 Introduction to Operating Systems

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

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

Process Synchronization

QUESTION BANK. UNIT II: PROCESS SCHEDULING AND SYNCHRONIZATION PART A (2 Marks)

Chapter 6: Synchronization

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

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Concurrency: Mutual Exclusion and

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

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

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

Synchronization Principles I

Process Synchronization

Synchronization for Concurrent Tasks

UNIT II PROCESS MANAGEMENT 9

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

CSE Opera,ng System Principles

High-level Synchronization

Chapter 6: Process Synchronization

Introduction to Operating Systems

Chapter 6 Process Synchronization

Process Synchronization

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

Synchronization Basic Problem:

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Process Synchronization

Module 6: Process Synchronization

Interprocess Communication By: Kaushik Vaghani

Comp 310 Computer Systems and Organization

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

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

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

CS370 Operating Systems

Process Synchronization

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS3733: Operating Systems

Chapter 6 Synchronization

Process/Thread Synchronization

OS Process Synchronization!

Unit 1: Introduction

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

Real-Time Operating Systems M. 5. Process Synchronization

Concurrency: Mutual Exclusion and Synchronization

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

Concept of a process

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Process/Thread Synchronization

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

CSC501 Operating Systems Principles. Process Synchronization

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

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

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

CS370 Operating Systems Midterm Review

Concurrency and Synchronisation

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Co-ordination OPERATING SYSTEMS

Concurrency and Synchronisation

Synchronization Spinlocks - Semaphores

Operating Systems. Synchronization Based on Ch. 5 of OS Concepts by SGG

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

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

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

Transcription:

Process Synchronization Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Process Synchronization 1 / 28

Cooperating processes Reference: Section 4.4. Cooperating process: shares data with other processes Independent process: does not share data with other processes Means of cooperation: Synchronization Communication M. Mitra (ISI) Process Synchronization 2 / 28

Producer-consumer problem Problem: a producer process generates output that is used by a consumer process as input Examples: print program (producer) + printer driver (consumer) $ ls -l more Implementation: Producer and consumer access a shared buffer Concurrent access is allowed Consumer must wait if buffer is empty Unbounded buffer: no limit on size of buffer Bounded buffer: buffer is of fixed size producer must wait if buffer is full M. Mitra (ISI) Process Synchronization 3 / 28

Producer-consumer implementation Initially, in = out = 0 Empty queue: in == out full queue: in+1 % n == out Buffer can hold at most n-1 items in..... out buffer[n] M. Mitra (ISI) Process Synchronization 4 / 28

Producer-consumer implementation Initially, in = out = 0 Empty queue: in == out full queue: in+1 % n == out Buffer can hold at most n-1 items in..... out buffer[n] producer p = produce(); while (in+1 % n == out); /* buffer full, skip */ buffer[in] = p; in = in+1 % n; consumer while (in == out); // skip p = buffer[out]; out = out+1 % n; consume(p); M. Mitra (ISI) Process Synchronization 4 / 28

Race condition. Reference: Section 6.1 producer p = produce(); while (count==n); // full buffer[in] = p; in = in+1 % n; count++; consumer while (count==0); // empty p = buffer[out]; out = out+1 % n; count- -; consume(p); MOV count R0 /* R0 = 5 */ ADD #1 R0 /* R0 = 6 */ MOV R0 count /* count=6 */ MOV count R0 /* R0 = 5 */ SUB #1 R0 /* R0 = 4 */ MOV R0 count /* count=4 */ M. Mitra (ISI) Process Synchronization 5 / 28

Race condition. Reference: Section 6.1 producer p = produce(); while (count==n); // full buffer[in] = p; in = in+1 % n; count++; consumer while (count==0); // empty p = buffer[out]; out = out+1 % n; count- -; consume(p); MOV count R0 /* R0 = 5 */ ADD #1 R0 /* R0 = 6 */ MOV R0 count /* count=6 */ MOV count R0 /* R0 = 5 */ SUB #1 R0 /* R0 = 4 */ MOV R0 count /* count=4 */ Race condition: several processes manipulate the same data concurrently s.t. outcome depends on the order in which the processes execute M. Mitra (ISI) Process Synchronization 5 / 28

Critical section problem (CSP). Reference: Section 6.2 Critical section (CS): segment of code in which processes access shared data Synchronization scheme: while (1) { entry_section(); critical_section(); exit_section(); remainder_section(); M. Mitra (ISI) Process Synchronization 6 / 28

CSP solutions Desiderata: Mutual exclusion: At most one process may execute code from CS at any given time Progress: If no process is executing in CS and some processes want to enter CS, only processes not in the remainder section can participate in deciding which process next enters CS. Selection of a process cannot be postponed indefinitely. Bounded waiting: there exists a bound on # 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 Assumptions: Basic machine instructions (load, store, test) are executed atomically M. Mitra (ISI) Process Synchronization 7 / 28

Two-process solutions Algorithm 1: strict alternation shared int turn = 0; P_i() { while (1) { while (turn!= i); // wait critical_section(); turn = j; // j = 1-i remainder_section(); Mutual exclusion satisfied Progress not satisfied M. Mitra (ISI) Process Synchronization 8 / 28

Two-process solutions Algorithm 2: shared char want[2] = {0,0; P_i() { while (1) { want[i] = 1; while (want[j]); critical_section(); want[i] = 0; remainder_section(); Mutual exclusion satisfied Progress not satisfied M. Mitra (ISI) Process Synchronization 9 / 28

Two-process solutions Algorithm 3: shared char want[2] = {0,0; shared int turn = 0; P_i() { while (1) { want[i] = 1; turn = j; while (want[j] && turn!=i); critical_section(); want[i] = 0; remainder_section(); Mutual exclusion, progress, bounded waiting satisfied M. Mitra (ISI) Process Synchronization 10 / 28

Two-process solutions Incorrect solutions: shared char want[2] = {0,0; shared int turn = 0; 1. P_i() 2. { while (1) { 3. turn = j; want[i] = 1; 4. want[i] = 1; while (want[j] && turn!=i); 5. while (want[j] && turn!=i); OR critical_section(); 6. critical_section(); want[i] = 0; 7. want[i] = 0; turn = j; 8. remainder_section(); remainder_section(); 9. 10. P 0 : (3) P 1 : (3 5) P 0 : (4 5) M. Mitra (ISI) Process Synchronization 11 / 28

Multiple-process solution (Lamport s) bakery algorithm Each process wanting to enter CS gets a token number Process with lowest token number enters CS If two processes have same token number, process with lower PID enters CS M. Mitra (ISI) Process Synchronization 12 / 28

Bakery algorithm shared char choosing[n] = {0,..., 0; shared int number[n] = {0,..., 0; do { choosing[i] = 1; // why?? number[i] = MAX(number[0], number[1],..., number[n-1]) + 1; choosing[i] = 0; // why?? for (j = 0; j < n; j++) { while (choosing[j]) ; while ((number[j]!= 0) && (number[j],j < number[i],i)) ; critical_section(); number[i] = 0; remainder_section while (1); M. Mitra (ISI) Process Synchronization 13 / 28

Hardware solutions Reference: Section 6.3. Disabling interrupts Critical section executes without preemption Adopted by non-preemptive Unix kernels Atomic test-and-set TestAndSet primitive: char TestAndSet(char *flag) { char rv = *flag; *flag = 1; return rv; CSP solution: shared char lock = 0;... while (TestAndSet(&lock)); critical_section(); lock = 0;... M. Mitra (ISI) Process Synchronization 14 / 28

Hardware solutions Atomic swap Swaps the contents of two words atomically CSP solution: shared char lock = 0;... key = 1; do Swap(lock,key) while (key!= 0); critical_section(); lock = 0; M. Mitra (ISI) Process Synchronization 15 / 28

Hardware solutions n-process CSP: shared char lock, waiting[n];... waiting[i] = 1; key = 1; while (waiting[i] && key) { key = TestAndSet(lock); waiting[i] = 0; critical_section(); j = i+1 % n; while (j!=i and waiting[j]==0) { j = j+1 % n; if (j==i) then lock = 0; else waiting[j] = 0; M. Mitra (ISI) Process Synchronization 16 / 28

Semaphores Reference: Section 6.4. Definition: a counting semaphore S is an integer variable that can be accessed only through two atomic operations, wait(s) (or P (S)) and signal(s) (or V (S)) wait(s): while (S <= 0); S- -; signal(s): S++; n-process CSP: shared semaphore mutex = 1; P_i() { wait(mutex); critical_section(); signal(mutex);... M. Mitra (ISI) Process Synchronization 17 / 28

Semaphores Synchronization problems Example: P 1, P 2 are 2 concurrent processes Statement S 2 in P 2 should be executed after S 1 in P 1 shared semaphore synch = 0; P1:... S1; signal(synch);... P2:... wait(synch); S2;... Careless use may lead to deadlock P1: wait(s1); wait(s2);... signal(s1); signal(s2); P2: wait(s2); wait(s1);... signal(s2); signal(s1); M. Mitra (ISI) Process Synchronization 18 / 28

Busy waiting vs. blocking Spinlocks/busy waiting Processes execute a loop while waiting for entry to critical section waste of CPU cycles Useful in multiprocessor systems if locks are held for short intervals ( context switch can be avoided) Blocking typedef struct {int value; struct process *L; semaphore; wait(s): S.value--; if (S.value < 0) { add process to S.L; sleep(); signal(s): S.value++; if (S.value <= 0) { remove process P from S.L; wakeup(p); M. Mitra (ISI) Process Synchronization 19 / 28

Implementation issues For bounded waiting, L should be maintained as a FIFO queue wait(s), signal(s) must be atomic two processes cannot be in wait/signal simultaneously wait / signal must be implemented as CS (!!) use hardware solutions (disable interrupt, etc.), software solutions busy waiting is limited to the CS in wait / signal only CS in wait/signal is short ( 10 instructions) and occupied for very short periods of time (CS in application programs may be long and almost always occupied) OR M. Mitra (ISI) Process Synchronization 20 / 28

Binary semaphores Definition: semaphore whose integer value can be either 0 or 1 wait b (S): if (S.value == 0) { add process to S.L; sleep(); else S.value = 0; signal b (S): if (S.value == 0) { P = dequeue(s.l); if (P == NULL) S.value = 1; else wakeup(p); M. Mitra (ISI) Process Synchronization 21 / 28

Binary semaphores Implementing counting semaphores: binary semaphore S1 = 1, S2 = 0; int C = m; wait(s): wait_b(s3); // why? wait_b(s1); C--; if (C < 0) { signal_b(s1); wait_b(s2); else signal_b(s1); signal_b(s3); signal(s): wait_b(s1); C++; if (C <= 0) signal_b(s2); signal_b(s1); M. Mitra (ISI) Process Synchronization 22 / 28

Producer-consumer problem. Reference: Section 6.5.1 semaphore full = 0, empty = n, mutex = 1; producer while (1) { p = produce(); wait(empty); wait(mutex); AddToBuffer(p); signal(mutex); signal(full); consumer while (1) { wait(full); wait(mutex); p = RemoveFromBuffer(); signal(mutex); signal(empty); consume(p); M. Mitra (ISI) Process Synchronization 23 / 28

Readers-writers problem. Reference: Section 6.5.2 Shared object is accessed by several concurrent processes Reader: processes that only read the shared object Writer: processes that update (read + write) the shared object Synchronization constraint: two or more readers can access shared data simultaneously; any writer must have exclusive access Variants: first readers-writers problem: readers have (non-preemptive) priority second readers-writers problem: writers have (non-preemptive) priority M. Mitra (ISI) Process Synchronization 24 / 28

Readers-writers problem semaphore mutex = 1, write access = 1, readcount = 0; reader wait(mutex); readcount++; if (readcount == 1) wait(write_access); signal(mutex); writer wait(write_access); write(); signal(write_access); read(); wait(mutex); readcount--; if (readcount == 0) signal(write_access); signal(mutex); M. Mitra (ISI) Process Synchronization 25 / 28

Dining philosophers problem Reference: Section 6.5.3. philosopher() { while(1) { think(); get_chopsticks(); eat(); release_chopsticks(); Only the nearest chopsticks can be used Only free chopsticks can be used Chopsticks must be acquired one by one M. Mitra (ISI) Process Synchronization 26 / 28

Dining philosophers problem semaphore chopstick[5] = {1,1,1,1,1; while (1) { think(); wait(chopstick[i]); wait(chopstick[(i+1) % 5]); eat(); signal(chopstick[i]); signal(chopstick[(i+1) % 5]); Deadlock avoidance: Philosophers can pick up chopsticks only if both are available, Odd philosphers pick up left chopstick first; even philosophers pick up right chopstick first. OR M. Mitra (ISI) Process Synchronization 27 / 28

Summary Manipulation of shared data race conditions Shared data should be accessed within critical sections Solutions to CSP: Algorithm 3 (slide 9), Bakery algorithm assume only atomic loads, stores, tests Hardware solutions assume atomic TestAndSet or Swap instructions Semaphores semaphores use one of the above solutions internally to ensure mutually exclusive access to semaphore variable may be implemented as system calls which block if necessary Example synchronization problems: producer-consumer, reader-writer, dining philosopher abstractions of problems that occur in real systems M. Mitra (ISI) Process Synchronization 28 / 28