Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Signals.

Slides:



Advertisements
Similar presentations
Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
Advertisements

1 Select and poll and Signals CS 241 April 6, 2012 University of Illinois.
15-213, Fall 06 Outline Shell Lab Processes Signals.
System Programming Project 2 Due : 4/19...?. Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
SignalsSignals. What is a Signal? A signal is a notification that some event has occurred. Usually a signal is sent to a process asynchronously and whatever.
Signals Notifies a process of an event Generated when event that causes signal occurs Delivered when the process takes action based on the signal A signal.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
CS Lecture 17 Outline Named pipes Signals Lecture 17
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
1 Signals COS Goals of Today’s Lecture Overview of signals  Notifications sent to a process  UNIX signal names and numbers  Ways to generate.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
Signals Notifies a process of an event
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
COMP5102 Lecture 4 Operating Systems (OS) Inter-process Communication phones off (please)
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Lecture 4 Introduction to Unix Processes Introduction to Systems Programming: Processes and Signals.
1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 5: Signals, Time, Timers, and Token Rings.
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
Signals and Signal Handling. Signals A predefined message sent between two processes or from the kernel to a process, or by a user to a process A software.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
SignalsSignals. What is a Signal? A signal is a notification that some event has occurred. Usually a signal is sent to a process asynchronously and whatever.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Interprocess Communication Mechanisms. IPC Signals Pipes System V IPC.
Operating Systems Recitation 4, April th, 2002 Signals.
Signals. Introduction r A signal is a mechanism for notifying a process that an event has occurred. m When a signal is sent to a process is normal execution.
CSC Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 22, class 5.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks.
1 UNIX System Programming Signals. 2 Overview 1. Definition 2. Signal Types 3. Generating a Signal 4. Responding to a Signal 5. Common Uses of Signals.
KUKUM Real Time System Module #3 POSIX programming Lecture 2.
© 숙대 창병모 1 제 10 장 신호 (Signal). © 숙대 창병모 2 Contents 1. Signal Concepts 2. signal() 3. Interrupted System Calls 4. kill() /raise() 5. alarm() pause() 6.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
Chapter 8 Signals Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
Signals & Message queue Inter process mechanism in Linux system 3/24/
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
Signals and daemon processes Prepared by : Department of CSE Engineered for Tomorrow Course code: 10CS62.
CS 241 Section Week #10 (04/01/10) ‏. Topics This Section  MP5 Overview  Signals.
Unix System Programming
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Operating Systems Practical Session 3, Signals 1.
Operating Systems Summer Semester 2011 Practical Session 2, Signals 1.
Operating Systems Inter-Process Communication Signals Moti Geva
UNIX signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo
G.Jyostna.
Signals What is a Signal?
Chapter 5. TCP Client-Server Example
Task Control: Signals and Alarms Chapter 7 and 8
CS 3733 Operating Systems Topics: Signals (USP Chapter )
UNIT-IV Process and Signals
CSC Advanced Unix Programming, Fall 2015
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Inter-Process Communication ENCE 360
Signals.
Presentation transcript:

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Signals

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 Introduction to Signals What is Signal? A signal is a software notification to a process of an event. Why do we need Signals? In systems we need to enable asynchronous events Examples of asynchronous events: message arrives on my machine – mailing agent (user) process should retrieve Invalid memory access happens – OS should inform scheduler to remove process from the processor Alarm clock goes on – process which sets the alarm should catch it

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 How Signals Work Signal Generated Process Signal Handler Signal delivered Signal not blocked Signal Caught by handler Return from Signal Handler Process Resumed Signal Mask Signal Mask Signal Mask

Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Examples of POSIX Required Signals SignalDescriptiondefault action SIGABRTprocess abortimplementation dependent SIGALRMalarm clockabnormal termination SIGBUSaccess undefined part of memory objectimplementation dependent SIGCHLDchild terminated, stopped or continuedignore SIGILLinvalid hardware instructionimplementation dependent SIGINTinteractive attention signal (usually ctrl-C) abnormal termination SIGKILLterminated (cannot be caught or ignored) abnormal termination

Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 SignalDescriptiondefault action SIGSEGVInvalid memory referenceimplementation dependent SIGSTOPExecution stoppedstop SIGTERMterminationAbnormal termination SIGTSTPTerminal stopstop SIGTTINBackground process attempting readstop SIGTTOUBackground process attempting writestop SIGURGHigh bandwidth data available on socket ignore SIGUSR1User-defined signal 1abnormal termination Examples of POSIX Required Signals

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Generating Signals Signal has a symbolic name starting with SIG Signal names are defined in signal.h Users can generate signals (e.g., SIGUSR1) OS generates signals when certain errors occur (e.g., SIGSEGV – invalid memory reference) Specific calls generate signals such as alarm (e.g., SIGALRM)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Command Line Generates Signals You can send a signal to a process from the command line using kill kill -l will list the signals the system understands kill [-signal] pid will send a signal to a process. The optional argument may be a name or a number (default is SIGTERM). To unconditionally kill a process, use: kill -9 pid which is kill -SIGKILL pid.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Command Line Generates Signals CTRL-C is SIGINT (interactive attention signal CTRL-Z is SIGSTOP (execution stopped – cannot be ignored) CTRL-Y is SIGCONT (execution continued if stopped) CTRL-D is SIGQUIT (interactive termination: core dump)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 Timers Generate SIGALRM Signals #include unsigned alarm (unsigned seconds); alarm(20) creates SIGALRM to calling process after 20 real time seconds. Calls are not stacked alarm(0) cancels alarm

Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Examples: Programming Signals From a program you can use the kill system call: #include int kill(pid_t pid, int sig); Example 8.4: send SIGUSR1 to process 3423: if (kill(3423, SIGUSR1) == -1) perror("Failed to send the SIGUSR1 signal"); Example 8.5: a child kills its parent: if (kill(getppid(), SIGTERM) == -1) perror ("Failed to kill parent");

Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Programming Signals Example 8.5: a process sends a signal to itself: if (raise(SIGUSR1) != 0) perror("Failed to raise SIGUSR1"); Example 8.8: kill an infinite loop after 10 seconds: int main(void) { alarm(10); for ( ; ; ) ; }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Signal Masks Process can temporarily prevent signal from being delivered by blocking it. Signal Mask contains a set of signals currently blocked. Important! Blocking a signal is different from ignoring signal. Why? When a process blocks a signal, the OS does not deliver signal until the process unblocks the signal A blocked signal is not delivered to a process until it is unblocked. When a process ignores signal, signal is delivered and the process handles it by throwing it away.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Signal Sets Signal set is of type sigset_t Signal sets are manipulated by five functions: #include int sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigaddset(sigset_t *set, int signo); int sigdelset(sigset_t *set, int signo); int sigismember(const sigset_t *set, int signo);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 Signal Sets sigemptyset initializes the set to contain no signals sigfillset puts all signals in the set sigaddset adds one signal to the set sigdelset removes one signal from the set sigismember tests to see if a signal is in the set

Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Signal Masks SigIntSigQuitSigKill…SigContSigAbrt 001…10 SigMask Signal SigInt Bit 2, Signal Sigkill Bit 9, Signal SigChld Bit 20 A SIGSET is a collection of signals: # is SIGHUP + SIGINT

Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 SIGPROCMASK The function sigprocmask is used to modify the signal mask. #include int sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict oset) ‘how’ specifies the manner in which the signal mask is to be modified SIG_BLOCK – add collection of signals to those currently blocked SIG_UNBLOCKED – delete collection of signals from the currently blocked SIG_SETMASK – set collection of signals being blocked to the specified set

Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Example: Initialize Signal Set: if ((sigemptyset(&twosigs) == -1) || (sigaddset(&twosigs, SIGINT) == -1) || (sigaddset(&twosigs, SIGQUIT) == -1)) perror("Failed to set up signal mask");

Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Example: Add SIGINT to Set of Blocked Signals sigset_t newsigset; if ((sigemptyset(&newsigset) == -1) || (sigaddset(&newsigset, SIGINT) == -1)) perror("Failed to initialize the signal set"); else if (sigprocmask(SIG_BLOCK, &newsigset, NULL) == -1) perror("Failed to block SIGINT"); If SIGINT is already blocked, the call to sigprocmask has no effect.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 Catching and Ignoring Signals - SIGACTION sigaction function allows the caller to examine or specify action associated with a specific signal Program installs signal handler by calling sigaction with the name of a user- written function. The function sigaction is used to specify what is to happen to a signal when it is delivered.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 #include int sigaction(int signo, const struct sigaction *act, struct sigaction *oact); struct sigaction { void (*sa_handler)(int); /* SIG_DFL, SIG_IGN or pointer to function */ sigset_t sa_mask; /* additional signals to be blocked during execution of handler */ int sa_flags; /*special flags and options */ void(*sa_sigaction) (int, siginfo_t *, void *); /* real-time handler */ }; 1. Either act or oact may be NULL. 2. If SA_SIGINFO flag of sa_flags field is clear, sa_handler specifies the action to be taken. Catching and Ignoring Signals - SIGACTION

Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 Example: Set up Signal Handler for SIGINT struct sigaction newact; newact.sa_handler = mysighand; /*set new handler*/ newact.sa_flags = 0; /* no special options */ if ((sigemptyset(&newact,sa_mask) == -1) || (sigaction(SIGINT, &newact, NULL) == -1)) perror(“Failed to install SIGINT signal handler”);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 22 Set up Signal Handler that Catches SIGINT Generated by Ctrl-C void catchctrlc(int signo) { char handmsg[] = "I found Ctrl-C\n"; int msglen = sizeof(handmsg); write(STDERR_FILENO, handmsg, msglen); } … struct sigaction act; act.sa_handler = catchctrlc; act.sa_flags = 0; if ((sigemptyset(&act.sa_mask) == -1) || (sigaction(SIGINT, &act, NULL) == -1)) perror("Failed to set SIGINT to handle Ctrl-C"); Note: write is async-signal safe – meaning it can be called inside a signal handler. Not so for printf or strlen.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 23 Waiting for Signals Signals provide method for waiting for event without busy waiting Busy waiting Means continually using CPU cycles to test for occurrence of event Means a program does this testing by checking the value of variable in loop More Efficient Approach Suspend process until waited-for event occurs POSIX pause, sigsuspend, sigwait provide three mechanisms to suspend process until signal occurs pause() – too simple, and problematic if signals are delivered before pause (cannot unblock the signal and start pause() in atomic way)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 24 sigsuspend sigsuspend function sets signal mask and suspends process until signal is caught by the process sigsuspend returns when signal handler of the caught signal returns #include int sigsupend(const sigset_t *sigmask);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 25 Example: sigsuspend What’s wrong? sigfillset(&sigmost); sigdelset(&sigmost, signum); sigsuspend(&sigmost); signum is the only signal unblocked that can cause sigsuspend to return If the signal signum is delivered before the start of the code segment, the process still suspends itself and deadlocks if another signum is not generated

Copyright ©: Nahrstedt, Angrave, Abdelzaher 26 Example: sigsuspend (Correct Way to Wait for Single Signal) static volatile sig_atomic_t sigreceived =0; /*assume signal handler has been setup for signum and it sets sigreceived=1 */ sigset_t maskall, maskmost, maskold; int signum = SIGUSR1; sigfillset(&maskall); sigfillset(&maskmost); sigdelset(&maskmost, signum); sigprocmask(SIG_SETMASK, &maskall, &maskold); if (sigreceived == 0) sigsuspend(&maskmost); sigprocmask(SIG_SETMASK, &maskold, NULL);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 27 Use sigwait This function is much cleaner and avoids races and errors !!!! 1. First block all signals 2. Put the signals you want to wait for in sigset_t 3. Call sigwait 4. sigwait blocks the process until at least one of these signals is pending. 5. It removes one of the pending signals and gives you the corresponding signal number in the second parameter.. 6. Do what you want: no signal handler needed. 7. It returns 0 on success and -1 on error with errno set. #include int sigwait(const sigset_t *restrict sigmask, int *restrict signo);