15213 Recitation Section CProcess ConceptProcess IDs & Process GroupsProcess Tree for ShellSignalsImportant Signals (Fig 8.23)Signals: sendingSignals: receivingReceiving a SignalReaping Child ProcessSlide 11Busy WaitPauseSleepwaitpid ()Status in WaitpidMan pageRace Hazardeval & sigchld_handler Race HazardSlide 20Slide 21Blocking SignalsSummary15213 Recitation Section C• Process• Signals• Reaping Child Processes• Race HazardShimin ChenOct. 28, 2002Outline215213 Recitation C Shimin ChenProcess 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!315213 Recitation C Shimin ChenProcess 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() → −pid415213 Recitation C Shimin ChenProcess Tree for ShellFore-groundjobBack-groundjob #1Back-groundjob #2ShellChild Childpid=10pgid=10Foregroundprocess group 20Backgroundprocess group 32Backgroudprocess group 40pid=20pgid=20pid=32pgid=32pid=40pgid=40pid=21pgid=20pid=22pgid=20515213 Recitation C Shimin ChenSignals•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?–No615213 Recitation C Shimin ChenImportant 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 terminated715213 Recitation C Shimin ChenSignals: sendingOS Kernelblockedpending1Process 1Process 2other eventsOS procedurekill(pid, SIGINT)• divide by zero: SIGFPE• ctrl-c: SIGINT• child process exit: SIGCHLD815213 Recitation C Shimin ChenSignals: receivingOS Kernelblockedpending1Process 2OS procedure0Check when schedule the process to run915213 Recitation C Shimin ChenReceiving 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);1015213 Recitation C Shimin ChenReaping 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 background1115213 Recitation C Shimin ChenReaping Child Process•Two waits–sigchld_handler–eval: for foreground processes•One wait–sigchld_handler–But what about foreground processes?1215213 Recitation C Shimin ChenBusy Waitif(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ /* do nothing */ }}1315213 Recitation C Shimin ChenPauseif(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ pause(); }}If signal handled before call to pause, then pause will not return when foreground process sends SIGCHLD1415213 Recitation C Shimin ChenSleepif(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ sleep(1); }}1515213 Recitation C Shimin Chenwaitpid ()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)1615213 Recitation C Shimin ChenStatus in Waitpid•int status;waitpid(pid, &status, NULL)•Macros to evaluate status:–WIFEXITED(status): child exited normally–WEXITSTATUS(status): return code when child exits–WIFSIGNALED(status): child exited because of a signal not caught–WTERMSIG(status): gives the terminating signal number–WIFSTOPPED(status): child is currently stopped–WSTOPSIG(status): gives the stop signal number1715213 Recitation C Shimin ChenMan page•Check man page for details of a system call:–man waitpid1815213 Recitation C Shimin ChenRace 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.1915213 Recitation C Shimin Cheneval & sigchld_handler Race Hazardsigchld_handler() { pid = waitpid(…); deletejob(pid);}eval() { pid = fork(); if(pid == 0) { /* child */ execve(…); } /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…);}2015213 Recitation C Shimin ChenAn Okay ScheduleShellSignal Handler Childfork()addjob()execve()exit()sigchld_handler()deletejobs()time2115213 Recitation C Shimin ChenA Problematic ScheduleShellSignal Handler Childfork()execve()exit()sigchld_handler()deletejobs()timeaddjob()Job added to job list after the signal handler tried to delete it!2215213 Recitation C Shimin ChenBlocking Signalssigchld_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, …)}More details 8.5.6 (page 633)2315213 Recitation C Shimin ChenSummary•Process•Signals•Reaping Child Processes•Race Hazard•Check man page to understand the system calls better–man
View Full Document