G52CON: Concepts of Concurrency

Similar documents
G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

CS 4410 Operating Systems. Review 1. Summer 2016 Cornell University

Chapter 8. Basic Synchronization Principles

G52CON: Concepts of Concurrency

Semaphores. Derived Inference Rules. (R g>0) Q g (g 1) {R} V (g) {Q} R Q g (g+1) V (s), hs := 1; i

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Semaphores. Mutual Exclusion. Inference Rules. Fairness: V may release a waiting process. Weakly fair, Strongly fair, or FIFO

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

2 Threads vs. Processes

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Running. Time out. Event wait. Schedule. Ready. Blocked. Event occurs

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Concurrent Computing

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

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

Reminder from last time

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

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

G52CON: Concepts of Concurrency Lecture 15: Message Passing. Gabriela Ochoa School of Computer Science & IT

Threading and Synchronization. Fahd Albinali

Midterm Exam. October 20th, Thursday NSC

Process synchronization

Reminder from last time

PROCESS SYNCHRONIZATION

Lecture 10: Introduction to Semaphores

Chapter 6: Process Synchronization

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

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

Semaphores. A semaphore is an object that consists of a. methods (e.g., functions): signal and wait. semaphore. method signal. counter.

Operating Systems ECE344

Concept of a process

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

ECE469 - Operating Systems Engineering Exam # March 25

G52CON: Concepts of Concurrency

Chapter 2 Processes and Threads

Two hours. Question ONE is COMPULSORY UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 25th January 2013 Time: 14:00-16:00

Subject: Operating System (BTCOC403) Class: S.Y.B.Tech. (Computer Engineering)

Main Points of the Computer Organization and System Software Module

Synchronization Classic Problems

Opera&ng Systems ECE344

CS 431 Introduction to Computer Systems Solutions Mid semester exam 2005

Dealing with Issues for Interprocess Communication

IT 540 Operating Systems ECE519 Advanced Operating Systems

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

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

Lecture 3: Intro to Concurrent Processing using Semaphores

Concurrency and Synchronisation

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

Concurrency and Synchronisation

CSC501 Operating Systems Principles. Process Synchronization

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL. Chap. 2.2 Interprocess Communication

G52CON: Concepts of Concurrency

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

C09: Process Synchronization

10/17/2011. Cooperating Processes. Synchronization 1. Example: Producer Consumer (3) Example

IV. Process Synchronisation

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

Process Synchronization

Monitors; Software Transactional Memory

Chapter 6 Synchronization

Last Class: Synchronization

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

CSc33200: Operating Systems, CS-CCNY, Fall 2003 Jinzhong Niu December 10, Review

Critical Section Problem: Example

Concurrent Processes Rab Nawaz Jadoon

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

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

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

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

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Lecture 15: I/O Devices & Drivers

message passing model

Lecture 6. Process Synchronization

Synchronization for Concurrent Tasks

Performance Throughput Utilization of system resources

CS 153 Design of Operating Systems Winter 2016

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

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

Comp 204: Computer Systems and Their Implementation. Lecture 9: Deadlock

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

Resource management. Real-Time Systems. Resource management. Resource management

Chapter 5: Synchronization 1

Monitors; Software Transactional Memory

Semaphores. Blocking in semaphores. Two types of semaphores. Example: Bounded buffer problem. Binary semaphore usage

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

REAL-TIME MULTITASKING KERNEL FOR IBM-BASED MICROCOMPUTERS

Transcription:

G52CON: Concepts of Concurrency Lecture 8 Semaphores II Natasha Alechina School of Computer Science nza@cs.nott.ac.uk

Outline of this lecture problem solving with semaphores solving Producer-Consumer problems using buffers: single element buffer bounded buffer informal exercise 2 G52CON Lecture 8: Semaphores II 2

Producer-Consumer problem Given two processes, a producer which generates data items, and a consumer which consumes them, find a mechanism for passing data from the producer to the consumer such that: no items are lost or duplicated in transit; items are consumed in the order they are produced; and all items produced are eventually consumed. G52CON Lecture 8: Semaphores II 3

Variants of the problem The single Producer single Consumer problem can be generalised: multiple producers single consumer single producer multiple consumers multiple producers multiple consumers G52CON Lecture 8: Semaphores II 4

Buffer-based solutions In multiprogramming or multiprocessing implementations of concurrency, communication between a producer and a consumer is often implemented using a shared buffer: a buffer is an area of memory used for the temporary storage of data while in transit from one process to another. the producer writes into the buffer and the consumer reads from the buffer, e.g., a Unix pipe. G52CON Lecture 8: Semaphores II 5

Interprocess communication produce data item Producer put it in the buffer Buffer take item from the buffer consume (use) data item Consumer G52CON Lecture 8: Semaphores II 6

Synchronisation The general multiple Producer-multiple Consumer problem requires both mutual exclusion and condition synchronisation: mutual exclusion is used to ensure that more than one producer or consumer does not access the same buffer slot at the same time; condition synchronisation is used to ensure that data is not read before it has been written, and that data is not overwritten before it has been read. Synchronisation can be achieved using any of the techniques we have seen so far: e.g., Peterson s algorithm, semaphores. G52CON Lecture 8: Semaphores II 7

General synchronisation conditions Buffer-based solutions to the Producer Consumer problem should satisfy the following conditions: no items are read from an empty buffer; data items are read only once; data items are not overwritten before they are read; items are consumed in the order they are produced; and all items produced are eventually consumed. in addition to the properties of Mutual Exclusion, Absence of Deadlock, Absence of Unnecessary Delay and Eventual Entry. G52CON Lecture 8: Semaphores II 8

Solutions may be judged on different criteria, e.g., correctness, fairness, efficiency may use different sizes of buffer, and different protocols for synchronising access to the buffer a particular solution can be implemented using different synchronisation primitives, e.g., spin locks or semaphores a particular synchronisation primitive or protocol can be implemented in different ways, e.g., busy waiting, blocking G52CON Lecture 8: Semaphores II 9

Infinite buffer The producer and consumer communicate via an infinite shared buffer: no items are read from an empty buffer; data items are read only once; the producer may produce a new item at any time; items are consumed in the order they are produced; and all items produced are eventually consumed. G52CON Lecture 8: Semaphores II 10

Infinite buffer solution // Producer process Object v = null; integer in = 0; while(true) { // produce data v... buf[in] = v; in = in + 1; V(n); } // Consumer process Object w = null; integer out = 0; while(true) { P(n); w = buf[out]; out = out + 1; // use the data w... } // Shared variables Object[] buf = new Object[ ]; general semaphore n = 0; G52CON Lecture 8: Semaphores II 11

Problem 1: single element buffer Devise a solution to Producer Consumer problem using a single element buffer which ensures that: the producer may only produce an item when the buffer is empty; and the consumer may only consume an item when the buffer is full. Your solution should satisfy the general synchronisation requirements for the Producer-Consumer problem and the properties of Mutual Exclusion, Absence of Deadlock, Absence of Unnecessary Delay and Eventual Entry. G52CON Lecture 8: Semaphores II 12

Infinite vs single element buffer with an infinite buffer we had only one problem to prevent the consumer getting ahead of the producer with a single element buffer we have two problems preventing the consumer getting ahead of the producer; and preventing the producer getting ahead of the consumer. G52CON Lecture 8: Semaphores II 13

Problem 1: first attempt // Producer process Object x = null; while(true) { // produce data x... P(s); buf = x; V(s); } // Consumer process Object y = null; while(true) { P(s); y = buf; V(s); // use the data y... } // Shared variables Object buf; binary semaphore s = 1; G52CON Lecture 8: Semaphores II 14

Properties of the first attempt Does the first attempt satisfy the following properties: Mutual Exclusion: yes Absence of Deadlock: yes Absence of Unnecessary Delay: yes Eventual Entry: yes G52CON Lecture 8: Semaphores II 15

First attempt synchronisation conditions Does the solution satisfy the following properties: no items are read from an empty buffer: no data items are read only once: no data items are not overwritten before they are read: no items are consumed in the order they are produced: yes all items produced are eventually consumed: no G52CON Lecture 8: Semaphores II 16

Problem 1: second attempt // Producer process Object x = null; while(true) { // produce data x... buf = x; V(s); } // Consumer process Object y = null; while(true) { P(s); y = buf; // use the data y... } // Shared variables Object buf; binary semaphore s = 0; G52CON Lecture 8: Semaphores II 17

Properties of the second attempt Does the second attempt satisfy the following properties: Mutual Exclusion: no Absence of Deadlock: yes Absence of Unnecessary Delay: yes Eventual Entry: yes G52CON Lecture 8: Semaphores II 18

Second attempt synchronisation conditions Does the solution satisfy the following properties: no items are read from an empty buffer: yes data items are read only once: at most three times data items are not overwritten before they are read: no items are consumed in the order they are produced: yes all items produced are eventually consumed: no G52CON Lecture 8: Semaphores II 19

Second attempt synchronisation conditions Producer: write to shared variable Producer: V operation Producer: write to shared variable Producer: V operation Producer: write to shared variable Producer: V operation Consumer: P operation Consumer: read from shared variable Consumer: P operation Consumer: read from shared variable Consumer: P operation Consumer: read from shared variable G52CON Lecture 8: Semaphores II 20

Single element buffer solution // Producer process Object x = null; while(true) { // produce data x... P(empty); buf = x; V(full); } // Consumer process Object y = null; while(true) { P(full); y = buf; V(empty); // use the data y... } // Shared variables Object buf; binary semaphore empty = 1, full = 0; G52CON Lecture 8: Semaphores II 21

Properties of the single buffer solution The single element buffer solution satisfies the following properties: Mutual Exclusion: yes Absence of Deadlock: yes Absence of Unnecessary Delay: yes Eventual Entry: yes G52CON Lecture 8: Semaphores II 22

Single buffer solution synchronisation conditions The single element buffer solution satisfies the following properties: no items are read from an empty buffer: yes data items are read only once: yes data items are not overwritten before they are read: yes items are consumed in the order they are produced: yes all items produced are eventually consumed: yes G52CON Lecture 8: Semaphores II 23

Applications of Single element buffers I/O to all types of peripheral devices dedicated programs running on bare machines. G52CON Lecture 8: Semaphores II 24

Larger buffers A single element buffer works well if the Producer and Consumer processes run at the same rate: processes don t have to wait very long to access the single buffer many low-level synchronisation problems are solved in this way, e.g., interrupt driven I/O. If the speed of the Producer and Consumer is only the same on average, and fluctuates over short periods, a larger buffer can significantly increase performance by reducing the number of times processes block. G52CON Lecture 8: Semaphores II 25

Bounded buffers A bounded buffer of length n is a circular communication buffer containing n slots. The buffer contains a queue of items which have produced but not yet consumed. For example 0 1 2 3 4 5 6 7 8 9 o 5 o 6 o 1 o 2 o 3 o 4 in out out is the index of the item at the head of the queue, and in is the index of the first empty slot at the end of the queue. G52CON Lecture 8: Semaphores II 26

Problem 2: bounded buffer Devise a solution to Producer Consumer problem using a bounded buffer which ensures that: the producer may only produce an item when there is an empty slot in the buffer; and the consumer may only consume an item when there is a full slot in the buffer. Your solution should satisfy the general synchronisation requirements for the Producer-Consumer problem and the properties of Mutual Exclusion, Absence of Deadlock, Absence of Unnecessary Delay and Eventual Entry. G52CON Lecture 8: Semaphores II 27

Single element vs bounded buffer Note that the synchronisation conditions are really the same as for the single element (& infinite) buffer: the producer may only produce an item when the buffer is not full; and the consumer may only consume an item when the buffer is not empty. and the problems are the same: preventing the consumer getting ahead of the producer; and preventing the producer getting ahead of the consumer. G52CON Lecture 8: Semaphores II 28

Bounded buffer solution // Producer process Object x = null; integer in = 0; while(true) { // produce data x... P(empty); buf[in] = x; in = (in + 1) % n; V(full); } // Consumer process Object y = null; integer out = 0; while(true) { P(full); y = buf[out]; out = (out + 1) % n; V(empty); // use the data y... } // Shared variables integer n = BUFFER_SIZE; Object[] buf = new Object[n]; general semaphore empty = n, full = 0; G52CON Lecture 8: Semaphores II 29

Properties of the bounded buffer solution The bounded buffer solution satisfies the following properties: Mutual Exclusion: yes Absence of Deadlock: yes Absence of Unnecessary Delay: yes Eventual Entry: yes G52CON Lecture 8: Semaphores II 30

Bounded buffer solution synchronisation conditions The bounded buffer solution satisfies the following properties: data items are not overwritten before they are read: yes data items are read only once: yes no items are read from an empty buffer: yes items are consumed in the order they are produced: yes all items produced are eventually consumed: yes G52CON Lecture 8: Semaphores II 31

Bounded buffer solution 2 // Producer process Object x = null; integer in = 0; while(true) { // produce data x... P(empty); buf[in] = x; V(full); in = (in + 1) % n; } // Consumer process Object y = null; integer out = 0; while(true) { P(full); y = buf[out]; V(empty); out = (out + 1) % n; // use the data y... } // Shared variables final integer n = BUFFER_SIZE; Object[] buf = new Object[n]; general semaphore empty = n, full = 0; G52CON Lecture 8: Semaphores II 32

Applications of bounded buffers Bounded buffers are used for serial input and output streams in many operating systems: Unix maintains queues of characters for I/O on all serial character devices such as keyboards, screens and printers. Unix pipes are implemented using bounded buffers. G52CON Lecture 8: Semaphores II 33

Exercise 2: Semaphores a) devise a solution to multiple Producer multiple Consumer problem using a bounded buffer which ensures that: no items are read from an empty buffer; data items are read only once; data items are not overwritten before they are read; items are consumed in the order they are produced; and all items produced are eventually consumed. b) does your solution satisfy the properties of Mutual Exclusion, Absence of Deadlock, Absence of Unnecessary Delay and Eventual Entry? c) how many classes of critical sections does your solution have? G52CON Lecture 8: Semaphores II 34

The next lecture Suggested reading: Monitors Andrews (2000), chapter 5; Ben-Ari (1982), chapter 5; Burns & Davies (1993), chapter 7, sections 7.4 7.9. G52CON Lecture 8: Semaphores II 35