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

Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
1 Select and poll and Signals CS 241 April 6, 2012 University of Illinois.
Page 1 Task Control: Signals and Alarms Havilland and Salama’s Unix Systems Programming B. Ramamurthy.
1 Signals. 2 Communicating with the OS System call (last lecture)  Request to the operating system to perform a task  … that the process does not have.
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.
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.
B. RAMAMURTHY Pag e 1 Task Control: Signals and Alarms Chapter 7 and 8 7/2/2015.
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)
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
Operating Systems Yasir Kiani. 22-Sep Agenda for Today Review of previous lecture Process management commands: bg, fg, ^Z, jobs, ^C, kill Thread.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
The kernel considers each program running on your system to be a process A process lives as it executes, with a lifetime that may be short or long A process.
Operating Systems CSE 411 CPU Management Sept Lecture 9 Instructor: Bhuvan Urgaonkar.
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.
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R S I X Exception Handling.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
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.
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.
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.
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)
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
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, Abdelzaher1 Signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Operating Systems Inter-Process Communication Signals Moti Geva
UNIX signals.
Recitation 7 – 3/18/02 Outline fork and scheduling Signals
G.Jyostna.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Signals What is a Signal?
Task Control: Signals and Alarms Chapter 7 and 8
Linux/UNIX Programming
CS 3733 Operating Systems Topics: Signals (USP Chapter )
Practical Session 3, Signals
UNIT-IV Process and Signals
Shells, System Calls, and Signals
CSC Advanced Unix Programming, Fall 2015
Signals.
Operating Systems Lecture 12.
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Inter-Process Communication ENCE 360
Task Control: Signals and Alarms Chapter 7 and 8
Signals Jennifer Rexford.
Presentation transcript:

Signals

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: Email 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

Basic Signal Concept (Definitions) Signal is generated when the event that causes the signal occurs. Signal is delivered when the process takes action based on the signal. The lifetime of a signal is the interval between its generation and delivery. Signal that has been generated but not yet delivered is pending. Process catches signal if it executes signal handler when the signal is delivered. Alternatively, a process can ignore a signal when it is delivered, that is to take no action. Process can temporarily prevent signal from being delivered by blocking it. Signal Mask contains a set of signals currently blocked.

How Signals Work Process Signal Generated Signal Mask Signal delivered Signal Handler Signal delivered Signal not blocked Signal Caught by handler Return from Signal Handler Process Resumed Signal Mask

Examples of POSIX Required Signals Description default action SIGABRT process abort (can be caught but not ignored) implementation dependent SIGALRM alarm clock abnormal termination SIGBUS Invalid address alignment Non-existent physical address SIGCHLD child terminated, stopped or continued ignore SIGILL invalid hardware instruction SIGINT interactive attention signal (usually ctrl-C) SIGKILL terminated (cannot be caught or ignored)

Signal Description default action SIGSEGV Invalid memory reference implementation dependent SIGSTOP Execution stopped stop SIGTERM Termination: it can be caught and interpreted (or ignored) Abnormal termination SIGTSTP Terminal stop SIGTTIN Background process attempting read SIGTTOU Background process attempting write SIGURG High bandwidth data available on socket ignore SIGUSR1 User-defined signal 1 abnormal termination

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., SIGALARM)

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. The default is SIGTERM. To unconditionally kill a process, use: kill -9 pid which is kill -SIGKILL pid.

Command Line Generates Signals stty –a 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)

Timers Generate SIGALRM Signals #include <unistd.h> unsigned alarm (unsigned seconds); alarm(20) creates SIGALRM to calling process after 20 real time seconds. Calls are not stacked alarm(0) cancels alarm

Examples: Programming Signals From a program you can use the kill system call: #include <sys/types.h> #include <signal.h> 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");

Programming Signals if (raise(SIGUSR1) != 0) Example 8.5: a process sends a signal to itself: if (raise(SIGUSR1) != 0) perror("Failed to raise SIGUSR1"); //It is same as kill(getpid(), SIGUSR1); Example 8.8: kill an infinite loop after 10 seconds: int main(void) { alarm(10); for ( ; ; ) ; }

Signal Masks Process can temporarily prevent signal from being delivered by blocking it. Signal Mask contains a set of signals currently blocked. When a process blocks a signal, the OS does not deliver signal until the process unblocks the signal When a process ignores signal, signal is delivered and the process handles it by throwing it away.

Signal Sets #include <signal.h> int sigemptyset(sigset_t *set); Signal set is of type sigset_t Signal sets are manipulated by five functions: #include <signal.h> 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);

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

Signal Masks SigMask Signal SigInt Bit 2, Signal Sigkill Bit 9, SigQuit SigKill … SigCont SigAbrt 1 … 1 Signal SigInt Bit 2, Signal Sigkill Bit 9, Signal SigChld Bit 20 A SIGSET is a collection of signals: #000003 is SIGHUP + SIGINT

SIGPROCMASK The function sigprocmask is used to modify the signal mask. #include <signal.h> int sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict oset) If oset is non-null, the previous value of the signal mask is stored in oset. ‘how’ specifies the manner in which the signal mask is to be modified SIG_BLOCK The set of blocked signals is the union of the current set and the set argument. SIG_UNBLOCKED The signals in set are removed from the current set of blocked signals. SIG_SETMASK The set of blocked signals is set to the argument set.

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

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.