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

Similar documents
CSC209H Lecture 8. Dan Zingaro. March 4, 2015

Signals. Joseph Cordina

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

Goals of this Lecture

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

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

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

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

Signals! Goals of this Lecture! Help you learn about:" Sending signals" Handling 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

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

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

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

Embedded Systems Programming

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

CSE 421: Introduction to Operating Systems

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

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

System Programming. Signals I

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

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

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

Chapter 4 Multithreaded Programming

Systems Programming/ C and UNIX

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

Operating Systems 2010/2011

CSCI 4061: Signals and Signal Handlers

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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

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

Networks and Opera/ng Systems Chapter 14: Synchroniza/on

ECE 650 Systems Programming & Engineering. Spring 2018

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

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

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

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

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

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

Fortran Signal Handling

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

System Calls & Signals. CS449 Spring 2016

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

Chapter 5. TCP Client-Server

F28HS Hardware-Software Interface: Systems Programming

Chapter 4: Synchronization

Kernel and processes

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

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

COMP 2400 UNIX Tools

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

Signals and Signal Handling - Part 2

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

Example: Linux o(1) scheduler. Networks and Operating Systems ( ) Chapter 4: Synchronization. Problems with UNIX Scheduling

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

Lecture 4. Introduction to make Debugging with gdb and ddd Introduction to Systems Programming: Processes and Signals

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

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

Processes, Signals, I/O, Shell Lab : Introduc>on to Computer Systems Recita>on 9: Monday, Oct. 21, 2013 Marjorie Carlson Sec>on A

Lecture 4 Threads. (chapter 4)

CS240: Programming in C

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

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

CS 3733 Operating Systems

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

PVPSIDDHARTHA INSTITUTE OF TECHNOLOGY

void (*)(int) NAME signal: signal.h - description of signals SYNOPSIS #include <signal.h>

Basic POSIX signal concepts

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

UNIT 6 SIGNALS AND DAEMON PROCESSES

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

Processes. Processes (cont d)

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

COSC243 Part 2: Operating Systems

Process Control. Philipp Koehn. 23 April 2018

Systems Programming/ C and UNIX

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

Interprocess Communication

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

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

Inter Process Communication

Implementing Lightweight Threads

CS 33. Signals Part 2. CS33 Intro to Computer Systems XXIV 1 Copyright 2016 Thomas W. Doeppner. All rights reserved.

Chapter 5: Memory Management

Workshop on Inter Process Communication Solutions

Linux Signals and Daemons

Week 13. Final exam review

Unix System Programming - Chapter 8

COP4020 Programming Languages. Exception Handling Prof. Robert van Engelen

Exceptional Control Flow Exists at All Levels of a System. Systemprogrammering 2007 Föreläsning 3 Exceptional Control Flow Part II

Chapter 4: Threads. Operating System Concepts 9 th Edition

Lab 4. Out: Friday, February 25th, 2005

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

Chapter 9. Signals and Signal Processing

Process Management. Outline

ECF Exists at All Levels of a System Exceptional Control Flow Part II Oct. 22, 2002 Topics! Process Hierarchy! Shells! Signals!

Exceptional Control Flow Part II

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

Signal Example 1. Signal Example 2

Transcription:

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

Whoops $ SometimesUsefulProgram xyz Are you sure you want to proceed? Are you really sure? Reformatting of your disk will begin in 3 seconds. Everything you own will be deleted. There's little you can do about it. Too bad Oh dear Y Y CS33 Intro to Computer Systems XXII 2 Copyright 2017 Thomas W. Doeppner. All rights reserved.

A Gentler Approach Signals get a process s attention» send it a signal process must either deal with it or be terminated» in some cases, the latter is the only option CS33 Intro to Computer Systems XXII 3 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Stepping Back What are we trying to do? interrupt the execution of a program» cleanly terminate it or» cleanly change its course not for the faint of heart» it s difficult» it gets complicated» (not done in Windows) CS33 Intro to Computer Systems XXII 4 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Signals Generated (by OS) in response to exceptions (e.g., arithmetic errors, addressing problems)» synchronous signals external events (e.g., timer expiration, certain keystrokes, actions of other processes)» asynchronous signals Effect on process: termination (possibly producing a core dump) invocation of a function that has been set up to be a signal handler suspension of execution resumption of execution CS33 Intro to Computer Systems XXII 5 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Signal Types SIGABRT abort called term, core SIGALRM alarm clock term SIGCHLD death of a child ignore SIGCONT continue after stop cont SIGFPE erroneous arithmetic operation term, core SIGHUP hangup on controlling terminal term SIGILL illegal instruction term, core SIGINT interrupt from keyboard term SIGKILL kill forced term SIGPIPE write on pipe with no one to read term SIGQUIT quit term, core SIGSEGV invalid memory reference term, core SIGSTOP stop process forced stop SIGTERM software termination signal term SIGTSTP stop signal from keyboard stop SIGTTIN background read attempted stop SIGTTOU background write attempted stop SIGUSR1 application-defined signal 1 stop SIGUSR2 application-defined signal 2 stop CS33 Intro to Computer Systems XXII 6 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Sending a Signal int kill(pid_t pid, int sig) send signal sig to process pid Also kill shell command type ctrl-c» sends signal 2 (SIGINT) to current process type ctrl-\» sends signal 3 (SIGQUIT) to current process type ctrl-z» sends signal 20 (SIGTSTP) to current process do something illegal» bad address, bad arithmetic, etc. CS33 Intro to Computer Systems XXII 7 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Handling Signals #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signo, sighandler_t handler); sighandler_t OldHandler; OldHandler = signal(sigint, NewHandler); CS33 Intro to Computer Systems XXII 8 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Special Handlers SIG_IGN ignore the signal signal(sigint, SIG_IGN); SIG_DFL use the default handler» usually terminates the process signal(sigint, SIG_DFL); CS33 Intro to Computer Systems XXII 9 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Example int main() { void handler(int); signal(sigint, handler); while(1) ; return 1; void handler(int signo) { printf("i received signal %d. " "Whoopee!!\n", signo); CS33 Intro to Computer Systems XXII 10 Copyright 2017 Thomas W. Doeppner. All rights reserved.

sigaction int sigaction(int sig, const struct sigaction *new, struct sigaction *old); struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; ; int main() { struct sigaction act; void myhandler(int); sigemptyset(&act.sa_mask); // zeroes the mask act.sa_flags = 0; act.sa_handler = myhandler; sigaction(sigint, &act, NULL); CS33 Intro to Computer Systems XXII 11 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Example int main() { void handler(int); struct sigaction act; act.sa_handler = handler; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(sigint, &act, 0); while(1) ; return 1; void handler(int signo) { printf("i received signal %d. " "Whoopee!!\n", signo); CS33 Intro to Computer Systems XXII 12 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 1 int main() { void handler(int); struct sigaction act; act.sa_handler = handler; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(sigint, &act, 0); You run the example program, then quickly type ctrl-c. What is the most likely explanation if the program then terminates? a) you re really quick or the system is really slow b) this can t happen; thus there s a problem with the system c) there s something else going on we haven t yet explained while(1) ; return 1; void handler(int signo) { printf("i received signal %d. " "Whoopee!!\n", signo); CS33 Intro to Computer Systems XXII 13 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Getting More Out of Signals (1) Getting more than the signal number for example, which arithmetic problem caused a SIGFPE? Use sa_sigaction rather than sa_handler struct sigaction act; act.sa_sigaction = arith_error; /* not sa_handler! */ sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; /* means that we're using sa_sigaction */ sigaction(sigfpe, &act, 0); CS33 Intro to Computer Systems XXII 14 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Getting More Out of Signals (2) void arith_error(int signo, siginfo_t *infop, void *ctx) { if (infop->si_code == FPE_INTDIV) { /* deal with integer divide by zero */ CS33 Intro to Computer Systems XXII 15 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Waiting for a Signal signal(sigalrm, RespondToSignal); struct timeval waitperiod = {0, 1000; /* seconds, microseconds */ struct timeval interval = {0, 0; struct itimerval timerval; timerval.it_value = waitperiod; timerval.it_interval = interval; setitimer(itimer_real, &timerval, 0); /* SIGALRM sent in ~one millisecond */ pause(); /* wait for it */ printf("success!\n"); CS33 Intro to Computer Systems XXII 16 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 2 signal(sigalrm, RespondToSignal); This program is guaranteed to print success!. a) yes b) no struct timeval waitperiod = {0, 1000; /* seconds, microseconds */ struct timeval interval = {0, 0; struct itimerval timerval; timerval.it_value = waitperiod; timerval.it_interval = interval; setitimer(itimer_real, &timerval, 0); /* SIGALRM sent in ~one millisecond */ pause(); /* wait for it */ printf("success!\n"); CS33 Intro to Computer Systems XXII 17 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Masking Signals setitimer(itimer_real, &timerval, 0); /* SIGALRM sent in ~one millisecond */ No signals here, please! pause(); /* wait for it */ CS33 Intro to Computer Systems XXII 18 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Masking Signals mask SIGALRM setitimer(itimer_real, &timerval, 0); /* SIGALRM sent in ~one millisecond */ No signals here unmask and wait for SIGALRM CS33 Intro to Computer Systems XXII 19 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Doing It Safely sigset_t set, oldset; sigemptyset(&set); sigaddset(&set, SIGALRM); sigprocmask(sig_block, &set, &oldset); /* SIGALRM now masked */ setitimer(itimer_real, &timerval, 0); /* SIGALRM sent in ~one millisecond */ sigsuspend(&oldset); /* unmask sig and wait */ /* SIGALRM masked again */ sigprocmask(sig_setmask, &oldset, (sigset_t *)0); /* SIGALRM unmasked */ printf("success!\n"); CS33 Intro to Computer Systems XXII 20 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Signal Sets To clear a set: int sigemptyset(sigset_t *set); To add or remove a signal from the set: int sigaddset(sigset_t *set, int signo); int sigdelset(sigset_t *set, int signo); Example: to refer to both SIGHUP and SIGINT: sigset_t set; sigemptyset(&set); sigaddset(&set, SIGHUP); sigaddset(&set, SIGINT); CS33 Intro to Computer Systems XXII 21 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Masking (Blocking) Signals #include <signal.h> int sigprocmask(int how, const sigset_t *set, sigset_t *old); used to examine or change the signal mask of the calling process» how is one of three commands: SIG_BLOCK the new signal mask is the union of the current signal mask and set SIG_UNBLOCK the new signal mask is the intersection of the current signal mask and the complement of set SIG_SETMASK the new signal mask is set CS33 Intro to Computer Systems XXII 22 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Signal Handlers and Masking What if a signal occurs while a previous instance is being handled? inconvenient Signals are masked while being handled may mask other signals as well: struct sigaction act; void myhandler(int); sigemptyset(&act.sa_mask); // zeroes the mask sigaddset(&act.sa_mask, SIGQUIT); // also mask SIGQUIT act.sa_flags = 0; act.sa_handler = myhandler; sigaction(sigint, &act, NULL); CS33 Intro to Computer Systems XXII 23 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Timed Out! int TimedInput( ) { signal(sigalrm, timeout); alarm(30); /* send SIGALRM in 30 seconds */ GetInput(); /* possible long wait for input */ alarm(0); /* cancel SIGALRM request */ HandleInput(); return(0); nogood: return(1); void timeout( ) { goto nogood; /* not legal but straightforward */ CS33 Intro to Computer Systems XXII 24 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Doing It Legally (but Weirdly) sigjmp_buf context; int TimedInput( ) { signal(sigalrm, timeout); if (sigsetjmp(context, 1) == 0) { alarm(30); // cause SIGALRM in 30 seconds GetInput(); // possible long wait for input alarm(0); // cancel SIGALRM request HandleInput(); return 0; else return 1; void timeout() { siglongjmp(context, 1); /* legal but weird */ CS33 Intro to Computer Systems XXII 25 Copyright 2017 Thomas W. Doeppner. All rights reserved.

sigsetjmp/siglongjmp TimedInput sigsetjmp siglongjmp Stack CS33 Intro to Computer Systems XXII 26 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Exceptions Other languages support exception handling try { something_a_bit_risky(); catch(arithmeticexception e) { deal_with_it(e); Can we do something like this in C? CS33 Intro to Computer Systems XXII 27 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Exception Handling in C void Exception(int sig) { THROW(sig) int computation(int a) { return a/(a-a); int main() { signal(sigfpe, Exception); signal(sigsegv, Exception); TRY { computation(1); CATCH(SIGFPE) { fprintf(stderr, "SIGFPE\n"); CATCH(SIGSEGV) { fprintf(stderr, "SIGSEGV\n"); END return 0; CS33 Intro to Computer Systems XXII 28 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Exception Handling in C #define TRY \ { \ int excp; \ if ((excp = \ sigsetjmp(ctx, 1)) == 0) #define CATCH(a_excp) \ else if (excp == a_excp) #define END #define THROW(excp) \ siglongjmp(ctx, excp); CS33 Intro to Computer Systems XXII 29 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Exception Handling in C sigjmp_buf ctx; int main() {... { int excp; if ((excp = sigsetjmp(ctx, 1)) == 0) { computation(1); else if (excp == SIGFPE) { fprintf(stderr, "SIGFPE\n"); else if (excp == SIGSEGV) { fprintf(stderr, "SIGFPE\n"); return 0; THROW END CATCH CATCH void exception(int sig) { siglongjmp(ctx, sig); TRY CS33 Intro to Computer Systems XXII 30 Copyright 2017 Thomas W. Doeppner. All rights reserved.