100 likes | 239 Views
Chapter 8 Process Control. Chien -Chung Shen CIS, UD cshen@cis.udel.edu. Introduction. Process creation, execution, and termination IDs of a process. Process Identifiers. Every process has a unique process ID
E N D
Chapter 8Process Control Chien-Chung Shen CIS, UD cshen@cis.udel.edu
Introduction • Process creation, execution, and termination • IDs of a process
Process Identifiers • Every process has a unique process ID • often used as a piece of other identifiers to guarantee uniqueness, e.g., unique filenames • Special processes • ID 0: scheduler ([z]sched, swapper, etc.) • ID 1: init (invoked by the kernel at the end of the bootstrap procedure) • ID 2: page demon (for virtual memory)
fork Function • Fig. 8.1 (proc/fork1.c) • write()is notbuffered • Standard I/O library is buffered • standard output is line buffered if it’s connected to a terminal device (program runs interactively) • otherwise, it’s fully buffered
exit Function • Normal termination • Return from main() • Call exit() • Call _exit() or _Exit() • Return of the last thread from its start routine • Call pthread_exit()from the last thread • Abnormal termination • Call abort() – generate SIGABRT to itself • Receive a signal (generated by the kernel or some other process) • Response of the last thread to a cancellation request
exit Function • Normal termination - exit status in exit() [converted into termination status by kernel when _exit() called] • Abnormal termination – kernel generates a termination status indicating the reason • Parent process obtain termination status via wait() or waitpid()
Zombie (defunct) Process • What happen parent terminates before child? • initinherits child and becomes its parent • When child terminates before parent? • If child completely disappeared, parent wouldn’t be able to fetch its termination status when and if parent was finally ready to check if child had terminated • Kernel keeps a small amount of information (process ID, termination status, and amount of CPU time used) in the process tablefor every terminating process, so the information is available when parent of the terminating process calls wait()or waitpid() • Process that has terminated, but whose parent has not yet waited for it, is called a zombie
wait and waitpid Functions • When process terminates, either normally or abnormally, kernel notifies the parent by sending it SIGCHLD signal (to be ignored or handled) • Process that calls wait or waitpidcan • Block, if all of its children are still running • Return immediately with the termination status of a child, if a child has terminated and is waiting for its termination status to be fetched • Return immediately with an error, if it doesn’t have any child processes • Returns process ID of terminated process
wait and waitpid Functions • With multiple children, wait returns on termination of any process • waitpid waits for a specific process pid_twaitpid(pid_tpid, int *statloc, int options); • pid == -1: wait for any child • Pid > 0: wait for child of pid