15 213 The course that gives CMU its Zip L5 Writing Your Own Unix Shell October 16 2006 Topics L5 Shell Lab Processes Signals Reminders Shell Lab Due Oct 25 2006 wed Section A Donnie Kim recitation6 ppt some slides courtesy of Kun Gao S05 and Minglong Shao F04 L5 Tiny Shell tsh Things to learn from this lab Process Control Ch 8 Process ID PID and Process Group ID Parent and Child process Loading and running program fork execve waitpid Signals Ch 8 Sending and receiving signals Pending signal Blocking unblocking signal avoiding race hazards I O redirection Ch 11 dup2 2 15 213 F 06 Related Background Review Preview 3 15 213 F 06 Process Control Process ID Process Group ID and Parent Process ID Each process has its own unique process ID pid t getpid void returns my pid Every process belong to exactly one process group pid t getpgrp void returns my prg id Process creates process parent child pid t getppid void returns my parent s pid dhjkim bluefish tshlab handout ps jf UID PID PPID PGID SID C STIME dhjkim 5469 5465 5469 5469 0 00 17 dhjkim 6284 5469 6284 5469 99 00 54 test dhjkim 10139 5469 10139 5469 0 03 53 jf 4 TTY pts 7 pts 7 TIME CMD 00 00 00 tcsh 02 58 42 pts 7 00 00 00 ps 15 213 F 06 fork Creating New Processes int fork void creates a new process child process that is identical to the calling process parent process returns 0 to the child process returns child s pid to the parent process if fork 0 printf hello from child n else printf hello from parent n Fork is interesting and often confusing because it is called once but returns twice Any Scheduling order is Possible 5 First parent then child or first child then parent can be executed depending on how OS scheduler decides 15 213 F 06 exec Loading and Running Programs int execve char fname char argv char envp New Program fname overwrites its state and takes over the process PID main if fork 0 execve usr bin ls NULL NULL wait NULL exit 6 15 213 F 06 waitpid Waiting for a Specific Process waitpid pid status options Can wait for specific process and reap terminated child process Various options 0 wait for process with PID pid 1 wait for any process pid 1 wait for any process from group abs pid pid By default waitpid blocks until at least one zombie process becomes available options WNOHANG return immediately if no zombies available WUNTRACED also return if some process has been stopped WNOHANG WUNTRACED combination is very useful in the shell lab it detects all the necessary events and doesn t block if no events 7 15 213 F 06 Signals How to send signals To a single process int kill pid t pid int sig To every process in group abs gid int kill pid t gid int sig gid 0 pid t getpid void returns my pid How to receive signals Signal handler handler t signal int signum handler t handler How to block and unblock signals Explicitly Blocking Signals int sigprocmask int how sigset t oldset 8 15 213 F 06 Signals How it actually works Process 1 Process 2 kill pid SIGINT 1 blocked pending OS signal manager divide by zero SIGFPE ctrl c SIGINT child process exit SIGCHLD 9 other events OS Kernel 15 213 F 06 Signals How it actually works Process 2 first checks pending blocked vector when it gets scheduled Process 2 0 1 blocked pending OS signal manager OS Kernel 10 15 213 F 06 L5 Shell Lab 11 15 213 F 06 Your task eval Main routine that parses and interprets the command line 300 lines including helper functions sigchld handler Catches SIGCHILD signals 15 lines sigint handler Catches SIGINT ctrl c signals 15 lines sigint handler Catches SIGSTP ctrl z signals 15 lines 12 15 213 F 06 Overview pid 10 pgid 10 eval Background job 1 Forepid 20 pgid 20 ground job Child pid 21 pgid 20 Child pid 22 pgid 20 Foreground process group 20 13 Shell pid 32 pgid 32 Background process group 32 Background job 2 pid 40 pgid 40 Backgroud process group 40 Each job should have a unique process group id int setpgid pid t pid pid t pgid setpgid 0 0 15 213 F 06 Overview pid 5 pgid 5 eval pid 10 pgid 10 rd a rw Fo pid 21 pgid 20 14 Child pid 22 pgid 20 Foreground process group 20 Foreground job receives SIGINT SIGTSTP when you type ctrl c ctrl z tsh Background job 1 pid 20 Forepgid 20 ground job Child als n sig UNIX shell pid 32 pgid 32 Background process group 32 Background job 2 pid 40 pgid 40 Backgroud process group 40 int kill pid t pid int sig pid 0 send sig to process with PID pid pid 0 send sig to all processes in my group pid 1 send sig to all processes with PID 1 pid 1 send sig to group abs pid 15 213 F 06 Reaping Child Process When fg or bg job is finished child process terminated shell parent proces has to reap the child otherwise Foreground job We can wait Background job Can we wait Where should waitpid go 15 15 213 F 06 Race Hazards sigchld handler waitpid deletejob pid eval pid fork if pid 0 child execve parent signal handler may run BEFORE addjob addjob 16 15 213 F 06 Race Hazards Solution eval sigprocmask SIG BLOCK pid fork if pid 0 child sigprocmask SIG UNBLOCK execve parent signal handler might run BEFORE addjob addjob sigprocmask SIG UNBLOCK 17 15 213 F 06 Pop Quiz include unistd h include stdio h int cnt 0 int main void if fork 0 cnt in child fork cnt cnt printf d cnt return 0 18 15 213 F 06
View Full Document