Signals Tanzir Ahmed CSCE 313 Fall 2018.

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.
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 3 – Process Concepts Outline 3.1 Introduction 3.1.1Definition of Process 3.2Process States:
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
3.5 Interprocess Communication
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.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
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”
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
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.
* 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,
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)
Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns
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 *
Interprocess Communication Mechanisms. IPC Signals Pipes System V IPC.
UNIX Signals. A UNIX signal corresponds to an event –It is raised by one process (or hardware) to call another process’s attention to an event –It can.
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.
© 숙대 창병모 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.
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.
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, Abdelzaher, Caccamo1 Signals.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Computer System Structures Interrupts
UNIX signals.
Exceptional Control Flow
G.Jyostna.
Interrupts and exceptions
Processes and threads.
Exceptional Control Flow
Task Control: Signals and Alarms Chapter 7 and 8
Exceptional Control Flow
Chapter 3 – Process Concepts
Unix Process Management
INTER-PROCESS COMMUNICATION
Exceptional Control Flow: System Calls, Page Faults etc.
Threads and Cooperation
Exceptional Control Flow Part II
Exceptional Control Flow
CSC Advanced Unix Programming, Fall 2015
Operating Systems Lecture 12.
Process Description and Control
Exceptions Control Flow
Exceptional Control Flow
Inter-Process Communication ENCE 360
Task Control: Signals and Alarms Chapter 7 and 8
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Controlling Processes
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
Signals.
Presentation transcript:

Signals Tanzir Ahmed CSCE 313 Fall 2018

Inter-Process Communication IPC classes Pipes and FIFO Message Passing Shared Memory Semaphore Sets Signals References: Baseline slides: CSCE-313 Tyagi, Bettati, Gu Advanced Programming in the UNIX Environment, Third Edition, W. Richard Stevens and Stephen A. Rago, Addison-Wesley Professional Computing Series, 2013. Chapters 10, 15 Understanding Unix/Linux Programming, Bruce Molay, Chapter 6 Advanced Linux Programming Ch 5 Some material also directly taken or adapted with changes from Illinois course in System Programming (Prof. Angrave), UCSD (Prof. Snoeren), and USNA (Prof. Brown)

Inter-Process Communication Landscape Multiple approaches and mechanisms for processes to communicate to one another. Named Pipe (FIFO), Semaphores, Message Passing, Shared Memory, Signals are some of the principal mechanisms for IPC Rendering from Prof. Farrell (Kent State University)

Background: User-Mode Exceptional Flow So far exceptional control flow features have been usable only by the operating system Exceptions: Synchronous: faults, traps, aborts Asynchronous: interrupts All exception handlers run in protected (Kernel) mode Would like similar capabilities for user mode code Inter-Process communication to facilitate ‘exceptional control flow’ is subject of today’s discussion We will discuss them through “Signals” mechanism ……but also in a broader context beyond just IPC

Example: What does CTRL-C do? Taken from: Chapter 6 of “Understanding Unix/Linux Programming” by Bruce Molay

What is a Signal? A Signal is a one-word message A Green Light is a signal, A Referee’s whistle is a signal These items and events do not contain messages, they are messages! Each Signal has a numerical code So when we press CTRL-C key we ask the Kernel to send the interrupt signal to the currently running process

List of Signals Terminate and also dump core (for further investigation) Will be ignored by default, unless you override Process scheduler?? Source of table: http://man7.org/linux/man-pages/man7/signal.7.html Multiple values mean that they are different based on architecture

Signals – Some Points The “Action” in the previous page in many cases indicate the “Default Action” You are allowed to “override” the action for some signals (e.g., SIGINT, SIGUSRx, SIGTERM, SIGSTP), while for others (SIGKILL, SIGILL, SIGFPE, SIGSEGV, SIGSTOP) you cannot override This is also called “signal handling”, or “catching” SIGCHLD is the only signal so far that is Ignored by default Most others will kill the process except SIGCONT and SIGSTP Does that mean you can kill any process by sending any signal?? NO. You can only signal processes that you created SIGSTP (suspends a process) and SIGCONT (conitues/unsuspends a process) are very useful for “job control” How to control a process group tied by a shell session? All processes that you run make a group of processes that need special controlling You may suspend a process, run it in the background, move to foreground What happens to standard input/output of a background process?

Where do Signals come from? Today we’ll look at Signals in a broader context IPC is one of the contexts (one process sending to another) Others are facilitated by Users and Kernel [Users] Signals generated by external Input devices [Kernel] Exceptions This is a fairly accurate description of relationship between exceptions and signals as stated in WIKIPEDIA or any other commonly available references: A process's execution may result in the generation of a hardware exception, for instance, if the process attempts to divide by zero or incurs a TLB miss. In Unix-like operating systems, this event automatically changes the processor context to start executing a Kernel exception handler. In case of some exceptions, such as a page fault, the Kernel has sufficient information to fully handle the event itself and resume the process's execution. Other exceptions, however, the Kernel cannot process intelligently and it must instead defer the exception handling operation to the faulting process. This deferral is achieved via the signal mechanism, wherein the Kernel sends to the process a signal corresponding to the current exception. For example, if a process attempted integer divide by zero on an x86 CPU, a divide error exception would be generated and cause the Kernel to send the SIGFPE signal to the process. Similarly, if the process attempted to access a memory address outside of its virtual address space, the Kernel would notify the process of this violation via a SIGSEGVsignal. The exact mapping between signal names and exceptions is obviously dependent upon the CPU, since exception types differ between architectures. Taken from: Chapter 6 of “Understanding Unix/Linux Programming” by Bruce Molay

Where do Signals come from? (USER) Terminal-generated signals: triggered when user presses certain key on terminal. (e.g. ^C) kill(2) function: Sends any signal to another process. kill(1) command: The command-line interface to kill(2) raise(3): Sends a signal to itself A user can send signals to only his owned processes (Kernel) Exception-generated signals: CPU execution detects condition and notifies Kernel. (e.g. SIGFPE divide by 0, SIGSEGV invalid memory reference, SIGILL when an instruction will illegal opcode is found) (PROCESSES) Software-condition generated signals: Triggered by software event (e.g. SIGURG by out-of-band data on network connection, SIGPIPE by broken pipe, SIGALRM by timer)

Generating Signals: kill(2) and raise(3) #include <signal.h> int kill(pid_t pid, int sig); /* send signal ‘sig’ to process ‘pid’ */ /* example: send signal SIGUSR1 to process 1234 */ if (kill(1234, SIGUSR1) == -1) perror(“Failed to send SIGUSR1 signal”); /* example: kill parent process */ if (kill(getppid(), SIGTERM) == -1) perror(“Failed to kill parent”); #include <signal.h> int raise(int sig); /* Sends signal ‘sig’ to itself. Part of ANSI C library! */ “raise” is defined by ANSI C, not POSIX.1. Since ANSI C does not deal with multiple processes it could not define a function such as “kill” that requires a process ID argument. Raise sends a signal to the executing process Kill sends a signal to the specified process

Signals and the Kernel Many of the signals are generated by the Kernel in response to events and exceptions received SIGFPE – FP exception SIGILL – Illegal instruction SIGSEGV – Segment Violation Many others are routed through the Kernel, if not originating from the Kernel itself E.g. SIGHUP (terminal hang), SIGINT (CTRL-C keyboard), SIGTSTP (CTRL-Z), SIGKILL (KILL) As with system calls, the Kernel receives the signals from hardware and other processes on behalf of a process Then Kernel forwards the signal to the appropriate process

What can a Process do about a Signal? Tell the Kernel what to do with a signal: Accept Default action. All signals have a default action signal (SIGINT, SIG_DFL) Ignore the signal. Works for most signals signal (SIGINT, SIG_IGN) cannot ignore SIGKILL and SIGSTOP; also unwise to ignore hardware exception signals Catch the signal (call a function). Tell the Kernel to invoke a given function (signal handler) whenever signal occurs. signal (SIGINT, foo)

Signal – Subtle Differences SIGTERM The SIGTERM signal is a generic signal used to cause program termination. Unlike SIGKILL, this signal CAN be blocked, handled, or ignored. It is the normal way to politely ask a program to terminate. SIGINT The SIGINT (“program interrupt”) signal is sent when the user types Ctrl+C. CAN be handled and even ignored SIGKILL The SIGKILL signal is used to cause immediate program termination. It CANNOT be blocked, handled or ignored. It is a last resort, after first trying SIGINT or SIGTERM SIGHUP The SIGHUP (“hang-up”) signal is used to report that the user’s terminal is disconnected, perhaps because a network or telephone connection (????) was broken. https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html

Simple Signal Handling: Example Taken from: Chapter 6 of “Understanding Unix/Linux Programming” by Bruce Molay

Example – One Handler for Multiple Signals static void sig_usr(int); /* one handler for two signals */ int main (void) { if (signal(SIGUSR1, sig_usr) == SIG_ERR) perror(“cannot catch signal SIGUSR1”); if (signal(SIGUSR2, sig_usr) == SIG_ERR) perror(“cannot catch signal SIGUSR2”); for(;;) pause(); } static void sig_usr(int signo) { /*argument is signal number*/ if (signo == SIGUSR1) printf(“received SIGUSR1\n”); else if (signo == SIGUSR2) printf(“received SIGUSR2\n”); else error_dump(“received signal %d\n”, signo); return;

Signals: Terminology A signal is generated for a process when event that causes the signal occurs. (Hardware exception, software condition, etc.) A signal is delivered when action for a signal is taken. During the time between generation and delivery, signal is pending. That is also the lifecycle of a signal A process has the option of blocking the delivery of a signal. Signal remains blocked until process either (a) unblocks the signal, or (b) changes the action to ignore the signal.

Signals: Terminology A blocked signal is generated like other signals, but is not delivered to the target process What happens when blocked signal is generated more than once? If system delivers the signal more than once, the signal is queued signal mask is the mechanism to define set of signals that are blocked from delivery Checkout the function: sigprocmask(..) There are 2 critical signal sets that the Kernel maintains Pending Signals Set Blocked Signals Set

Pending Signals Set A bit vector known as pending bit vector of signals that are currently pending for the process These signals have been sent to the process, but haven’t yet been handled by the process A process does not manipulate the pending set directly Residency in pending set means that this signal is currently blocked (discussed next) by the process Each kind of signal has one bit assigned to it - If a particular signal type is already pending and is sent again, the second signal is dropped

Blocked Signals The Kernel also keeps a blocked bit-vector for each process The process itself manipulates this vector through system calls Each type of signal has a bit assigned to it If a particular type of signal is blocked, it will not be delivered to the process until unblocked In the meantime, the pending set will keep collecting this signal When the Kernel calls a signal handler on a process, that type of signal is automatically blocked Generally signal handlers don’t need to worry about being interrupted by the same kind of signal again Does this sound like “Interrupts being blocked while handling an interrupt”???

Blocked Signals (2) Example: A process with a SIGINT handler First, SIGINT received causes the SIGINT handler to be called Also causes SIGINT to be blocked for the process - If another SIGINT occurs during Handler execution, it is recorded in the pending bit vector, but not delivered When a signal handler returns, the blocked signal type is automatically unblocked When handler returns, signals of that type can start to be delivered again Several functions for manipulating these signal bit vectors sigpending (returns current pending signals for the process) sigprocmask (manipulate the set of blocked signals for the process)

Signals Concept Refresh Where do signals come from? User, Kernel, Process What can a process tell the Kernel to do about a Signal? Accept Default, Ignore, Catch A Signal is “Generated” when …… event that causes the signal occurs A Signal is “Delivered” when …… action for signal is taken

Signals Concept Refresh (2) A Signal is “Blocked” when ….. it is not allowed to be delivered signal mask can be used to control …… set of signals that are blocked from delivery When the Kernel calls a signal handler on a process, that type of signal is …… automatically blocked When a signal handler returns, the blocked signal type is ……. automatically unblocked

SUMMARY: Signals Signals share many common traits with hardware exception handling A user-mode version of hardware exceptions When a signal handler is invoked that type of signal is blocked until the handler returns Very similar to H/W interrupts Signals allow us to leverage exceptional control flow in user programs Enables powerful techniques in server programming Are widely used in server programs Web servers, email servers, DNS servers, Databases, etc.