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

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

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

Operating Systems 2010/2011

Signals. CSC209: Software Tools and Systems Programming. Furkan Alaca & Paul Vrbik

Overview. Administrative. * HW 1 grades. * HW 2 Due. Topics. * 5.1 What is a Signal? * Dealing with Signals - masks, handlers

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

CS 33. Signals Part 1. CS33 Intro to Computer Systems XXII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

CSE 421: Introduction to Operating Systems

Unix System Programming - Chapter 8

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

Real Time Operating Systems and Middleware

Goals of this Lecture

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

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

Lecture 7: Signals and Events. CSC 469H1F Fall 2006 Angela Demke Brown

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

Embedded Systems Programming

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

Systems Programming/ C and UNIX

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

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

Signals. Joseph Cordina

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

signals Communicating with the OS System call (last lecture) Signal (this lecture) User Process Operating System

F28HS Hardware-Software Interface: Systems Programming

POSIX Condition Variables ADT

Signals. POSIX defines a variety of signal types, each for a particular

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

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

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

ECE 650 Systems Programming & Engineering. Spring 2018

Lecture 9: Real-Time Software Design Issues

Linux Signals and Daemons

COSC243 Part 2: Operating Systems

Preview. The pause() System Call. The pause() System Call. The signal() System Call 10/18/2017

Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors

Exceptions, Processes and Signals

Systems Programming/ C and UNIX

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

UNIX System Programming. Overview. 2. Signal Types (31 in POSIX) Signal Sources. Signals. 1. Definition

System Calls & Signals. CS449 Spring 2016

User-Space Debugging Simplifies Driver Development

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

Chapter 4 Multithreaded Programming

CS240: Programming in C

Processes. Processes (cont d)

Interprocess Communication

CSCI 4061: Signals and Signal Handlers

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Implementing Lightweight Threads

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

CSC209 Review. Yeah! We made it!

Concurrent Server Design Multiple- vs. Single-Thread

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

THREADS VS. PROCESSES

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

System Programming. Signals I

Signal types (some of them) Networks and Operating Systems ( ) Chapter 5: Memory Management. Where do signals come from?

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

Lecture 4. Introduction to Unix Processes. Introduction to Systems Programming: Processes and Signals

n Unconditionally kill process 8371 n Interactive interrupt key n See /usr/include/bits/signum.h (e.g., SIGKILL 9) 2 -Ken Wong, Sep 2008

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

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

CS213. Exceptional Control Flow Part II. Topics Process Hierarchy Signals

Last time: Scheduling. Goals today. Recap: Hardware support for synchronization. Disabling interrupts. Disabling interrupts

POSIX / System Programming

Week 13. Final exam review

Sistemi in tempo reale Anno accademico

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

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

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

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

Signal Example 1. Signal Example 2

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

3. A Periodic Alarm: intdate.c & sigsend.c

COMP 2400 UNIX Tools

Start of Lecture on January 20, Chapter 3: Processes

VEOS high level design. Revision 2.1 NEC

Inter-process communication (IPC)

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

SMD149 - Operating Systems

ECE 650 Systems Programming & Engineering. Spring 2018

Basic POSIX signal concepts

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

Real-Time Systems Hermann Härtig Real-Time Operating Systems Brief Overview

Chapter 5. TCP Client-Server

Systems Programming/ C and UNIX

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

CSE410 Operating Systems Spring 2018 Project 1: Introduction to Unix/Linux Signals

Lecture 4 Threads. (chapter 4)

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

Computer Science & Engineering Department I. I. T. Kharagpur. Operating System: CS rd Year CSE: 5th Semester (Autumn ) Lecture VII

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

Transcription:

Overview Real-time Systems D0003E Lecture 13: More on inter-process communication (Burns & Wellings ch. 8, 11 & 10.6) Posix signals Posix timers timers in cygwin (lab environment) event-loop pattern semaphores POSIX signals Generation of signals A group of threads executing in a protected virtual address space forms the POSIX notion of a process A process can thus be though of as a virtual computer The POSIX signal concept is analogous to a virtual interrupt to such a virtual computer Signals predate threads and shared memory communication in Unix, although they are now standardized by POSIX The default handling of most signals is to kill the receiving process, but user-defined handler can be installed to override this behavior A signal can be sent to a process by executing kill(pid, sig); where pid is process number (0 means self) or by dividing by zero (equals kill(0, SIGFPE);) addressing outside your address space (SIGSEGV).. This actually mimics the interrupt sources on most real architectures: external hardware signals internal hardware misuse Some predefined signals Handling signals SIGINT SIGILL SIGKILL SIGALRM SIGCHLD SIGTSTP "interrupt" (Control-C) illegal instruction (internal) kill process (can't be ignored) timer alarm child process terminated terminal stop (Control-Z) SIGUSR1.. SIGUSR2 user defined SIGRTMIN.. SIGRTMAX user defined (with extra data payload) To install a handler for signal X: #include <signal.h> void handler(int signo) { /* Your signal handling code here */ struct sigaction act; act.sa_handler = handler; // your handler function act.sa_flags = 0; // extras, normally 0 sigemptyset( &act.sa_mask ); // signals to block while running handler sigaction( X, &act, NULL ); 1

Masking a signal Synchronizing with signals To temporarily mask (disable) signal handling: sigset_t sigs; sigemptyset(&sigs); sigaddset( &sigs, SIGINT ); sigprocmask( SIG_BLOCK, &sigs, NULL ); To unmask (enables handling of pending signals): sigprocmask( SIG_UNBLOCK, &sigs, NULL ); [C.f. the macros that enable or disable interrupts for various devices on the AVR] To wait for the handling of a signal: sigset_t ignore; // signals to ignore sigemptyset( &ignore ); sigaddset( &ignore, SIGINT ); sigsuspend( &ignore ); // return after handler has run once Note: sigsuspend stops execution until a signal not mentioned in the argument has arrived and the corresponding handler has run, just as if it were implemented as a busy-wait loop Alternatively POSIX timers To wait for a signal that doesn't have a handler: int received; sigaddset( &accept, ); sigwait( &accept, &received ); That is, sigwait stops execution until one of the signals in its first argument has arrived. Note that these signals must be blocked when sigwait is called (so that a handler won t interfere) OS abstraction built on top of the system ticks signalled by the clock hardware To create a new timer abstraction: #include <time.h> timer_t tmr; struct sigevent ev; SIGEV_SIGNAL( &ev, signo ); How alarms should be delivered doesn t have to be SIGALRM timer_create( CLOCK_REALTIME, &ev, &tmr ); These commands amount to "buying" a new alarm clock, aligning it with real time, and configuring it with a certain alarm signal Setting the alarm time More timer settings A timer is armed by supplying a single alarm time and an optional repetition period The alarm time can be relative (to the time of the call), or absolute To set a timer to ring in one second, and thereafter every 50 milliseconds, write struct itimerspec spec; spec.it_value.tv_sec = 1; spec.it_interval.tv_nsec = 50000000; To set a timer to ring just once, set the it_interval to 0 spec.it_value.tv_sec = 1; spec.it_interval.tv_nsec = 0; To temporarily disable a timer, set the it_value to 0 spec.it_value.tv_sec = 0; spec.it_interval.tv_nsec = 50000000; 2

A cyclic POSIX thread C.f. the periodic reactive object void *proc (void *arg) { timer_t tmr; struct sigevent ev; int sig_received; struct itimerspec spec = {{0,50000000, {0,50000000; SIGEV_SIGNAL( &ev, SIGALRM ); timer_create( CLOCK_REALTIME, &ev, &tmr ); sigwait( &sigset, &sig_received ); N.B.: signals are global within a POSIX process, and not tied to a specific thread. This means that for this example to work, no other thread should use this signal (SIGALRM) for any other purpose Reactive object: POSIX thread: int m( Class *self, int arg ) { AFTER( MILLISEC(500), self, m, exp ); Apart from the obvious differences in size, there s another, more fundamental, difference in structure as well: m m m // do sigwait // do sigwait // do sigwait Cygwin timers 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 older 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 ] A cyclic POSIX thread (2) 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; Like an itimerspec, but units are seconds and microseconds setitimer( ITIMER_REAL, &x, NULL); Older name for CLOCK_REALTIME int sig_received; sigwait( &accept, &sig_received ); Handling multiple events The event-loop pattern Reactive object: int m( Class *self, int arg ) { AFTER( MILLISEC(500), self, m, exp ); m m m2 m int m2( Class *self, int x ) { // update self A standard pattern for encoding reactions in terms of a continuous (POSIX) thread: INITIALIZE; PARK; REACT; INITIALIZE Indefinite length PAR K REACT POSIX thread: // do sigwait // do sigwait? // do sigwait The parking operation is at the core of this pattern it causes the thread to stop execution indefinitely How do we tell a POSIX thread to wait for a timer signal and be prepared to update its state at the same time? Implicit side-condition: neither INITIALIZE nor REACT must call any parking operation 3

Parking operations (def) In principle: any operation implemented by busy-waiting on an external state change In practice: an operation provided by the underlying operating system that behaves as if it were implemented by busy-waiting on an external state change Core questions regarding a parking op: what events does it wait for? Just one? Many? Is it extensible? Example: sigwait waits for a defineable set of POSIX signals (but not for keyboard entry, for example) Example: getchar waits for data from one input source only (the keyboard), but not for any signals A thread analogous to a reactive object! The multi-event-loop pattern Ideally we would like a parking operation that waits for exactly those events a thread is interested in: INITIALIZE; x = PARK; switch (x) { case 0: REACT0; break; case 1: REACT1; break; case n: REACTn; break; INITIALIZE PAR K REACT0 REACT1 REACTn Unfortunately, a truly generic parking op doesn t exist POSIX select() A way to wait for input on multiple files (keyboard, pipes, network sockets, etc) simultaneously Returns info on which file descriptor data arrived first Also supports an optional timeout value, equivalent to a multiple wait that includes a delay Syntax: number of bits in sets can be read Sets of files that can be written have pending exceptions int select( int nfds, fd_set *r, fd_set *w, fd_set *e, struct timeval *t ) optional timeout number of ready descriptors Note: select still doesn t wait for signals or internal events Semaphores Original process synchronization device (due to Dijkstra 1965) Supports two operations: wait and signal (signal is called post in POSIX) The general semaphore is counting; i.e., it remembers the number of signal calls, and allows the same number of wait calls to succeed without stopping A counting semaphore must be initialized with its starting value (the number of initially allowed wait calls) Semaphore vs. mutex A mutex is a semaphore that is binary (only distinguishes between the values 0 and 1) automatically initialized to 1 restricted so that signal/post can only be called by the thread that has successfully called wait Mutexes use the name lock for wait, and unlock for signal A semaphore Bounded Buffer void put(t item) { sem_wait( &space ); sem_wait( &mut ); buf[head] = item; head = (head + 1) % SIZE; sem_post( &mut ); sem_post( &items ); #include <semaphore.h> sem_t mut; sem_t space, items; int head = 0, tail = 0; T buf[size]; sem_init( &mut, 0, 1 ); sem_init( &items, 0, 0 ); sem_init( &space, 0, SIZE ); And the parking pattern void get(t *item) { sem_wait( &items ); sem_wait( &mut ); *item = buf[tail]; tail = (tail + 1) % SIZE; sem_post( &mut ); sem_post( &space ); Notice the lock/unlock pattern 4

Semaphores for parking Generic parking General pattern: One thread produces tokens by means of the signal call Another thread consumes tokens by means of the wait call, and parks if none are available The initial number of tokens can be any number 0 This parking pattern is often referred to as conditional synchronization Notice: the producer and the consumer must agree on what the tokens mean i.e., number of items in a buffer, the fact that a variable is not empty, the fact that one of two variables is not empty,.. In general, a generic parking mechanism cannot be implemented under POSIX Instead, the established coding style is to use separate threads for each kind of event, and rely on shared memory and semaphores to synchronize these threads in turn Some operating systems like QNX offer a message mechanism as an alternative, but even here the parking operation is not fully extensible to handle signals, etc Reactive objects as we know them can therefore not be directly supported in POSIX Neither can our direct interrupt handling, and the notion of baselines and deadlines But of course: POSIX programs are much more portable! 5