DOC PREVIEW
Harvey Mudd CS 105 - Exceptional Control Flow Part II

This preview shows page 1-2-3-27-28-29 out of 29 pages.

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

Unformatted text preview:

Exceptional Control Flow Part IIECF 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 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 Levels of a SystemECF Exists at All Levels of 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– 3 –CS 105The World of MultitaskingThe World of MultitaskingSystem Runs Many Processes ConcurrentlySystem Runs Many Processes ConcurrentlyProcess: executing programState consists of memory image + register values + program counterContinually switches from one process to anotherSuspend process when it needs I/O resource or when timer event occursResume process when I/O available or given scheduling priorityAppears to user(s) as if all processes executing simultaneouslyEven though most systems can only execute one process at a timeExcept possibly with lower performance than if running alone– 4 –CS 105Programmer’s Model of MultitaskingProgrammer’s Model of 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.– 5 –CS 105Unix Process HierarchyUnix Process HierarchyLogin shellChildChildChildGrandchildGrandchild[0]Daemone.g. httpdinit [1]– 6 –CS 105Unix 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 (kept in ROM).2. Bootstrap program loads the boot block (disk block 0).3. Boot block loads intermediate boot program (e.g., grub).3. Intermediate booter loads kernel binary (e.g., /boot/vmlinux).4. Booter passes control to kernel.5. Kernel handcrafts the data structures for process 0.Process 0 forks child process 1– 7 –CS 105Unix 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 –CS 105Unix Startup: Step 3Unix Startup: Step 3init [1][0]The getty process execs a login programlogin– 9 –CS 105Unix 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 –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


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?