Unit 2 Os
Unit 2 Os
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.
● 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.
1. New
A program which is going to be picked up by the OS into the main memory is called a new process.
2. 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.
3. 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.
4. 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.
5. 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.
6. 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.
7. 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.
Process Schedulers
In the Operating System, CPU schedulers are used to handle the scheduling of various processes that are coming for its
execution by the CPU. Schedulers are responsible for transferring a process from one state to the other. Basically, we have
three types of schedulers i.e.
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.
Short term or CPU scheduler :
It is responsible for selecting one process from ready state for scheduling it on the running state. Note: Short-
term scheduler only selects the process to schedule it doesn’t load the process on running. Here is when all the
scheduling algorithms are used. The CPU scheduler is responsible for ensuring there is no starvation owing to
high burst time processes.
Dispatcher is responsible for loading the process selected by Short-term scheduler on the CPU (Ready to
Running State) Context switching is done by dispatcher only
Sometimes, you need to send the running process to the ready state or to the wait/block state. For example, in the round-
robin process, after a fixed time quantum, the process is again sent to the ready state from the running state. So, these things
are done with the help of Medium-Term schedulers.
Medium-term schedulers are those schedulers whose decision will have a mid-term effect on the performance of the system.
It is responsible for swapping of a process from the Main Memory to Secondary Memory and vice-versa.
It is helpful in maintaining a perfect balance between the I/O bound and the CPU bound. It reduces the degree of
multiprogramming.
The following diagram will give a brief about the working of the medium-term schedulers.
Process control block (PCB) is a data structure which is associated with any process and provides all the complete
information about that process.It is very important for process management as the data structuring for processes is done in
terms of the PCB. It also defines the current state of the operating system.
The following are the various components that are associated with the process control block PCB:
1. Process ID:
In computer system there are various process running simultaneously and each process has its unique ID. This Id helps
system in scheduling the processes. This Id is provided by the process control block.
In other words, it is an identification number that uniquely identifies the processes of computer system.
2. Process state:
As we know that the process state of any process can be New, running, waiting, executing, blocked, suspended, terminated.
Process control block is used to define the process state of any process.
In other words, process control block refers the states of the processes.
3. Program counter:
Program counter is used to point to the address of the next instruction to be executed in any process. This is also managed by
the process control block.
4. Register Informat These are the CPU registers which includes: accumulator, base, registers and general purpose
registers.:.
5. Scheduling information:
Scheduling information is used to set the priority of different processes. This is very useful information which is set by the
process control block. In computer system there were many processes running simultaneously and each process have its
priority. The priority of primary feature of RAM is higher than other secondary features. Scheduling information is very
useful in managing any computer system.
7. Accounting information:
This section of process control block stores the details relate to central processing unit (CPU) utilization and execution time
of a process.
The Context switching is a technique or method used by the operating system to switch a process
from one state to another to execute its function using CPUs in the system. When switching
perform in the system, it stores the old running process's status in the form of registers and
assigns the CPU to a new process to execute its tasks. While a new process is running in the
system, the previous process must wait in a ready queue. The execution of the old process starts
at that point where another process stopped it. It defines the characteristics of a multitasking
operating system in which multiple processes shared the same CPU to perform multiple tasks
without the need for additional processors in the system.
CPU Scheduling is a process of determining which process will own CPU for execution while another process is on hold.
The main task of CPU scheduling is to make sure that whenever the CPU remains idle, the OS at least select one of the
processes available in the ready queue for execution. The selection process will be carried out by the CPU scheduler. It
selects one of the processes in memory that are ready for execution.
Preemptive Scheduling
In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is important to run a task
with a higher priority before another lower priority task, even if the lower priority task is still running. The lower priority
task holds for some time and resumes when the higher priority task finishes its execution.
Non-Preemptive Scheduling
In this type of scheduling method, the CPU has been allocated to a specific process. The process that keeps the CPU
busy will release the CPU either by switching context or terminating. It is the only method that can be used for various
hardware platforms. That's because it doesn't need special hardware (for example, a timer) like preemptive scheduling.
Maximize:
CPU utilization: CPU utilization is the main task in which the operating system needs to make sure that CPU remains as
busy as possible. It can range from 0 to 100 percent. However, for the RTOS, it can be range from 40 percent for low-level
and 90 percent for the high-level system.
Throughput: The number of processes that finish their execution per unit time is known Throughput. So, when the CPU is
busy executing the process, at that time, work is being done, and the work completed per unit time is called Throughput.
Minimize:
Waiting time: Waiting time is an amount that specific process needs to wait in the ready queue.
Response time: It is an amount to time in which the request was submitted until the first response is produced.
Turnaround Time: Turnaround time is an amount of time to execute a specific process. It is the calculation of the total time
spent waiting to get into the memory, waiting in the queue and, executing on the CPU. The period between the time of
process submission to the completion time is the turnaround time.
Interval Timer
Timer interruption is a method that is closely related to preemption. When a certain process gets the CPU allocation, a timer
may be set to a specified interval. Both timer interruption and preemption force a process to return the CPU before its CPU
burst is complete.
Arrival Time: Time at which the process arrives in the ready queue.
Completion Time: Time at which process completes its execution.
Burst Time: Time required by a process for CPU execution.
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
Response time
Response time is the time spent when the process is in the ready state and gets the CPU for the first time.
FCFS Scheduling
1. First come first serve (FCFS) scheduling algorithm simply schedules the jobs according to their arrival time. The
scheduling method is non preemptive, the process will run to the completion.
The job which comes first in the ready queue will get the CPU first. The lesser the arrival time of the job, the sooner will
the job 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
o Simple
o Easy
o First come, First serv
Disadvantages of FCFS
2. Due to the non-preemptive nature of the algorithm, the problem of starvation may occur.
3. 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.
Example
Ques.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.e -
0 0 2
1 1 6
2 2 4
3 3 9
4 4 12
Process ID Arrival Time Burst Time Completion Time Turn Around Waiting Time
Time
0 0 2 2 2 0
1 1 6 8 7 1
2 2 4 12 10 4
3 3 9 21 18 9
4 4 12 33 29 17
Ques 2.
process Burst time Arrival time
P1 6 2
P2 3 5
P3 8 1
P4 3 0
P5 4 4
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 .
Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
P1 3 4
P2 5 3
P3 0 2
P4 5 1
P5 4 3
SJF Scheduling-
• Out of all the available processes, CPU is assigned to the process having smallest burst time.
• In case of a tie, it is broken by FCFS Scheduling.
• SJF Scheduling can be used in both preemptive and non-preemptive mode.
• Preemptive mode of Shortest Job First is called as Shortest Remaining Time First (SRTF).
Consider the set of 5 processes whose arrival time and burst time are given below-
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
If the CPU scheduling policy is SJF non-preemptive, calculate the average waiting time and average turn around time.
Now, we know-
Consider the set of 6 processes whose arrival time and burst time are given below-
P1 0 7
P2 1 5
P3 2 3
P4 3 1
P5 4 2
P6 5 1
If the CPU scheduling policy is shortest remaining time first, calculate the average waiting time and average turn
around time.
Solution-
Gantt Chart-
Now, we know-
● Turn Around time = Exit time – Arrival time
P1 19 19 – 0 = 19 19 – 7 = 12
P2 13 13 – 1 = 12 12 – 5 = 7
P3 6 6–2=4 4–3=1
P4 4 4–3=1 1–1=0
P5 9 9–4=5 5–2=3
P6 7 7–5=2 2–1=1
Now,
In the above example, every process will be given 2ms in one turn because we have taken the time quantum to be 2ms. So
process P1 will be executed for 2ms, then process P2 will be executed for 2ms, then P3 will be executed for 2 ms. Again
process P1 will be executed for 2ms, then P2, and so on. The waiting time and turnaround time of the processes will be:
---------------------------------------------
| Process | Waiting Time | Turnaround Time |
---------------------------------------------
| P1 | 13ms | 23ms |
| P2 | 10ms | 15ms |
| P3 | 13ms | 21ms |
---------------------------------------------
Total waiting time: (13 + 10 + 13) = 36ms
Average waiting time: (36/3) = 12ms
● No starvation will be there in round-robin because every process will get chance for its execution.
Disadvantages of round-robin:
● We have to perform a lot of context switching here, which will keep the CPU idle
In Priority Scheduling,
● Out of all the available processes, CPU is assigned to the process having the highest priority.
Preemptive Scheduling
In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is important to run a task with a
higher priority before another lower priority task, even if the lower priority task is still running. The lower priority task holds
for some time and resumes when the higher priority task finishes its execution. Problem-02:
Consider the set of 5 processes whose arrival time and burst time are given below-
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
If the CPU scheduling policy is priority preemptive, calculate the average waiting time and average turn around time.
(Higher number represents higher priority)
Solution-
Gantt Chart-
Now, we know-
P1 15 15 – 0 = 15 15 – 4 = 11
P2 12 12 – 1 = 11 11 – 3 = 8
P3 3 3–2=1 1–1=0
P4 8 8–3=5 5–5=0
P5 10 10 – 4 = 6 6–2=4
Now,
Non-Preemptive Scheduling
In this type of scheduling method, the CPU has been allocated to a specific process. The process that keeps the CPU busy,
will release the CPU either by switching context or terminating. It is the only method that can be used for various hardware
platforms. That's because it doesn't need special hardware (for example, a timer) like preemptive scheduling.
Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time and average turn around time.
(Higher number represents higher priority)
Solution-
Gantt Chart-
Now, we know-
P2 15 15 – 1 = 14 14 – 3 = 11
P3 12 12 – 2 = 10 10 – 1 = 9
P4 9 9–3=6 6–5=1
P5 11 11 – 4 = 7 7–2=5
Now,
● 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.
● 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.
In HRRN Scheduling,
● Out of all the available processes, CPU is assigned to the process having highest response ratio.
Response Ratio (RR) for any process is calculated by using the formula-
where-
Problem-
Consider the set of 5 processes whose arrival time and burst time are given below-
P0 0 3
P1 2 6
P2 4 4
P3 6 5
P4 8 2
If the CPU scheduling policy is Highest Response Ratio Next, calculate the average waiting time and average turn around
time.
Solution-
Step-01:
Step-03:
● The process having the highest response ratio will be executed next.
Since process P2 has the highest response ratio, so process P2 executes till completion.
Step-04:
● The process having the highest response ratio will be executed next.
Since process P4 has the highest response ratio, so process P4 executes till completion.
Step-05:
Now, we know-
P0 3 3–0=3 3–3=0
P1 9 9–2=7 7–6=1
P2 13 13 – 4 = 9 9–4=5
P3 20 20 – 6 = 14 14 – 5 = 9
P4 15 15 – 8 = 7 7–2=5
Now,
●
Average Turn Around time = (3 + 7 + 9 + 14 + 7) / 5 = 40 / 5 = 8 units
It may happen that processes in the ready queue can be divided into different classes where each class has its own scheduling
needs. For example, a common division is a foreground (interactive) process and background (batch) processes.These
two classes have different scheduling needs. For this kind of situation Multilevel Queue Scheduling is used.Now, let us see
how it works.
Ready Queue is divided into separate queues for each class of processes. For example, let us take three different types of
process System processes, Interactive processes and Batch Processes. All three process have there own queue. Now,look at
the below figure.
All three different type of processes have there own queue. Each queue have its own Scheduling algorithm. For example,
queue 1 and queue 2 uses Round Robin while queue 3 can use FCFS to schedule there processes.
This Scheduling is like Multilevel Queue(MLQ) Scheduling but in this process can move between the queues. Multilevel
Feedback Queue Scheduling (MLFQ) keep analyzing the behavior (time of execution) of processes and according to
which it changes its priority.Now, look at the diagram and explanation below to understand it properly.
Now let us suppose that queue 1 and 2 follow round robin with time quantum 4 and 8 respectively and queue 3 follow
FCFS.One implementation of MFQS is given below –
1. When a process starts executing then it first enters queue 1.
2. In queue 1 process executes for 4 unit and if it completes in this 4 unit or it gives CPU for I/O operation in
this 4 unit than the priority of this process does not change and if it again comes in the ready queue than it
again starts its execution in Queue 1.
3. If a process in queue 1 does not complete in 4 unit then its priority gets reduced and it shifted to queue 2.
4. Above points 2 and 3 are also true for queue 2 processes but the time quantum is 8 unit.In a general case if a
process does not complete in a time quantum than it is shifted to the lower priority queue.
5. In the last queue, processes are scheduled in FCFS manner.
6. A process in lower priority queue can only execute only when higher priority queues are empty.
7. A process running in the lower priority queue is interrupted by a process arriving in the higher priority queue.
What is Memory?
Computer memory can be defined as a collection of some data represented in the binary format. On the basis of various
functions, memory can be classified into various categories. We will discuss each one of them later in detail.
A computer device that is capable to store any information or data temporally or permanently, is called storage device