Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.

Similar presentations


Presentation on theme: "CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process."— Presentation transcript:

1 CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process Zombie process Lecture 141CS 311 - Operating Systems 1

2 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. Lecture 142CS 311 - Operating Systems 1

3 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 Lecture 143CS 311 - Operating Systems 1

4 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 Lecture 144CS 311 - Operating Systems 1

5 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) Lecture 145CS 311 - Operating Systems 1

6 Important process states in UNIX Runnable Waiting for I/O, timer alarm, or signal - also known as "blocked" Waiting for CPU On the CPU Zombie Exited, waiting for parent to clean it up Running Waiting Lecture 146CS 311 - Operating Systems 1

7 How shell runs a utility Parent PID 34 Running shell Parent PID 34 Running shell Waiting for child Child PID 35 running shell Duplicate: fork() Child PID 35 running utility Differentiate: exec() Child PID 35 terminates Terminate: exit() Parent PID 34 running shell awakens Wait for child: wait() Signal: SIGCHLD Lecture 147CS 311 - Operating Systems 1

8 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 Lecture 148CS 311 - Operating Systems 1

9 Fork() forms a family tree fork() Process - pid 3456 Process - pid 4578Process - pid 4583 fork() Process - pid 4800Process - pid 4807 fork() Lecture 149CS 311 - Operating Systems 1

10 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. Lecture 1410CS 311 - Operating Systems 1

11 Orphan process If a parent dies before its child process, the child is automatically adopted by the original init process. init Parent dies first Child survives the parent adopt child Lecture 1411CS 311 - Operating Systems 1

12 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. Lecture 1412CS 311 - Operating Systems 1

13 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. Lecture 1413CS 311 - Operating Systems 1

14 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. Lecture 1414CS 311 - Operating Systems 1

15 waitpid() Prototype: pid_t waitpid(pid_t pid, int *status, int options) — 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) Lecture 1415CS 311 - Operating Systems 1

16 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. Lecture 14CS 311 - Operating Systems 116

17 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. Lecture 14CS 311 - Operating Systems 117


Download ppt "CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process."

Similar presentations


Ads by Google