Exceptional Control Flow Part II October 7, 2008ECF Exists at All Levels of a SystemThe World of MultitaskingProgrammer’s Model of Multitaskingwait: Synchronizing with ChildrenSlide 6wait() Examplewaitpid(): Waiting for a Specific Processexec: Loading and Running ProgramsSlide 10Shell ProgramsSimple Shell eval Function“Background Job”?Problem with Simple Shell ExampleECF to the Rescue!SignalsSignal ConceptsSignal Concepts (continued)Slide 19Signal ConceptsProcess GroupsSending Signals with kill ProgramSending Signals with kill FunctionReceiving SignalsDefault ActionsInstalling Signal HandlersSignal Handling ExampleSignals Handlers as Concurrent FlowsAnother View of Signal Handlers as Concurrent FlowsNonlocal Jumps: setjmp/longjmpsetjmp/longjmp (cont)setjmp/longjmp ExampleLimitations of Nonlocal JumpsLimitations of Long Jumps (cont.)Putting It All Together: A Program That Restarts Itself When ctrl-c’dSummarySending Signals from the KeyboardExample of ctrl-c and ctrl-zSignal Handler FunkinessLiving With Nonqueuing SignalsSignal Handler Funkiness (Cont.)A Program That Reacts to Externally Generated Events (ctrl-c)A Program That Reacts to Internally Generated EventsExceptional Control FlowPart IIOctober 7, 2008Exceptional Control FlowPart IIOctober 7, 2008TopicsTopicsProcess HierarchyShellsSignalsNonlocal jumpslecture-12.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, F’08ECF 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’08The World of MultitaskingThe World of MultitaskingSystem Runs Many Processes ConcurrentlySystem Runs Many Processes ConcurrentlyProcess: executing programState includes memory image + register values + program counterRegularly 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’08Programmer’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 new program in 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’08wait: Synchronizing with Childrenwait: Synchronizing with Childrenint wait(int *child_status)int wait(int *child_status)suspends current process until one of its children terminatesreturn value is the pid of the child process that terminatedif child_status != NULL, then the object it points to will be set to a status indicating why the child process terminated– 6 –15-213, F’08wait: Synchronizing with Childrenwait: Synchronizing with Childrenvoid fork9() { int child_status; if (fork() == 0) { printf("HC: hello from child\n"); } else { printf("HP: hello from parent\n"); wait(&child_status); printf("CT: child has terminated\n"); } printf("Bye\n"); exit();}HPHC ByeCT Bye– 7 –15-213, F’08wait() Examplewait() ExampleIf multiple children completed, will take in arbitrary orderCan use macros WIFEXITED and WEXITSTATUS to get information about exit statusvoid fork10(){ pid_t pid[N]; int i; int child_status; for (i = 0; i < N; i++)if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) {pid_t wpid = wait(&child_status);if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status));else printf("Child %d terminated abnormally\n", wpid); }}– 8 –15-213, F’08waitpid(): Waiting for a Specific Processwaitpid(): Waiting for a Specific Processwaitpid(pid, &status, options)suspends current process until specific process terminatesvarious options (that we won’t talk about)void fork11(){ pid_t pid[N]; int i; int child_status; for (i = 0; i < N; i++)if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) {pid_t wpid = waitpid(pid[i], &child_status, 0);if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status));else printf("Child %d terminated abnormally\n", wpid); }– 9 –15-213, F’08exec: Loading and Running Programsexec: Loading and Running Programsint execl(char *path, char *arg0, char *arg1, …, 0)int execl(char *path, char *arg0, char *arg1, …, 0)Loads and runs executable at path with args arg0, arg1, …path is the complete path of an executable object fileBy convention, arg0 is the name of the executable object file“Real” arguments to the program start with arg1, etc.List of args is terminated by a (char *)0 argumentEnvironment taken from char **environ, which points to an array of “name=value” strings:»USER=droh»LOGNAME=droh»HOME=/afs/cs.cmu.edu/user/drohReturns -1 if error, otherwise doesn’t return!Family of functions includes execv, execve (base function), execvp, execl, execle, and execlp– 10 –15-213, F’08exec: Loading and Running Programsexec: Loading and Running Programsmain() { if (fork() == 0) { execl("/usr/bin/cp", "cp", "foo", "bar", 0); } wait(NULL); printf("copy completed\n"); exit();}– 11 –15-213, F’08Shell 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 shell (Stephen Bourne, AT&T Bell Labs, 1977)csh – BSD Unix C shell (tcsh: csh enhanced at CMU and elsewhere) bash – “Bourne-Again” Shell int main() { char cmdline[MAXLINE]; while (1) {/* read */printf("> "); Fgets(cmdline,
View Full Document