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 IITopicsTopicsProcess HierarchyShellsSignalsexcept2.pptCS 105“Tour of the Black Holes of Computing!”– 2 –CS 105ECF Exists at All Levelsof a SystemECF Exists at All Levelsof a SystemExceptionsExceptionsHardware and operating system kernel softwareConcurrent processesConcurrent processesHardware timer and kernel softwareSignalsSignalsKernel softwareNon-local jumpsNon-local jumpsApplication codePrevious LectureThis Lecture– 4 –CS 105Programmer’s Modelof MultitaskingProgrammer’s Modelof MultitaskingBasic FunctionsBasic Functionsfork() spawns new processCalled once, returns twiceexit() terminates own processCalled once, never returnsPuts it into “zombie” statuswait() and waitpid() wait for and reap terminated childrenexecl() and execve() run a new program in an existing processCalled once, (normally) never returnsProgramming ChallengeProgramming ChallengeUnderstanding the nonstandard semantics of the functionsAvoiding improper use of system resourcesE.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 Shellcsh – 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’sThe 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 signalKernel 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 signalA 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 runningContinue 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 queuedIf 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