DOC PREVIEW
Princeton COS 217 - Goals of this Lecture

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11Signals2Goals of this Lecture• Help you learn about:• Sending signals• Handling signals… and thereby …• How the OS exposes the occurrence of some exceptions to application processes• How application processes can control their behavior in response to those exceptions23Outline1. Unix Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions and Critical Sections6. Blocking Signals7. Alarms8. (If time) Interval Timers9. Conclusion4Unix Process ControlNon-ExistingProcessRunningForegroundProcessStoppedBackgroundProcessRunningBackgroundProcess↓ command↑ Ctrl-c↓ Ctrl-z↑ fg↓ kill -20 pid↑ bg↓ command & ↑ kill –2 pid↑ kill –2 pid← fg35Unix Process Control[Demo of Unix process control using infloop.c]6Process Control ImplementationExactly what happens when you:• Type Ctrl-c?• Keystroke generates interrupt• OS handles interrupt• OS sends a 2/SIGINT signal• Type Ctrl-z?• Keystroke generates interrupt• OS handles interrupt• OS sends a 20/SIGTSTP signalRecall “Exceptions andProcesses” lecture47Process Control Implementation (cont.)Exactly what happens when you:• Issue a “kill –sig pid ” command?• kill command executes trap• OS handles trap• OS sends a sig signal to the process whose id is pid• Issue a “fg”or “bg” command?• fg or bg command executes trap• OS handles trap• OS sends a 18/SIGCONT signal (and does some other things too!)Recall “Exceptions andProcesses” lecture8Outline1. Unix Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions and Critical Sections6. Blocking Signals7. Alarms8. (If time) Interval Timers9. Conclusion59Definition of SignalSignal: A notification of an event• Exception occurs (interrupt, trap, fault, or abort) • Context switches to OS• OS sends signal to application process• Sets a bit in a vector indicating that a signal of type X occurred• When application process regains CPU, default action for that signal executes• Can install a signal handler to change action• (Optionally) Application process resumes where it left offmovlpushlcall faddlmovl...Processvoid handler(int iSig) {…}signal10Examples of SignalsUser types Ctrl-c• Interrupt occurs• Context switches to OS• OS sends 2/SIGINT signal to application process• Default action for 2/SIGINT signal is “terminate”Process makes illegal memory reference• Fault occurs• Context switches to OS• OS sends 11/SIGSEGV signal to application process• Default action for 11/SIGSEGV signal is “terminate”611Outline1. Unix Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions and Critical Sections6. Blocking Signals7. Alarms8. (If time) Interval Timers9. Conclusion12Sending Signals via KeystrokesThree signals can be sent from keyboard:• Ctrl-c Æ 2/SIGINT signal• Default action is “terminate”• Ctrl-z Æ 20/SIGTSTP signal• Default action is “stop until next 18/SIGCONT”• Ctrl-\ Æ 3/SIGQUIT signal• Default action is “terminate”713Sending Signals via Commandskill Commandkill -signal pid• Send a signal of type signal to the process with id pid• No signal type name or number specified => sends 15/SIGTERM signal• Default action for 15/SIGTERM is “terminate”• Editorial: Better command name would be sendsigExampleskill –2 1234kill -SIGINT 1234• Same as pressing Ctrl-c if process 1234 is running in foreground14Sending Signals via Function Calls raise()int raise(int iSig);• Commands OS to send a signal of type iSig to current process• Returns 0 to indicate success, non-0 to indicate failureExampleint iRet = raise(SIGINT); /* Process commits suicide. */assert(iRet != 0); /* Shouldn't get here. */815Sending Signals via Function Callskill()int kill(pid_t iPid, int iSig);• Sends a iSig signal to the process whose id is iPid• Equivalent to raise(iSig) when iPid is the id of current process• Editorial: Better function name would be sendsig()Examplepid_t iPid = getpid(); /* Process gets its id.*/int iRet = kill(iPid, SIGINT); /* Process sends itself aassert(iRet != 0); SIGINT signal (commitssuicide) */16Outline1. Unix Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions and Critical Sections6. Blocking Signals7. Alarms8. (If time) Interval Timers9. Conclusion917Handling SignalsEach signal type has a default action• For most signal types, default action is “terminate”A program can install a signal handler to change action of (almost) any signal type18Uncatchable SignalsSpecial cases: A program cannot install a signal handler for signals of type:•9/SIGKILL• Default action is “terminate”• 19/SIGSTOP• Default action is “stop until next 18/SIGCONT”1019Installing a Signal Handlersignal()sighandler_t signal(int iSig, sighandler_t pfHandler);• Installs function pfHandler as the handler for signals of type iSig•pfHandler is a function pointer:typedef void (*sighandler_t)(int);• Returns the old handler on success, SIG_ERR on error• After call, (*pfHandler) is invoked whenever process receives a signal of type iSig20Installing a Handler Example 1Program testsignal.c:#define _GNU_SOURCE /* Use modern handling style */#include <stdio.h>#include <assert.h>#include <signal.h>static void myHandler(int iSig) {printf("In myHandler with argument %d\n", iSig);}…1121Installing a Handler Example 1 (cont.)Program testsignal.c (cont.):…int main(void) {void (*pfRet)(int);pfRet = signal(SIGINT, myHandler);assert(pfRet != SIG_ERR);printf("Entering an infinite loop\n");for (;;);return 0;}22Installing a Handler Example 1 (cont.)[Demo of testsignal.c]1223Installing a Handler Example 2Program testsignalall.c:#define _GNU_SOURCE#include <stdio.h>#include <assert.h>#include <signal.h>static void myHandler(int iSig) {printf("In myHandler with argument %d\n", iSig);}…24Installing a Handler Example 2 (cont.)Program testsignalall.c (cont.):…int main(void) {void (*pfRet)(int);pfRet = signal(SIGHUP, myHandler); /* 1 */pfRet = signal(SIGINT, myHandler); /* 2 */pfRet = signal(SIGQUIT, myHandler); /* 3 */pfRet = signal(SIGILL, myHandler); /* 4 */pfRet = signal(SIGTRAP, myHandler); /* 5 */pfRet = signal(SIGABRT, myHandler); /* 6 */pfRet = signal(SIGBUS, myHandler); /* 7 */pfRet = signal(SIGFPE, myHandler); /* 8 */pfRet = signal(SIGKILL, myHandler); /* 9 */…This call fails1325Installing a Handler Example 2 (cont.)Program


View Full Document

Princeton COS 217 - Goals of this Lecture

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Goals of this Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Goals of this Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Goals of this Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?