213 Recitation Jiri Simsa jsimsa cs cmu edu Outline Tiny shell hints Executing programs Understanding process tree Reap all child processes Avoid race hazard I O redirection Execute program int execve const char fname char const argv char const envp Examples execve bin ls NULL NULL execve mytest argv envp Process tree for a shell pid 10 pgid 10 Shell pid 20 Forepgid 20 ground job Child pid 21 pgid 20 Child pid 22 pgid 20 Foreground process group 20 Background job 1 pid 32 pgid 32 Background process group 32 Background job 2 pid 40 pgid 40 Backgroud process group 40 Each job has a unique process group id int setpgid pid t pid pid t pgid setpgid 0 0 Process tree for a shell pid 10 pgid 10 tsh Foreground job receives SIGINT SIGTSTP when you type ctrl c ctrl z ls ard pid 20 Fore Forw pgid 20 ground job Child Child pid 21 pid 22 pgid 20 pgid 20 Foreground process group 20 a ign s Background job 1 Backpid 32 pgid 32 ground job 2 pid 40 pgid 40 Background Backgroud process group 32 process group 40 int kill pid t pid int sig pid 0 send sig to specified process pid 0 send sig to all processes in same group pid 1 send sig to group pid Reaping child process waitpid pid t pid int status int options pid wait for child process with pid process id or group id 1 wait for any child process status tell why child terminated options WNOHANG return immediately if no children zombied WUNTRACED report status of terminated or stopped children In Lab 4 use waitpid 1 status WNOHANG WUNTRACED In sigchld handler while c pid waitpid 0 Reaping child process int status waitpid pid status WNOHANG WUNTRACED What to check in sigchld handler WIFEXITED status child exited normally the child finished and quit normally on its own WEXITSTATUS status returns code when child exits WIFSIGNALED status child exited because a signal is not caught SIGINT or typing CTRL C WTERMSIG status gives the terminating signal number WIFSTOPPED status child is stopped by the receipt of a signal SIGSTOP or typing CTRL Z WSTOPSIG status gives the stop signal number Reaping child process Where to put waitpid eval vs sigchld handler In sigchld handler for both eval should wait for a fg job Busy wait for a fg job In eval if fork 0 parent addjob while fg process still alive do nothing Sleep In eval if fork 0 parent addjob while fg process still alive sleep 1 Race hazard A data structure is shared by two pieces of code that can run concurrently Different behaviors of program depending upon how the schedule interleaves the execution of code An example of race hazard sigchld handler while pid waitpid 0 deletejob pid eval pid fork if pid 0 child execve parent signal handler may run BEFORE addjob addjob time Shell Signal Handler Child fork addjob execve exit sigchld handler deletejob time Shell Signal Handler Child fork execve exit sigchld handler deletejob addjob Job added to job list after the signal handler tried to delete it Solution blocking signals sigchld handler pid waitpid deletejob pid 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 I O redirection Do it before call execve in child process eval pid fork if pid 0 child Redirect I O if redirect input dup2 redirect STDIN if redirect output dup2 redirect STDOUT execve addjob dup2 int oldfd int newfd Covered in Chapter 11 oldfd old file descriptor newfd new file descriptor Dup2 makes newfd be a copy of the oldfd Some examples Get input from in fd instead of standard input dup2 in fd STDIN FILENO Print output to out fd instead of standard output dup2 out fd STDOUT FILENO Reminders Some important system calls fork execve waitpid sigprocmask setpgid kill Check man pages for details about system calls man 2 kill Check return values of all system calls STEP by STEP Test your shell by typing commands first Each trace worth the same work on easy ones first Start tomorrow
View Full Document