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

Similar documents
Unix System Programming - Chapter 8

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

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

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

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

Signals. Joseph Cordina

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

CSCI 4061: Signals and Signal Handlers

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

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

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

Operating Systems 2010/2011

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

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

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

Interprocess Communication

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

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

Embedded Systems Programming

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

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

System Programming. Signals I

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

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

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

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

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

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

CS240: Programming in C

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

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

Linux Signals and Daemons

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

Exceptions, Processes and Signals

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 421: Introduction to Operating Systems

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

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

Processes. Processes (cont d)

Systems Programming/ C and UNIX

Signals and Signal Handling - Part 2

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

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

Chapter 5. TCP Client-Server

Implementing Lightweight Threads

Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

Workshop on Inter Process Communication Solutions

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

3.1 Introduction. Computers perform operations concurrently

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

CS631 - Advanced Programming in the UNIX Environment. Process Groups, Sessions, Signals

Process Concept. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Chapter 4 Multithreaded Programming

ECE 650 Systems Programming & Engineering. Spring 2018

系統程式 郭大維教授 / 施吉昇教授臺灣大學資訊工程系

Introduction. Interprocess communication. Terminology. Shared Memory versus Message Passing

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

o Reality The CPU switches between each process rapidly (multiprogramming) Only one program is active at a given time

Operating Systems 9/6/ SIGINT Terminate Interrupt from keyboard (ctl-c) 9 SIGKILL Terminate Kill program (cannot override or ignore)

SMD149 - Operating Systems

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

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

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

CHAPTER 3 - PROCESS CONCEPT

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

System Calls & Signals. CS449 Spring 2016

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Project 2: Signals. Consult the submit server for deadline date and time

Killing Zombies, Working, Sleeping, and Spawning Children

Each terminal window has a process group associated with it this defines the current foreground process group. Keyboard-generated signals are sent to

POSIX Condition Variables ADT

Recitation 8 Processes, Signals, Tshlab

CS 3733 Operating Systems

Recitation Processes, Signals,UNIX error handling

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

Goals of this Lecture

The Big Picture So Far. Chapter 4: Processes

Process Management. Outline

Exceptional Control Flow: Signals

The Big Picture So Far. Chapter 4: Processes

Course Syllabus. Operating Systems, Spring 2016, Meni Adler, Danny Hendler & Amnon Meisels

Parameter Passing in C. Pointer Arithmetic. Parameter Passing in C. Pointer Arithmetic. Pointer Arithmetic. Tevfik Ko!ar

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

Diagram of Process State Process Control Block (PCB)

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

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

! The Process Control Block (PCB) " is included in the context,

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

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

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

Chapter 3: Process Concept

Chapter 3: Process Concept

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

Transcription:

Overview Administrative * HW 1 grades * HW 2 Due Topics * 5.1 What is a Signal? * 5.2-3 Dealing with Signals - masks, handlers * 5.4 Synchronization: pause(), sigsuspend() * 5.6 Interaction with other systems calls * problems with signals for communications * 5.7-9 Rest of signals Readings: Ch. 5. (p. 167-204)

2 What is a Signal? Motivation * Get immediatereal-time attention, ˆC and infinite loop * Increase concurrency, e.g. disk controller, CPU * Attend to unpredictable events, e.g. errors - But, Asynchronous = Hard to understand Signals * Software notification of an event to a process * Lifecycle: event - generate signal - OS queues blocked one, delivers others to the process - Process catches it and executes its handler

3 5.1 Value Domain for Signals Symbolic Names for Signals (Tables 5.1-2, pp. 170-1) * Defined in signal.h, example of required ones * SIGKILL - terminate * SIGFPE - error in arithmetic or divide by zero * SIGSEGV - invalid memory address * SIGINT - interactive attention signal (e.g. ˆC) Generating Signals by command line : * ˆC (SIGINT) or ˆ (SIGQUIT) * Q? How to determine SIGINT character on your terminal? * stty -a grep intr ; stty -a grep quit (pp 174) kill -s signal pid, Ex. Kill -INT 3423 kill -l [exit_status] # list names of signals kill [-signal] pid

4 5.1 Value Domain for Signals Generating signals by System calls: int kill( pid_t pid, int sig); * (Pid > 0) - Send signal to process pid * (Pid < 0) - Send signal to process group id = pid * (Pid = 0) - Send signal to process group of sender * Returns 0 on success - Returns -1 if User-id of caller and receiver differ System call: int raise(int sig); unsigned int alarm(unsigned int seconds); /* SIGALARM */ Example Code Segment: #include signal.h kill( 3423, SIGKILL); raise(siguser1) alarm(10); for( ; ; ) { }

5 Exercises on "What is a Signal?" Q? Compare signals and pipes for coomunication b/w processes P1 & P2 * relationship b/w P1, P2 * bandwidth * possibility of blocking * buffer size Q? Classify signals into synchronous and asynchronous ones: * timer expiry, file does not exist, mouse click * end-of-file found, ˆC on keyboard, Q? Which tasks can signals be used for? * Exception handling, e.g. bad pointer, divide by 0 * Process termination in abnormal circumtances - parent terminates a child process - a child process terminates its parent * Process notification of asynchronous events - e.g. I/O complete, timer expiration * Interprocess communication (message passing) * Emulation of multitasking

6 Dealing with Signals What can a process do with signals? * block for a while: postpone delivery * ignore signals as if they never arrived * handle signal- set up a routine to be called - whenever s particular signal arrives Implementation of Process preferences * Signal mask * Table mapping signal-type to handler Signal Mask = list of currently blocked signals * Changed by sigprocmask() Signal Handler * A user defined procedure or "SIG_DFL" or "SIG_IGN" * "SIG_IGN" will throw it away with no queueing * Installed via sigaction() * Invocation: implicit at signal delivery to process

7 5.2 Handling Signals - masks Signal Mask = list of currently blocked signals * Blocked signals are queued, i.e.not lost Create a signal-mask : system calls (pp. 175) - return 0 if successful, -1 on error int sigemptyset( sigset_t *set); /* no signal */ int sigfillset( sigset_t *set); /* all signals */ int sigaddset(sigset_t *set, int signo); int sigdelete(sigset_t *set, int signo); int sigismember(const sigset_t *set, int signo); - Return 1 if member, 0 otherwise Example 5.8 (pp. 175) * Create a mask with 2 signals

8 Dealing with Signals - mask Change signal mask for a process: * - examine or modify signal mask * - add/delete a set of signals * - union of two blocked sets sigprocmask(), pp. 176 * Parameter 1: how, i.e. add/delete/assignment * Parameter 2: new sigset_t * Parameter 3: old sigset_t Example 5.9 (pp. 176) * Add SIGINT to blocked set of signals * Simple usage Example 5.12 (pp. 178) * Typical use of blocking - protect crucial sections! * signals are masked during fork() Alternative: sigaction() as shown later

9 Dealing with Signals - mask Which is closer to maksing signals? * Telephone: block calls from certain numbers - calls are lost * Post Office: place a hold on delivery for a few days - mail delivery is postponed but mail is not lost. Masks and fork() * Is fork() signal safe? * Does child process inherit mask of parent? * Does a child share mask with its parent? * Can a parent process change masks for its child process? What can Masks be used for? * Postpone signals of certain types * Ignore signals of specific types * Block signals from specific processes

10 5.3 Dealing with Signals - handler routines Handler is a C function / subroutine * Returns no value * Gets the signal number as input * Asynchronous invocation Installing signal handlers: sigaction() - pp. 180 * Parameter 1: signal number * Parameter 2: new handler structure * Parameter 3: old handler structure Handler structure (struct sigaction) * Field 1: pointer to handler function - or SIG_DFL - default handler function - or SIG_IGN - ignore signal, i.e do nothing - Example 5.17 (pp. 182) - testing for ignored signal * Field 2: mask - additional signals to be blocked during - execution of the signal handler subroutine * Field 3: special flags (0 for now!) - e.g. automtic restart of system call interuppted by signal - in spec 1170 not in POSIX

11 Dealing with Signals - handler routines Example 5.13 (pp. 180) * Install handler for SIGINT Example 5.15 (pp. 181) char message = "I found ˆC 0 ; void catch_ctrl_c( int signo); { } write(stderr, message, strlen(message)); struct sigaction act; act.sa_handler = catch_ctrl_c; sigemptyset(act.sa_mask); act.sa_flags = 0; if (sigaction(sigint, act, NULL) 0) { }

! " # $ 12 Exercises on Dealing with Signals Compare and contrast the following: * (a) Postpone signals vs. Ignore signals * (b) mask set by sigprocmask() vs. mask set by sigaction() Q? Is write() signal safe? (Table 5.3, pp. 191) * Is fprintf() signal safe? * Why use signal safe system calls within a handler? How would one simulate the following policies for - for signals arriving during execution of a handler: * Telephone: call waiting - attended to new signal immediately * Telephone: disable call waiting w/ no voice mailbox - new signals are lost * Telephone: disable call waiting + voice mailbox - new signals are saved for later processing Compare the above policies for masking signal inside handlers. * When would you use each policy?

% & ' ( 13 Handling Signals- Process synchronization 5.4 Waiting for a signal * Motivation: recall parent-child synchronization * Chapter 2: exit() and wait() * Chapter 5.4: kill() and pause()/sigsuspend() system call pause(); (pp. 182) * wait till a unblocked signal comes * Example 5.18 (pp. 183) * Notice external variable signal_received * signal must arrive during pause() to set signal_received * window of vulnerability - b/w testing of signal_received and call to pause() new system call sigsuspend(); (pp. 183) * Closes window of vulnerability * Atomic step to unblock signal and start wait int sigsuspend(const sigset_t *sigmask); /* unblocked signals (change mask) and wait for them */ Example 5.20 (pp. 184) * Wait for signal number signum

) * +, 14 Exercises on Signals + Process synchronization Compare and contrast synchronization methods * exit() - wait() * kill() - pause() * kill() - sigsuspend() Can the window of vulnerability for pause() be closed - by masking signals during test of signal_received? Analyze window of vulnerability for Ex. 5.20. * Who sets signal_received to non-zero value? * What is mask during sigsuspend()? * What is mask during test (signal_received == 0)? Compare and contrast the following: * mask set by sigprocmask() * mask set by sigaction() * mask set by sigsuspend()

-. / 15 5.6-7 Implications for System calls Interaction b/w signals and system calls * Example 5.22 (pp. 189-90) - Limit wait on input to 10 second * restart the system calls interuppted by signals? * non-reentrant system calls Restart issues * Q? What happens if a process gets a signal - while executing a system call? * Interuptt "Slow" system calls - e.g. terminal I/O has indefinite wait - interuptted call return -1 with errno = EINTR - program can restart te system if needed * Other system calls are not interrupted - e.g. disk I/O, getpid() - finite or no wait Example 5.21 (pp. 189) * while loop restarts read() if interuppted by signal

0 1 16 5.6-7 Implications for System calls Non-reentrant system call issue * Use of global data, e.g. errno, signal_received * static data-structure - malloc(), free() * executing 2 occurrence of subroutine => problems * e.g. signal handler and main program Async-signal safe function = - can be called safely with a handler * Does not use static data structures or malloc() * Does not use global data in a non-entrant way * Table 5.3 (pp. 191) lists async-signal safe system calls

2 3 4 5 17 5.6-7 Exercises on Implications for System calls Q? Why following guidline for signal hanling? (pp. 190) * explicitly restart system calls within a program * use async-signal safe system calls within a handler * block signals to prevent unwanted interactions What are following? What are those used for? - List a few system calls for operating on each. * (a) signals, (b) masks, (a) candlers Organizing the knowledge * List the system calls and structures learned in ch. 5. * Group these into C++/Java like classes * Identify inheritance and part-of relationships Q? Where does a program return to after executing handler - for a signal arriving during a system call * (1) next machine language instruction * (2) next high-level language statement * (3) end of current function or system call * (4) end of current process (i.e. program)

6 7 18 Problems with communicating with signals POSIX.1 * Lack of signals for application use - only 2, ie SIGUSR1, SIGUSR2 * Lack of signal queueing - 5 signals of same type during blocked period - process may get 1 or 2 after unblocking * Signal delivery order - multiple pending signals -> no priority scheme * Information content is minimal - bit or an integer * Asynchrony - Must block signals during crucial sections POSIX.4 real-time signals * Address some of the problems * queued, delivered in order, carry extra data

8 9 : ; < 19 Rest of Signals 5.7 Explicit control of return place after handler * System calls: Siglongjmp(), sigsetjmp() (pp. 192) * Like "goto" and "set label" but * Unravel function call stack properly! * Ex. Program 5.2, pp. 192-3 5.8 Real-Time Signals (POSIX.1b) Expands sigaction structure - with special member function sa_sigaction - Which takes 3 parameters: (a) signal number, - (b) info structure = signal no., cause of signal, signal value - (c) Context - no defined - Cause of signal = user, queue, timer, asyncio, mesgq - Signal Value allows an interger /pointer parameter to handler Ex. Program 5.4 (pp. 196) 5.9 Asynchronous I/O aio.h, aio_read(), aio_write(),...