But first... Re: generic parking. A cyclic POSIX thread (2) Real-time Systems SMD138. Lecture 14: Real-time languages (Burns & Wellings ch.

Similar documents
Overview. Lab advice. Lab advice. Readers-writers problem. Readers-writers solution. Real-time Systems D0003E 3/10/2009

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

Reminder from last time

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

CSE 120 Principles of Operating Systems Spring 2016

Introduction to OS Synchronization MOS 2.3

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

CSE 153 Design of Operating Systems

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

Operating Systems ECE344

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

Multitasking / Multithreading system Supports multiple tasks

Lecture 6 (cont.): Semaphores and Monitors

Opera&ng Systems ECE344

Concurrent Programming Lecture 10

Operating systems and concurrency (B08)

CS510 Operating System Foundations. Jonathan Walpole

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

Performance Throughput Utilization of system resources

Concurrent Programming

What's wrong with Semaphores?

Synchronization 1. Synchronization

CS 318 Principles of Operating Systems

CS F-MID Midterm October 25, 2001

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

1 Process Coordination

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

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

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

Lecture 8: September 30

More Shared Memory Programming

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

Lecture 7: CVs & Scheduling

User Space Multithreading. Computer Science, University of Warwick

High Performance Computing Course Notes Shared Memory Parallel Programming

Last Class: Synchronization

CS 450 Exam 2 Mon. 11/7/2016

Synchronization 1. Synchronization

Sharing Objects Ch. 3

CSE 120 Principles of Operating Systems Spring 2016

Systems Programming/ C and UNIX

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

LSN 13 Linux Concurrency Mechanisms

ANSI/IEEE POSIX Standard Thread management

Administrivia. Assignments 0 & 1 Class contact for the next two weeks Next week. Following week

Monitors; Software Transactional Memory

Concurrency. Lecture 14: Concurrency & exceptions. Why concurrent subprograms? Processes and threads. Design Issues for Concurrency.

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

CS 318 Principles of Operating Systems

Signals are a kernel-supported mechanism for reporting events to user code and forcing a response to them. There are actually two sorts of such

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Shared Memory Programming. Parallel Programming Overview

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

CPS 310 second midterm exam, 11/14/2014

Threads and Parallelism in Java

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

Today: Synchronization. Recap: Synchronization

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Monitors; Software Transactional Memory

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Synchronization SPL/2010 SPL/20 1

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

A read or write being atomic means that its effect is as if it happens instantaneously.

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Programming Languages for Real-Time Systems. LS 12, TU Dortmund

CS533 Concepts of Operating Systems. Jonathan Walpole

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

There are 8 total numbered pages, 6 Questions. You have 60 minutes. Budget your time carefully!

Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors

LECTURE 18. Control Flow

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

CSC 533: Organization of Programming Languages. Spring 2005

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

Condition Variables & Semaphores

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

CSE 451 Midterm 1. Name:

Multiple Inheritance. Computer object can be viewed as

Concurrency, Thread. Dongkun Shin, SKKU

Process Management And Synchronization

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

Concurrency: Deadlock and Starvation. Chapter 6

1 Introduction. 2 Managing contexts. Green, green threads of home. Johan Montelius HT2018

Real Time Operating Systems and Middleware

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

CS 450 Exam 2 Mon. 4/11/2016

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Java Threads and intrinsic locks

Operating Systems 2010/2011

Semaphores and Monitors: High-level Synchronization Constructs

Synchronization Primitives

Concurrency: State Models & Design Patterns

Deviations are things that modify a thread s normal flow of control. Unix has long had signals, and these must be dealt with in multithreaded

Chapter 13 Topics. Introduction. Introduction

Models of concurrency & synchronization algorithms

1. Introduction. 2. Project Submission and Deliverables

CSE 451: Operating Systems Spring Module 10 Semaphores, Condition Variables, and Monitors

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

POSIX Condition Variables ADT

Transcription:

But first... Real-time Systems SMD138 Lecture 14: Real-time languages (Burns & Wellings ch. 8 & 9) As it happens, Cygwin the PC side lab programming environment doesn t support timer_create() and timer_settime() (so much for POSIX compatibility!) Instead, a predefined interval timer for each process is offered, in the style of BSD Unix This timer always generates the signal SIGALRM, and is configured via the system call setitimer() [ Actually, one such interval timer can be configured for each thread, but since they all use SIGALRM for output, using multiple interval timers in a process is a little tricky and not recommended ] 1 2 A cyclic POSIX thread (2) Re: generic parking void *fun( void *arg ) { sigset_t accept; struct itimerval x; x.it_value.tv_sec = 0; x.it_value.tv_usec = 1000; x.it_interval.tv_sec = 0; x.it_interval.tv_usec = 1000; setitimer( ITIMER_REAL, &x, NULL); sigemptyset( &accept ); sigaddset( &accept, SIGALRM ); sigprocmask( SIG_BLOCK, &accept, NULL ); while (1) { int sig_received; sigwait( &accept, &sig_received ); // do periodic action Like an itimerspec, but units are seconds and microseconds Older name for CLOCK_REALTIME Instead of encoding reactive objects as follows... Fully generic, can wait for any combination of events Can safely share thread-local variables, as reactions are automatically serialized PARK REACT0 REACT1... REACTn 3 4

Re: generic parking Criticism of semaphores We are often forced to resort to the following pattern: Not generic, will only wait for some specific events PARK0 PARK1 PARKn REACT0 REACT1... REACTn Can only share global variables, reactions must be manually serialized via a mutex or semaphore Elegant, but error-prone low-level primitive The two uses of semaphores, mutual exclusion and conditional synchronization, are not distinguished There is nothing that binds a mutual exclusion semaphore to the shared variables it protects A misplaced of forgotten semaphore can lead to total program corruption or deadlock Historically important, but mostly inadequate for larger-scale concurrent programming 5 6 Monitors A monitor BB Idea: objects and modules successfully control the visibility of shared variables why not make mutual exclusion automatic for such a construct? This is the idea behind monitors, a synchronization construct found in many early concurrent languages (Modula-1, Concurrent Pascal, Mesa) Monitors elegantly solve the mutual exclusion problem; for conditional synchronization a mechanism of condition variables is used We give an example in C-like syntax; note, though, that neither C nor C++ support monitors directly 7 monitor BoundedBuffer { condition notempty, notfull; int count = 0, head = 0, tail = 0; T buf[size]; void put(t item) { if (count == SIZE) wait(notfull); buf[head] = item; head = (head + 1) % SIZE; count++; signal(notempty); void get(t *item) { if (count == 0) wait(notempty); *item = buf[tail]; tail = (tail + 1) % SIZE; count-- signal(notfull); These variables are only visible within methods put and get Only one thread can be running put or get at any time (the others are queued) However, a thread can temporarily relinquish control of the monitor by calling wait 8

Things to note Monitors and POSIX While mutual exclusion is made implicit, conditional synchronization is still handled explicitly The condition variables are local to a monitor, hence the risk of improper use is reduced Important distinction: condvars are not counting every wait call blocks While a thread blocks on wait, the monitor is temporarily opened up to some other thread (so that someone may be able to call signal eventually) Technical concern: who gets to run after a signal? Different monitor variants take different routes... Although C doesn t offer any explicit monitor construct, the POSIX library does provide the building blocks for constructing a monitor We have already seen one part of this support: the mutex variables The other part is a mechanism for building condition variables: A datatype: pthread_cond_t An init operation: pthread_cond_init(&cv, &attr) A signal operation: pthread_cond_signal(&cv) A wait operation: pthread_cond_wait(&cv, &mut) Note how the wait call specifies the mutex to be temporarily released 9 10 A monitor BB in POSIX/C pthread_mutex_t m; pthread_cond_t notempty, notfull; int count = 0, head = 0, tail = 0; T buf[size]; pthread_mutex_init(&m, NULL); pthread_cond_init(&notempty, NULL); pthread_cond_init(&notfull, NULL); void put(t item) { pthread_mutex_lock(&m); if (count == SIZE) pthread_cond_wait(&notfull, &m); buf[head] = item; head = (head + 1) % SIZE; count++; pthread_cond_signal(&notempty); pthread_mutex_unlock(&m); void get(t *item) { pthread_mutex_lock(&m); if (count == 0) pthread_cond_wait(&notempty, &m); *item = buf[tail]; tail = (tail + 1) % SIZE; count--; pthread_cond_signal(&notfull); pthread_mutex_unlock(&m); Java concurrency The Java threading model views threads and objects as orthogonal concepts (an object is not concurrent, a thread does not encapsulate state) However, Java uses objects to represent threads (just like POSIX/C uses variables of type pthread_t) class MyThread extends Thread { public void run() { // your code... MyThread t1 = new MyThread(); t1.start(); // continues in parallel with code in t1.run() 11 12

Java and monitors Note that methods in object t1 can still be called from the main thread in a normal fashion. Likewise, thread t1 can freely call methods in any object However, to solve the inevitable synchronization problems, Java takes the route that every object may also double as a monitor! Restriction 1: to achieve mutual exclusion, each method of an object must be prepended with the keyword synchronized Restriction 2: only one condition variable per object (monitor) is supported, identified with the object itself 13 A Java BB public class BoundedBuffer { private int count = 0, head = 0, tail = 0; private T buf[] = new T[SIZE]; public synchronized void put(t item) { while (count == SIZE) wait(); buf[head] = item; head = (head+1) % SIZE; count++; notify(); public synchronized T get() { while (count == 0) wait(); T item = buf[tail]; tail = (tail+1) % SIZE; count--; notify(); return item; Data must be marked private to be truly encapsulated wait may only be called if the monitor lock is held; i.e., within synchronized methods Because only one condvar is supported, there s no guarantee that one s condition is true when waking use while to iterate 14 Criticism of monitors Guards Monitors elegantly solve the mutual exclusion problem, but...... the conditional synchronization problem is still dealt with using low-level variables All criticisms surrounding semaphores apply to condition variables as well The use of nested monitor calls (monitors that call other monitors) is inherently problematic only the innermost monitor will open up while the caller waits Can a higher-level alternative to condition variables be devised? Assume a monitor where the condition that avoids the wait call is lifted out to guard the whole monitor method instead That is, instead of letting threads in only to find that some condition doesn t hold, keep the threads out until the condition does hold This requires a language construct that allows statedependent boolean expressions outside the actual methods, but still protected from concurrent access Such a construct can be found in Ada, in the form of guards 15 16

Ada Ada provides two relevant constructs for interprocess communication: the protected objects (a monitor-like construct) and a general messagepassing mechanism Threads in Ada are called tasks, and are introduced by declaration (imagine a keyword in C indicating that a function body should start executing as a thread, without any call to pthread_create) Monitor methods as well as message-passing endpoints are called entries in Ada Ada makes a clear distinction between the interface of a protected object or a task, and its implementation body 17 An Ada protected object BB protected BoundedBuffer is entry put (item : in T; entry get (item : out T); private count : Integer := 0; head : Integer := 0; tail : Integer := 0; buf : array (0..SIZE-1) of T; Guard expressions protected body BoundedBuffer is entry put (item : in T) when count /= SIZE is begin buf(head) := item; head := head + 1 mod SIZE; count := count + 1; end put; entry get (item : out T) when count /= 0 is begin item := buf(tail); tail := tail + 1 mod SIZE; count := count - 1; end get; 18 Things to note The Ada type system limits the guards to simple boolean expressions In principle the guards should be constantly reevaluated; in practice a good compiler will only insert evaluation code when an entry is called (if the guard depends on an argument) or when an entry exits (if the guard depends on the local state) Note that the guards must be run under mutual exclusion if they depend on local state (the locks are handled by the compiler) The good thing is that the separation between real code and entry conditions is made explicit Message-passing in Ada Ada s message-passing mechanism is characterized by: Full type safety (no need for type casts) Selective waiting (blocking for several messages) Message guards (like for the protected objects) Sending a message just amounts to calling an entry in a task s exported interface Receiving a message is done by an accept statement, that mentions the name of an exported entry task BoundedBuffer is entry put (item : in T); entry get (item : out T); Interface BoundedBuffer.put (x); Sending accept put (item : in T) do... Receiving 19 20

An Ada task-based BB Remarks An infinite loop thread task body BoundedBuffer is count : Integer := 0; head : Integer := 0; tail : Integer := 0; buf : array (0..SIZE-1) of T; begin loop Additional guards select when count < SIZE => accept put (item : in T) do buf(head) := item; head := head+1 mod SIZE; count := count+1; end; or Message reception when count > 0 => accept get (item : out T) do item := buf(tail); tail := tail+1 mod SIZE; count := count-1; end; end select; end loop; Event alternatives 21 Ada s tasks closely resemble our reactive objects: Entries are sequential, thus local task variables are automatically protected from concurrent access Any number of unique entries (=methods) can be defined for a task The select statement can also contain delays and interrupt handlers in parallel with ordinary entries Asynchronous methods can be mimiced by inserting arbitrary code after an (empty) entry It seems like select is the generic parking operation we ve been looking for! 22 Criticism of select The perils of silent parking However, notice that there s nothing that guarantees that a task will respond to its entries just because it is parked in a select. The programmer may just as well select not to include a entry! As a result, an entry call might freeze indefinitely, until the event occurs that causes the receiving task to do a select that accepts it i.e., every entry call becomes a potentially parking operation! A similar effect is caused by guards, and (in other systems) by condition variables and semaphores Excluding entries (or putting callers on hold using condition variables or semaphores) makes it temptingly easy to write software services like the Bounded Buffer, since calls occuring at the wrong time can essentially be ignored However, the downside is that writing clients is made correspondingly harder if a client must react within a deadline it cannot risk becoming parked on a put() in the middle of a reaction In fact, the whole notion of a reaction becomes blurred if parking may silently occur anywhere where does one reaction start and where does another one stop? 23 24

The perils of silent parking The perils of silent parking Our notion of a reaction: A reaction that parks internally... hmm... waiting in readyq running blocked because somebody else is using claimed resource waiting in readyq running blocked because somebody else is using claimed resource event reaction done event event event2 reaction done Note one-to-one correspondance! parked on an entry/monitor/semaphore call not handled until event2 occurs Which reaction? 25 26 The perils of silent parking Avoiding indefinite blocking inside reactions is essential to obtaining predictable reaction times, hence programs that use Ada select / condition variables / semaphores must be written very restrictively The model enforced by our reactive objects directly capture the desired program structure! See lecture 8 (Examples of reactive objects) for a bunded buffer implementation that does not rely on silent parking 27