Operating Systems Recitation 4, April 14-15 th, 2002 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

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.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
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.
UNIX IPC CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of Concurrency2 Creating a UNIX process A process is created by making an exact.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
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.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
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.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
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.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Operating Systems Chapter 2
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
* 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,
System calls for Process management
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns
Operating Systems Process Creation
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
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 *
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.
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.
4300 Lines Added 1800 Lines Removed 1500 Lines Modified PER DAY DURING SUSE Lab.
© 숙대 창병모 1 제 10 장 신호 (Signal). © 숙대 창병모 2 Contents 1. Signal Concepts 2. signal() 3. Interrupted System Calls 4. kill() /raise() 5. alarm() pause() 6.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Signals & Message queue Inter process mechanism in Linux system 3/24/
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
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, Abdelzaher, Caccamo1 Signals.
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
UNIX signals.
Recitation 7 – 3/18/02 Outline fork and scheduling Signals
G.Jyostna.
Task Control: Signals and Alarms Chapter 7 and 8
Protection of System Resources
Unix Process Management
Tarek Abdelzaher Vikram Adve Marco Caccamo
Threads and Cooperation
Exceptional Control Flow Part II
Structure of Processes
CSC Advanced Unix Programming, Fall 2015
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Exceptional Control Flow
Tutorial: The Programming Interface
Processes in Unix and Windows
Signals.
Presentation transcript:

Operating Systems Recitation 4, April th, 2002 Signals

Motivation Notify a process or a group of processes that a specific event occurred. Force a process to execute a signal handler function included in its code.

generated by event sent to process received & handled by process associated default action catch by invoking user-defined handler ignore abort dump ignore stop continue

Signals Represented by a number identifying the signal. May be sent to a process at any time at any state (signals sent to a non-running process must be saved by the kernel until that process resumes execution). Consumable, each signal sent can be received once.

Generating a signal By a process or internally generated by the kernel. Some examples: divide by 0 (hardware exception), child terminated, a timer expired, terminal generated signal, calling the kill function.

Sending a signal Kernel operations Check whether a process has the privileges to send a signal to another process. Update destination process descriptor. May wake up the process and force it to receive the signal. Check whether the signal nullifies other pending signals for a process.

Receiving a signal Kernel forces the destination process to react to the signal by changing its execution state or by starting the execution of a specified signal handler (or both). For example, the kernel noticed the arrival of the signal, updated the process descriptor of the process that is supposed to receive the signal. In case that process was not running on the CPU at the moment, the kernel deferred the task of waking it up until now. Check for non-blocked pending signals, loop until no more are left.

Handling options Ignore. Perform default action predefined by the kernel depending on the type of signal: –Abort: process is terminated –Dump: abort and a core file containing execution context is created (for debug purposes), contains process address space and CPU registers. –Ignore. –Stop: put process in stopped state. –Continue: if stopped then back to running state. Catch the signal by calling a user-defined function, the signal handler.

Blocking specific signals Signals of a specific type can be selectively blocked by a process and not received. Different from ignoring a signal that is received (as ignoring an or a ringing phone is different from blocking an such that we don’t receive it). sigprocmask system call - examine or modify the set of blocked signals

Pending signals Signals sent but not yet received are pending. Only one pending of any given type for each process, additional pending are discarded. Examples If a signal that is blocked is generated for a process & if action for that signal is default action or to catch, then the signal remains pending, until the process unblocks it or changes its action to ignore. Non-blocked pending signal sigpending system call – examine the set of pending blocked signals.

Signal handler User-defined function. Cannot be interrupted by another occurrence of the handled signal. When a process executes a signal-handler function it masks the corresponding signal (automatically blocking the signal until the handler terminates).

Signals #signaldescriptiondefault action 17SIGCHLDsent to parent when child terminatesignore 8SIGFPEarithmetic exception (for example divide by 0)dump = abort + core SIGINTterminal interrupt key (delete, Ctrl+c)abort = terminate 9SIGKILLterminate a processabort 13SIGPIPEwrites to a pipe but the reader has terminatedabort 30SIGPWRpower failure (for example to switch to UPS)ignore 11SIGSEGVprocess has made invalid memory referencedump 15SIGTERMtermination a process (can be ignored)abort 10SIGUSR1available to processesabort

Tasks Remember which signals are blocked by which process. Determine whether the signal can be ignored. Handle the signal, which may require switching the process to a handler function at any point during its execution and restoring the original execution context after the function returns.

Data structures Signals sent to a process: array of bits, one for each signal type. Blocked signals: array of bits. Flag set if one or more non-blocked signals are pending. Pointer to a signal data structure describing how each signal is handled. A structure that is shared, specifying how signals are handled by several process. Functions and macros that operate on these structures, set bits in arrays, logical operations (for example AND of pending signals with blocked signals, and see what is left)

Signal function #include void (*signal (int signo, void (*func)(int)))(int); Returns previous disposition of signal if OK, SIG_ERR on error. signo – integer, name of the signal func – pointer to a function that takes a single integer and returns nothing. –SIG_IGN, ignore –SIG_DFL, default action –address of function to be called when the signal occurs, handler Returns a pointer to a function that returns nothing, which is the previous signal handler

Example #include main(int argc, char* argv[]) { // install signal handlers signal(SIGTERM, sig_handler); // sent by default kill command signal(SIGSEGV, sig_handler); // segmentation fault signal(SIGUSR1, sig_handler); // user defined signal } void sig_handler(int sig) { if (sig == SIGTERM) … // handle default kill command else if (sig == SIGSEGV) … // handle segmentation fault else if (sig == SIGUSR1) printf(“received SIGUSR1\n”); … }

kill command Send any signal to a process, misnomer $ a.out & [1]1019 $ kill –USR received SIGUSR1 $ kill 1019 I will survive

kill function #include … int kill(pid_t pid, int signo); Returns 0 if OK, -1 on error.

sleep function #include unsigned int sleep(unsigned int seconds); Returns 0 or the number of un-slept seconds. Causes the calling process to be suspended until either –seconds has elapsed –A signal is caught by the process and the signal handler returns.

Exercise description 1.Write a program that types the string “continue?” and waits for the input ‘y’. For any other input print the question again and wait for an answer. After receiving ‘y’ the program continues, and puts a value in address 0: char* ptr = NULL; … *ptr = ‘x’; 2.Run the program and when it waits for the input send it a SIGTERM signal from another window using the kill command.

3.Add a signal handler. Check the type of signal and if its SIGTERM then print: I will survive. Install the signal handler in the main. Explain what happens when you run the program and try to kill the process using the kill command (SIGTERM). 4.Explain what happens when you try to kill the process by using the command kill -9 (SIGKILL). Can this case be handled like SIGTERM?

5.Handle the signal SIGSEGV as well, and print the same string if it occurs. Explain what happens when the program reaches the line assigning a value to address 0. 6.Change the signal handler to execute gdb when the program receives a SIGSEGV signal. Store the process ID using getpid and create a new process using fork. The child process should use execv to run the debugger with the ID of the parent, whereas the parent process should sleep until the debugger is activated. 7.Explain what happens when you run the program.

Exercise notes Before running the program, limit the number of processes that can be created under the shell with the command: $ limit maxproc 10 where 10 is the limit on the number of processes. Use the following arguments to execute gdb: –“/usr/X11R6/bin/xterm” (which is also the 1 st argument for execv) –“-e” –“gdb” –argv[0] of main –parents PID as a string (use the function sprintf to convert it). –NULL

gdb - GNU DeBugger Allows you to step through C,C++ programs in order to find the point at which they break. The program to be debugged is normally specified on the command, you can also specify a core or, if you want to investigate a running program, a process ID. Common commands: run: execute the program. c: continue execution from a breakpoint. print: print the value of a variable or expression where: in the program kill: abort the process running under gdb’s control quit: exit gdb info, help

Exercise submission Monday, April 29 th. Software –directory: ~username/os02b/ex-sig –files: ex-sig.c –permissions: chmod ugo+rx (to above) Hardcopy –name, ID, login, CID –ex-sig.c –answers to question. –submit in mailbox 380, Elad Sarver,

References Operating Systems, chapter 4.11, Sivan Toledo, Advanced Programming in the UNIX Environment, chapter 10, Richard Stevens, For more details see: Understanding the Linux Kernel, chapter 9, Bovet & Cesati, 2000.