Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

Similar presentations


Presentation on theme: "Operating Systems Recitation 4, April 14-15 th, 2002 Signals."— Presentation transcript:

1 Operating Systems Recitation 4, April 14-15 th, 2002 Signals

2 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.

3 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

4 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.

5 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.

6 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.

7 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.

8 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.

9 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 email or a ringing phone is different from blocking an email such that we don’t receive it). sigprocmask system call - examine or modify the set of blocked signals

10 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.

11 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).

12 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

13 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.

14 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)

15 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

16 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”); … }

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

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

19 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.

20 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.

21 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?

22 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.

23 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

24 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

25 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, eladsa@post.tau.ac.il eladsa@post.tau.ac.il

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


Download ppt "Operating Systems Recitation 4, April 14-15 th, 2002 Signals."

Similar presentations


Ads by Google