170 likes | 397 Views
CS 311 – Lecture 14 Outline. Process management system calls Introduction System calls fork() getpid() getppid() wait() exit() Orphan process Zombie process. Introduction to process management in UNIX. What is a process? process is a unique instance of a running or runnable program
E N D
CS 311 – Lecture 14 Outline • Process management system calls • Introduction • System calls • fork() • getpid() • getppid() • wait() • exit() • Orphan process • Zombie process CS 311 - Operating Systems 1
Introduction to process management in UNIX • What is a process? • process is a unique instance of a running or runnable program • Every process in Unix has these attributes • code: which denotes the task to be done • data: parameters to the code or other info about the process • stack: stack traces are useful to keep log of events or subroutine calls • a unique Process ID number (PID) • When UNIX is started there's only one visible program called “init” with PID 1. CS 311 - Operating Systems 1
A UNIX process consists of... • A unique identify (process id) • A virtual address space • Program code, program data in memory • user/group identity, umask • An execution environment, with attributes such as: • Environment variables, current working directory • List of open files • A description of actions to take on receiving signals • Resource limits, scheduling priority • and more… - See the exec() man page CS 311 - Operating Systems 1
Programs vs processes • A program is the executable code • A process is a running instance of a program • More than one process can be concurrently executing the same program code • They will have separate execution resources • Different virtual address spaces, process id, etc. • On a modern OS - some execution resources will be shared if two processes are executing the same program code CS 311 - Operating Systems 1
Process management structure • “init” is the ancestor of all the processes running in UNIX/Linux. • Creation of new process happens in two phases • Phase 1: Duplication of parent process into two processes. • Phase 2: Replace the code of the child with that of another executable file. • Parent and child process are virtually identical in Phase 1. All the code, data, stack info of the parent are copied to the child process. • When a child process terminates, it notifies the parent so that it can take appropriate actions (Signal: SIGCHLD) CS 311 - Operating Systems 1
Important process states in UNIX Waiting for I/O, timer alarm, or signal - also known as "blocked" On the CPU Running Waiting Zombie Runnable Waiting for CPU Exited, waiting for parent to clean it up CS 311 - Operating Systems 1
How shell runs a utility Parent PID 34 Running shell Duplicate: fork() Parent PID 34 Running shell Waiting for child Child PID 35 running shell Differentiate: exec() Wait for child: wait() Child PID 35 running utility Terminate: exit() Parent PID 34 running shell awakens Child PID 35 terminates Signal: SIGCHLD CS 311 - Operating Systems 1
Creating a process • fork() system call helps you to create a process manually. • Prototype: int fork() • fork() returns the PID of the child process that is created from the parent. • Within the child process the fork() returns a 0. • If something went wrong, fork() returns -1 to the parent process and sets the global variable errno. If -1 is returned, then no child process was created CS 311 - Operating Systems 1
Fork() forms a family tree Process - pid 3456 fork() fork() Process - pid 4578 Process - pid 4583 fork() fork() Process - pid 4800 Process - pid 4807 CS 311 - Operating Systems 1
getpid() and getppid() • getpid() returns process id of a process. • getppid() returns parent's process id. • They always succeed. • init() 's getppid() will return 1. CS 311 - Operating Systems 1
Orphan process • If a parent dies before its child process, the child is automatically adopted by the original init process. init Parent dies first adopt child Child survives the parent CS 311 - Operating Systems 1
Terminating a process • exit() is used to terminate a process at any time. • Prototype:void exit(int status) • closes all of a process's file descriptors, deallocates its code, data, and stack and then terminates the process. • When a child process terminates it sends SIGCHLD signal and waits for its status code to be accepted by the parent. CS 311 - Operating Systems 1
Zombie process • A process that terminates cannot leave the system until its status code is accepted by its parent. • If a parent is alive but never accepts the return code, the process will remain a zombie. • A zombie process doesn't have any code, data or stack so it doesn't use up many system resources. CS 311 - Operating Systems 1
Waiting for a process • A parent waits for its child to terminate and then accepts its terminating code by executing wait() • Prototype:pid_t wait(int *status) • A successful call returns the pid of the child that terminated and places a status code into status • If a process executes a wait() and has no children, it returns a -1. CS 311 - Operating Systems 1
waitpid() • Prototype:pid_twaitpid(pid_tpid, int *status, intoptions) • waits for a particular child process pid. • options < -1 - wait for any child process whose process group ID is equal to the absolute value of pid. • options = -1 meaning wait for any child process. • options = 0 meaning wait for any child process whose process group ID is equal to that of the calling process. • options > 0 meaning wait for the child whose process ID is equal to the value of pid. • waitpid(-1, &status, 0) is same as wait(&status) CS 311 - Operating Systems 1
Wait macros in sys/wait.h • WIFEXITED(stat_val) Evaluates to a non-zero value if status was returned for a child process that terminated normally. • WEXITSTATUS(stat_val) If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main(). • WIFSIGNALED(stat_val) Evaluates to a non-zero value if status was returned for a child process that terminated due to the receipt of a signal that was not caught. • WTERMSIG(stat_val) If the value of WIFSIGNALED(stat_val) is non-zero, this macro evaluates to the number of the signal that caused the termination of the child process. CS 311 - Operating Systems 1
More macros in sys/wait.h • WIFSTOPPED(stat_val) Evaluates to a non-zero value if status was returned for a child process that is currently stopped. • WSTOPSIG(stat_val) If the value of WIFSTOPPED(stat_val) is non-zero, this macro evaluates to the number of the signal that caused the child process to stop. • WIFCONTINUED(stat_val) Evaluates to a non-zero value if status was returned for a child process that has continued from a job control stop. CS 311 - Operating Systems 1