DOC PREVIEW
Princeton COS 217 - Signals

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 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 34 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 34 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 34 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 34 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 34 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 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11Signals2Communicating with the OS• System call (last lecture)o Request to the operating system to perform a tasko … that the process does not have permission to perform• Signal (this lecture)o Asynchronous notification sent to a processo … to notify the process of an event that has occurred User ProcessOperating Systemsignals systems calls23Outline1. UNIX Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions6. Blocking Signals7. Conclusion8. (optional) Alarms and Interval Timers4UNIX 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?o Keyboard sends hardware interrupto Hardware interrupt is handled by OSo OS sends a 2/SIGINT signal• Type Ctrl-z?o Keyboard sends hardware interrupto Hardware interrupt is handled by OSo OS sends a 20/SIGTSTP signal• Issue a “kill –sig pid” command?o OS sends a sig signal to the process whose id is pid• Issue a “fg” or “bg” command?o OS sends a 18/SIGCONT signal (and does some other things too!)47Outline1. UNIX Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions6. Blocking Signals7. Conclusion8. (optional) Alarms and Interval Timers8Definition of SignalSignal: A notification of an evento Event gains attention of the OSo OS stops the application process immediately, sending it a signalo Default action for that signal executes– Can install a signal handler to change actiono Application process resumes where it left offmovlpushlcall faddlmovl...Processvoid handler(int iSig){…}signal59Examples of SignalsUser types Ctrl-co Event gains attention of OSo OS stops the application process immediately, sending it a 2/SIGINT signalo Default action for 2/SIGINT signal is “terminate”Process makes illegal memory referenceo Event gains attention of OSo OS stops application process immediately, sending it a 11/SIGSEGV signalo Default action for 11/SIGSEGV signal is “terminate”10Outline1. UNIX Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions6. Blocking Signals7. Conclusion8. (optional) Alarms and Interval Timers611Sending Signals via KeystrokesThree signals can be sent from keyboard:o Ctrl-c Æ 2/SIGINT signal–Default action is “terminate”o Ctrl-z Æ 20/SIGTSTP signal–Default action is “stop until next 18/SIGCONT”o Ctrl-\ Æ 3/SIGQUIT signal–Default action is “terminate”12Sending Signals via Commandskill Commandkill -signal pid– Send a signal of type signal to the process with id pid– Can specify either signal type name (-SIGINT) or number (-2)o No signal type name or number specified => sends 15/SIGTERM signal– Default action for 15/SIGTERM is “terminate”o Editorial: Better command name would be sendsigExampleskill –2 1234kill -SIGINT 1234o Same as pressing Ctrl-c if process 1234 is running in foreground713Sending Signals via Function Calls raise()int raise(int iSig);o Commands OS to send a signal of type iSig to current processo Returns 0 to indicate success, non-0 to indicate failureExampleint ret = raise(SIGINT); /* Process commits suicide. */assert(ret != 0); /* Shouldn't get here. */14Sending Signals via Function Callskill()int kill(pid_t iPid, int iSig);o Sends a iSig signal to the process whose id is iPido Equivalent to raise(iSig) when iPid is the id of current processo Editorial: Better function name would be sendsig()Examplepid_t iPid = getpid(); /* Process gets its id.*/kill(iPid, SIGINT); /* Process sends itself aSIGINT signal (commitssuicide) */815Outline1. UNIX Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions6. Blocking Signals7. Conclusion8. (optional) Alarms and Interval Timers16Handling SignalsEach signal type has a default actiono For most signal types, default action is “terminate”A program can install a signal handler to change action of (almost) any signal type917Uncatchable SignalsSpecial cases: A program cannot install a signal handler for signals of type:o 9/SIGKILL– Default action is “terminate”– Catchable termination signal is 15/SIGTERMo 19/SIGSTOP– Default action is “stop until next 18/SIGCONT”– Catchable suspension signal is 20/SIGTSTP18Installing a Signal Handlersignal()sighandler_t signal(int iSig, sighandler_t pfHandler);o Installs function pfHandler as the handler for signals of type iSigo pfHandler is a function pointer:typedef void (*sighandler_t)(int);o Returns the old handler on success, SIG_ERR on erroro After call, (*pfHandler) is invoked whenever process receives a signal of type iSig1019Installing 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);}…20Installing 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;}1121Installing a Handler Example 1 (cont.)[Demo of testsignal.c]22Installing 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);}…1223Installing 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 */…24Installing a Handler Example 2 (cont.)Program testsignalall.c (cont.):…/* Etc., for every signal. */printf("Entering an infinite loop\n");for (;;);return 0;}1325Installing a Handler Example 2 (cont.)[Demo of testsignalall.c]26Installing a Handler Example 3Program generates lots of temporary datao Stores the data in a temporary fileo Must delete the file before exiting…int


View Full Document

Princeton COS 217 - Signals

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

Lecture

Lecture

19 pages

Load more
Download Signals
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 Signals 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 Signals 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?