COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Similar documents
Process Synchronization

Chapter 7: Process Synchronization. Background

Chapter 7: Process Synchronization. Background. Illustration

Chapter 7: Process Synchronization!

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

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

CS420: Operating Systems. Process Synchronization

Chapter 6: Process Synchronization

Lesson 6: Process Synchronization

Process Coordination

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

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

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

CHAPTER 6: PROCESS SYNCHRONIZATION

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

Synchronization Principles

Chapter 5: Process Synchronization

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

Chapter 5: Process Synchronization

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

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

Module 6: Process Synchronization

CS370 Operating Systems Midterm Review

Process Synchronization(2)

Module 6: Process Synchronization

Chapter 5: Process Synchronization

Process Synchronization(2)

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

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

Dept. of CSE, York Univ. 1

Synchronization for Concurrent Tasks

Synchronization Principles I

CSE 4/521 Introduction to Operating Systems

Process Synchronization

Process Synchronization

High-level Synchronization

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

Process Synchronization

Process Synchronization

Process Synchronization(2)

Process Synchronization

CS3733: Operating Systems

Process Synchronization

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

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

CS370 Operating Systems

Chapter 6: Process Synchronization

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Real-Time Operating Systems M. 5. Process Synchronization

Multitasking / Multithreading system Supports multiple tasks

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

Process Synchronization

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

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

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

CS370 Operating Systems

Process Synchronization

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

Chapter 6 Synchronization

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

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

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

Chapter 6: Process Synchronization

Process Synchronization

Lecture 5: Inter-process Communication and Synchronization

Process/Thread Synchronization

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

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

Synchronization Classic Problems

Process/Thread Synchronization

CSE Opera,ng System Principles

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

Lecture 6. Process Synchronization

Concurrency and Synchronisation

Interprocess Communication By: Kaushik Vaghani

Concurrency and Synchronisation

Synchronization. Dr. Yingwu Zhu

Chapter 7 Process Synchronization

Unit 1: Introduction

CS370 Operating Systems

Chapter 6: 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 Synchronization (Part I)

Process Synchronization Mechanisms

Process Co-ordination OPERATING SYSTEMS

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

UNIT II PROCESS MANAGEMENT 9

Synchronization Spinlocks - Semaphores

IV. Process Synchronisation

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

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

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

Classic Problems of Synchronization

1. Motivation (Race Condition)

Lecture 3: Synchronization & Deadlocks

CS370 Operating Systems

Transcription:

COP 4225 Advanced Unix Programming Synchronization Chi Zhang czhang@cs.fiu.edu 1

Cooperating Processes Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation Information sharing Computation speed-up Modularity Convenience 2

Producer-Consumer Problem Share the variables Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process. bounded-buffer (circular array) assumes that there is a fixed buffer size. A variable counter, initialized to 0 and incremented each time a new item is added to the buffer 3

Problem Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. 4

Bounded-Buffer: Producer Process item nextproduced; while (1) { while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = nextproduced; in = (in + 1) % BUFFER_SIZE; counter++; } 5

Bounded-Buffer: Consumer Process item nextconsumed; while (1) { while (counter == 0) ; /* do nothing */ nextconsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; } 6

Bounded Buffer The following statements must be performed atomically. counter++; register1 = counter register1 = register1 + 1 counter = register1 counter--; register2 = counter register2 = register2 1 counter = register2 7

Bounded Buffer If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved. Interleaving depends upon how the producer and consumer processes are scheduled. 8

The Critical-Section Problem Race condition: The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized. Each process has a code segment, called critical section, in which the shared data is accessed. Mutual Exclusion ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section. 9

Semaphores Semaphore S integer variable can only be accessed via two indivisible (atomic) operations. (S initialized to be the number of concurrent processes allowed. S==1 Mutex) wait (S): signal (S): while S 0 do no-op; S--; S++; 10

Critical Section of n Processes Shared data: semaphore mutex; //initially mutex = 1 Process Pi: do { wait(mutex); critical section signal(mutex); remainder section } while (1); 11

Semaphore Implementation: SpinLock Busy waiting Waste of CPU Useful with Multiple Processors and short lock time Context Switch is expensive Disable interrupt and use atomic operations with SMP spin_lock: 1: lock; decb slp jns 3f spin_unlock: Lock; movb $1, slp 2: cmpb $0, slp pause jle 2b 3: jmp 1b 12

Semaphore Implementation Define a semaphore as a record typedef struct { int value; struct process *L; // a queue of PCB } semaphore; Assume two simple operations: block suspends the process that invokes it. wakeup(p) resumes the execution of a blocked process P. 13

Implementation Semaphore operations now defined as wait(s): S.value--; if (S.value < 0) { add this process to S.L; block; } o signal(s): S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(p); } S<0: its magnitude is the number of waiting processes 14

Bounded-Buffer Problem Shared data mutex:mutual exclusion for the critical section full: the number of full buffers; for synchronization empty: the number of empty buffers; for synchronization. semaphore full, empty, mutex; Initially: full = 0, empty = n, mutex = 1 15

Bounded-Buffer Problem Producer Process do { produce an item in nextp wait(empty); wait(mutex); add nextp to buffer signal(mutex); signal(full); } while (1); 16

Bounded-Buffer Problem Consumer Process do { wait(full) wait(mutex); remove an item from buffer to nextc signal(mutex); signal(empty); consume the item in nextc } while (1); 17

Critical Regions High-level synchronization construct A shared variable v of type T, is declared as: v: shared T Variable v accessed only inside statement region v when B do S where B is a boolean expression. 18

Solaris 2 Synchronization Implements a variety of locks to support multitasking, multithreading (including real-time threads), and multiprocessing. Uses adaptive mutexes for efficiency when protecting data from short code segments. On a multiple processor system, an adaptive mutex starts as a spinlock. If the thread holding the lock is not currently running, the calling thread blocks and sleeps until the lock is released. On a uniprocessor system, the thread always sleep rather than spin. Uses condition variables and readers-writers locks when longer sections of code need access to data. Multiple threads may read data concurrently. 19