Unit III
Unit III
What is a Process?
A process is an active program, i.e., a program that is under execution. It is more than
the program code, as it includes the program counter, process stack, registers, program
code etc. Compared to this, the program code is only the text section.
When a computer program is triggered to execute, it does not run directly, but it first
determines the steps required for execution of the program, and following these steps of
execution is referred to as a process. An individual process takes its own memory space
and does not share this space with other processes.
Processes can be classified into two types namely – clone process and parent process.
A clone process, also called a child process, is one which is created by another process,
while the main process is one that is responsible for creating other processes to perform
multiple tasks at a time is called as parent process.
What is a Thread?
A thread is basically a subpart of a large process. In a process, all the threads within it are
interrelated to each other. A typical thread contains some information like data segment,
code segment, etc. This information is being shared to their peer threads during execution.
The most important feature of threads is that they share memory, data, resources, etc.
with their peer threads within a process to which they belong. Also, all the threads within
a process are required to be synchronized to avoid unexpected results.
The following table highlights the major differences between a process and a thread −
Treatment by All the different processes All user level peer threads
OS are treated separately by are treated as a single
the operating system. task by the operating
system.
Conclusion
The most significant difference between a process and a thread is that a process is defined
as a task that is being completed by the computer, whereas a thread is a lightweight
process that can be managed independently by a scheduler.
Preemptive Scheduling
Preemptive scheduling is used when a process switches from the running state to the
ready state or from the waiting state to the ready state. The resources (mainly CPU
cycles) are allocated to the process for a limited amount of time and then taken away,
and the process is again placed back in the ready queue if that process still has CPU
burst time remaining. That process stays in the ready queue till it gets its next chance
to execute.
Algorithms based on preemptive scheduling are Round Robin (RR), Shortest Remaining
Time First (SRTF), Priority (preemptive version), etc.
Non-preemptive scheduling has both advantages and disadvantages. The following are
non-preemptive scheduling’s benefits and drawbacks:
Advantages
1. It has a minimal scheduling burden.
2. It is a very easy procedure.
3. Less computational resources are used.
4. It has a high throughput rate.
Disadvantages
1. Its response time to the process is super.
2. Bugs can cause a computer to freeze up.
Comparison Chart
PREEMPTIVE NON-PREEMPTIVE
Parameter SCHEDULING SCHEDULING
It has overheads of
Overhead It does not have overheads.
scheduling the processes.
In preemptive
It is low in non preemptive
CPU Utilization scheduling, CPU
scheduling.
utilization is high.
Round Robin is a CPU scheduling algorithm where each process is cyclically assigned a
fixed time slot. It is the preemptive version of the First come First Serve CPU Scheduling
algorithm.
• Round Robin CPU Algorithm generally focuses on Time Sharing technique.
• The period of time for which a process or job is allowed to run in a pre-
emptive method is called time quantum.
• Each process or job present in the ready queue is assigned the CPU for that
time quantum, if the execution of the process is completed during that time,
then the process will end else the process will go back to the waiting
table and wait for its next turn to complete the execution.
Characteristics of Round Robin Algorithm
• It is simple, easy to implement, and starvation-free as all processes get a
fair share of CPU.
• One of the most commonly used techniques in CPU scheduling is a core.
• It is preemptive as processes are assigned CPU only for a fixed slice of time
at most.
• The disadvantage of it is more overhead of context switching.
Example-1: Consider the following table of arrival time and burst time for four
processes P1, P2, P3, and P4 and given Time Quantum = 2
P1 5 ms 0 ms
P2 4 ms 1 ms
Process Burst Time Arrival Time
P3 2 ms 2 ms
P4 1 ms 4 ms
The Round Robin CPU Scheduling Algorithm will work on the basis of steps as
mentioned below:
At time = 0,
• The execution begins with process P1, which has burst time 5.
• Here, every process executes for 2 milliseconds (Time Quantum Period).
P2 and P3 are still in the waiting queue.
Read Initia
Time y Runnin l Remainin
Instanc Proces Arriva Queu g Executio Burst g Burst
e s l Time e Queue n Time Time Time
At time = 2,
• The processes P1 and P3 arrives in the ready queue and P2 starts executing
for TQ period
Read Initia
Time y Runnin l Remainin
Instanc Proces Arriva Queu g Executio Burst g Burst
e s l Time e Queue n Time Time Time
At time = 4,
• The process P4 arrives in the ready queue,
• Then P3 executes for TQ period.
Read Initia
Time y Runnin l Remainin
Instanc Proces Arriva Queu g Executio Burst g Burst
e s l Time e Queue n Time Time Time
P1,
P3 2ms P4, P2 2ms 2ms 0ms
At time = 6,
• Process P3 completes its execution
• Process P1 starts executing for TQ period as it is next in the b.
Read Initia
Time y Runnin l Remainin
Instanc Proces Arriva Queu g Executio Burst g Burst
e s l Time e Queue n Time Time Time
(Non Preemptive)
What is First Come First Serve Scheduling?
The First come first serve scheduling algorithm is non-preemptive in nature i.e, if a
process is already running, then it is not interrupted by another process until the
currently running process is executed completely
P1 0 4
Processes Arrival Time Burst Time
P2 1 3
P3 2 1
P4 3 2
P5 4 5
The First come First serve CPU Scheduling Algorithm will work on the basis of steps
as mentioned below:
Step 0: At time = 0,
• The process begins with P1
• As it has an arrival time 0
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
Step 1: At time = 1,
• The process P2 arrives
• But process P1 still executing,
• Thus, P2 is kept on a waiting table and waits for its execution.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
1-2ms
Step 3: At time = 2,
• The process P3 arrives and kept in a waiting queue
• While process P1 is still executing as its burst time is 4.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
Step 4: At time = 3,
• The process P4 arrives and kept in the waiting queue
• While process P1 is still executing as its burst time is 4
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
3-4ms
P3 2ms P2, P3 0ms 1ms 1ms
P2, P3,
P4 3ms 0ms 2ms 2ms
P4
Step 5: At time = 4,
• The process P1 completes its execution
• Process P5 arrives in waiting queue while process P2 starts executing
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
4-5ms
P4 3ms P3, P4 0ms 2ms 2ms
P3, P4,
P5 4ms 0ms 5ms 5ms
P5
Advantages of FCFS
Easy to implement
It is well suited for batch systems where the longer time periods for each process are
often acceptable.
Disadvantages of FCFS
As it is a Non-preemptive CPU Scheduling Algorithm, hence it will run till it finishes the
execution.
The average waiting time in the FCFS is much higher than in the others
Processes that are at the end of the queue, have to wait longer to finish.
It is not suitable for time-sharing operating systems where each process should get the
same amount of CPU time.
Shortest Job First (SJF) is an algorithm in which the process having the smallest
execution time is chosen for the next execution. This scheduling method can be
preemptive or non-preemptive. It significantly reduces the average waiting time for
other processes awaiting execution. The full form of SJF is Shortest Job First.
Non-Preemptive SJF
Preemptive SJF
Characteristics of SJF Scheduling
Non-Preemptive SJF
In non-preemptive scheduling, once the CPU cycle is allocated to process, the process
holds it till it reaches a waiting state or terminated.
Consider the following five processes each having its own unique burst time and arrival
time.
Step 3) At time = 3, process P4 will finish its execution. The burst time of P3 and P1 is
compared. Process P1 is executed because its burst time is less compared to P3.
Step 4) At time = 4, process P5 arrives and is added to the waiting queue. P1 will
continue execution.
Step 5) At time = 5, process P2 arrives and is added to the waiting queue. P1 will
continue execution.
Step 6) At time = 9, process P1 will finish its execution. The burst time of P3, P5, and
P2 is compared. Process P2 is executed because its burst time is the lowest.
Step 8) At time = 11, process P2 will finish its execution. The burst time of P3 and P5 is
compared. Process P5 is executed because its burst time is lower.
Step 9) At time = 15, process P5 will finish its execution.
Advantages of SJF
It reduces the average waiting time over FIFO (First in First Out) algorithm.
SJF method gives the lowest average waiting time for a specific set of processes.
It is appropriate for the jobs running in batch, where run times are known in advance.
For the batch system of long-term scheduling, a burst time estimate can be obtained
from the job description.
For Short-Term Scheduling, we need to predict the value of the next burst time.
Disadvantages/Cons of SJF
It leads to the starvation that does not reduce average turnaround time.
Elapsed time should be recorded, that results in more overhead on the processor.
Summary
SJF is an algorithm in which the process having the smallest execution time is chosen for
the next execution.
This algorithm method is helpful for batch-type processing, where waiting for jobs to
complete is not critical.
There are basically two types of SJF methods 1) Non-Preemptive SJF and 2) Preemptive
SJF.
In non-preemptive scheduling, once the CPU cycle is allocated to process, the process
holds it till it reaches a waiting state or terminated.
In Preemptive SJF Scheduling, jobs are put into the ready queue as they come.
Although a process with short burst time begins, the current process is removed or
preempted from execution, and the job which is shorter is executed 1st.
It reduces the average waiting time over FIFO (First in First Out) algorithm.
In SJF scheduling, Job completion time must be known earlier, but it is hard to predict.
SJF can’t be implemented for CPU scheduling for the short term. It is because there is no
specific method to predict the length of the upcoming CPU burst.