Harvey Mudd CS 105 - Exceptional Control Flow Part II

Unformatted text preview:

Exceptional Control Flow Part IIECF Exists at All Levels of a SystemProgrammer’s Model of MultitaskingShell ProgramsSimple Shell eval FunctionProblem with Simple Shell ExampleSignalsSignal ConceptsSignal Concepts (cont.)Slide 16Signal ConceptsProcess GroupsSending Signals with killSending Signals from the KeyboardExample of ctrl-c and ctrl-zSending Signals with kill FunctionReceiving SignalsDefault ActionsInstalling Signal HandlersSignal Handling ExampleSignal Handler FunkinessLiving With Nonqueuing SignalsSummaryExceptional Control FlowPart IIExceptional Control FlowPart IITopicsTopicsProcess HierarchyShellsSignalsexcept2.pptCS 105“Tour of the Black Holes of Computing!”– 2 –CS 105ECF Exists at All Levelsof a SystemECF Exists at All Levelsof a SystemExceptionsExceptionsHardware and operating system kernel softwareConcurrent processesConcurrent processesHardware timer and kernel softwareSignalsSignalsKernel softwareNon-local jumpsNon-local jumpsApplication codePrevious LectureThis Lecture– 4 –CS 105Programmer’s Modelof MultitaskingProgrammer’s Modelof MultitaskingBasic FunctionsBasic Functionsfork() spawns new processCalled once, returns twiceexit() terminates own processCalled once, never returnsPuts it into “zombie” statuswait() and waitpid() wait for and reap terminated childrenexecl() and execve() run a new program in an existing processCalled once, (normally) never returnsProgramming ChallengeProgramming ChallengeUnderstanding the nonstandard semantics of the functionsAvoiding improper use of system resourcesE.g. “Fork bombs” can disable a system.– 10 –CS 105Shell ProgramsShell ProgramsA A shellshell is an application program that runs programs on is an application program that runs programs on behalf of the user.behalf of the user.sh – Original Unix Bourne Shellcsh – BSD Unix C Shell, tcsh – Enhanced C Shell bash –Bourne-Again Shell int main() { char cmdline[MAXLINE]; while (1) {/* read */printf("> "); Fgets(cmdline, MAXLINE, stdin); if (feof(stdin)) exit(0);/* evaluate */eval(cmdline); } }Execution is a sequence of Execution is a sequence of read/evaluate stepsread/evaluate steps– 11 –CS 105Simple Shell eval FunctionSimple Shell eval Functionvoid eval(char *cmdline) { char *argv[MAXARGS]; /* argv for execve() */ int bg; /* should the job run in bg or fg? */ pid_t pid; /* process id */ bg = parseline(cmdline, argv); if (!builtin_command(argv)) { if ((pid = Fork()) == 0) { /* child runs user job */ execve(argv[0], argv, environ); printf("%s: Command not found.\n", argv[0]); exit(0);}if (!bg) { /* parent waits for fg job to terminate */ int status; if (waitpid(pid, &status, 0) < 0)unix_error("waitfg: waitpid error");}else /* otherwise, don’t wait for bg job */ printf("%d %s", pid, cmdline); }}– 12 –CS 105Problem with Simple Shell ExampleProblem with Simple Shell ExampleShell correctly waits for and reaps foreground jobs.Shell correctly waits for and reaps foreground jobs.But what about background jobs?But what about background jobs?Will become zombies when they terminate.Will never be reaped because shell (typically) will not terminate.Creates a memory leak that will eventually crash the kernel when it runs out of memory.Solution: Reaping background jobs requires a Solution: Reaping background jobs requires a mechanism called a mechanism called a signalsignal..– 13 –CS 105SignalsSignalsA A signalsignal is a small message that notifies a process that is a small message that notifies a process that an event of some type has occurred in the system.an event of some type has occurred in the system.Kernel abstraction for exceptions and interrupts.Sent from the kernel (sometimes at the request of another process) to a process.Different signals are identified by small integer ID’sThe only information in a signal is its ID and the fact that it arrived.IDIDNameNameDefault ActionDefault ActionCorresponding EventCorresponding Event22SIGINTSIGINTTerminateTerminateInterrupt from keyboard (Interrupt from keyboard (ctl-cctl-c))99SIGKILLSIGKILLTerminateTerminateKill program (cannot override or ignore)Kill program (cannot override or ignore)1111SIGSEGVSIGSEGVTerminate & DumpTerminate & DumpSegmentation violationSegmentation violation1414SIGALRMSIGALRMTerminateTerminateTimer signalTimer signal1717SIGCHLDSIGCHLDIgnoreIgnoreChild stopped or terminatedChild stopped or terminated– 14 –CS 105Signal Concepts Signal Concepts Sending a signalSending a signalKernel sends (delivers) a signal to a destination process by updating some state in the context of the destination process.Kernel sends a signal for one of the following reasons:Kernel has detected a system event such as divide-by-zero (SIGFPE) or the termination of a child process (SIGCHLD)Another process has invoked the kill system call to explicitly request that the kernel send a signal to the destination process.– 15 –CS 105Signal Concepts (cont.)Signal Concepts (cont.)Receiving a signalReceiving a signalA destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal.Five possible ways to react:Ignore the signal (do nothing)Terminate the process.Stop the process from runningContinue a stopped process (let it run again)Catch the signal by executing a user-level function called a signal handler.»Akin to a hardware exception handler being called in response to an asynchronous interrupt.– 16 –CS 105Signal Concepts (cont.)Signal Concepts (cont.)A signal is A signal is pendingpending if it has been sent but not yet if it has been sent but not yet received.received.There can be at most one pending signal of any particular type.Important: Signals are not queuedIf a process has a pending signal of type k, then subsequent signals of type k that are sent to that process are discarded.A process can A process can blockblock the receipt of certain signals. the receipt of certain signals.Blocked signals can be delivered, but will not be received until the signal is unblocked.A pending signal is received at most once.A pending signal is received at most once.– 17 –CS 105Signal ConceptsSignal ConceptsKernel maintains Kernel maintains pendingpending and and blockedblocked bit vectors in


View Full Document

Harvey Mudd CS 105 - Exceptional Control Flow Part II

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Download Exceptional Control Flow Part II
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 Exceptional Control Flow Part II 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 Exceptional Control Flow Part II 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?