You are on page 1of 55

Acropolis Institute of Technology &

Research, Indore
www.acropolis.in
Operating System
By: Nupur Agrawal
What is a Program
❖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.
❖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.
❖Eaxmple- a C program or Java Program

18 March 2024 nupuragrawal@acropolis.in 3


What is a Process?
❖A process is basically a program in execution. The execution of a
process must progress in a sequential fashion.
❖A process is defined as an entity which represents the basic unit of
work to be implemented in the system.
❖To put it in simple terms, we write our computer programs in a text
file and when we execute this program, it becomes a process which
performs all the tasks mentioned in the program.

18 March 2024 nupuragrawal@acropolis.in 4


How a process work
❖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.

18 March 2024 nupuragrawal@acropolis.in 5


18 March 2024 nupuragrawal@acropolis.in 6
Process States

18 March 2024 nupuragrawal@acropolis.in 7


What is Process State

❖The process, from its creation to completion, passes through various


states. The minimum number of states is five.
❖The names of the states are not standardized although the process
may be in one of the following states during execution.

18 March 2024 nupuragrawal@acropolis.in 8


States of 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 put 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.

18 March 2024 nupuragrawal@acropolis.in 9


Cont..
❖Running
❖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.

18 March 2024 nupuragrawal@acropolis.in 10


Cont..
❖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 move this process to the block or
wait state and assigns the CPU to the other processes.

18 March 2024 nupuragrawal@acropolis.in 11


Cont..
❖ 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 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 suspend ready state.
❖ If the main memory is full and a higher priority process comes for the
execution then the OS have to make the room for the process in the main
memory by throwing the lower priority process out into the secondary
memory. The suspend ready processes remain in the secondary memory
until the main memory gets available.

18 March 2024 nupuragrawal@acropolis.in 12


Cont…
❖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
make room for the higher priority process. These processes complete
their execution once the main memory gets available and their wait
is finished.

18 March 2024 nupuragrawal@acropolis.in 13


CPU and IO Bound Processes
❖If the process is intensive in terms of CPU operations then it is called
CPU bound process. Similarly, If the process is intensive in terms of
I/O operations then it is called IO bound process.
❖.

18 March 2024 nupuragrawal@acropolis.in 14


Types of schedulers
1. Long term scheduler–
❖ Long term scheduler is also known as job scheduler. It chooses the
processes from the pool (secondary memory) and keeps them in the ready
queue maintained in the primary memory.
❖ Long Term scheduler mainly controls the degree of Multiprogramming. The
purpose of long term scheduler is to choose a perfect mix of IO bound and
CPU bound processes among the jobs present in the pool.
❖ If the job scheduler chooses more IO bound processes then all of the jobs
may reside in the blocked state all the time and the CPU will remain idle
most of the time. This will reduce the degree of Multiprogramming.
Therefore, the Job of long term scheduler is very critical and may affect the
system for a very long time.
18 March 2024 nupuragrawal@acropolis.in 15
Short term scheduler
❖Short term scheduler is also known as CPU scheduler. It selects one
of the Jobs from the ready queue and dispatch to the CPU for the
execution.
❖A scheduling algorithm is used to select which job is going to be
dispatched for the execution. The Job of the short term scheduler
can be very critical in the sense that if it selects job whose CPU burst
time is very high then all the jobs after that, will have to wait in the
ready queue for a very long time.
❖This problem is called starvation which may arise if the short term
scheduler makes some mistakes while selecting the job.
18 March 2024 nupuragrawal@acropolis.in 16
Medium term scheduler
❖Medium term scheduler takes care of the swapped out processes.If
the running state processes needs some IO time for the completion
then there is a need to change its state from running to waiting.
❖Medium term scheduler is used for this purpose. It removes the
process from the running state to make room for the other
processes. Such processes are the swapped out processes and this
procedure is called swapping. The medium term scheduler is
responsible for suspending and resuming the processes.
❖It reduces the degree of multiprogramming. The swapping is
necessary to have a perfect mix of processes in the ready queue.
18 March 2024 nupuragrawal@acropolis.in 17
Various Times related to the Process
1. Arrival Time
❖The time at which the process enters into the ready queue is called
the arrival time.
2. Burst Time
❖The total amount of time required by the CPU to execute the whole
process is called the Burst Time.
3. Completion Time
❖The Time at which the process enters into the completion state or
the time at which the process completes its execution, is called
completion time.

18 March 2024 nupuragrawal@acropolis.in 18


Cont..
4. Turnaround time
❖The total amount of time spent by the process from its arrival to its
completion, is called Turnaround time.
5. Waiting Time
❖The Total amount of time for which the process waits for the CPU to
be assigned is called waiting time.
6. Response Time
❖The difference between the arrival time and the time at which the
process first gets the CPU is called Response Time.

18 March 2024 nupuragrawal@acropolis.in 19


CPU Scheduling
❖In Multiprogramming systems, the Operating system schedules the
processes on the CPU to have the maximum utilization of it and this
procedure is called CPU scheduling. The Operating System uses
various scheduling algorithm to schedule the processes.
❖This is a task of the short term scheduler to schedule the CPU for the
number of processes present in the Job Pool. Whenever the running
process requests some IO operation then the short term scheduler
saves the current context of the process (also called PCB) and
changes its state from running to waiting. During the time, process is
in waiting state; the Short term scheduler picks another process from
the ready queue and assigns the CPU to this process. This procedure
is called context switching.
18 March 2024 nupuragrawal@acropolis.in 20
Why do we need Scheduling?
❖In Multiprogramming, if the long term scheduler picks more I/O
bound processes then most of the time, the CPU remains idol. The
task of Operating system is to optimize the utilization of resources.
❖If most of the running processes change their state from running to
waiting then there may always be a possibility of deadlock in the
system. Hence to reduce this overhead, the OS needs to schedule the
jobs to get the optimal utilization of CPU and to avoid the possibility
to deadlock.

18 March 2024 nupuragrawal@acropolis.in 21


What is saved in the Process Control Block?
❖The Operating system maintains a process control block during the
lifetime of the process. The Process control block is deleted when the
process is terminated or killed. There is the following information
which is saved in the process control block and is changing with the
state of the process.

18 March 2024 nupuragrawal@acropolis.in 22


❖ PID (Process ID): Identifies a unique process; used by the operating system to manage and track
processes.
❖ Program Counter (PC): Stores the address of the next instruction to be executed by the process.
❖ Status (S): Indicates the current state of the process (e.g., running, waiting, terminated).
❖ CPU Registers: Stores the contents of CPU registers for the process during execution.
❖ Memory Limits: Defines the memory boundaries allocated to the process for execution.
❖ Priority: Specifies the priority level assigned to the process for CPU scheduling.
❖ Owner Information: Contains details about the user or system account that owns the process.
❖ File Pointers: Records the file descriptors and pointers associated with open files.
❖ Process Accounting Information: Logs resource usage statistics such as CPU time, memory
consumption, and I/O activity.
❖ Signals: Tracks the signals received by the process, indicating events or notifications from the
operating system or other processes.

18 March 2024 nupuragrawal@acropolis.in 23


Scheduling Algorithms
❖There are various algorithms which are used by the Operating System
to schedule the processes on the processor in an efficient way.
❖There are some popular process scheduling algorithms which we are
going to discuss
❖First-Come, First-Served (FCFS) Scheduling
❖Shortest-Job-Next (SJN) Scheduling
❖Priority Scheduling
❖Shortest Remaining Time
❖Round Robin(RR) Scheduling

18 March 2024 nupuragrawal@acropolis.in 24


Non-preemptive or preemptive
❖These algorithms are either non-preemptive or preemptive.
Non-preemptive algorithms are designed so that once a process
enters the running state, it cannot be preempted until it completes
its allotted time, whereas the preemptive scheduling is based on
priority where a scheduler may preempt a low priority running
process anytime when a high priority process enters into a ready
state.

18 March 2024 25
Preemptive Scheduling Non-preemptive Scheduling
Description Operating system can interrupt processes Processes continue execution until
and allocate CPU to others. completion or voluntary release.

Interruption Processes can be interrupted during Processes are not interrupted once they
execution. start executing.

CPU Allocation CPU can be allocated to higher-priority Processes execute until completion,
processes if available. regardless of priorities.

Fairness Ensures fair resource allocation and Simplifies scheduling but may lead to
responsiveness. lower system responsiveness.

Examples Round Robin, Shortest Remaining Time First First-Come, First-Served (FCFS), Shortest
(SRTF), Priority Scheduling Job Next (SJN/SJF)

18 March 2024 26
First Come First Serve (FCFS)
❖ In the "First come first serve" scheduling algorithm, as the name suggests,
the process which arrives first, gets executed first, or we can say that the
process which requests the CPU first, gets the CPU allocated first.
❖ First Come First Serve, is just like FIFO(First in First out) Queue data
structure, where the data element which is added to the queue first, is the
one who leaves the queue first.
❖ This is used in Batch Systems.
❖ It's easy to understand and implement programmatically, using a Queue
data structure, where a new process enters through the tail of the queue,
and the scheduler selects process from the head of the queue.
❖ A perfect real life example of FCFS scheduling is buying tickets at ticket
counter.
18 March 2024 27
Advantages of FCFS

❖Simple
❖Easy
❖First come, First serve

18 March 2024 28
Disadvantages of FCFS
❖The scheduling method is non preemptive, the process will run to the
completion.
❖Due to the non-preemptive nature of the algorithm, the problem of
starvation may occur.
❖Although it is easy to implement, but it is poor in performance since
the average waiting time is higher as compare to other scheduling
algorithms.

18 March 2024 29
18 March 2024 30
Implementation
❖Input the number of process with their burst time.
❖Find Waiting time(WT) for all the process.
❖WT[i] = BT[i-1] +WT[i-1]
❖Find turn around time
TAT=WT+BT ( for a the process)
❖Find average WT
AWT=total WT/Total no. of process.
❖Find average TAT
avg TAT = total TAT/ total no. of process

18 March 2024 nupuragrawal@acropolis.in 31


Convoy Effect in FCFS
❖FCFS may suffer from the convoy effect if the burst time of the first
job is the highest among all. As in the real life, if a convoy is passing
through the road then the other persons may get blocked until it
passes completely. This can be simulated in the Operating System
also.
❖If the CPU gets the processes of the higher burst time at the front
end of the ready queue then the processes of lower burst time may
get blocked which means they may never get the CPU if the job in
the execution has a very high burst time. This is called convoy
effect or starvation.

18 March 2024 nupuragrawal@acropolis.in 32


Example

18 March 2024 nupuragrawal@acropolis.in 33


Starvation and Convoy effect might sound similar but
are two different things.

❖Convoy Effect is phenomenon associated with the First Come First


Serve (FCFS) algorithm, in which the whole Operating System slows
down due to few slow processes.
❖Starvation occurs when a process waits for an indefinite period of
time to get the resource it requires(usually occurs in Priority Queue).

18 March 2024 nupuragrawal@acropolis.in 34


Numerical
Arrival Time
Process (AT) Burst Time (BT)

P1 0 5

P2 1 3

P3 2 8

P4 3 6

18 March 2024 nupuragrawal@acropolis.in 35


Shortest Job First or Shortest Remaining Time First
❖Out of all available process, CPU is assigned to the process having
smallest burst time requirement.(no priority, no seniority)
❖If there is a tie, FCFS is used to break tie.
❖Can be used with both preemptive and non-preemptive approach.
❖In non-preemptive scheduling, once the CPU cycle is allocated to
process, the process holds it till it reaches a waiting state or
terminated.

18 March 2024 nupuragrawal@acropolis.in 36


18 March 2024 nupuragrawal@acropolis.in 37
Advantages

❖SJF is frequently used for long term scheduling.


❖It reduces the average waiting time over FIFO algorithm.

18 March 2024 nupuragrawal@acropolis.in 38


Disadvantages
❖Job Completion time or Burst Time must be known earlier but it is
hard to predict.
❖It leads to starvation.

18 March 2024 nupuragrawal@acropolis.in 39


Shortest Remaining Time First(preemptive)
❖It is a preemptive version of SJF.
❖In SRTF, the execution of the process can be stopped after certain
amount of time, if the process with least burst time occurs.
❖OR
❖If a process with even a shorter burst time arrives, the current
process is removed or preempted from execution, and the shorter
job is allocated CPU cycle.

18 March 2024 nupuragrawal@acropolis.in 40


18 March 2024 nupuragrawal@acropolis.in 41
LJF(Longest Job First Scheduling)
❖The LJF scheduling algorithm is just as the SJF. The only difference is
that The SJF scheduling algorithm executes the processes with the
shortest burst time first, whereas, in LJF, the processes with the
longest burst time are executed first. However, LJF is not considered
to be an efficient way of process scheduling because there are many
drawbacks of it like the Convoy effect is displayed by it, it has very
large average turn-around time and average waiting time. This results
in slow processing and decreases the effectiveness of the system.

18 March 2024 nupuragrawal@acropolis.in 42


18 March 2024 nupuragrawal@acropolis.in 43
18 March 2024 nupuragrawal@acropolis.in 44
18 March 2024 nupuragrawal@acropolis.in 45
18 March 2024 nupuragrawal@acropolis.in 46
Priority Based Scheduling
❖A CPU algorithm that schedules processes based on priority.
❖It used in Operating systems for performing batch processes.
❖If two jobs having the same priority are READY, it works on a FIRST
COME, FIRST SERVED basis.
❖In priority scheduling, a number is assigned to each process that
indicates its priority level.
❖Lower the number, higher is the priority.
❖In this type of scheduling algorithm, if a newer process arrives, that is
having a higher priority than the currently running process, then the
currently running process is preempted.

18 March 2024 nupuragrawal@acropolis.in 47


Example

18 March 2024 nupuragrawal@acropolis.in 48


Gantt Chart

18 March 2024 nupuragrawal@acropolis.in 49


18 March 2024 nupuragrawal@acropolis.in 50
Round Robin Scheduling
❖ Round robin is a pre-emptive algorithm
❖ The CPU is shifted to the next process after fixed interval time, which is
called time quantum/time slice.
❖ The process that is preempted is added to the end of the queue.
❖ Round robin is a hybrid model which is clock-driven
❖ Time slice should be minimum, which is assigned for a specific task that
needs to be processed. However, it may differ OS to OS.
❖ It is a real time algorithm which responds to the event within a specific
time limit.
❖ Round robin is one of the oldest, fairest, and easiest algorithm.
❖ Widely used scheduling method in traditional OS.
18 March 2024 nupuragrawal@acropolis.in 51
18 March 2024 nupuragrawal@acropolis.in 52
Q&A
18 March 2024 nupuragrawal@acropolis.in 53
Acknowledgement and Disclaimer
❖ This presentation is the culmination of content extracted from a variety of sources namely
lecture notes, presentations, you tube lecture, books and websites. In many cases, the
identity of the original contributor is not available with the author. The author wish to take
this opportunity to thank individual contributors of these extracts.
❖ The author wishes to record that this presentation is solely used for education purpose and
copyright violation if any, is unintentional. The author is willing to remove any such extract
from the presentation with immediate effect.
❖ The materials available at referred sites are not maintained by Acropolis Institute and we are
not responsible for the contents thereof.
❖ All trademarks, service marks, and trade names used in this presentation are the marks of
the respective owner(s).

18 March 2024 nupuragrawal@acropolis.in 54


THANKS
18 March 2024 nupuragrawal@acropolis.in 55

You might also like