15 213 The course that gives CMU its Zip Exceptional Control Flow Part II October 7 2008 Topics lecture 12 ppt Process Hierarchy Shells Signals Nonlocal jumps ECF Exists at All Levels of a System Exceptions Hardware and operating system kernel software Previous Lecture Concurrent processes Hardware timer and kernel software Signals Kernel software This Lecture Non local jumps 2 Application code 15 213 F 08 The World of Multitasking System Runs Many Processes Concurrently Process executing program State includes memory image register values program counter Regularly switches from one process to another Suspend process when it needs I O resource or 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 3 15 213 F 08 Programmer s Model of Multitasking Basic 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 new program in existing process Called once normally never returns Programming Challenge Understanding the nonstandard semantics of the functions Avoiding improper use of system resources E g Fork bombs can disable a system 4 15 213 F 08 wait Synchronizing with Children int wait int child status 5 suspends current process until one of its children terminates return value is the pid of the child process that terminated if child status NULL then the object it points to will be set to a status indicating why the child process terminated 15 213 F 08 wait Synchronizing with Children void 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 6 HC Bye HP CT Bye 15 213 F 08 wait Example If multiple children completed will take in arbitrary order Can use macros WIFEXITED and WEXITSTATUS to get information about exit status void 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 7 15 213 F 08 waitpid Waiting for a Specific Process waitpid pid status options suspends current process until specific process terminates various 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 8 15 213 F 08 exec Loading and Running Programs 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 file By 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 argument Environment taken from char environ which points to an array of name value strings USER droh LOGNAME droh HOME afs cs cmu edu user droh 9 Returns 1 if error otherwise doesn t return Family of functions includes execv execve base function execvp execl execle and execlp 15 213 F 08 exec Loading and Running Programs main if fork 0 execl usr bin cp cp foo bar 0 wait NULL printf copy completed n exit 10 15 213 F 08 Shell Programs A shell is an application program that runs programs on 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 MAXLINE stdin if feof stdin exit 0 11 Execution is a sequence of read evaluate steps evaluate eval cmdline 15 213 F 08 Simple Shell eval Function void 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 12 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 15 213 F 08 Background Job What is a background job Users generally run one command at a time Some programs run for a long time Type command read output type another command Example delete this file in two hours sleep 7200 rm tmp junk hours A shell stuck for 2 background job is a process we don t want to wait for sleep 7200 rm tmp junk 1 907 ready for next command 13 15 213 F 08 Problem with Simple Shell Example Shell correctly waits for and reaps foreground jobs But what about background jobs Will become zombies when they terminate Will never be reaped because shell typically will not terminate Will create a memory leak that could theoretically run the kernel out of memory In modern Unix once you exceed your process quota your shell can t run any new commands for you fork returns 1 limit maxproc csh syntax maxproc 3574 ulimit u bash syntax 3574 14 15 213 F 08 ECF to the Rescue Problem The By shell doesn t know when a background job will finish nature it could happen at any time The shell s regular control flow can t reap exited background processes in a timely fashion Regular control flow is wait until running job completes then reap it Solution Exceptional control flow The kernel will interrupt regular processing to alert us when a background process completes In 15 Unix the alert mechanism is called a signal 15 213 F 08 Signals A signal is a small message that notifies a process that an event of some type has occurred in the system ID akin to exceptions and interrupts sent from the kernel sometimes at the request of another process to a process signal type is identified by small integer ID s 1 30 only information in a signal is its ID and the fact that it arrived Name 2 SIGINT 9 SIGKILL Default Action Corresponding Event Terminate Interrupt e g ctl c from keyboard Terminate Kill program cannot override or ignore 11 SIGSEGV 14 SIGALRM Terminate Dump Segmentation violation Terminate Timer signal 17
View Full Document