OS - Module2 - Apsima
OS - Module2 - Apsima
MODULE 2
Processes and Process Scheduling
Syllabus
● What is a Process?
○ Process is the execution of a program that performs the actions specified in
that program.
○ It can be defined as an execution unit where a program runs.
○ The OS helps you to create, schedule, and terminate the processes which are
used by the CPU.
○ A process created by the main process is called a child process.
○ Process operations can be easily controlled with the help of PCB(Process
Control Block).
○ You can consider it as the brain of the process, which contains all the crucial
information related to processing like process id, priority, state, CPU registers,
etc.
○ What does a process look like in memory?
○
●
○ Text Section: A Process, sometimes known as the Text Section, also includes
the current activity represented by the value of the Program Counter.
Operating System_Btech2019_PartTime_Semester2 2
● Process states
○ 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.
○ A process state is a condition of the process at a specific instant of time.
○ It also defines the current position of the process.
○
○
○ New :
■ A program which is going to be picked up by the OS into the main
memory is called a new process.
○ Ready :
■ Whenever a process is created, it directly enters in the ready state, in
which it waits for the CPU to be assigned.
■ The OS picks the new processes from the secondary memory and puts
all of them in the main memory.
■ The processes which are ready for the execution and reside in the main
memory are called ready state processes.
■ There can be many processes present in the ready state.
○ Run :
■ One of the processes from the ready state will be chosen by the OS
depending upon the scheduling algorithm.
■ Hence, if we have only one CPU in our system, the number of running
processes for a particular time will always be one.
■ If we have n processors in the system then we can have n processes
running simultaneously.
Operating System_Btech2019_PartTime_Semester2 3
○ Block or wait:
■ From the Running state, a process can make the transition to the block
or wait state depending upon the scheduling algorithm or the intrinsic
behavior of the process.
■ When a process waits for a certain resource to be assigned or for the
input from the user then the OS moves this process to the block or wait
state and assigns the CPU to the other processes.
○ Completion or termination:
■ When a process finishes its execution, it comes in the termination state.
■ All the context of the process (Process Control Block) will also be
deleted and the process will be terminated by the Operating system.
○ Suspend ready:
■ A process in the ready state, which is moved to secondary memory
from the main memory due to lack of the resources (mainly primary
memory) is called in the suspended ready state.
■ If the main memory is full and a higher priority process comes for the
execution then the OS has to make room for the process in the main
memory by throwing the lower priority process out into the secondary
memory.
■ The suspended ready processes remain in the secondary memory until
the main memory gets available.
○ Suspend wait:
■ Instead of removing the process from the ready queue, it's better to
remove the blocked process which is waiting for some resources in the
main memory.
■ Since it is already waiting for some resource to get available hence it is
better if it waits in the secondary memory and makes room for the
higher priority process.
■ These processes complete their execution once the main memory gets
available and their wait is finished.
○
○ Process ID: Unique identification for each of the processes in the operating
system.
○ Process state: A process can be new, ready, running, waiting, etc.
○ Pointer :A pointer to the parent process.
○ Program counter: The program counter lets you know the address of the next
instruction, which should be executed for that process.
○ CPU registers: This component includes accumulators, index and
general-purpose registers, and information of condition code.
○ CPU scheduling information: This component includes a process priority,
pointers for scheduling queues, and various other scheduling parameters.
○ Accounting and business information: It includes the amount of CPU and
time utilities like real time used, job or process numbers, etc.
○ Memory-management information: This information includes the value of
the base and limit registers, the page, or segment tables. This depends on the
memory system, which is used by the operating system.
○ I/O status information: This block includes a list of open files, the list of I/O
devices that are allocated to the process, etc.
● Threads
○ What is a Thread?
■ A thread is a path of execution within a process.
■ A process can contain multiple threads.
○ Why Multithreading?
■ A thread is also known as a lightweight process.
■ The idea is to achieve parallelism by dividing a process into multiple
threads.
Operating System_Btech2019_PartTime_Semester2 5
● Process Creation
○ (i). When a new process is created, the operating system assigns a
unique Process Identifier (PID) to it and inserts a new entry in the
primary process table.
○ (ii). Then the required memory space for all the elements of the
process such as program, data and stack is allocated including space
for its Process Control Block (PCB).
○ (iii). Next, the various values in PCB are initialized such as,
■ Process identification part is filled with the PID assigned to it
in step (1) and also its parent’s PID.
■ The processor register values are mostly filled with zeroes,
except for the stack pointer and program counter.
■ Stack pointer is filled with the address of stack allocated to it
in step (ii) and program counter is filled with the address of its
program entry point.
■ The process state information would be set to ‘New’.
■ Priority would be lowest by default, but users can specify any
priority during creation.
■ In the beginning, the process is not allocated to any I/O devices
or files.
■ The user has to request them or if this is a child process it may
inherit some resources from its parent.
○ (vi). Then the operating system will link this process to the scheduling
queue and the process state would be changed from ‘New’ to ‘Ready’.
Now the process is competing for the CPU.
○ (v). Additionally, the operating system will create some other data
structures such as log files or accounting files to keep track of
processes activity.
● Process Termination:
○ Processes are terminated by themselves when they finish’1 executing
Operating System_Btech2019_PartTime_Semester2 7
their last statement, then the operating system USES exit( ) system call
to delete its context.
○ Then all the resources held by that process like physical and virtual
memory, 10 buffers, open files etc., are taken back by the operating
system.
○ A process P can be terminated either by the operating system or by the
parent process of P.
○ A parent may terminate a process due to one of the following reasons,
■ (i). When a task given to the child is not required now.
■ (ii). When a child has taken more resources than its limit.
■ (iii). The parent of the process is exiting, as a result all its
children are deleted. This is called cascaded termination.
○ Shared Memory:
■ Communication between processes using shared memory requires processes to share
some variable, and it completely depends on how the programmer will implement it.
■ One way of communication using shared memory can be imagined like this: Suppose
process1 and process2 are executing simultaneously, and they share some resources or
use some information from another process.
■ Process1 generates information about certain computations or resources being used
and keeps it as a record in shared memory.
■ When process2 needs to use the shared information, it will check in the record stored
in shared memory and take note of the information generated by process1 and act
accordingly.
■ Processes can use shared memory for extracting information as a record from another
process as well as for delivering any specific information to other processes.
■
Operating System_Btech2019_PartTime_Semester2 9
● Establish a communication link (if a link already exists, no need to establish it again.)
● Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destination) or send(message)
– receive(message, host) or receive(message)
●
● Here are different ways to communicate using message passing:
○ 1. Direct or Indirect communication
○ 2. Synchronous or asynchronous communication
○ 3. Automatic or explicit buffering
● 1. Direct or Indirect communication
○ Under Direct communication, processes must explicitly name the sender or
receiver in the communication.
○ The send() and receive() are defined as:
■ send(P, message) – send a message to process P
■ receive(Q, message) – receive a message from process Q
○ The communication link in direct communication has the following properties:
■ The link is established automatically. Processes need each other’s
identity to send messages.
■ A link is associated with exactly two processes.
■ Between two processes, there is only one link.
○ There is a symmetry in addressing. In asymmetrical addressing, the sending
process needs to address the receiver, but the recipient does not need to
address the sender.
Operating System_Btech2019_PartTime_Semester2 11
○
○ The disadvantage in symmetric and asymmetric schemes is in changing the
identifier of a process.
○ We need to change all the other process definitions and references to the old
identifier and replace it with the new one.
○ In indirect communication, messages are sent and received from mailboxes
or ports.
○ The processes can place messages into a mailbox or remove messages from
them.
○ The mailbox has a unique identification.
○ Two processes can communicate only if they have a shared mailbox.
■ send(A, message) – send a message to mailbox A
■ receive(A, message) – receive a message from mailbox A
○ In this scheme, a communication link has the following properties:
■ 1. A link is established between a pair of processes only if both have
the same shared mailbox.
■ 2. A link may be associated with more than two processes.
■ 3. There may be different links, with each link corresponding to one
mailbox, between pairs of communicating processes.
○
Operating System_Btech2019_PartTime_Semester2 12
● Process Scheduling
○ The act of determining which process is in the ready state, and should be
moved to the running state is known as Process Scheduling.
○ The prime aim of the process scheduling system is to keep the CPU busy all
the time and to deliver minimum response time for all programs.
○ For achieving this, the scheduler must apply appropriate rules for swapping
processes IN and OUT of the CPU.
○ Scheduling fell into one of the two general categories:
■ Non Preemptive Scheduling: When the currently executing process
Operating System_Btech2019_PartTime_Semester2 13
■
■ 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.
■ A process continues this cycle until it terminates, at which time it is
removed from all queues and has its PCB and resources deallocated.
○ Types of Schedulers
■ There are three types of schedulers available:
● Long Term Scheduler
● Short Term Scheduler
● Medium Term Scheduler
Operating System_Btech2019_PartTime_Semester2 14
○ Scheduling Criteria
■ CPU utilisation –
● The main objective of any CPU scheduling algorithm is to keep
the CPU as busy as possible.
● Theoretically, CPU utilisation can range from 0 to 100 but in a
real-time system,
● it varies from 40 to 90 percent depending on the load upon the
system.
■ Throughput –
● A measure of the work done by CPU is the number of
processes being executed and completed per unit time.
● This is called throughput.
● The throughput may vary depending upon the length or
duration of processes.
■ Turnaround time –
● For a particular process, an important criteria is how long it
takes to execute that process.
Operating System_Btech2019_PartTime_Semester2 16
3. Maximum throughput
7.
Operating System_Btech2019_PartTime_Semester2 17
4. Priority Scheduling
● FCFS Scheduling
○ First come first serve (FCFS) scheduling algorithm simply schedules the jobs
according to their arrival time.
○ The job which comes first in the ready queue will get the CPU first.
○ The lesser the arrival time of the job, the sooner the job will get the CPU.
○ FCFS scheduling may cause the problem of starvation if the burst time of the
first process is the longest among all the jobs.
○ Advantages of FCFS
■ Simple
■ Easy
■ First come, First serv
○ Disadvantages of FCFS
■ The scheduling method is non preemptive, the process will run to
completion
■ Due to the non-preemptive nature of the algorithm, the problem of
starvation may occur.
■ Although it is easy to implement, it is poor in performance since the
average waiting time is higher as compared to other scheduling
algorithms.
○ Example
■ Let's take an example of The FCFS scheduling algorithm. In the
Following schedule, there are 5 processes with process ID P0, P1, P2,
P3 and P4. P0 arrives at time 0, P1 at time 1, P2 at time 2, P3 arrives at
time 3 and Process P4 arrives at time 4 in the ready queue. The
processes and their respective Arrival and Burst time are given in the
following table.
● The Turnaround time and the waiting time are calculated by
using the following formula.
1. Turn Around Time = Completion Time - Arrival Time
● Solution:
■
■ Since, No Process arrives at time 0 hence; there will be an empty slot
in the Gantt chart from time 0 to 1 (the time at which the first process
arrives).
■ According to the algorithm, the OS schedules the process which is
having the lowest burst time among the available processes in the
ready queue.
■ Till now, we have only one process in the ready queue hence the
scheduler will schedule this to the processor no matter what is its burst
time.
■ This will be executed till 8 units of time.
■ Till then we have three more processes arriving in the ready queue
hence the scheduler will choose the process with the lowest burst time.
■ Among the processes given in the table, P3 will be executed next since
it is having the lowest burst time among all the available processes.
■ So that's how the procedure will go on in the shortest job first (SJF)
scheduling algorithm.
■
■ Avg Waiting Time = 27/5
○ The context of the process is saved in the Process Control Block when the
process is removed from the execution and the next process is scheduled.
○ This PCB is accessed on the next execution of this process.
○ Example
■ In this Example, there are five jobs P1, P2, P3, P4, P5 and P6. Their
arrival time and burst time are given below in the table.
■
■ Avg Waiting Time = 24/6
■ The Gantt chart is prepared according to the arrival and burst time
given in the table.
1. Since, at time 0, the only available process is P1 with CPU
therefore it is scheduled.
Till now, there are two processes available in the ready queue.
The OS has executed P1 for one unit of time till now; the
6. The Next Process P6 arrives at time unit 5, till this time, the
processes till now, that are P1 (7), P2 (3), P5 (3) and P6 (2).
scheduled. Since, now, all the processes are available hence the
till its completion and then the process with the least remaining
○ Disadvantages
1. The higher the time quantum, the higher the response time in the
system.
2. The lower the time quantum, the higher the context switching overhead
in the system.
system.
○ Example
■ In the following example, 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.
Operating System_Btech2019_PartTime_Semester2 24
■
■ Avg Waiting Time = (12+16+6+8+15+11)/6 = 76/6 units
● Priority Scheduling
○ In Priority scheduling, there is a priority number assigned to each process.
○ In some systems, the lower the number, the higher the priority.
○ While, in the others, the higher the number, the higher will be the priority.
○ The Process with the higher priority among the available processes is given
the CPU.
○ There are two types of priority scheduling algorithms.
○ One is Preemptive priority scheduling while the other is Non Preemptive
Priority scheduling.
○ The priority number assigned to each of the processes may or may not vary.
○ If the priority number doesn't change itself throughout the process, it is called
static priority,
○ while if it keeps changing itself at the regular intervals, it is called dynamic
priority.
○ Non Preemptive Priority Scheduling
■ In the Non Preemptive Priority scheduling, The Processes are
scheduled according to the priority number assigned to them.
■ Once the process gets scheduled, it will run till the completion.
■ Generally, the lower the priority number, the higher is the priority of
the process.
■ The people might get confused with the priority numbers, hence in the
GATE, they clearly mention which one is the highest priority and
which one is the lowest one.
■ Example
Operating System_Btech2019_PartTime_Semester2 25
● In the Example, there are 7 processes P1, P2, P3, P4, P5, P6
and P7. Their priorities, Arrival Time and burst time are given
in the table.
●
● Avg Waiting Time = (0+11+2+7+12+2+18)/7 = 52/7 units
●
○ Preemptive Priority Scheduling
■ In Preemptive Priority Scheduling, at the time of arrival of a process in
the ready queue, its Priority is compared with the priority of the other
processes present in the ready queue as well as with the one which is
being executed by the CPU at that point of time.
■ The One with the highest priority among all the available processes
will be given the CPU next.
■ The difference between preemptive priority scheduling and non
preemptive priority scheduling is that, in the preemptive priority
scheduling, the job which is being executed can be stopped at the
arrival of a higher priority job.
■ Once all the jobs get available in the ready queue, the algorithm will
behave as non-preemptive priority scheduling, which means the job
scheduled will run till the completion and no preemption will be done.
■ Example
● There are 7 processes P1, P2, P3, P4, P5, P6 and P7 given.
Their respective priorities, Arrival Times and Burst times are
given in the table below.
Operating System_Btech2019_PartTime_Semester2 26
●
● Avg Waiting Time = (0+14+0+7+1+25+16)/7 = 63/7 = 9 units