Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors

Similar documents
Operating Systems 2010/2011

Interprocess Communication

Chapter 4 Multithreaded Programming

System Programming. Signals I

Signals. Joseph Cordina

ECE 650 Systems Programming & Engineering. Spring 2018

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

System Calls & Signals. CS449 Spring 2016

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 6

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

CSE 421: Introduction to Operating Systems

Princeton University. Computer Science 217: Introduction to Programming Systems. Signals

Fortran Signal Handling

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

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

COSC243 Part 2: Operating Systems

1 Process Coordination

Lecture 4 Threads. (chapter 4)

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Concept of a process

Dealing with Issues for Interprocess Communication

Signals. Goals of this Lecture. Help you learn about: Sending signals Handling signals

CSE Traditional Operating Systems deal with typical system software designed to be:

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Signals and Session Management. Signals. Mechanism to notify processes of system events

Chapter 1 Introduction

Shell Execution of Programs. Process Groups, Session and Signals 1

Concurrent Processes Rab Nawaz Jadoon

Signals: Management and Implementation. Sanjiv K. Bhatia Univ. of Missouri St. Louis

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Embedded Systems Programming

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

APPLIED INFORMATICS Processes. Bash characteristics. Command type. Aliases.

IV. Process Synchronisation

Preview. Process Termination. wait and waitpid() System Call. wait and waitpid() System Call. waitpid() System Call 10/23/2018

So far, we know how to create processes. How do we communicate with them? Primary mechanism: signals

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Last Class: Synchronization

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

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

Process. Signal #8. Signals are software interrupts from unexpected events. a power failure. an alarm clock. the death of a child process

Lecture 9: Real-Time Software Design Issues

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

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Overview. POSIX signals. Generation of signals. Handling signals. Some predefined signals. Real-time Systems D0003E 3/3/2009

* What are the different states for a task in an OS?

Acontecimentos assíncronos (POSIX signals) Sincronização com múltiplos acontecimentos

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

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

Systems Programming/ C and UNIX

Critical Section Problem: Example

Concurrency. Chapter 5

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

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

CS Operating Systems

CS Operating Systems

11 Resource Management Deadlock and Starvation

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

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Chapter 4: Threads. Operating System Concepts 9 th Edition

CS370 Operating Systems

Concurrency and Synchronisation

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

Goals of this Lecture

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

Note: in this document we use process and thread interchangeably.

C09: Process Synchronization

CS240: Programming in C

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Operating Systems ECE344

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

CS370 Operating Systems

Technical Document Series. POSIX Signal Handling in Java

Lecture 8: September 30

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

Chapter 9. Signals and Signal Processing

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

Interprocess Communication By: Kaushik Vaghani

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

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

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

Midterm Exam. October 20th, Thursday NSC

Process Synchronization Mechanisms

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

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

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

IT 540 Operating Systems ECE519 Advanced Operating Systems

Synchronization Lecture 24 Fall 2018

Lesson 6: Process Synchronization

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

Concurrent Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Transcription:

4 Concurrency Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors 2009 Universität Karlsruhe (TU), System Architecture Group 1

Roadmap for Today Concurrency Problems Producer / Consumer and Reader / Writer Synchronization Mechanisms Signal Semaphore Monitor 2009 Universität Karlsruhe (TU), System Architecture Group 2

Concurrency Problems Producer Consumer Problem Reader Writer Problem 2009 Universität Karlsruhe (TU), System Architecture Group 3

Producer/Consumer & Bounded Buffer Concurrency Problems 6 5 4 3 2 1 Concurrency problems with bounded buffers? Problems with p>1 producers or c>1 consumers? 2009 Universität Karlsruhe (TU), System Architecture Group 4

Concurrency Problems Reader/Writer Problem file-document Problems? Possible solutions? 2009 Universität Karlsruhe (TU), System Architecture Group 5

Assignment 2 All concurrency problems have to be solved using Java monitors or specific self-made semaphores implemented by Java monitors Study how to use those Java monitors (some hints are given in the assignments) We do not accept solutions where one centralized thread is used to do the sequencing job, ie somewhere in your code there must be properly positioned assignments with wait() and notify() Do a nice graphic to visualize your solutions of the experiments 2009 Universität Karlsruhe (TU), System Architecture Group 6

Signal Mechanism History of Signals Application of signals Don t mix up with Unix signals 2009 Universität Karlsruhe (TU), System Architecture Group 7

Signaling Semantics of Signals Pay Attention (see a siren) Stop (see road signs) Go Ahead (officer at a train station) Interrupt or (arbiter in a soccer game) Resume Playing 2009 Universität Karlsruhe (TU), System Architecture Group 8

Signaling Implementing a Signal Flag 1 = Signal set, 0 = Signal reset Continuation (signaled thread may continue) Stop (signaled thread has to wait) Abort (signaled thread has to be aborted) see signal vector in Unix or Linux Counter Any value may have a different meaning or just reflects the number of pending signals Problem: Try to find out when a flag is sufficient or when you better use a counter variable! 2009 Universität Karlsruhe (TU), System Architecture Group 9

Synchronization Synchronizing a Precedence Relation {Thread 1} { section a1 } T1 a1 T2 b1 {Thread 2} { section b1 } { section a2 } a2 b2 { section b2 } Problem: How to achieve that a1 <* b2 (a1 precedes b2), ie section b2 has to wait until section a1 has completed 2009 Universität Karlsruhe (TU), System Architecture Group 10

Synchronize a Precedence Relation Synchronization 1:1_signal s; /* type 1:1_signal_object */ {Thread 1} { section a1 } Signal(s) { section a2 } {Thread 2} { section b1 } Wait(s) { section b2 } Problem: How to implement a 1:1_signal_object? 2009 Universität Karlsruhe (TU), System Architecture Group 11

Signaling User-Level Signal Object: FLAG Simple flag s as a common shared global variable of both threads signal(s) set s busy waiting no wait(s) s == set? reset s Hint: Discuss this approach carefully! Does it work on every system effectively and/or efficiently? 2009 Universität Karlsruhe (TU), System Architecture Group 12

Synchronization Principal Types of Solutions Software Solutions (at application level) Algorithms neither rely on special processor hardware nor on special OS features Hardware Solutions Rely on some special machine instructions Offering a kind of atomicity OS Solutions (offered by kernel) Provide kernel-interface functions for application programmers Remark: Most systems offer only a subset of these solutions 2009 Universität Karlsruhe (TU), System Architecture Group 13

Semaphores 2009 Universität Karlsruhe (TU), System Architecture Group 14

Signaling Dijkstras (Counting) Semaphores Definition: A semaphore S is an integer variable that, apart from initialization, can only be accessed by 2 atomic and mutually exclusive operations P(S) V(S) P ~ Passeren (from Dutch signaling language some say proberen ~ decrement) V ~ Verlaaten (see above, some say verhogen ~ increment ) 2009 Universität Karlsruhe (TU), System Architecture Group 15

Signaling Dijkstras (Counting) Semaphores How to design and implement counting semaphores? To avoid busy waiting: When thread cannot passeren inside of P(S) put calling thread into a blocked queue waiting for an event Occurrence of event will be signaled via V(S) by another thread (hopefully) What happens if not? 2009 Universität Karlsruhe (TU), System Architecture Group 16

Signaling Dijkstras Semaphores Semantic of a counting semaphore (for signaling): A positive value of counter indicates: #signals currently pending A negative value of the counter indicates: #threads waiting for a signal, ie are queued within the semaphore object If counter == 0 no thread is waiting and no signal is pending Remark (from Margo Seltzer, Harvard USA): A semaphore offers a simple and elegant mechanism for mutual exclusion and other things 2009 Universität Karlsruhe (TU), System Architecture Group 17

Counting Semaphores (First solution) module semaphore export p, v import BLOCK, UNBLOCK type semaphore = record Count: integer = 0 QWT: list of Threads = empty end p(s:semaphore) p SCount = SCount - 1 if SCount < 0 then insert (SQWT, myself) sleep(myself) fi v(s:semaphore) end SCount = SCount + 1 if SCount <= 0 then wakeup(delete first(sqwt)) fi {no signal pending} {no waiting threads} {+ 1 waiting thread} {+ 1 pending signal} Signaling 2009 Universität Karlsruhe (TU), System Architecture Group 18

Unix Signaling Unix Signals Besides a terrible notation (eg kill = signal) no common semantics nor a widely accepted interface They are four different versions: System-V unreliable BSD System-V reliable POSIX Using Unix signals may lead to severe race conditions Programming is quite cumbersome 2009 Universität Karlsruhe (TU), System Architecture Group 19

Unix Signals Unix Signaling SIGNAL ID DEFAULT DESCRIPTION =========================================================== SIGHUP 1 Termination Hang up on controlling terminal SIGINT 2 Termination Interrupt Generated when we enter CTRL-C SIGQUIT 3 Core Generated when at terminal we enter CTRL-\ SIGILL 4 Core Generated when we execute an illegal instruction SIGTRAP 5 Core Trace trap (not reset when caught) SIGABRT 6 Core Generated by the abort function SIGFPE 8 Core Floating Point error SIGKILL 9 Termination Termination (can't catch, block, ignore) SIGBUS 10 Core Generated in case of hardware fault or invalid address SIGSEGV 11 Core Generated in case of illegal address SIGSYS 12 Core Generated when we use a bad argument in a system service call SIGPIPE 13 Termination Generated when writing to a pipe/socket when no reader anymore SIGALRM 14 Termination Generated by clock when alarm expires SIGTERM 15 Termination Software termination signal SIGURG 16 Ignore Urgent condition on IO channel SIGCHLD 20 Ignore A child process has terminated or stopped SIGTTIN 21 Stop Generated when a background process reads from terminal SIGTTOUT 22 Stop Generated when a background process writes to terminal SIGXCPU 24 Discard CPU time has expired SIGUSR1 30 Termination User defiled signal 1 SIGUSR2 31 Termination User defined signal 2 2009 Universität Karlsruhe (TU), System Architecture Group 20

Recommended Reading Bacon, J: OS (9, 10, 11) Exhaustive (all POSIX thread functions) Event handling, Path Expressions etc Nehmer, J: Grundlagen moderner BS (6, 7, 8) Silberschatz, A: OS Concepts (3, 4, 6) Stallings, W: OS (5, 6) Tanenbaum, A: MOS (2) 2009 Universität Karlsruhe (TU), System Architecture Group 21

Mutual Exclusion 2009 Universität Karlsruhe (TU), System Architecture Group 22

Critical Sections When a thread accesses shared data or an exclusive resource, thread executes a critical section (CS) A thread may have different CSs, even nested ones Executing a CS must be mutually exclusive, ie at any time, only 1 thread is allowed to execute the related CS Each thread must request the permission to enter a critical section (CS), ie it must obey a certain protocol 2009 Universität Karlsruhe (TU), System Architecture Group 23

Critical Sections Suppose: All Ti are KLTs of same Task (IP of) T 1 is in its red CS Question: What IP i are valid at the same time? T 1 T 2 T 3 T 4 T 5 T 6 IP 7 IP 6 IP 1 IP 2 IP 3 IP 4 2009 Universität Karlsruhe (TU), System Architecture Group 24

Mutual Exclusion Again Counting Semaphore Semantic for mutual exclusion of CSs: 1 Positive value of counter #threads that can enter their CS If mutual exclusion, # allowed threads = 1 2 Negative value of counter #waiting threads in front of CS, ie being queued at semaphore object 3 Counter == 0 no thread is waiting respectively maximal #threads currently in CS Still an open problem: How to establish atomic semaphore-operations? 2009 Universität Karlsruhe (TU), System Architecture Group 25

Mutual Exclusion Application of Counting Semaphores Suppose: n concurrent threads Initialize SCount to 1 only 1 thread allowed to enter its CS (ie mutual exclusion) Initialize SCount to k k threads allowed to enter their CS thread Ti: repeat p(s); CS v(s); RS forever 2009 Universität Karlsruhe (TU), System Architecture Group 26

Why Atomic Semaphore Operation? T 1 T 2 T 1 T 2 IP 1 P IP 2 P IP 1 IP 2 V V We have to implement P() and V() in such a way, that these operations are hopefully shorter critical sections!!! 2009 Universität Karlsruhe (TU), System Architecture Group 27

Atomic Semaphore Operation Problem: p() and v() -each consisting of multiple machine instructionshave to be atomic! Solution: Use another type of critical sections, hopefully with shorter execution times, establishing atomic and exclusive semaphore operations very short enter_section p(s) very short exit_section 2009 Universität Karlsruhe (TU), System Architecture Group 28

Monitors 2009 Universität Karlsruhe (TU), System Architecture Group 29

Monitors Monitor (1) High-level language construct ~ semantic of binary semaphore, but easier to control Offered in concurrent programming languages Concurrent Pascal, Modula-3, Java, Can be implemented by semaphores or other synchronization mechanisms 2009 Universität Karlsruhe (TU), System Architecture Group 30

Monitors Monitor (2) A software module * containing: one or more interface procedures an initialization sequence local data variables Characteristics: ti local variables accessible only by monitor s procedures thread enters the monitor by invoking an interface procedure only one thread can be executed in the monitor at any time, ie a monitor may be used for implementing mutual exclusion * Java s synchronized classes enable monitor-objects (already used in Assignment 2) 2009 Universität Karlsruhe (TU), System Architecture Group 31

Monitors Monitor (3) Monitor already ensures mutual exclusion no need to program this constraint explicitly Hence, shared data are protected automatically by placing them inside a monitor Monitor locks its data whenever a thread enters Additional thread synchronization inside the monitor can be done by the programmer using condition variables A condition variable represents a certain condition (eg an event) that has to be met before a thread may continue to execute one of the monitor procedures 2009 Universität Karlsruhe (TU), System Architecture Group 32

Monitors Condition Variables in Java: wait() Local to the monitor (accessible only inside the monitor) can be accessed only by: CondWait(cv) blocks execution of the calling thread on condition variable cv This blocked thread can resume its execution only if another thread will execute CondSignal(cv) CondSignal(cv) resumes execution of some thread blocked on this condition variable cv If there are several such threads: choose any one If no such thread exists: void, ie nothing to do In Java: notify() or notifyall() 2009 Universität Karlsruhe (TU), System Architecture Group 33

Monitors Monitor (4) Waiting threads are either in the entrance queue or in a condition queue A thread puts itself into the condition queue cn by invoking CondWait(cn) CondSignal(cn) enables one thread, waiting at condition queue cn, to continue Hence CondSignal(cn) blocks the calling thread and puts it into the urgent queue (unless csignal is the last operation of the monitor procedure) 2009 Universität Karlsruhe (TU), System Architecture Group 34

Example of a Monitor * (without condition variables) Monitors Contiguous array as the cyclic buffer of N slots with interface operations fetch() and deposit() 1 n occupied free head fetch tail deposit 2009 Universität Karlsruhe (TU), System Architecture Group 35

Monitors monitor module bounded_buffer buffer = record array buffer[n] of datatype head: integer = 0 tail: integer = 0 count: integer = 0 end head fetch monitor procedure deposit(b:buffer, d:datatype) begin bbuffer[btail] = d btail = btail mod N bcount = bcount + 1 end procedure fetch(b:buffer, result:datatype) begin result = bbuffer[bhead] bhead = bhead mod N bcount = bcount - 1 end end monitor modul 1 n occupie d tail deposit Automatically with mutual exclusion free Automatically with mutual exclusion Concurrent deposits or fetches are serialized, but you can still deposit to a full buffer and you can still try to fetch from an empty buffer! two additional constraints have to be considered 2009 Universität Karlsruhe (TU), System Architecture Group 36

Monitors Monitor Solution Two types of threads: Producer(s) Consumer(s) Synchronization is now confined to the monitor deposit() and fetch() are monitor interface methods If these 2 methods are correct, synchronization will be correct for all participating threads ProducerI: repeat produce v; deposit(v); forever ConsumerI: repeat fetch(v); consume v; forever 2009 Universität Karlsruhe (TU), System Architecture Group 37