Introduction to programming with semaphores Fri 1 Sep 2017

Similar documents
Lecture 3: Semaphores (chap. 6) K. V. S. Prasad Dept of Computer Science Chalmer University 6 Sep 2013

Lecture 11: More on invariants and textual reasoning - examples. K. V. S. Prasad Dept of Computer Science Chalmers University Friday 30 Sep 2016

G52CON: Concepts of Concurrency

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

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

PROCESS SYNCHRONIZATION

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

Concurrency. Chapter 5

Threading and Synchronization. Fahd Albinali

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

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

EECS 482 Introduction to Operating Systems

CS370 Operating Systems

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

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

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Models of concurrency & synchronization algorithms

CMSC421: Principles of Operating Systems

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

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

Semaphores and Monitors: High-level Synchronization Constructs

Semaphores. May 10, Mutual exclusion with shared variables is difficult (e.g. Dekker s solution).

Concurrent Processes Rab Nawaz Jadoon

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

Introduction to OS Synchronization MOS 2.3

Concurrency: a crash course

Synchronization 1. Synchronization

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

Lecture 5: Synchronization w/locks

1 Process Coordination

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Remaining Contemplation Questions

CSE 153 Design of Operating Systems

Symmetric Multiprocessors: Synchronization and Sequential Consistency

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

CS 550 Operating Systems Spring Concurrency Semaphores, Condition Variables, Producer Consumer Problem

Process Management And Synchronization

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Process Synchronization

CS 153 Design of Operating Systems Winter 2016

CoSc 450: Programming Paradigms. The Critical Section Problem

CS 361 Concurrent programming Drexel University Fall 2004 Lecture 8. Proof by contradiction. Proof of correctness. Proof of mutual exclusion property

Coordination 1. To do. Mutual exclusion Election algorithms Next time: Global state. q q q

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

Lecture 3: Intro to Concurrent Processing using Semaphores

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd 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

[module 2.2] MODELING CONCURRENT PROGRAM EXECUTION

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

Dealing with Issues for Interprocess Communication

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

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

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

IV. Process Synchronisation

IT 540 Operating Systems ECE519 Advanced Operating Systems

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

Synchronization 1. Synchronization

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

Reminder from last time

Concurrent & Distributed Systems Supervision Exercises

Interprocess Communication By: Kaushik Vaghani

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

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

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

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

C09: Process Synchronization

Computing with Infinitely Many Processes under assumptions on concurrency and participation -M.Merritt&G.Taubenfeld. Dean Christakos & Deva Seetharam

CS 318 Principles of Operating Systems

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

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

UNIX Input/Output Buffering

CSC501 Operating Systems Principles. Process Synchronization

Chapters 5 and 6 Concurrency

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

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

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Concurrent Programming TDA381/DIT390

Lesson 6: Process Synchronization

Introduction to Operating Systems

Process Synchronization Mechanisms

CS3502 OPERATING SYSTEMS

Introducing Shared-Memory Concurrency

Operating Systems ECE344

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

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

Operating Systems EDA092, DIT 400 Exam

Chapter 5 Asynchronous Concurrent Execution

CSCI 447 Operating Systems Filip Jagodzinski

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

Max and Programming Is Max a Programming Language?

Operating Systems. Synchronization

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Transcription:

Introduction to programming with semaphores Fri 1 Sep 2017 K. V. S. Prasad Dept of Computer Science Chalmers University Lecture 2 of TDA384/DIT301, August October2017 Assignments? How s it going with the Erlang tutorial? Anyone seriously worried about FP? Did you look at the Java in lab 3? Comfortable with it? Did you glance through Ben-Ari or at least his slides? 1

Recap Motivations Simulate multi-agent systems; 2 agents or 10 million For greater speed through parallelism Main abstraction a process Could be a CPU, a person, an ant, a bot, Cooperates, coordinates, competes, communicates with other processes Usingshared CPUs, memorycells, communication channels, Time The main trick is to manage time In a purely parallel setup like music, every process follows the time axis. In cinema, time expands, shrinks, is skipped, and goes back and forth. Intercutting = processes share the screen (CPU) -- may or may not run off screen. 2

Concurrent systems Real time systems are a bit like music actual time matters Parallel programs are like music too all play all the time (counting rests as playing) Concurrent systems are like multi-screen cinema But no skipping, no going back Must show sending of a signal or message or event before receipt Time reduced to sequencing simpler, allows flexible scheduling But don t sneak actual time in the back door Remember the train crash never mind token, you have time to escape Execution model and scenarios Of all the processes in the program pick any runnable one, and execute one step Or, pick any subset of runnable processes and do one step of each Since the picking is arbitrary, we get non-determinism So usual debugging (breakpoints, stepping, starting over, etc.) fails Need to check every possible run Done by model checkers such as SPIN Or by examining the program text 3

Sleep instead of busy wait Don t check every cycle for condition (event, message) Instead, say I ll sleep. Wake me up on condition. Run time support or Operating system will wake you up. It will also schedule a runnable (non-sleeping) process onto an available CPU. If no CPU available, a process is runnable but not running Hence Ben-Ari chap 6 model of process states People trying to borrow the lone library book - failing program book := free a1: loop until book=free; a2: book:= borrowed b1: loop until book=free; b2: book:= borrowed students a and b might execute a1 and b1 in parallel, and then a2 and b2 in parallel, resulting in double booking. And yet both politely checked availability first. a1 and b1 use a test instruction (=, yielding true/false), while a2 and b2 are set instructions. It is the separation between test and set that allows interference. 4

Hardware solution an atomic test-and-set instruction tset(mine,library) = atomic{mine:=library; library:=0} Mine and library say how many books (0 or 1) we each have To begin with, library=1, and both students have respective mine=0 library := 1 a: loop tset(mine,library) until mine=1; b : loop tset(mine,library) until mine=1; Students a and b can execute their loops a and b in parallel, and only one will emerge with the book. But these loops are busy waits. Semaphore = software test-and-set, plus wake-up services Suppose you had a wait(book) instruction, which if book=free, gives you the book if book=borrowed, puts you to sleep and wakes you when it is free This achieves the same as the loops in the previous slide You emerge with a book or go to sleep (instead of being stuck in a busy loop) 5

9/1/17 Returning the book? In the hardware solution, simply set library:=1 If you only use mine via the tset, you don t need to set mine:=0 In the software solution, we need to be a little more sophisticated 6

9/1/17 Alg 6.1 of Ben-Ari Each student does loop other stuff //non-critical section (NCS) sleep till you borrow book // pre-protocol read book //critical section (CS) return book //post-protocol 7

CS assumptions and requirements Assumptions NCS may loop - Students may drop out of course here But CS must terminate must return borrowed book Requirements book is issued to at most one student at a time (mutex) Attempt to borrow will succeed (liveness, no starvation) Multiple students trying to borrow -> one will succeed i.e., no deadlock (= all sleeping, so no one wakes-up another) Proof by state diagram, figure 6.1 Extends easily to multiple students need fair semaphores ensure no one is ignored all the time Extends easily to multiple copies of book need general semaphore 8

Producer - consumer Buffers can only even out transient delays Average speed must be same for both Infinite buffer first. Means Producer neverwaits Only one semaphore needed Need partial state diagram Signal in a loop See algs 6.6 and 6.7 Infinite buffer is correct Invariant #sem = #buffer 0 initially Incremented by append-signal Need more detail if this is not atomic Decremented by wait-take So cons cannot take from empty buffer Only cons waits so no deadlock or starvation, since prod will always signal 9

Bounded buffer See alg 6.8 (p 119, s 6.12) Two semaphores Cons waits if buffer empty Prodwaits if buffer full Each proc needs the other to release its sem Different from CS problem Split semaphores Invariant notempty + notfull = initially empty places Old Psuedo-code for single place buffer flag := empty producer loop d1: loop until flag=empty; R := new record; d2: flag := full consumer loop p1: loop until flag=full; temp:= R; p2: flag := empty 10

Why does this single flag code work? Just one semaphore? The two conditions are mutually exclusive and complementary Flag can only be empty or full, and not both at once Each process flips the flag to the value the other is waiting for The flag is like an ad-hoc semaphore with both wait and wait -wait = wait for 0 instead of wait for 1 Such ad-hoc solutions might exist in many cases. The two semaphore solution for the bounded buffer uses standard primitives, an advantage in itself Ad hoc solutions require more care 11