Understanding Process Management in OS
Understanding Process Management in OS
CPU and I/O bound, operating system services for process and thread
management,
Main Secondar
memory y Storage
CPU
Process Program
PROCESS CONCEPT An operating system executes a variety of programs:
Batch system – jobs
• Process – a program in execution; Time-shared systems – user programs or tasks
Textbook uses the terms job and process almost
• It is also called as job,task,unit of interchangeably
work. Multiple parts
The program code, also called text section
• Process execution must progress Current activity including program counter,
in sequential fashion processor registers
• Stack containing temporary data
A process is an activity and it has
Function parameters, return addresses, local
program - data Resources variables
state Data section containing global variables
Heap containing memory dynamically allocated
• Logically : each process has its during run time
separate virtual CPU. Program is passive entity stored on disk (executable
Real : CPU switches from one file), process is active Program becomes process when
process to another. executable file loaded into memory
Execution of program started via GUI mouse clicks,
command line entry of its name, etc
One program can be several processes. Consider
multiple users executing the same program
ALTERNATING SEQUENCE OF CPU AND I/O BURSTS
PROCESS IN MEMORY
When a program is loaded into the memory and it becomes a process, it can be divided into four sections ─
stack, heap, text and data. The following image shows a simplified layout of a process inside main memory
A program is a piece of code which may be a single line or millions of lines. A computer
program is usually written by a computer programmer in a programming language.
For example, here is a simple program written in C programming language −
#include <stdio.h>
int main()
{
printf("Hello, World! \n"); return 0;
}
A computer program is a collection of instructions that performs a specific task when executed
by a computer. When we compare a program with a process, we can conclude that a process is
a dynamic instance of a computer program.
A part of a computer program that performs a well-defined task is known as an algorithm.
A collection of computer programs, libraries and related data are referred to as a software.
PROCESS IN MEMORY/ Process Life Cycle
When a process executes, it passes through different states. These stages may differ in different operating systems, and the
names of these states are also not standardized.
In general, a process can have one of the following
five states at a time.
Ready Running
Time-out
Event Event
Occurs Wait
Waiting
PROCESS TRANSITIONS (1)
• Ready –> Running
• When it is time, the dispatcher selects a new
process to run.
• Running –> Ready
• the running process has expired his time slot.
• the running process gets interrupted because
a higher priority process is in the ready state.
PROCESS TRANSITIONS (2)
• Running –> Waiting
• When a process requests something for which it must wait:
• a service that the OS is not ready to perform.
• an access to a resource not yet available.
• initiates I/O and must wait for the result.
• waiting for a process to provide input.
• Waiting –> Ready
• When the event for which it was waiting occurs.
FIVE-STATE PROCESS MODEL
PROCESS ATTRIBUTES
• Process ID
• Parent process ID
• User ID
• Process state/priority
• Program counter
• CPU registers
• Memory management information
• I/O status information
• Access Control
• Accounting information
PROCESS CONTROL BLOCK (PCB)
Information associated with each process
Information associated with each
process (also called task control block)
Process state – running, waiting, etc
• Process state
Program counter – location of instruction to next
• Process Privileges
execute
• Process Id CPU registers – contents of all process-centric
• Pointer
registers
CPU scheduling information- priorities, scheduling
• Program counter
queue pointers
• CPU registers Memory-management information – memory allocated
• CPU scheduling information
to the process
Accounting information – CPU used, clock time
• Memory-management information
elapsed since start, time limits
• Accounting information I/O status information – I/O devices allocated to
• I/O status information
process, list of open files
PROCESS CONTROL BLOCK (PCB)
PROCESSES IN MEMORY (2)
A. Frank - P. Weisberg
TYPICAL PROCESS TABLE ENTRY
A. Frank - P. Weisberg
COMPONENTS OF PROCESS CONTROL BLOCK (PCB)
• Memory management –
• pointers to segment/page tables assigned to this process.
OPERATIONS ON PROCESS: PROCESS CREATION AND TERMINATION
• New state –
• OS has performed the necessary actions to
create the process:
• has created a process identifier.
• has created tables needed to manage the
process.
• but has not yet committed to execute the
process (not yet admitted):
• because resources are limited.
• Terminated state –
• Program termination moves the process to this state.
• It is no longer eligible for execution.
• Tables and other info are temporarily preserved for
auxiliary program –
• Example: accounting program that cumulates
resource usage for billing the users.
• The process (and its tables) gets deleted when the data is
no more needed.
REASONS FOR PROCESS CREATION
• System initialization.
• Submission of a batch job.
• User logs on.
• Created by OS to provide a service to a user (e.g., printing a
file).
• A user request to create a new process.
• Spawned by an existing process
• a program can dictate the creation of a number of processes.
PROCESS CREATION (1)
• Parent process create children processes, which, in turn create other
processes, forming a tree of processes.
• Possible resource sharing:
• Parent and children share all resources.
• Children share subset of parent’s resources.
• Parent and child share no resources.
• Possible execution:
• Parent and children execute concurrently.
• Parent waits until children terminate.
PROCESS CREATION (2)
• Assign a unique process identifier (PID).
• Allocate space for the process image.
• Initialize process control block
• many default values (e.g., state is New, no I/O devices or files...).
• Address space
• Child duplicate of parent.
• Child has a program loaded into it.
• UNIX examples
• fork system call creates new process.
• exec system call used after a fork to replace
process memory space with a new program.
PROCESS CREATION (3)
WHEN DOES A PROCESS GET TERMINATED?
• Arithmetic error
• Time overrun
• process waited longer than a specified maximum for an event.
REASONS FOR PROCESS TERMINATION (2)
• I/O failure
• Invalid instruction
• happens when trying to execute data.
• Privileged instruction
• Operating system intervention
• such as when deadlock occurs.
• Pre-Emptive Scheduling
• if a new process arrives with a CPU burst length less than the
remaining time of the current executing process, then it will pre-empt
the currently executing process.
• Non-Pre-Emptive Scheduling
• once the CPU is given to the process, it cannot be preempted until it
completes its CPU burst or till it switches to the waiting state.
Three-level Scheduling
• Long-term scheduler
(or job scheduler) –
SHORT-TERM
selects which processes
should be brought into
the ready queue
• Short-term scheduler
(or CPU scheduler) –
selects which process
should be executed
next and allocates CPU
LONG-TERM MID-TERM
SINGLE PROCESSOR SCHEDULING
ALGORITHMS
SINGLE PROCESSOR SCHEDULING ALGORITHMS
P2 3
P3 3
• Case #1: Suppose that the processes arrive in the order: P1 , P2 , P3
P1 P2 P3
P4 = 0-0 = 0
P3 = 3-1 = 2
PI = 11-2 = 9
P5= 17-4 = 13
P2= 21-5= 16
Average Waiting Time
= 40/5= 8
Processes Arrival Time Burst Time
P1 0 4
P2 1 3
P3 2 1
P4 3 2
P5 4 5
•The overall execution of the processes will be as shown below:
Remaining
Time Execution Initial Burst
Process Arrival Time Waiting Table Burst Time
Instance Time Time
P2 0.0 4
P3 P2 P4 P1
P3 0.0 1
0 1 5 10 16
P4 0.0 5
•SJF (non-preemptive, simultaneous arrival)
•Average waiting time = (0 + 1 + 5 + 10)/4 = 4
•Average turn-around time = (1 + 5 + 10 + 16)/4 = 8
EXAMPLE #2: NON-PRE-EMPTIVE SJF(VARIED ARRIVAL TIMES)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P1 P3 P2 P4
P4 5.0 4
0 3 7 8 12 16
• SJF (non-preemptive, varied arrival times)
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4 P1 P2 P3 P2 P4 P1
P1 0.0 9
P2 2.0 2
P3 4.0 5
ADVANTAGES AND DISADVANTAGES OF SJF SCHEDULING ALGORITHM
•Advantages:
Overall performance is slightly improved in terms of response time
It’s having the least average waiting time, average turn around time and average
response time
•Disadvantages:
There is a risk of starvation of longer processes
It is difficult to know the length of the next CPU burst time
SHORTEST REMAINING TIME NEXT (SRTN)
• Conversely, if a process has a CPU burst greater than the time quantum,
the process will be suspended if it reaches a quantum of time, and then
queuing back to the position of the tail of the ready queue and then
execute the next process.
• If there are n processes in the ready queue and the time quantum q, then
each process gets 1/n of the CPU time at most q time units at once CPU
scheduling. No process waits for more than (n-1) q time units.
EXAMPLE OF RR WITH TIME QUANTUM = 20
Proces Brust
s Time
P1 53
P2 17
P3 68
P4 24
EXAMPLE OF RR WITH TIME QUANTUM = 20
P2 17 _
P3 68 48 28 8
P4 24 4 _
• The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
• Typically, higher average turnaround than SJF, but better response time
• Average waiting time
= ( [(0 – 0) + (77 - 20) + (121 – 97)] + (20 – 0) + [(37 – 0) + (97 - 57) + (134 – 117)] + [(57 – 0) + (117
– 77)] ) / 4
•
= (0 + 57 + 24) + 20 + (37 + 40 + 17) + (57 + 40) ) / 4
•
= (81 + 20 + 94 + 97)/4
•
= 292 / 4 = 73
• There are six processes named as P1, P2, P3, P4, P5 and P6. Their arrival time
and burst time are given below in the table. The time quantum of the system
is 4 units. Find out the average waiting time using RR scheduling algorithm
• Advantages of Round-robin Scheduling
• If you know the total number of processes on the run queue, then you can also assume the worst-
case response time for the same process.
• This scheduling method does not depend upon burst time. That’s why it is easily implementable
on the system.
In this example, the processes P1, P2, and P3 arrive at t=0, but still, P1 runs first as it
belongs to queue number 1, which has a higher priority. After the P1 process is over, the
P2 process in queue 2 runs before the process P3 which is present in queue 3 because
queue 2 has a higher priority than queue 3, and then P3 runs. While the P3 process is
running, the process P4 belonging to queue 1 arrives. Since queue 1 has higher priority
than queue 3, process P3 is stopped (paused), and P4 starts executing. After the
execution of P4 is completed, the execution of P3 is resumed.
MULTI-LEVEL FEEDBACK QUEUE SCHEDULING
MULTILEVEL FEEDBACK QUEUE SCHEDULING
• In multi-level feedback queue scheduling, a process
In Multilevel
can moveFeedback Queue, Process
between can move
the various between
queues; the queues.
aging can be It uses many ready queue and
associate a different priority
implemented thiswith
wayeach queue. Multilevel Queue scheduling doesn’t allow us to do this.
So, this was the disadvantages of Multilevel Queue scheduling.
Consider Processes with different CPU burst characteristic. If a process uses too much of the CPU, it will be
moved to a lower priority queue. In same way, a process that waits too much in a lower priority queue may be
moved to a higher priority queue.
While implementing a Multilevel Feedback queue, there are various parameters that define the schedulers –
•The number of queues
•The scheduling algorithm for each queue
•The method used to demote processes to lower priority queues
•The method used to promote processes to higher priority queues
•The method used to determine which queue a process will enter
Let us take an example to understand:
Let’s consider there are three queues Q1, Q2 and Q3.
– Q1 Time quantum 9 milliseconds
– Q2 Time quantum 18 milliseconds
– Q3 FCFS
MULTILEVEL FEEDBACK QUEUE SCHEDULING
Criteria of Multilevel Feedback Queue –
•The scheduler first executes all processes in Q1.
•Only when Q1 is empty will it executes processes in Q2.
•Similarly, processes in Q3 will be executed only if Q1 and Q2 are
empty.
•A process that arrives for Q2 will preempt a process in Q3.
•A process that arrives for Q3 will, in turn, preempt a process in
Q3.
• A Q1 (RR) job receives 16 milliseconds. If it still does not complete, it is preempted and moved
to queue Q2 (FCFS).
Q0
Q1
Q2
DEADLOCKS
• In a multiprogramming environment, several processes may compete for a finite number of
resources. A process requests resources; if the resources are not available at that time, the
process enters a waiting state. Sometimes, a waiting process is never again able to change
state, because the resources it has requested are held by other waiting processes. This
situation is called a deadlock.
• This results from sharing resources such as memory, devices, files, links.
• Operating systems typically do not provide deadlock-prevention facilities, and it remains the
responsibility of programmers to ensure that they design deadlock-free programs.
93
DEADLOCKS Bridge Crossing
Example
• If a process requests an instance of a resource type, the allocation of any instance of the type will satisfy the request. If it will not, then the instances are not
identical, and the resource type classes have not been defined properly..
No Preemption
There is only voluntary release of a resource - nobody else can make a process give up a resource.
Circular Wait
A set { P0 , Pl, ... , P11 } of waiting processes must exist such that P0 is waiting for a resource held
by P1, P1 is waiting for a resource held by P2, ... , Pn-1 is waiting for a resource held by Pn and Pn is
waiting for a resource held by P0.
METHODS FOR HANDLING DEADLOCKS
Prevention
Ensure that the system will never enter a deadlock state. Prevent any one of the 4 conditions
from happening
Avoidance
Ensure that the system will never enter an unsafe state. Allow all deadlock conditions, but
calculate cycles about to happen and stop dangerous operations.
Detection
Allow the system to enter a deadlock state and then recover
Do Nothing
Ignore the problem and let the user or system administrator respond to the problem; used by
most operating systems, including Windows and UNIX
To ensure that deadlocks never occur, the system can either use a deadlock-prevention or a
deadlock-avoidance scheme
DEADLOCKS Deadlock Prevention
99
Do not allow one of the four conditions
to occur.
Mutual exclusion:
CIRCULAR WAIT