CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson

Slides:



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

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
15-213, Fall 06 Outline Shell Lab Processes Signals.
CS591 (Spring 2001) The Linux Kernel: Signals & Interrupts.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
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.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
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, Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Process Description and Control A process is sometimes called a task, it is a program in execution.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems CSE 411 CPU Management Sept Lecture 9 Instructor: Bhuvan Urgaonkar.
* 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.
System calls for Process management
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
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.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
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 (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
UNIX signals & pipes. UNIX Signals A UNIX signal corresponds to an event –It is raised by one process (or hardware) to call another process’s attention.
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 제 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.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
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/
ACCESS CONTROL. Components of a Process  Address space  Set of data structures within the kernel - process’s address space map - current status - execution.
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)
System calls for Process management Process creation, termination, waiting.
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 Summer Semester 2011 Practical Session 2, Signals 1.
UNIX signals.
G.Jyostna.
Task Control: Signals and Alarms Chapter 7 and 8
OS – Process Creation and Destruction
Exceptional Control Flow Part II
Structure of Processes
CSC Advanced Unix Programming, Fall 2015
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Exceptional Control Flow
Inter-Process Communication ENCE 360
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Signals.
Process Description and Control in Unix
Presentation transcript:

CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson

Signals A signal is a mechanism for notifying a program that some event has occurred. – intuition: signal  “software interrupt” – when a signal is sent to a program, its normal execution is interrupted – depending on (1) the state of the program, and (2) the type of signal, the program may enter some pre-specified signal handler; or take some default action. 2

Example Signals (not a complete list) Signal NameNumberDescription SIGHUP1Hangup (POSIX) SIGINT2Terminal interrupt (ANSI) SIGQUIT3Terminal quit (POSIX) SIGILL4Illegal instruction (ANSI) SIGTRAP5Trace trap (POSIX) SIGFPE8Floating point exception (ANSI) SIGKILL9Kill(can't be caught or ignored) (POSIX) SIGSEGV11Invalid memory segment access (ANSI) SIGTERM15Termination (ANSI) SIGSTKFLT16Stack fault SIGSTOP19Stop executing(can't be caught or ignored) (POSIX) SIGPROF27Profiling alarm clock (4.2 BSD) SIGWINCH28Window size change (4.3 BSD, Sun) SIGPWR30Power failure restart (System V) ……… 3

Synchronous vs. Asynchronous Signals Synchronous signals: – arise from executing an instruction in the process’s instruction stream e.g.: illegal instruction (SIGILL); illegal address (SIGSEGV) – causes a trap into the OS kernel trap handler sometimes referred to as “traps” – directed to the process/thread that executed the instruction Asynchronous signals: – source is external to the current execution e.g.: profiling clock (SIGPROF); terminal interrupt, ^C (SIGINT) 4

What’s going on: A high-level view Signal sending: – OS kernel updates info for destination process Signal receiving: – kernel forces target process to handle signal Pending signals are sent but not yet received A process can block some signals 5 Operating system kernel processes devices signals interrupts

Dealing with Signals The way in which a process deals with a given signal is called the disposition of that signal in that process. Possible dispositions: – ignore – default different for different signals – programmer-specified handler Exceptions: SIGKILL and SIGSTOP – defaults cannot be changed for these 6

Specifying a Signal Handler 7

8 changes the signal handler

Specifying a Signal Handler 9 signal number (can’t be SIGKILL or SIGSTOP)

Specifying a Signal Handler 10 info about new handler)

Specifying a Signal Handler 11 if non-NULL, where to save old handler info

Specifying a Signal Handler 12 pointer to handler function: void handler(int signal_num) or SIG_DFL or SIG_IGN

Specifying a Signal Handler 13 specifies handler if sa_flags contains SA_SIGINFO

Specifying a Signal Handler 14 specifies signals that should be blocked while the handler is executing

A Simple Example 15 structure to hold info about handler

A Simple Example 16 signal handler function

A Simple Example 17 specifying the handler

A Simple Example 18 didn’t change the default signal handler NULL pointer dereference produces a Segmentation fault

A Simple Example 19 signal handler changed but why does it get executed over and over?

Behind the scenes of a SIGSEGV When a program tries to access a bad address: – execution traps into the OS kernel – if no handler is specified: kernel invokes the default handler default handler prints out “Segmentation fault” and kills the process – if a handler is specified: kernel executes the handler – the expectation is that the handler fixes the problem restarts the offending operation – this allows programmer-controlled recovery from errors 20

Another Example: weird factorial 21 no base case???

weird factorial: cont’d 22 j =  x = x * y i = 0 y-1

weird factorial: cont’d 23 j = 1 * 2 * … * n = n!

weird factorial: cont’d 24 signal handler for SIGSEGV (signal raised by dereferencing a bad pointer in factorial() ) sets k = n! prints out k = n! and exits

Sending signals A program can send a signal to another program using the kill() system call: int kill(pid_t pid, int sig) sends the signal number sig to process pid (see /usr/include/asm-generic/signal.h) A user can send a signal from the command line using the kill command: kill –N pid E.g., “kill -9 pid”(9 = SIGKILL) 25

Asynchronous signals 26 SIGINT will be handled by my_handler() send SIGINT to process with process-id = target

Asynchronous signals – cont’d 27

Blocking signals Each process has a list (bit-vector) of blocked signals – when a signal is blocked, it remains pending Related system calls: – sigprocmask(2) system call changes the list of blocked signals – sigpending(2) shows which signals are (blocked and) pending – sigsuspend(2) suspends the calling process until the specified signal is received 28

Signal voodoo When a process forks off a child, it may want to hear back about how things went – when the child process exits, it becomes a zombie until its exit status is reported to the parent process – when something interesting happens to the child (it exits, crashes, stops, etc.), a SIGCHLD signal is sent to its parent – the parent can use the wait() system call (or variants) to find the child’s exit status If the parent is not interested, it can use sigaction() to explicitly specify that SIGCHLD should be ignored 29