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

Similar documents
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

Goals of this Lecture

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

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

Signals. Joseph Cordina

CSE 421: Introduction to Operating Systems

System Calls & Signals. CS449 Spring 2016

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

System Programming. Signals I

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

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

Fortran Signal Handling

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

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

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

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

Chapter 4 Multithreaded Programming

ECE 650 Systems Programming & Engineering. Spring 2018

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Interprocess Communication

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

Operating Systems 2010/2011

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

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

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

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

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

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

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

PVPSIDDHARTHA INSTITUTE OF TECHNOLOGY

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

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

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

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

CS240: Programming in C

Final Precept: Ish. Slides Originally Prepared by: Wonho Kim

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

Embedded Systems Programming

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

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

F28HS Hardware-Software Interface: Systems Programming

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

Inter Process Communication

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

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

Exceptions, Processes and Signals

Signal Example 1. Signal Example 2

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

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

Lecture 24: Multitasking and Signals

Processes. Processes (cont d)

Chapter 1 Introduction

Unix System Programming - Chapter 8

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

Technical Document Series. POSIX Signal Handling in Java

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Process Control. Philipp Koehn. 23 April 2018

Chapter 4: Threads. Operating System Concepts 9 th Edition

Systems Programming/ C and UNIX

Lecture 4 Threads. (chapter 4)

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

UNIT 6 SIGNALS AND DAEMON PROCESSES

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

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

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

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

Giving credit where credit is due

Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

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

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

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

COMP 2400 UNIX Tools

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

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

Exceptional Control Flow Part II

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

Kernel and processes

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

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

What is an Operating System? Signals, Processes, & Threads. Resource Sharing. Resource Abstraction ... Operating Systems 10/20/2010

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

CSCI 4061: Signals and Signal Handlers

Operating Systems Grado en Informática. Course

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

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

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

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

Lecture 9: Real-Time Software Design Issues

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

COSC243 Part 2: Operating Systems

Signals and Signal Handling - Part 2

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Recitation Processes, Signals,UNIX error handling

Chapter 9. Signals and Signal Processing

Programs. Program: Set of commands stored in a file Stored on disk Starting a program creates a process static Process: Program loaded in RAM dynamic

Last Class. System Calls, Kernel Mode, and Process Implementation. Processes. Today. Operating Systems 9/11/2018 CSC 256/456 1

Transcription:

Signals! 1 Goals of this Lecture! Help you learn about: Sending signals Handling signals and thereby How the OS exposes the occurrence of some exceptions to application processes How application processes can control their behavior in response to those exceptions 2 1

Definition of Signal! Signal: A notification of an event Exception occurs (interrupt, trap, fault, or abort) Context switches to OS OS sends signal to application process Sets a bit in a vector indicating that a signal of type X occurred When application process regains CPU, default action for signal executes Can install a signal handler to change action (Optionally) Application process resumes where it left off Process movl pushl call f addl movl... void handler(int isig) { signal! 3 Examples of Signals! User types Ctrl-c Interrupt occurs Context switches to OS OS sends 2/SIGINT signal to application process Default action for 2/SIGINT signal is terminate Ctrl-z as above, but generates 20/SIGSTP Process makes illegal memory reference Fault occurs Context switches to OS OS sends 11/SIGSEGV signal to application process Default action for 11/SIGSEGV signal is terminate 4 2

Outline! 1. Signals 2. Causing a Signal to be Sent! 3. Handling Signals 4. Blocking Signals 5. Alarms 6. (If time) Interval Timers 7. Conclusion 5 Causing Signals via Keystrokes! Three signals can be sent from keyboard: Ctrl-c à 2/SIGINT signal Default action is terminate Ctrl-z à 20/SIGTSTP signal Default action is stop until next 18/SIGCONT Ctrl-\ à 3/SIGQUIT signal Default action is terminate 6 3

Causing Signals via Shell Commands! kill Command kill -signal pid kill command executes trap! OS handles trap OS sends a signal of type signal to the process whose id is pid! If no signal specified, 15/SIGTERM (default action to terminate ) Editorial: Better command name would be sendsig fg or bg command fg or bg command executes trap. OS handles trap. OS sends a 18/ SIGCONT signal (and does some other things too) Examples kill 2 1234 kill -SIGINT 1234 Same as pressing Ctrl-c if process 1234 is running in foreground 7 Causing Signals via Function Calls! raise() int raise(int isig); Commands OS to send a signal of type isig to current process Returns 0 to indicate success, non-0 to indicate failure Example int iret = raise(sigint); /* Process commits suicide. */ assert(iret!= 0); /* Shouldn't get here. */ 8 4

Causing Signals via Function Calls! kill() int kill(pid_t ipid, int isig); Sends a isig signal to the process whose id is ipid Equivalent to raise(isig) when ipid is the id of current process Editorial: Better function name would be sendsig() Example pid_t ipid = getpid(); /* Process gets its id.*/ int iret = kill(ipid, SIGINT); /* Process sends itself a assert(iret!= 0); SIGINT signal (commits suicide) */ 9 Unix Process Control! command Ctrl-c Non-Existing Process command & kill 2 pid Running Foreground Process fg kill 2 pid Running Background Process Ctrl-z fg Stopped Background Process kill -20 pid bg 10 5

Outline! 1. Signals 2. Causing Signals to be Sent 3. Handling Signals! 4. Blocking Signals 5. Alarms 6. (If time) Interval Timers 7. Conclusion 11 Handling Signals! Each signal type has a default action For most signal types, default action is terminate (This led to poor naming for commands/functions: kill ) A program can install a signal handler to change action of (almost) any signal type 12 6

Uncatchable Signals! Special cases: A program cannot install a signal handler for signals of type: 9/SIGKILL Default action is terminate 19/SIGSTOP Default action is stop until next 18/SIGCONT 13 Installing a Signal Handler! signal() sighandler_t signal(int isig, sighandler_t pfhandler); Installs function pfhandler as the handler for signals of type isig pfhandler is a function pointer: typedef void (*sighandler_t)(int); Returns the old handler on success, SIG_ERR on error After call, function (*pfhandler) is invoked whenever process receives a signal of type isig 14 7

Installing a Handler: Example 1! Program testsignal.c: #define _GNU_SOURCE /* Use modern handling style */ #include <stdio.h> #include <assert.h> #include <signal.h> static void myhandler(int isig) { printf(in myhandler with argument %d\n, isig); 15 Installing a Handler: Example 1 (cont.)! Program testsignal.c (cont.): int main(void) { void (*pfret)(int); pfret = signal(sigint, myhandler); assert(pfret!= SIG_ERR); printf(entering an infinite loop\n); for (;;) ; return 0; 16 8

Installing a Handler: Example 2! A program that generates a lot of temporary data Stores the data in a temporary file Must delete the file before exiting int main(void) { FILE *psfile; psfile = fopen(temp.txt, w); fclose(psfile); remove(temp.txt); return 0; 17 Example 2 Problem! What if user types Ctrl-c? OS sends a 2/SIGINT signal to the process Default action for 2/SIGINT is terminate Problem: The temporary file is not deleted Process terminates before remove() is executed Challenge: Ctrl-c could happen at any time Which line of code will be interrupted? Solution: Install a signal handler Define a clean up function to delete the file Install the function as a signal handler for 2/SIGINT 18 9

Example 2 Solution! static FILE *psfile; /* Must be global. */ static void cleanup(int isig) { fclose(psfile); remove(temp.txt); exit(0); int main(void) { void (*pfret)(int); psfile = fopen(temp.txt, w); pfret = signal(sigint, cleanup); cleanup(0); /* or raise(sigint); */ return 0; /* Never get here. */ 19 SIG_IGN! Predefined value: SIG_IGN! Can use as argument to signal() to ignore signals int main(void) { void (*pfret)(int); pfret = signal(sigint, SIG_IGN); assert(pfret!= SIG_ERR); Subsequently, process will ignore 2/SIGINT signals 20 10

SIG_DFL! Predefined value: SIG_DFL! Can use as argument to signal() to restore default action! int main(void) { void (*pfret)(int); pfret = signal(sigint, somehandler); assert(pfret!= SIG_ERR); pfret = signal(sigint, SIG_DFL); assert(pfret!= SIG_ERR); Subsequently, process will handle 2/SIGINT signals using default action for 2/SIGINT signals ( terminate ) 21 Outline! 1. Signals 2. Causing Signals to be Sent 3. Handling Signals 4. Blocking Signals 5. Alarms 6. (If time) Interval Timers 7. Conclusion 22 11

Blocking Signals! Blocking signals To block a signal is to queue it for delivery at later time When it is unblocked Different from ignoring a signal Each process has a signal mask in the kernel Tells the OS which signals to not deliver User program can modify mask with sigprocmask() Define a signal set Add it to or delete it from the mask, or install it as the mask 23 Function for Blocking Signals! sigprocmask() int sigprocmask(int ihow, const sigset_t *psset, sigset_t *psoldset); psset: Pointer to a signal set psoldset: (Irrelevant for our purposes) ihow: How to modify the signal mask SIG_BLOCK: Add psset to the current mask SIG_UNBLOCK: Remove psset from the current mask SIG_SETMASK: Install psset as the signal mask Returns 0 iff successful Functions for constructing signal sets sigemptyset(), sigaddset(), 24 12

Blocking Signals Example! int main(void) { sigset_t sset; signal(sigint, myhandler); sigemptyset(&sset); sigaddset(&sset, SIGINT); sigprocmask(sig_block, &sset, NULL); sigprocmask(sig_unblock, &sset, NULL); Block SIGINT signals Unblock SIGINT signals 25 What if executing a handler?! When handler for signal of type x is executing Signals of type x are blocked automatically When/if signal handler returns, block is removed 26 13

Outline! 1. Signals 2. Causing Signals to be Sent 3. Handling Signals 4. Blocking Signals! 5. Alarms! 6. (If time) Interval Timers 7. Conclusion 27 Alarms! alarm() unsigned int alarm(unsigned int uisec); Sends 14/SIGALRM signal to calling process after uisec seconds If parameter (uisec) is 0, cancels pending alarm Uses real time, i.e. wall-clock time! Return value is irrelevant for our purposes Used to implement time-outs 28 14

Alarms: Example 2! Program testalarmtimeout.c: If user types a number within 5 sec, echo it, otherwise time out and say user took too long. #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <signal.h> #include <unistd.h> static void myhandler(int isig) { printf(\nsorry. You took too long.\n); exit(exit_failure); 29 Alarms: Example 2 (cont.)! Program testalarmtimeout.c (cont.): int main(void) { int i; sigset_t sset; Safe, but shouldn t be necessary /* Make sure SIGALRM signals are not blocked. */ sigemptyset(&sset); sigaddset(&sset, SIGALRM); sigprocmask(sig_unblock, &sset, NULL); 30 15

Alarms: Example 2 (cont.)! Program testalarmtimeout.c (cont.): signal(sigalrm, myhandler); printf(enter a number: ); alarm(5); scanf(%d, &i); alarm(0); printf(you entered the number %d.\n, i); return 0; 31 Outline! 1. Signals 2. Causing Signals to be Sent 3. Handling Signals 4. Blocking Signals! 5. Alarms 6. (If time) Interval Timers! 7. Conclusion 32 16

Interval Timers! setitimer() int setitimer(int iwhich, const struct itimerval *psvalue, struct itimerval *psoldvalue); Sends 27/SIGPROF signal continually psvalue specifies timing psoldvalue is irrelevant for our purposes Uses virtual time, alias CPU time! Time spent executing other processes does not count Time spent waiting for user input does not count Returns 0 iff successful Used by execution profilers 33 Interval Timer Example! Program testitimer.c: #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <signal.h> #include <sys/time.h> static void myhandler(int isig) { printf(in myhandler with argument %d\n, isig); 34 17

Interval Timer Example (cont.)! Program testitimer.c (cont.): int main(void) { struct itimerval stimer; signal(sigprof, myhandler); 35 Interval Timer Example (cont.)! Program testitimer.c (cont.): /* Send first signal in 1 second, 0 microseconds. */ stimer.it_value.tv_sec = 1; stimer.it_value.tv_usec = 0; /* Send subsequent signals in 1 second, 0 microseconds intervals. */ stimer.it_interval.tv_sec = 1; stimer.it_interval.tv_usec = 0; setitimer(itimer_prof, &stimer, NULL); printf(entering an infinite loop\n); for (;;) ; return 0; 36 18

Outline! 1. Signals 2. Sending Signals 3. Handling Signals 4. Blocking Signals! 5. Alarms 6. (If time) Interval Timers 7. Conclusion!! 37 Predefined Signals! List of the predefined signals: $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX See Bryant & O Hallaron book for default actions, triggering exceptions Application program can define signals with unused values 38 19

Summary! Signals A signal is an asynchronous event Causing signals to be sent Keyboard actions and shell commands (kill, fg, bg, ) raise() or kill() sends a signal Catching signals signal() installs a signal handler! Most signals are catchable! Blocking signals! sigprocmask() and signal sets Signals of type x automatically are blocked while handler for type x signals is running 39 Summary (cont.)! Alarms Call alarm() to deliver 14/SIGALRM signals in real/wall-clock time! Alarms can be used to implement time-outs! Interval Timers Call setitimer() to deliver 27/SIGPROF signals in virtual/cpu time! Interval timers are used by execution profilers! 40 20

Summary (cont.)! For more information: Bryant & O Hallaron, Computer Systems:! A Programmer s Perspective, Chapter 8 41 Installing a Handler Example 1 (cont.)! [Demo of testsignal.c] 42 21

Installing a Handler Example 2! Program testsignalall.c: #define _GNU_SOURCE #include <stdio.h> #include <assert.h> #include <signal.h> static void myhandler(int isig) { printf(in myhandler with argument %d\n, isig); 43 Installing a Handler Example 2 (cont.)! Program testsignalall.c (cont.): int main(void) { void (*pfret)(int); pfret = signal(sighup, myhandler); /* 1 */ pfret = signal(sigint, myhandler); /* 2 */ pfret = signal(sigquit, myhandler); /* 3 */ pfret = signal(sigill, myhandler); /* 4 */ pfret = signal(sigtrap, myhandler); /* 5 */ pfret = signal(sigabrt, myhandler); /* 6 */ pfret = signal(sigbus, myhandler); /* 7 */ pfret = signal(sigfpe, myhandler); /* 8 */ pfret = signal(sigkill, myhandler); /* 9 */ This call fails 44 22

Installing a Handler Example 2 (cont.)! Program testsignalall.c (cont.): /* Etc., for every signal. */ printf(entering an infinite loop\n); for (;;) ; return 0; 45 Installing a Handler Example 2 (cont.)! [Demo of testsignalall.c] 46 23

Outline! 1. Unix Process Control 2. Signals 3. Sending Signals 4. Handling Signals 5. Race Conditions and Critical Sections! 6. Blocking Signals 7. Alarms 8. (If time) Interval Timers 9. Conclusion 47 Race Conditions and Critical Sections!! Race Condition! A flaw in a program whereby the correctness of the program is critically dependent on the sequence or timing of events beyond the program s control Critical Section! A part of a program that must execute atomically (i.e. entirely without interruption, or not at all) 48 24

Race Condition Example! Race condition example: int ibalance = 2000; static void addbonus(int isig) { ibalance += 50; int main(void) { signal(sigint, addbonus); ibalance += 100; To save slide space, we ignore error handling here and subsequently 49 Race Condition Example (cont.)! Race condition example in assembly language int ibalance = 2000; void addbonus(int isig) { ibalance += 50; int main(void) { signal(sigint, addbonus); ibalance += 100; movl ibalance, %ecx addl $50, %ecx movl %ecx, ibalance movl ibalance, %eax addl $100, %eax movl %eax, ibalance Let s say the compiler generates the above assembly language code 50 25

Race Condition Example (cont.)! (1) main() begins to execute int ibalance = 2000; void addbonus(int isig) { movl ibalance, %ecx ibalance += 50; addl $50, %ecx movl %ecx, ibalance int main(void) { signal(sigint, addbonus); ibalance += 100; movl ibalance, %eax addl $100, %eax movl %eax, ibalance 2000 2100 51 Race Condition Example (cont.)! (2) SIGINT signal arrives; control transfers to addbonus() int ibalance = 2000; void addbonus(int isig) { movl ibalance, %ecx ibalance += 50; addl $50, %ecx movl %ecx, ibalance int main(void) { signal(sigint, addbonus); ibalance += 100; movl ibalance, %eax addl $100, %eax movl %eax, ibalance 2000 2050 2050 2000 2100 52 26

Race Condition Example (cont.)! (3) addbonus() terminates; control returns to main() int ibalance = 2000; void addbonus(int isig) { movl ibalance, %ecx ibalance += 50; addl $50, %ecx movl %ecx, ibalance int main(void) { signal(sigint, addbonus); ibalance += 100; movl ibalance, %eax addl $100, %eax movl %eax, ibalance Lost $50!!! 2000 2050 2050 2000 2100 2100 53 Critical Sections! Solution: Must make sure that critical sections of code are not interrupted int ibalance = 2000; void addbonus(int isig) { ibalance += 50; int main(void) { signal(sigint, addbonus); ibalance += 100; Critical section Critical section 54 27

Alarm Example (cont.)! [Demo of testalarmtimeout.c] 55 Alarms: Example 1! Program testalarm.c: #define _GNU_SOURCE #include <stdio.h> #include <assert.h> #include <signal.h> #include <unistd.h> static void myhandler(int isig) { printf(in myhandler with argument %d\n, isig); /* Set another alarm. */ alarm(2); 56 28

Alarms: Example 1 (cont.)! Program testalarm.c (cont.): int main(void) { sigset_t sset; Safe, but shouldn t be necessary; compensates for a Linux bug /* Make sure SIGALRM signals are not blocked. */ sigemptyset(&sset); sigaddset(&sset, SIGALRM); sigprocmask(sig_unblock, &sset, NULL); signal(sigalrm, myhandler); 57 Alarms: Example 1 (cont.)! Program testalarm.c (cont.): /* Set an alarm. */ alarm(2); printf(entering an infinite loop\n); for (;;) ; return 0; 58 29

Interval Timer Example (cont.)! [Demo of testitimer.c] 59 30