process management - part1

124
Processes (Part I) Amir H. Payberah [email protected] Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 1 / 56

Upload: amir-payberah

Post on 16-Jul-2015

106 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Process Management - Part1

Processes (Part I)

Amir H. [email protected]

Amirkabir University of Technology(Tehran Polytechnic)

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 1 / 56

Page 2: Process Management - Part1

What Is A Process?

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 2 / 56

Page 3: Process Management - Part1

Process

An instance of a program running.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 3 / 56

Page 4: Process Management - Part1

Program vs. Process

I Program is a passive entity stored on disk (executable file).

I Process is an active entity.

I Program becomes process when executable file loaded into memory.

I One program can be several processes.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 4 / 56

Page 5: Process Management - Part1

Program vs. Process

I Program is a passive entity stored on disk (executable file).

I Process is an active entity.

I Program becomes process when executable file loaded into memory.

I One program can be several processes.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 4 / 56

Page 6: Process Management - Part1

Program vs. Process

I Program is a passive entity stored on disk (executable file).

I Process is an active entity.

I Program becomes process when executable file loaded into memory.

I One program can be several processes.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 4 / 56

Page 7: Process Management - Part1

Program vs. Process

I Program is a passive entity stored on disk (executable file).

I Process is an active entity.

I Program becomes process when executable file loaded into memory.

I One program can be several processes.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 4 / 56

Page 8: Process Management - Part1

Parts of a Process

I A process is more than the program code.

I Multiple parts of a process:

• The program code (text section).

• Current activity, e.g., program counter,processor registers.

• Data section containing global variables.

• Stack containing temporary data.

• Heap containing memory dynamicallyallocated during run time.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 5 / 56

Page 9: Process Management - Part1

Parts of a Process

I A process is more than the program code.

I Multiple parts of a process:

• The program code (text section).

• Current activity, e.g., program counter,processor registers.

• Data section containing global variables.

• Stack containing temporary data.

• Heap containing memory dynamicallyallocated during run time.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 5 / 56

Page 10: Process Management - Part1

Parts of a Process

I A process is more than the program code.

I Multiple parts of a process:

• The program code (text section).

• Current activity, e.g., program counter,processor registers.

• Data section containing global variables.

• Stack containing temporary data.

• Heap containing memory dynamicallyallocated during run time.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 5 / 56

Page 11: Process Management - Part1

Parts of a Process

I A process is more than the program code.

I Multiple parts of a process:

• The program code (text section).

• Current activity, e.g., program counter,processor registers.

• Data section containing global variables.

• Stack containing temporary data.

• Heap containing memory dynamicallyallocated during run time.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 5 / 56

Page 12: Process Management - Part1

Parts of a Process

I A process is more than the program code.

I Multiple parts of a process:

• The program code (text section).

• Current activity, e.g., program counter,processor registers.

• Data section containing global variables.

• Stack containing temporary data.

• Heap containing memory dynamicallyallocated during run time.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 5 / 56

Page 13: Process Management - Part1

Parts of a Process

I A process is more than the program code.

I Multiple parts of a process:

• The program code (text section).

• Current activity, e.g., program counter,processor registers.

• Data section containing global variables.

• Stack containing temporary data.

• Heap containing memory dynamicallyallocated during run time.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 5 / 56

Page 14: Process Management - Part1

Process Control Block (PCB)

I The information of each process.

I Process state: running, waiting, etcI Program counter: location of instruction to next executeI CPU registers: contents of all process-centric registersI CPU scheduling: priorities, scheduling queue pointersI Memory-management: memory allocated to the processI Accounting: CPU used, clock time elapsed since start, time limitsI I/O status: I/O devices allocated to process, list of open files

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 6 / 56

Page 15: Process Management - Part1

Process Control Block (PCB)

I The information of each process.

I Process state: running, waiting, etc

I Program counter: location of instruction to next executeI CPU registers: contents of all process-centric registersI CPU scheduling: priorities, scheduling queue pointersI Memory-management: memory allocated to the processI Accounting: CPU used, clock time elapsed since start, time limitsI I/O status: I/O devices allocated to process, list of open files

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 6 / 56

Page 16: Process Management - Part1

Process Control Block (PCB)

I The information of each process.

I Process state: running, waiting, etc

I Program counter: location of instruction to next execute

I CPU registers: contents of all process-centric registersI CPU scheduling: priorities, scheduling queue pointersI Memory-management: memory allocated to the processI Accounting: CPU used, clock time elapsed since start, time limitsI I/O status: I/O devices allocated to process, list of open files

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 6 / 56

Page 17: Process Management - Part1

Process Control Block (PCB)

I The information of each process.

I Process state: running, waiting, etcI Program counter: location of instruction to next execute

I CPU registers: contents of all process-centric registers

I CPU scheduling: priorities, scheduling queue pointersI Memory-management: memory allocated to the processI Accounting: CPU used, clock time elapsed since start, time limitsI I/O status: I/O devices allocated to process, list of open files

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 6 / 56

Page 18: Process Management - Part1

Process Control Block (PCB)

I The information of each process.

I Process state: running, waiting, etcI Program counter: location of instruction to next executeI CPU registers: contents of all process-centric registers

I CPU scheduling: priorities, scheduling queue pointers

I Memory-management: memory allocated to the processI Accounting: CPU used, clock time elapsed since start, time limitsI I/O status: I/O devices allocated to process, list of open files

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 6 / 56

Page 19: Process Management - Part1

Process Control Block (PCB)

I The information of each process.

I Process state: running, waiting, etcI Program counter: location of instruction to next executeI CPU registers: contents of all process-centric registersI CPU scheduling: priorities, scheduling queue pointers

I Memory-management: memory allocated to the process

I Accounting: CPU used, clock time elapsed since start, time limitsI I/O status: I/O devices allocated to process, list of open files

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 6 / 56

Page 20: Process Management - Part1

Process Control Block (PCB)

I The information of each process.

I Process state: running, waiting, etcI Program counter: location of instruction to next executeI CPU registers: contents of all process-centric registersI CPU scheduling: priorities, scheduling queue pointersI Memory-management: memory allocated to the process

I Accounting: CPU used, clock time elapsed since start, time limits

I I/O status: I/O devices allocated to process, list of open files

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 6 / 56

Page 21: Process Management - Part1

Process Control Block (PCB)

I The information of each process.

I Process state: running, waiting, etcI Program counter: location of instruction to next executeI CPU registers: contents of all process-centric registersI CPU scheduling: priorities, scheduling queue pointersI Memory-management: memory allocated to the processI Accounting: CPU used, clock time elapsed since start, time limits

I I/O status: I/O devices allocated to process, list of open files

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 6 / 56

Page 22: Process Management - Part1

Process States

I As a process executes, it changes state.

I new: The process is being created.

I ready: The process is waiting to be assigned to a processor.

I running: Instructions are being executed.

I waiting: The process is waiting for some event to occur.

I terminated: The process has finished execution.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 7 / 56

Page 23: Process Management - Part1

Process States

I As a process executes, it changes state.

I new: The process is being created.

I ready: The process is waiting to be assigned to a processor.

I running: Instructions are being executed.

I waiting: The process is waiting for some event to occur.

I terminated: The process has finished execution.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 7 / 56

Page 24: Process Management - Part1

Process States

I As a process executes, it changes state.

I new: The process is being created.

I ready: The process is waiting to be assigned to a processor.

I running: Instructions are being executed.

I waiting: The process is waiting for some event to occur.

I terminated: The process has finished execution.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 7 / 56

Page 25: Process Management - Part1

Process States

I As a process executes, it changes state.

I new: The process is being created.

I ready: The process is waiting to be assigned to a processor.

I running: Instructions are being executed.

I waiting: The process is waiting for some event to occur.

I terminated: The process has finished execution.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 7 / 56

Page 26: Process Management - Part1

Process States

I As a process executes, it changes state.

I new: The process is being created.

I ready: The process is waiting to be assigned to a processor.

I running: Instructions are being executed.

I waiting: The process is waiting for some event to occur.

I terminated: The process has finished execution.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 7 / 56

Page 27: Process Management - Part1

Process States

I As a process executes, it changes state.

I new: The process is being created.

I ready: The process is waiting to be assigned to a processor.

I running: Instructions are being executed.

I waiting: The process is waiting for some event to occur.

I terminated: The process has finished execution.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 7 / 56

Page 28: Process Management - Part1

Threads

I A process can have a single thread or multiple threads.

I Multi-thread process• Multiple program counters in a PCB: multiple locations can execute

at once.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 8 / 56

Page 29: Process Management - Part1

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 9 / 56

Page 30: Process Management - Part1

Process Data Structure in Linux Kernel (1/2)

I Represented by task struct in the Linux kernel.• in the <linux/sched.h>

struct task_struct {

volatile long state;

long counter;

struct task_struct *next_task, *prev_task;

int pid;

struct task_struct *p_pptr; // pointers to the parent

struct task_struct *p_cptr; // pointers to the youngest child

struct task_struct *p_ysptr // pointers to the younger sibling

struct task_struct *p_osptr; // pointers to the older sibling

struct wait_queue *wait_chldexit;

unsigned short uid, euid, suid, fsuid;

...

}

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 10 / 56

Page 31: Process Management - Part1

Process Data Structure in Linux Kernel (2/2)

I All active processes are represented using a doubly linked list oftask struct.

struct task_struct {

...

struct task_struct *next_task, *prev_task;

...

}

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 11 / 56

Page 32: Process Management - Part1

Process ID (1/2)

I Each process is assigned a unique identifier, the process ID (PID).

I The kernel allocates PIDs to processes in a strictly linear fashion.

I The maximum PID value of 32768.

cat /proc/sys/kernel/pid_max

I The first process that the kernel executes after booting the system,is init process, with the PID 1.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 12 / 56

Page 33: Process Management - Part1

Process ID (1/2)

I Each process is assigned a unique identifier, the process ID (PID).

I The kernel allocates PIDs to processes in a strictly linear fashion.

I The maximum PID value of 32768.

cat /proc/sys/kernel/pid_max

I The first process that the kernel executes after booting the system,is init process, with the PID 1.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 12 / 56

Page 34: Process Management - Part1

Process ID (1/2)

I Each process is assigned a unique identifier, the process ID (PID).

I The kernel allocates PIDs to processes in a strictly linear fashion.

I The maximum PID value of 32768.

cat /proc/sys/kernel/pid_max

I The first process that the kernel executes after booting the system,is init process, with the PID 1.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 12 / 56

Page 35: Process Management - Part1

Process ID (1/2)

I Each process is assigned a unique identifier, the process ID (PID).

I The kernel allocates PIDs to processes in a strictly linear fashion.

I The maximum PID value of 32768.

cat /proc/sys/kernel/pid_max

I The first process that the kernel executes after booting the system,is init process, with the PID 1.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 12 / 56

Page 36: Process Management - Part1

Process ID (2/2)

I The PID is represented by the pid t type.

I The getpid() system call returns the PID of the invoking process.

I Defined in <sys/types.h>

#include <sys/types.h>

#include <unistd.h>

pid_t getpid(void);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 13 / 56

Page 37: Process Management - Part1

Process ID (2/2)

I The PID is represented by the pid t type.

I The getpid() system call returns the PID of the invoking process.

I Defined in <sys/types.h>

#include <sys/types.h>

#include <unistd.h>

pid_t getpid(void);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 13 / 56

Page 38: Process Management - Part1

Process ID (2/2)

I The PID is represented by the pid t type.

I The getpid() system call returns the PID of the invoking process.

I Defined in <sys/types.h>

#include <sys/types.h>

#include <unistd.h>

pid_t getpid(void);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 13 / 56

Page 39: Process Management - Part1

Process Scheduling

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 14 / 56

Page 40: Process Management - Part1

Process Scheduling

I The multiprogramming objective: to maximize CPU utilization.

I The timesharing objective: to switch the CPU among processesso frequently that users can interact with each program while it isrunning.

I To meet these objectives: the process scheduler selects an availableprocess for program execution on the CPU.

• If there are more processes, the rest will have to wait until the CPUis free and can be rescheduled.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 15 / 56

Page 41: Process Management - Part1

Process Scheduling

I The multiprogramming objective: to maximize CPU utilization.

I The timesharing objective: to switch the CPU among processesso frequently that users can interact with each program while it isrunning.

I To meet these objectives: the process scheduler selects an availableprocess for program execution on the CPU.

• If there are more processes, the rest will have to wait until the CPUis free and can be rescheduled.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 15 / 56

Page 42: Process Management - Part1

Process Scheduling

I The multiprogramming objective: to maximize CPU utilization.

I The timesharing objective: to switch the CPU among processesso frequently that users can interact with each program while it isrunning.

I To meet these objectives: the process scheduler selects an availableprocess for program execution on the CPU.

• If there are more processes, the rest will have to wait until the CPUis free and can be rescheduled.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 15 / 56

Page 43: Process Management - Part1

Scheduling Queues (1/2)

I Job queue: set of all processes in the system.

I Ready queue: set of all processes residing in main memory, readyand waiting to execute.

• This queue is generally stored as a linked list.• Pointers to the first and final PCBs in the list.• Each PCB points to the next PCB in the ready queue.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 16 / 56

Page 44: Process Management - Part1

Scheduling Queues (1/2)

I Job queue: set of all processes in the system.

I Ready queue: set of all processes residing in main memory, readyand waiting to execute.

• This queue is generally stored as a linked list.• Pointers to the first and final PCBs in the list.• Each PCB points to the next PCB in the ready queue.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 16 / 56

Page 45: Process Management - Part1

Scheduling Queues (1/2)

I Job queue: set of all processes in the system.

I Ready queue: set of all processes residing in main memory, readyand waiting to execute.

• This queue is generally stored as a linked list.• Pointers to the first and final PCBs in the list.• Each PCB points to the next PCB in the ready queue.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 16 / 56

Page 46: Process Management - Part1

Scheduling Queues (2/2)

I Device queues: set of processes waiting for an I/O device.• Each device has its own device queue.

I Processes migrate among the various queues.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 17 / 56

Page 47: Process Management - Part1

Scheduling Queues (2/2)

I Device queues: set of processes waiting for an I/O device.• Each device has its own device queue.

I Processes migrate among the various queues.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 17 / 56

Page 48: Process Management - Part1

Queuing Diagram (1/2)

I A new process is initially put in the ready queue.

I It waits there until it is selected for execution or dispatched.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 18 / 56

Page 49: Process Management - Part1

Queuing Diagram (2/2)

I After allocating the CPU to a process:

• The process could issue an I/O request and be placed in an I/Oqueue.

• The process could create a new child process and wait for thechild’s termination.

• The process could be removed forcibly from the CPU, as a result ofan interrupt, and be put back in the ready queue.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 19 / 56

Page 50: Process Management - Part1

Queuing Diagram (2/2)

I After allocating the CPU to a process:• The process could issue an I/O request and be placed in an I/O

queue.

• The process could create a new child process and wait for thechild’s termination.

• The process could be removed forcibly from the CPU, as a result ofan interrupt, and be put back in the ready queue.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 19 / 56

Page 51: Process Management - Part1

Queuing Diagram (2/2)

I After allocating the CPU to a process:• The process could issue an I/O request and be placed in an I/O

queue.• The process could create a new child process and wait for the

child’s termination.

• The process could be removed forcibly from the CPU, as a result ofan interrupt, and be put back in the ready queue.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 19 / 56

Page 52: Process Management - Part1

Queuing Diagram (2/2)

I After allocating the CPU to a process:• The process could issue an I/O request and be placed in an I/O

queue.• The process could create a new child process and wait for the

child’s termination.• The process could be removed forcibly from the CPU, as a result of

an interrupt, and be put back in the ready queue.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 19 / 56

Page 53: Process Management - Part1

Schedulers (1/3)

I Short-term scheduler (CPU scheduler)• Selects which process should be executed next and allocates CPU.• Invoked frequently (milliseconds): must be fast

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 20 / 56

Page 54: Process Management - Part1

Schedulers (2/3)

I Long-term scheduler (job scheduler)• Selects which processes should be brought into the ready queue.• Controls the degree of multiprogramming.• Invoked infrequently (seconds, minutes): may be slow.

I It strives for good mix of I/O-bound and CPU-bound processes.• I/O-bound process: more time doing I/O than computations.• CPU-bound process: more time doing computations.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 21 / 56

Page 55: Process Management - Part1

Schedulers (2/3)

I Long-term scheduler (job scheduler)• Selects which processes should be brought into the ready queue.• Controls the degree of multiprogramming.• Invoked infrequently (seconds, minutes): may be slow.

I It strives for good mix of I/O-bound and CPU-bound processes.• I/O-bound process: more time doing I/O than computations.• CPU-bound process: more time doing computations.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 21 / 56

Page 56: Process Management - Part1

Schedulers (3/3)

I Medium-term scheduler• It can be added if degree of multiprogramming needs to decrease.• Remove process from memory, store on disk, bring back in from

disk to continue execution: swapping

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 22 / 56

Page 57: Process Management - Part1

Context Switching (1/2)

I When CPU switches to another process:

• The state of the old process is saved by the system.• The saved state of the new process is loaded via a context switch.• Called context switching.

I Context of a process represented in the PCB.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 23 / 56

Page 58: Process Management - Part1

Context Switching (1/2)

I When CPU switches to another process:• The state of the old process is saved by the system.

• The saved state of the new process is loaded via a context switch.• Called context switching.

I Context of a process represented in the PCB.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 23 / 56

Page 59: Process Management - Part1

Context Switching (1/2)

I When CPU switches to another process:• The state of the old process is saved by the system.• The saved state of the new process is loaded via a context switch.

• Called context switching.

I Context of a process represented in the PCB.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 23 / 56

Page 60: Process Management - Part1

Context Switching (1/2)

I When CPU switches to another process:• The state of the old process is saved by the system.• The saved state of the new process is loaded via a context switch.• Called context switching.

I Context of a process represented in the PCB.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 23 / 56

Page 61: Process Management - Part1

Context Switching (1/2)

I When CPU switches to another process:• The state of the old process is saved by the system.• The saved state of the new process is loaded via a context switch.• Called context switching.

I Context of a process represented in the PCB.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 23 / 56

Page 62: Process Management - Part1

Context Switching (2/2)

I Context-switch time is an overhead.• The system does no useful work while switching.• The more complex the OS and PCB → the longer the context

switch.

I Time dependent on hardware support.• Some hardware provides multiple sets of registers per CPU →

multiple contexts loaded at once.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 24 / 56

Page 63: Process Management - Part1

Context Switching (2/2)

I Context-switch time is an overhead.• The system does no useful work while switching.• The more complex the OS and PCB → the longer the context

switch.

I Time dependent on hardware support.• Some hardware provides multiple sets of registers per CPU →

multiple contexts loaded at once.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 24 / 56

Page 64: Process Management - Part1

Operations on Processes

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 25 / 56

Page 65: Process Management - Part1

Operations on Processes

I OS must provide mechanisms for:• Process creation• Process termination• ...

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 26 / 56

Page 66: Process Management - Part1

Process Creation

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 27 / 56

Page 67: Process Management - Part1

Process Creation

I A process may create several new processes.• The creating process: the parent process.• The new processes: the children processes.

I These processes are forming a tree of processes.

# it lists complete information for all active processes in the system

ps -el

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 28 / 56

Page 68: Process Management - Part1

Process Creation

I A process may create several new processes.• The creating process: the parent process.• The new processes: the children processes.

I These processes are forming a tree of processes.

# it lists complete information for all active processes in the system

ps -el

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 28 / 56

Page 69: Process Management - Part1

Process Creation

I A process may create several new processes.• The creating process: the parent process.• The new processes: the children processes.

I These processes are forming a tree of processes.

# it lists complete information for all active processes in the system

ps -el

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 28 / 56

Page 70: Process Management - Part1

Parent-Child Resource Sharing Options

I Parent and children share all resources.

I Children share subset of parent’s resources.

I Parent and child share no resources.• The child obtains the required resources directly from the OS.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 29 / 56

Page 71: Process Management - Part1

Parent-Child Resource Sharing Options

I Parent and children share all resources.

I Children share subset of parent’s resources.

I Parent and child share no resources.• The child obtains the required resources directly from the OS.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 29 / 56

Page 72: Process Management - Part1

Parent-Child Resource Sharing Options

I Parent and children share all resources.

I Children share subset of parent’s resources.

I Parent and child share no resources.• The child obtains the required resources directly from the OS.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 29 / 56

Page 73: Process Management - Part1

Parent-Child Execution Options

I The parent continues to execute concurrently with its children.

I The parent waits until some or all of its children have terminated.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 30 / 56

Page 74: Process Management - Part1

Parent-Child Execution Options

I The parent continues to execute concurrently with its children.

I The parent waits until some or all of its children have terminated.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 30 / 56

Page 75: Process Management - Part1

Parent-Child Address Space Options

I The child process is a duplicate of the parent process (it has thesame program and data as the parent).

I The child process has a new program loaded into it.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 31 / 56

Page 76: Process Management - Part1

Parent-Child Address Space Options

I The child process is a duplicate of the parent process (it has thesame program and data as the parent).

I The child process has a new program loaded into it.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 31 / 56

Page 77: Process Management - Part1

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 32 / 56

Page 78: Process Management - Part1

Running a New Process in Linux (1/2)

I Executing a new program (exec())

• A system call loads a binary program into memory.• Replacing the previous contents of the address space.• Begins execution of the new program.

I Creating a new process (fork())

• The new process (child) initially is a near-duplicate of its parentprocess.

• Often, the new process immediately executes a new program.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 33 / 56

Page 79: Process Management - Part1

Running a New Process in Linux (1/2)

I Executing a new program (exec())• A system call loads a binary program into memory.

• Replacing the previous contents of the address space.• Begins execution of the new program.

I Creating a new process (fork())

• The new process (child) initially is a near-duplicate of its parentprocess.

• Often, the new process immediately executes a new program.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 33 / 56

Page 80: Process Management - Part1

Running a New Process in Linux (1/2)

I Executing a new program (exec())• A system call loads a binary program into memory.• Replacing the previous contents of the address space.

• Begins execution of the new program.

I Creating a new process (fork())

• The new process (child) initially is a near-duplicate of its parentprocess.

• Often, the new process immediately executes a new program.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 33 / 56

Page 81: Process Management - Part1

Running a New Process in Linux (1/2)

I Executing a new program (exec())• A system call loads a binary program into memory.• Replacing the previous contents of the address space.• Begins execution of the new program.

I Creating a new process (fork())

• The new process (child) initially is a near-duplicate of its parentprocess.

• Often, the new process immediately executes a new program.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 33 / 56

Page 82: Process Management - Part1

Running a New Process in Linux (1/2)

I Executing a new program (exec())• A system call loads a binary program into memory.• Replacing the previous contents of the address space.• Begins execution of the new program.

I Creating a new process (fork())

• The new process (child) initially is a near-duplicate of its parentprocess.

• Often, the new process immediately executes a new program.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 33 / 56

Page 83: Process Management - Part1

Running a New Process in Linux (1/2)

I Executing a new program (exec())• A system call loads a binary program into memory.• Replacing the previous contents of the address space.• Begins execution of the new program.

I Creating a new process (fork())• The new process (child) initially is a near-duplicate of its parent

process.

• Often, the new process immediately executes a new program.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 33 / 56

Page 84: Process Management - Part1

Running a New Process in Linux (1/2)

I Executing a new program (exec())• A system call loads a binary program into memory.• Replacing the previous contents of the address space.• Begins execution of the new program.

I Creating a new process (fork())• The new process (child) initially is a near-duplicate of its parent

process.• Often, the new process immediately executes a new program.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 33 / 56

Page 85: Process Management - Part1

Running a New Process in Linux (2/2)

I Executing a new program in a new process:1 First, a fork to create a new process,2 and then an exec to load a new binary into that process.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 34 / 56

Page 86: Process Management - Part1

The Exec Family of Calls (1/3)

I There is no single exec function.

I The simplest of these calls is execl().• It replaces the current process image with a new one.

#include <unistd.h>

int execl(const char *path, const char *arg, ...);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 35 / 56

Page 87: Process Management - Part1

The Exec Family of Calls (2/3)

I Example

int ret;

ret = execl("/bin/vi", "vi", "/home/kidd/hooks.txt", NULL);

if (ret == -1)

perror("execl");

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 36 / 56

Page 88: Process Management - Part1

The Exec Family of Calls (3/3)

I l and v: arguments are provided via a list or an array (vector).

I p: the user’s full path is searched.

I e: a new environment is supplied for the new process.

#include <unistd.h>

int execlp(const char *file, const char *arg, ...);

int execle(const char *path, const char *arg, ..., char * const envp[]);

int execv(const char *path, char *const argv[]);

int execvp(const char *file, char *const argv[]);

int execve(const char *filename, char *const argv[], char *const envp[]);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 37 / 56

Page 89: Process Management - Part1

Creating a New Process (1/4)

I The only way to create a new process.

I The new process (child) running the same image as the current one(parent).

I Both processes continue to run, if nothing special had happened.

#include <sys/types.h>

#include <unistd.h>

pid_t fork(void);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 38 / 56

Page 90: Process Management - Part1

Creating a New Process (2/4)

I This fork() function is called once, but it returns twice.• The PID of the new child → to the parent.• 0 → to the child.

I The child and the parent process are identical, except for:

• Their PIDs and their parents’ PID.• Reset resource statistics at the child.• Clear any pending signals at the child.• The acquired file locks are not inherited by the child.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 39 / 56

Page 91: Process Management - Part1

Creating a New Process (2/4)

I This fork() function is called once, but it returns twice.• The PID of the new child → to the parent.• 0 → to the child.

I The child and the parent process are identical, except for:

• Their PIDs and their parents’ PID.• Reset resource statistics at the child.• Clear any pending signals at the child.• The acquired file locks are not inherited by the child.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 39 / 56

Page 92: Process Management - Part1

Creating a New Process (2/4)

I This fork() function is called once, but it returns twice.• The PID of the new child → to the parent.• 0 → to the child.

I The child and the parent process are identical, except for:• Their PIDs and their parents’ PID.

• Reset resource statistics at the child.• Clear any pending signals at the child.• The acquired file locks are not inherited by the child.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 39 / 56

Page 93: Process Management - Part1

Creating a New Process (2/4)

I This fork() function is called once, but it returns twice.• The PID of the new child → to the parent.• 0 → to the child.

I The child and the parent process are identical, except for:• Their PIDs and their parents’ PID.• Reset resource statistics at the child.

• Clear any pending signals at the child.• The acquired file locks are not inherited by the child.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 39 / 56

Page 94: Process Management - Part1

Creating a New Process (2/4)

I This fork() function is called once, but it returns twice.• The PID of the new child → to the parent.• 0 → to the child.

I The child and the parent process are identical, except for:• Their PIDs and their parents’ PID.• Reset resource statistics at the child.• Clear any pending signals at the child.

• The acquired file locks are not inherited by the child.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 39 / 56

Page 95: Process Management - Part1

Creating a New Process (2/4)

I This fork() function is called once, but it returns twice.• The PID of the new child → to the parent.• 0 → to the child.

I The child and the parent process are identical, except for:• Their PIDs and their parents’ PID.• Reset resource statistics at the child.• Clear any pending signals at the child.• The acquired file locks are not inherited by the child.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 39 / 56

Page 96: Process Management - Part1

Creating a New Process (3/4)

I Example

pid_t pid = fork();

if (pid == -1) {

perror("fork");

exit(1);

}

if (pid > 0)

printf("I am the parent of pid = %d!\n", pid);

else

printf("I am the child!\n");

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 40 / 56

Page 97: Process Management - Part1

Creating a New Process (4/4)

I Example

pid_t pid = fork();

if (pid == -1) {

perror("fork");

exit(1);

}

if (pid == 0) { // the child

const char *args[] = {"windlass", NULL};

int ret;

ret = execv("/bin/windlass", args);

if (ret == -1) {

perror("execv");

exit(1);

}

}

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 41 / 56

Page 98: Process Management - Part1

Process Termination

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 42 / 56

Page 99: Process Management - Part1

Process Termination (1/4)

I Process executes last statement and then asks the OS to delete itusing the exit() system call.

• Returns status data from the child to the parent via wait().• Process resources are deallocated by the OS.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 43 / 56

Page 100: Process Management - Part1

Process Termination (1/4)

I Process executes last statement and then asks the OS to delete itusing the exit() system call.

• Returns status data from the child to the parent via wait().

• Process resources are deallocated by the OS.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 43 / 56

Page 101: Process Management - Part1

Process Termination (1/4)

I Process executes last statement and then asks the OS to delete itusing the exit() system call.

• Returns status data from the child to the parent via wait().• Process resources are deallocated by the OS.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 43 / 56

Page 102: Process Management - Part1

Process Termination (2/4)

I A parent may terminate the execution of its children via abort().

I Some reasons for doing so:• Child has exceeded allocated resources.• Task assigned to child is no longer required.• The parent is exiting and the OS does not allow a child to continue

if its parent terminates.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 44 / 56

Page 103: Process Management - Part1

Process Termination (2/4)

I A parent may terminate the execution of its children via abort().

I Some reasons for doing so:• Child has exceeded allocated resources.• Task assigned to child is no longer required.• The parent is exiting and the OS does not allow a child to continue

if its parent terminates.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 44 / 56

Page 104: Process Management - Part1

Process Termination (3/4)

I Some OSs do not allow child to exists if its parent has terminated.

I If a process terminates, all its children must also be terminated.• Cascading termination: all children, grandchildren, etc. are

terminated.• The termination is initiated by the OS.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 45 / 56

Page 105: Process Management - Part1

Process Termination (4/4)

I The parent process may wait for termination of a child via wait().

I The wait() returns the status information and the PID of the ter-minated process.

I If a process has terminated, but whose parent has not yet calledwait(), the process is a zombie.

I If the parent terminated without invoking wait(), the process is anorphan.

• In Linux, the init process becomes the parent of all orphans.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 46 / 56

Page 106: Process Management - Part1

Process Termination (4/4)

I The parent process may wait for termination of a child via wait().

I The wait() returns the status information and the PID of the ter-minated process.

I If a process has terminated, but whose parent has not yet calledwait(), the process is a zombie.

I If the parent terminated without invoking wait(), the process is anorphan.

• In Linux, the init process becomes the parent of all orphans.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 46 / 56

Page 107: Process Management - Part1

Process Termination (4/4)

I The parent process may wait for termination of a child via wait().

I The wait() returns the status information and the PID of the ter-minated process.

I If a process has terminated, but whose parent has not yet calledwait(), the process is a zombie.

I If the parent terminated without invoking wait(), the process is anorphan.

• In Linux, the init process becomes the parent of all orphans.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 46 / 56

Page 108: Process Management - Part1

Process Termination (4/4)

I The parent process may wait for termination of a child via wait().

I The wait() returns the status information and the PID of the ter-minated process.

I If a process has terminated, but whose parent has not yet calledwait(), the process is a zombie.

I If the parent terminated without invoking wait(), the process is anorphan.

• In Linux, the init process becomes the parent of all orphans.

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 46 / 56

Page 109: Process Management - Part1

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 47 / 56

Page 110: Process Management - Part1

The exit() System Call

I The exit() performs some basic shutdown steps, then instructs thekernel to terminate the process.

I The status is used to denote the process’s exit status.

#include <stdlib.h>

void exit(int status);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 48 / 56

Page 111: Process Management - Part1

Waiting for Terminated Child Processes (1/3)

I When a child dies before its parent, the kernel puts the child intothe zombie state.

I Some basic kernel data structures containing potentially useful datais kept for the process.

I A process in this state waits for its parent to inquire about its status.

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(int *status);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 49 / 56

Page 112: Process Management - Part1

Waiting for Terminated Child Processes (1/3)

I When a child dies before its parent, the kernel puts the child intothe zombie state.

I Some basic kernel data structures containing potentially useful datais kept for the process.

I A process in this state waits for its parent to inquire about its status.

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(int *status);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 49 / 56

Page 113: Process Management - Part1

Waiting for Terminated Child Processes (1/3)

I When a child dies before its parent, the kernel puts the child intothe zombie state.

I Some basic kernel data structures containing potentially useful datais kept for the process.

I A process in this state waits for its parent to inquire about its status.

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(int *status);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 49 / 56

Page 114: Process Management - Part1

Waiting for Terminated Child Processes (1/3)

I When a child dies before its parent, the kernel puts the child intothe zombie state.

I Some basic kernel data structures containing potentially useful datais kept for the process.

I A process in this state waits for its parent to inquire about its status.

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(int *status);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 49 / 56

Page 115: Process Management - Part1

Waiting for Terminated Child Processes (2/3)

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 50 / 56

Page 116: Process Management - Part1

Waiting for Terminated Child Processes (3/3)

int main (void) {

int status;

pid_t pid;

if (fork() == 0) return 1; // the child

pid = wait(&status);

if (pid == -1) perror("wait");

printf("pid = %d\n", pid);

return 0;

}

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 51 / 56

Page 117: Process Management - Part1

More on Launching and Waiting for Processes

I waitpid() to wait for a process with a known PID.

#include <sys/types.h>

#include <sys/wait.h>

pid_t waitpid(pid_t pid, int *status, int options);

I Launching and waiting for a new process

#include <stdlib.h>

int system(const char *command);

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 52 / 56

Page 118: Process Management - Part1

Summary

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 53 / 56

Page 119: Process Management - Part1

Summary

I Process vs. Program

I Process states: new, running, waiting, ready, terminated

I Process Control Block (PCB)

I Process scheduling: scheduling queues, context switching

I Process operations: creation (parent-child), termination

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 54 / 56

Page 120: Process Management - Part1

Summary

I Process vs. Program

I Process states: new, running, waiting, ready, terminated

I Process Control Block (PCB)

I Process scheduling: scheduling queues, context switching

I Process operations: creation (parent-child), termination

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 54 / 56

Page 121: Process Management - Part1

Summary

I Process vs. Program

I Process states: new, running, waiting, ready, terminated

I Process Control Block (PCB)

I Process scheduling: scheduling queues, context switching

I Process operations: creation (parent-child), termination

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 54 / 56

Page 122: Process Management - Part1

Summary

I Process vs. Program

I Process states: new, running, waiting, ready, terminated

I Process Control Block (PCB)

I Process scheduling: scheduling queues, context switching

I Process operations: creation (parent-child), termination

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 54 / 56

Page 123: Process Management - Part1

Summary

I Process vs. Program

I Process states: new, running, waiting, ready, terminated

I Process Control Block (PCB)

I Process scheduling: scheduling queues, context switching

I Process operations: creation (parent-child), termination

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 54 / 56

Page 124: Process Management - Part1

Questions?

Amir H. Payberah (Tehran Polytechnic) Processes 1393/6/31 55 / 56