Chapter Two Process Management
Chapter Two Process Management
Chapter- Two
PROCESS MANAGEMENT
2.1 PROCESSES
Process State: As a process executes, it changes state. The state of a process is defined
in part by the current activity of that process. Each process may be in one of the
following states:
New: The process is being created
Ready: The process is waiting to be assigned to a processor
Running: Instructions are being executed.
Waiting: The process is waiting for some event to occur( such as an I/O
completion or reception of a signal )
Terminated: The process has finished execution.
Chapter-02 Process Management
New Terminated
Admitted Interrupt Exit
Ready Running
Scheduler dispatch
It is important to realize that only one process can be running on any processor at any
instant. Many processes may be ready and waiting, however. The state diagram
corresponding to these states is presented in Figure 1.
Process Control Block: Each process is represented in the operating system by a
process control block (PCB) also called a task control block. A PCB is shown in Figure 2. It
contains many pieces of information associated with a specific process, including these:
Process state: The state may be new, ready, running, and waiting, halted, and so
on.
Program counter: The counter indicates the address of the next instruction to
be executed for this process.
CPU registers: The registers vary in number and type, depending on the
computer architecture. They include accumulators, index registers, stack
pointers, and general-purpose registers, plus any condition-code information.
Along with the program counter, this state information must be saved when an
interrupt occurs, to allow the process to be continued correctly afterward.
CPU scheduling information: this information includes a process priority
pointer to scheduling queues, and any other scheduling parameters.
Memory management information: This information may include such
information as the value of the base and limit registers, the page tables or the
segment tables depending on the memory system used by the operating system.
Accounting information: This information includes the amount of CPU and real
time used, time limits, account numbers, job or process numbers, and so on.
Chapter-02 Process Management
I/O status information: The information includes the list of I/O devices
allocated to this process, a list of open files and so on
.
Scheduling Queues:
As processes enter the system, they are put into a job queue. This queue consists
of all processes in the system.
The processes that are residing in main memory and are ready and waiting to
execute are kept on a list called the ready queue. This queue is generally stored
as a linked list. A ready queue header will contain pointer to the first and last
PCBs in the list. Each PCB has a pointer field that points to the next process in the
ready queue.
When a process is allocated the CPU, it executes for a while and eventually quits,
is interrupted, or waits for the occurrence of a particular event, such as the
Chapter-02 Process Management
completion of an I/O request; such a request may be to a dedicated tape drive, or
to a shared device, such as a disk. The list of processes waiting for a particular
I/O device is called a device queue. Each device has its own device queue.
A common representation for a discussion of process scheduling is a queuing
diagram.
Each rectangular box represents a queue. Two types of queues are present: the
ready queue and a set of a device queues.
The circles represent the resources that serve the queues and the arrows
indicate the flow of processes in the system.
A new process is initially put in the ready queue. It waits in the ready queue until
it is selected for execution and is given the CPU. once the process is allocated the
CPU and is executing , one of several events could occur:
The process could issue an I/O request, and then be placed in an I/O
queue.
The process could create a new sub process and wait for its termination.
The process could be removed forcibly from the CPU, as a result of an
interrupt and then put back in the ready queue.
In the first two cases, the process eventually switches from the waiting state to
the ready state, and is then put back in the ready queue.
Time slice
expired
Child Child
Fork a child
terminates executes
Swap in swap
Partially executed swapped out processes out
Ready queue C P U
e n
d
New
LTS
STS
Ready Running
MTS
Waiting
Chapter-02 Process Management
Context switch
Switching the CPU to another process requires saving the state of the old process and
loading the saved state for the new process. This task is known as a context switch.
Context-switch time is pure overhead, because the system does no useful work
while switching.
Context-switch times are highly dependent on hardware support.
A context-switch simply includes changing the pointer to the current register set.
Of course, if there are more active processes than there are register sets, the
system resorts to copying register data to and from memory, as before.
The concurrent processes executing in the operating system may be either independent
or cooperating processes.
Independent processes: A process is independent if it cannot affect or be affected by
the processes executing in the system. i.e., any process that does not share any data
with any other process is independent.
Cooperating processes: A processes is cooperating if it can affect or be affected by the
other processes executing in the system. i.e., any process that shares data with other
processes is a cooperating process.
There are several reasons for providing an environment that allows process
cooperation:
Information sharing
Computation speedup
Modularity
Convenience
2.3 THREADS
A thread is a base unit of CPU utilization, consists of a program counter, register set, and
a stack space. It shares with peer threads its code section, data section, and operating
system. A thread is also called a light weight process (LWP).
CPU Scheduler
Whenever the CPU becomes idle, the operating system must select one of the processes
in the ready queue to be executed. The selection processes is carried out by the short-
term scheduler (CPU scheduler).
CPU scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state.
(For example, I/O request, or invocation of wait for the termination of one of the
child processes)
2. when a process switched from the running state to the ready state (for example,
when an interrupt occurs)
3. When a process switches from the waiting state to the ready state.(for example
completion of I/O )
4. When a process terminates.
In case of 1 and 4 there is no choice in terms of scheduling, but there is a choice for 2
and 3.
Chapter-02 Process Management
When scheduling takes place only under circumstances 1 and 4, we say the scheduling
scheme is non preemptive; otherwise, the scheduling scheme is preemptive.
Under non preemptive scheduling, once the CPU has been allocated to a process, the
process keeps CPU until it releases the CPU either by terminating or by switching to the
waiting state.
Dispatcher:
Dispatcher is the module that gives control of the CPU to the process selected by
the short-term scheduler.
This function involves:
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that
program
The dispatcher should be as fast as possible, given that it is invoked during every
process switch.
The time it takes for the dispatcher stop one process and start another running
is known as the dispatch latency
Different CPU scheduling algorithms have different properties and may favor one class
of processes over another. In choosing which algorithm to use in a particular situation,
we must consider the properties of the various algorithms. Different criteria have been
suggested, given below, for comparing CPU scheduling algorithms.
This is the simplest scheduling algorithm. In this scheme, the process that
requests the CPU first is allocated the CPU first.
This FCFS algorithm is implemented using FIFO queue.
When a process enters the ready queue, its PCB is linked onto the tail of the
queue. When the CPU is free, it is allocated to the process at the head of the
queue.
The code for the FCFS scheduling is simple to write and understand.
The average waiting time under the FCFS policy is often quite long.
FCFS is a non-preemptive. Once the CPU has been allocated to a process, that
process keeps the CPU until it releases the CPU, either by terminating or by
requesting I/O.
FCFS algorithm is particularly troublesome for time-sharing systems, where it is
important that each user get a share of the CPU at regular intervals.
Example1:
1. Consider the following set of processes that arrive at time 0, with the length of the
CPU-burst time given in milliseconds.
Process : P1 P2 P3
Burst time : 24 3 3
Chapter-02 Process Management
If the process arrive in the order P1, P2, P3 and are served in FCFS order then waiting
time, turnaround time, and Response times are given below.
Gantt chart is P1 P2 P3
0 24 27 30
If the process arrive in the order P2, P3, P1 and are served in FCFS order then waiting
time, turnaround time, and Response times are given below.
Gantt chart is
P2 P3 P1
0 3 6 30
The average time under FCFS policy in general is not minimal, and may vary
substantially if the process CPU-burst times vary greatly.
Chapter-02 Process Management
Example 2. For the following data perform the same as in previous problem.
Process : P1 P2 P3 P4 P5
Arrival time : 0 2 4 6 8
Burst time : 3 6 4 5 2
Gantt chart:
P1 P2 P3 P4 P5
0 3 9 13 18 20
This algorithm associates with each process the length of the latter’s next CPU
burst. When the CPU is available, it is assigned to the process that has the
smallest next CPU burst. If two processes have the same length next CPU burst,
FCFS scheduling is used to break the tie.
Example1: consider the following set of processes, with the length of the CPU burst
time given in milliseconds.
Process : P1 P2 P3 P4
Arrival time : 0 0 0 0
Burst time : 6 8 7 3
Chapter-02 Process Management
Find the average waiting time, response time and show the schedule using Gantt chart.
Gantt chart:
P4 P1 P3 P2
0 3 9 16 24
Example2: consider the following set of processes, with the length of the CPU burst
time given in milliseconds.
Process : P1 P2 P3 P4
Arrival time : 0 1 2 3
Burst time : 8 4 9 5
Find the average waiting time and response times using Gantt charts.
Chapter-02 Process Management
Gantt chart:
P1 P2 P4 P1 P3
0 1 5 10 17 26
Process Arrival Burst Response Waiting Turnaround
time Time time time time
P1 0 8 0 9
P2 1 4 0 0
P3 2 9 15 15
P4 3 5 2 2
26
The SJF scheduling algorithm is provably Optimal, in that it gives the minimum
average waiting time for a given set of processes.
Although the SJF algorithm is optimal, it cannot be implemented at the level of
shortest-term CPU scheduling. There is no way to know the length of the next
CPU burst.
One approach is to try to approximate SJF scheduling. We may not know the
length of the next CPU burst; we may able to predict its value. Thus, by
computing an approximation of the length of the next CPU burst, we can pick the
process with the shortest predicted CPU burst.
The SJF algorithm is either preemptive or non-preemptive. Preemptive SJF
Scheduling is called shortest-remaining-time-first scheduling.
3. Priority Scheduling
Example1: consider the following set of processes, assumed to have arrived at time 0, in
the order P1, P2, P3, P4, P5, with the length of the CPU-burst time given in milliseconds. Find
the average waiting time and turnaround time using Gantt charts.
Process : P1 P2 P3 P4 P5
Burst time : 10 1 2 1 5
Priority : 3 1 3 4 2
Sol:
Gantt chart: P2 P5 P1 P3 P4
0 1 6 16 18 19
Example2: consider the following set of processes, in the order P1, P2, P3, P4, with the
length of the CPU-burst time given in milliseconds and Priorities. Find the average
waiting time and turn around time using Gantt charts.
Chapter-02 Process Management
Process : P1 P2 P3 P4
Arrival time : 0 2 4 6
Burst time : 8 4 9 5
Priority : 3 1 4 2
Gantt chart:
P1 P2 P4 P1 P3
0 2 6 11 17 26
4. Round-Robin Scheduling
The round-robin (RR) scheduling algorithm is designed especially for time- sharing
systems.
A small unit of time, called a time quantum, or time slice, is defined. The ready
queue is treated as a circular queue. The CPU scheduler goes around the ready
queue, allocating the CPU to each process for a time interval of up to 1 time
quantum.
To implement the RR scheduling, we keep the ready queue as FIFO queue of
processes. New processes are added to the tail of the ready queue. The CPU
scheduler picks the first processes from the ready queue, sets a timer to
interrupt after 1 time quantum, and dispatches the process.
The average waiting time under the RR policy, however, is often quite long.
Example 1: consider the following set of processes, assumed to have arrived at time 0,
in the order P1, P2, P3, P4, P5, with the length of the CPU-burst time given in
milliseconds. Find the average waiting time and turnaround time using Gantt charts for
RR scheduling. (Time quantum is 3 milliseconds).
Process : P1 P2 P3 P4 P5
Burst time : 10 1 2 1 5
Gantt chart:
P1 P2 P3 P4 P5 P1 P5 P1 P1
0 3 4 6 7 10 13 15 18 19
19 | P a g e
Chapter-02 Process Management
Average waiting time of a process is = 32/5 = 6.2 milliseconds.
One the selection criteria have been defined, we want to evaluate the various algorithms under
consideration.
Deterministic modeling
Queueing models.
Simulations.
Implementations.
20 | P a g e