Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lab 9 CIS 370 Umass Dartmouth.  A pipe is typically used as a one-way communications channel which couples one related process to another.  UNIX deals.
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.
1 Select and poll and Signals CS 241 April 6, 2012 University of Illinois.
Lesson 10-Controlling User Processes. Overview Managing and processing processes. Managing jobs. Exiting/quitting when jobs have been stopped.
Page 1 Task Control: Signals and Alarms Havilland and Salama’s Unix Systems Programming B. Ramamurthy.
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.
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.
CS 497C – Introduction to UNIX Lecture 27: - The Process Chin-Chih Chang
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
Process Description and Control A process is sometimes called a task, it is a program in execution.
COMP5102 Lecture 4 Operating Systems (OS) Inter-process Communication phones off (please)
Process states inWindows 2000 and Linux Module 2.1.
5 UNIX Processes. Introduction  Processes  How to list them  How to terminate them  Process priorities  Scheduling jobs  Signals.
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.
* 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.
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.
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.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
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.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
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.
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.
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.
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.
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)
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
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
UNIX signals.
G.Jyostna.
Task Control: Signals and Alarms Chapter 7 and 8
CSC Advanced Unix Programming, Fall 2015
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
CSE 451: Operating Systems Spring 2006 Module 4 Processes
Signals.
Presentation transcript:

Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth

Good Programming 4 It is always good practice to write several small programs that do specific things and combine them to do a task rather than write a large monolithic program. 4 Ideally you would like several cooperating processes. 4 UNIX provides a rich environment for IPCs – IPC can be accomplished with message passing, signals, pipes and FIFOs.

Signals 4 We will focus on signals in this chapter and get to pipes and FIFOs later.  Suppose you’re running a UNIX command $gcc large_program.c 4 If the command does not terminate in a reasonable period of time, one typically terminates the process by pressing ctrl-c. 4 The command is terminated and the shell prompt returns.

What actually happens? 4 The part of the kernel responsible for the keyboard input sees the interrupt character.  The kernel then sends a signal called SIGINT to all the processes that recognize the terminal as their controlling terminal. 4 This includes the invocation of gcc.  When gcc receives the signal, it performs the default action associated with SIGINT and terminates.

What actually happens? 4 The shell process also receives the signal. 4 It sensibly ignores the signal!  Programs can elect to “catch” SIGINT. 4 When they catch the signal they execute a special interrupt routine (aka interrupt service routine (ISR)).

Signals and the kernel 4 Signals are also used by the kernel to deal with certain kinds of severe error. 4 Suppose a corrupt program containing illegal machine instructions is executed (accidentally or intentionally). 4 A process begins execution of the program.

Signals and the kernel  The kernel will detect the attempt to execute the illegal instructions and send the process the signal SIGILL (ILL stands for illegal) to terminate it. It may look something like this $./faulty_prog Illegal instruction. Core dumped.

Process to process signals 4 Signals can also be sent between processes. 4 Consider the following: $ gcc very_big_prog.c & [1] 1034 $ // after a long time $ kill n terminated $ The command kill sends a SIGTERM signal. SIGTERM will terminate the process.

Process response to signals A process can do three things with signals: 4 Choose the way it responds when it receives a particular signal (signal handling). 4 Block out signals (that is, leave them for later) for a specific piece of critical code. 4 Send a signal to another process.

Signal names 4 Signals are given mnemonic names.  They are defined in, with the pre-processor directive #define. 4 They stand for small positive integers. 4 Most are intended for the kernel, although a few are provided to be sent from process to process.

Signal names 4 SIGABRT - Process abort signal (abnormal termination - core dumped) 4 SIGALRM - Alarm clock (sent by the kernel after timer has expired). 4 SIGBUS - Bus error (sent if a hardware fault is detected - abnormal termination) 4 SIGCHLD - Child process terminated (whenever a child process stops or terminates, the kernels sends this to the parent).

4 SIGCONT - Continue executing if stopped (This is a job control signal which will continue if the process is stopped, else ignored, inverse of SIGSTOP) 4 SIGFPE - Floating-point exception (AT). 4 SIGHUP - The hangup signal. (The kernel sends this to all processes attached to a control terminal when the terminal is disconnected - applies to sessions as well). 4 SIGILL - Illegal instruction. (AT)

4 SIGKILL - Kill (special signal sent from one process to another to terminate the receiver - cannot be ignored). 4 SIGPIPE - Write on pipe or socket when recipient has terminated. (discussed later). 4 SIGPOLL - Pollable event. (signal is generated by the kernel when an open file descriptor is ready for input or output). 4 SIGPROF - Profiling time expired. (signal is generated when timer expires and can be used by interpreters to profile execution).

4 SIGQUIT - Quit. (similar to SIGINT) 4 SIGSEGV - Invalid memory reference. (AT) (segmentation violation, generated when a process attempts to access invalid memory) 4 SIGSTOP - Stop execution. (job control signal, cannot be ignored) 4 SIGSYS - Invalid system call. (AT) (sent by kernel when a process attempts to execute a machine instruction which is not a sys call) 4 SIGTERM - Software termination signal (used to terminate a process)

4 SIGTRAP - Trace trap. (AT) (special signal used by debuggers such as sdb and adb, along with ptrace system call). 4 SIGTSTP - Terminal stop signal. (signal generated by the user typing ctrl-z, can be ignored) 4 SIGTTIN - Background process attempting read. (when a bg process attempts to read from the controlling terminal, this signal is sent and the process is typically stopped)

4 SIGTTOU - Background process attempting write. (generated when the bg process attempts to write to the controlling terminal) 4 SIGURG - High bandwidth data is available at a socket. (signal tells a process that urgent or out of band data has been received on a network connection) 4 SIGUSR1 and SIGUSR2 - user defined signals

4 SIGVTALRM - Virtual timer expired. (sent when user-mode time expires). 4 SIGXCPU - CPU time limit exceeded. (signal generated when the process exceeds its maximum CPU time limit). 4 SIGXFSZ - File size limit exceeded. (AT) (generated when a process exceeds its maximum file size limit)

Normal termination 4 For most signals normal termination occurs when a signal is received. 4 The exit status returned to the parent in this circumstance tells the parent what happened 4 There are macros defined in which allow the parent to determine the cause of termination and in this particular case the value of the signal which was responsible.

Abnormal Termination 4 The signals SIGABRT, SIGBUS, SIGSEGV, SIGQUIT, SIGILL, SIGTRAP, SIGSYS, SIGXCPU, SIGXFSZ and SIGFPE cause abnormal termination - core dumped. 4 The memory dump of the process is written to a file called core. 4 The core file will include the values of the program variables, h/w registers and control information - in binary.

Signal Handling 4 Once a signal is received the process may: –Take the default action, which is to terminate the process. SIGSTOP will stop the process. –Ignore the signal and carry on processing. In case of batch programs signals may be ignored. SIG_IGN flag will ignore signal. –Take a specified user-defined action. Whenever a program exits, whatever the cause, a programmer might want to perform clean-up operations such as removing work files.

Signal sets 4 Signal sets are one of the main parameters passed to system calls that deal with signals. 4 They simply specify a list of signals you want to do something with. 4 Signal sets are defined of the type sigset_t 4 You can empty the set, or remove a few signals, keep only the ones that interest you.

Signal sets 4 Usage #include // initialize int sigempty(sigset_t *set); int sigfillset(sigset_t *set); // manipulate int sigaddset(segset_t *set, int signo); int sigdelset(segset_t *set, int signo);

Setting the signal action 4 Once you have defined a signal set, you can choose a particular method of handling a signal using sigaction. 4 Usage #include int sigaction(int signo, const struct sigaction *act, const struct sigaction *oact);

sigaction continued 4 sigaction structure contains a signal set. 4 The first parameter signo specifies the signal for which we want to specify action 4 SIGSTOP and SIGKILL are exempt. 4 The second parameter act gives the action you want to set for signo. 4 The third parameter oact will be filled out with the current settings.

Without interrupts With an interrupt

Restoring a previous action

The sigsetjmp and siglongjmp A program position is saved in in an object of type sigjmp_buf (defined in

sigsetjmp and siglongjmp signals 4 Sometimes it is useful to go back to a certain part of a program if a signal occurs 4 sigsetjmp saves the mask of the current signal set and the current position in the program 4 siglongjmp restores the mask if encountered (like a long-distance, non-local goto statement) and returns control to the point in the program where it was saved.

Signal Blocking 4 If a program is performing a sensitive task, it may well need to be protected from interrupts. 4 Rather than ignore the signals, a process can block the signals 4 The signals will be handled after the completion of the task.

The sigprocmask system call 4 Usage #include int sigprocmak(int how, const sigset_t *set, sigset_t *oset); The how parameter tells the system call what specific action to take. For example it coul be set to SIG_SETMASK, which means block out the signals in the second parameter set from now on. The third parameter is simply filled with the current mask of blocked signals.

Sending signals to other processes 4 The kill system call 4 Usage #include int kill(pid_t pid, int sig);

Sending signals using raise and alarm 4 raise() simply sends a signal to the executing process. 4 alarm() is a simple and useful call that sets up a process alarm clock. Signals are used to tell the process that the clock’s timer has expired. 4 alarm is not like sleep, which suspends the process execution, alarm returns immediately. 4 After a fork, however the alarm clock is turned off in a child process.

The pause system call 4 pause is a companion to alarm. 4 pause suspends the calling process, in a way that will not waste CPU cycles, until any signal, such as SIGALRM, is received. 4 If the process ignores the signal, the pause also ignores the signal. 4 If the signal is caught, when the appropriate interrupt routine is finished, pause returns a -1.