Exceptional Control Flow Part II Oct. 22, 2002ECF Exists at All Levels of a SystemThe World of MultitaskingProgrammer’s Model of MultitaskingUnix Process HierarchyUnix Startup: Step 1Unix Startup: Step 2Unix Startup: Step 3Unix Startup: Step 4Shell ProgramsSimple Shell eval FunctionProblem with Simple Shell ExampleSignalsSignal ConceptsSignal Concepts (cont)Slide 16Signal ConceptsProcess GroupsSending Signals with kill ProgramSending Signals from the KeyboardExample of ctrl-c and ctrl-zSending Signals with kill FunctionReceiving SignalsDefault ActionsInstalling Signal HandlersSignal Handling ExampleSignal Handler FunkinessLiving With Nonqueuing SignalsA Program That Reacts to Externally Generated Events (ctrl-c)A Program That Reacts to Internally Generated EventsNonlocal Jumps: setjmp/longjmpsetjmp/longjmp (cont)setjmp/longjmp ExamplePutting It All Together: A Program That Restarts Itself When ctrl-c’dLimitations of Nonlocal JumpsLimitations of Long Jumps (cont.)SummaryExceptional Control FlowPart IIOct. 22, 2002Exceptional Control FlowPart IIOct. 22, 2002TopicsTopicsProcess HierarchyShellsSignalsNonlocal jumpsclass17.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, F’02ECF Exists at All Levels of a SystemECF Exists at All Levels of a SystemExceptionsExceptionsHardware and operating system kernel softwareConcurrent processesConcurrent processesHardware timer and kernel softwareSignalsSignalsKernel softwareNon-local jumpsNon-local jumpsApplication codePrevious LectureThis Lecture– 3 –15-213, F’02The World of MultitaskingThe World of MultitaskingSystem Runs Many Processes ConcurrentlySystem Runs Many Processes ConcurrentlyProcess: executing programState consists of memory image + register values + program counterContinually switches from one process to anotherSuspend process when it needs I/O resource or timer event occursResume process when I/O available or given scheduling priorityAppears to user(s) as if all processes executing simultaneouslyEven though most systems can only execute one process at a timeExcept possibly with lower performance than if running alone– 4 –15-213, F’02Programmer’s Model of MultitaskingProgrammer’s Model of 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.– 5 –15-213, F’02Unix Process HierarchyUnix Process HierarchyLogin shellChildChildChildGrandchildGrandchild[0]Daemone.g. httpdinit [1]– 6 –15-213, F’02Unix Startup: Step 1Unix Startup: Step 1init [1][0]Process 0: handcrafted kernel processChild process 1 execs /sbin/init1. Pushing reset button loads the PC with the address of a small bootstrap program.2. Bootstrap program loads the boot block (disk block 0).3. Boot block program loads kernel binary (e.g., /boot/vmlinux)4. Boot block program passes control to kernel.5. Kernel handcrafts the data structures for process 0.Process 0 forks child process 1– 7 –15-213, F’02Unix Startup: Step 2Unix Startup: Step 2init [1][0]gettyDaemonse.g. ftpd, httpd/etc/inittabinit forks and execs daemons per /etc/inittab, and forks and execs a getty program for the console– 8 –15-213, F’02Unix Startup: Step 3Unix Startup: Step 3init [1][0]The getty process execs a login programlogin– 9 –15-213, F’02Unix Startup: Step 4Unix Startup: Step 4init [1][0]login reads login and passwd.if OK, it execs a shell.if not OK, it execs another gettytcsh– 10 –15-213, F’02Shell 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 –15-213, F’02Simple 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 */ if (execve(argv[0], argv, environ) < 0) {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 –15-213, F’02Problem 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 –15-213, F’02SignalsSignalsA 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
View Full Document