15213 Recitation Section C Shimin Chen Oct 28 2002 Outline Process Signals Reaping Child Processes Race Hazard Process Concept An instance of running program Multiple processes run concurrently by time slicing What is time slicing Preemptive scheduler of OS it can stop a program at any point 15213 Recitation C 2 Shimin Chen Process IDs Process Groups A process has its own unique process ID pid t getpid A process belongs to exactly one process group pid t getpgrp A new process belongs to which process group Its parent s process group A process can make a process group for itself and its children pid t pid getpid setpgid 0 0 getpgrp pid 15213 Recitation C 3 Shimin Chen Process Tree for Shell pid 10 pgid 10 pid 20 pgid 20 Shell Background job 1 Foreground job Child Child pid 21 pgid 20 pid 22 pgid 20 pid 32 pgid 32 Background process group 32 Background job 2 pid 40 pgid 40 Backgroud process group 40 Foreground process group 20 15213 Recitation C 4 Shimin Chen Signals Section 8 5 in text Read at least twice really A signal tells our program that some event has occurred Can we use signals to count events No 15213 Recitation C 5 Shimin Chen Important Signals Fig 8 23 SIGINT Interrupt signal from terminal ctrl c SIGTSTP Stop signal from terminal ctrl z SIGCHLD A child process has stopped or terminated 15213 Recitation C 6 Shimin Chen Signals sending Process 1 Process 2 kill pid SIGINT 1 blocked pending OS procedure divide by zero SIGFPE ctrl c SIGINT child process exit SIGCHLD 15213 Recitation C other events OS Kernel 7 Shimin Chen Signals receiving Check when schedule the process to run Process 2 0 1 blocked pending OS procedure OS Kernel 15213 Recitation C 8 Shimin Chen Receiving a Signal Default action The process terminates and dumps core The process stops until restarted by a SIGCONT signal The process ignore the signal Can modify additional action Handle the signal void sigint handler int sig signal SIGINT sigint handler 15213 Recitation C 9 Shimin Chen Reaping Child Process Child process becomes zombie when terminates Still consume system resources Parent performs reaping on terminated child wait waitpid Straightforward for reaping a single child Tricky for Shell implementation multiple child processes both foreground and background 15213 Recitation C 10 Shimin Chen Reaping Child Process Two waits sigchld handler eval for foreground processes One wait sigchld handler But what about foreground processes 15213 Recitation C 11 Shimin Chen Busy Wait if fork 0 parent addjob while fg process still alive do nothing 15213 Recitation C 12 Shimin Chen Pause if fork 0 parent addjob while fg process still alive pause 15213 Recitation C 13 Shimin Chen Sleep if fork 0 parent addjob while fg process still alive sleep 1 15213 Recitation C 14 Shimin Chen waitpid pid t waitpid pid t pid int status int options pid wait until child process with pid has terminated 1 wait for any child process status tells why child terminated options WNOHANG return immediately if no children zombied returns 1 WUNTRACED report status of stopped children too wait status equivalent to waitpid 1 status 0 15213 Recitation C 15 Shimin Chen Status in Waitpid int status waitpid pid status NULL WIFEXITED status WEXITSTATUS status WIFSIGNALED status WTERMSIG status WIFSTOPPED status WSTOPSIG status 15213 Recitation C 16 Shimin Chen Man page Check man page for details of a system call man waitpid 15213 Recitation C 17 Shimin Chen 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 15213 Recitation C 18 Shimin Chen eval sigchld handler Race Hazard sigchld handler pid waitpid deletejob pid eval pid fork if pid 0 child execve parent signal handler might run BEFORE addjob addjob 15213 Recitation C 19 Shimin Chen An Okay Schedule fork addjob execve exit sigchld handler deletejobs 15213 Recitation C 20 Shimin Chen A Problematic Schedule fork execve exit sigchld handler deletejobs addjob 15213 Recitation C 21 Shimin Chen 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 15213 Recitation C 22 Shimin Chen Summary Process Signals Reaping Child Processes Race Hazard Check man page to understand the system calls better man waitpid 15213 Recitation C 23 Shimin Chen
View Full Document