DOC PREVIEW
Princeton COS 217 - Signals

This preview shows page 1-2-3-4-31-32-33-34-35-64-65-66-67 out of 67 pages.

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

Unformatted text preview:

SignalsCommunicating with the OSOutlineUNIX Process ControlSlide 5Process Control ImplementationSlide 7Definition of SignalExamples of SignalsSlide 10Sending Signals via KeystrokesSending Signals via CommandsSending Signals via Function CallsSending Signals via Function CallsSlide 15Handling SignalsUncatchable SignalsInstalling a Signal HandlerInstalling a Handler Example 1Installing a Handler Example 1 (cont.)Slide 21Installing a Handler Example 2Installing a Handler Example 2 (cont.)Slide 24Slide 25Installing a Handler Example 3Example 3 ProblemExample 3 SolutionSIG_IGNSIG_DFLSlide 31Race ConditionsRace Condition ExampleRace Condition Example (cont.)Slide 35Slide 36Slide 37Race Conditions in GeneralSlide 39Blocking SignalsBlocking Signals in GeneralBlocking Signals Example 1Blocking Signals in HandlersInstalling a Handler with BlockingBlocking Signals Example 2Blocking Signals Example 2 (cont.)Slide 47Slide 48Predefined SignalsSummarySlide 51Slide 52AlarmsAlarm Example 1Alarm Example 1 (cont.)Slide 56Slide 57Alarm Example 2Alarm Example 2 (cont.)Slide 60Slide 61Interval TimersInterval Timer ExampleInterval Timer Example (cont.)Slide 65Slide 66Slide 671Signals2Communicating with the OS•System call (last lecture)Request to the operating system to perform a task… that the process does not have permission to perform•Signal (this lecture)Asynchronous notification sent to a process… to notify the process of an event that has occurred User ProcessOperating Systemsignals systems calls3Outline1. 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 ← fg5UNIX Process Control[Demo of UNIX process control using infloop.c]6Process Control ImplementationExactly what happens when you:•Type Ctrl-c?Keyboard sends hardware interruptHardware interrupt is handled by OSOS sends a 2/SIGINT signal•Type Ctrl-z?Keyboard sends hardware interruptHardware interrupt is handled by OSOS sends a 20/SIGTSTP signal•Issue a “kill –sig pid” command?OS sends a sig signal to the process whose id is pid•Issue a “fg” or “bg” command?OS sends a 18/SIGCONT signal (and does some other things too!)7Outline1. UNIX Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions6. Blocking Signals7. Conclusion8. (optional) Alarms and Interval Timers8Definition of SignalSignal: A notification of an eventEvent gains attention of the OSOS stops the application process immediately, sending it a signalDefault action for that signal executes–Can install a signal handler to change actionApplication process resumes where it left offmovlpushlcall faddlmovl...Processvoid handler(int iSig){…}signal9Examples of SignalsUser types Ctrl-cEvent gains attention of OSOS stops the application process immediately, sending it a 2/SIGINT signalDefault action for 2/SIGINT signal is “terminate”Process makes illegal memory referenceEvent gains attention of OSOS stops application process immediately, sending it a 11/SIGSEGV signal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 Timers11Sending 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”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)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 foreground13Sending 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 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);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.*/kill(iPid, SIGINT); /* Process sends itself a SIGINT signal (commits suicide) */15Outline1. UNIX Process Control2. Signals3. Sending Signals4. Handling Signals5. Race Conditions6. Blocking Signals7. Conclusion8. (optional) Alarms and Interval Timers16Handling 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 type17Uncatchable SignalsSpecial cases: A program cannot install a signal handler for signals of type:9/SIGKILL–Default action is “terminate”–Catchable termination signal is 15/SIGTERM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);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 iSig19Installing 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,


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

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