400Level_Operating_Systems_Lecture_Notes
400Level_Operating_Systems_Lecture_Notes
hardware. The purpose of an operating system is to provide an environment in which a user can
execute programs conveniently and efficiently. OR
An operating system is a software that manages computer hardware. The hardware must provide
appropriate mechanisms to ensure the correct operation of the computer system and to prevent
user programs from interfering with the proper operation of the system.
Or
An operating system is a program that controls the execution of application programs and
acts as an interface between the user of a computer and the computer hardware.
A more common definition is that the operating system is the one program running at all
times on the computer (usually called the kernel), with all else being application
programs.
An operating system is concerned with the allocation of resources and services, such as
memory, processors, devices, and information. The operating system correspondingly
includes programs to manage these resources, such as a traffic controller, a scheduler, a
memory management module, I/O programs, and a file system.
(2) Features of Operating system – Operating system has the following features:
1
Security/Privacy Management: Privacy is also provided by the Operating system by
means of passwords so that unauthorized applications can’t access programs or data. For
example, Windows uses Kerberos authentication to prevent unauthorized access to data.
(4) Explanation of The process on how the operating system acts as a User Interface:
Every computer must have an operating system to run other programs. The operating system
coordinates the use of the hardware among the various system programs and application
programs for various users. It simply provides an environment within which other programs
can do useful work.
The operating system is a set of special programs that run on a computer system that allows it
to work properly. It performs basic tasks such as recognizing input from the keyboard, keeping
track of files and directories on the disk, sending output to the display screen, and controlling
2
peripheral devices.
1. It controls the allocation and use of the computing System’s resources among the various
user and tasks.
2. It provides an interface between the computer hardware and the programmer that simplifies
and makes it feasible for coding and debugging of application programs.
The Operating system must support the following tasks. The tasks are:
1. Provides the facilities to create and modify of programs and data files using an editor.
2. Access to the compiler for translating the user program from high-level language to machine
language.
3. Provide a loader program to move the compiled program code to the computer’s memory for
execution.
4. Provide routines that handle the details of I/O programming
3
the machine language translation of a program on a secondary device and a loader places it in
the core. The loader places into memory the machine language version of the user’s program
and transfers control to it. Since the loader program is much smaller than the assembler, those
make more core available to the user’s program.
An Operating System performs all the basic tasks like managing files, processes, and memory.
Thus operating system acts as the manager of all the resources, i.e. resource manager. Thus,
the operating system becomes an interface between user and machine.
4
Types of Operating Systems: Some widely used operating systems are as follows-
5
Advantages of Time-Sharing OS:
Each task gets an equal opportunity
Fewer chances of duplication of software
CPU idle time can be reduced
Disadvantages of Time-Sharing OS:
Reliability problem
One must have to take care of the security and integrity of user programs and data
Data communication problem
Examples of Time-Sharing OSs are: Multics, Unix, etc.
3. Distributed Operating System –
These types of the operating system is a recent advancement in the world of computer
technology and are being widely accepted all over the world and, that too, with a great pace.
Various autonomous interconnected computers communicate with each other using a shared
communication network. Independent systems possess their own memory unit and CPU. These
are referred to as loosely coupled systems or distributed systems. These system’s processors
differ in size and function. The major benefit of working with these types of the operating
system is that it is always possible that one user can access the files or software which are not
actually present on his system but some other system connected within this network i.e.,
remote access is enabled within the devices connected in that network.
6
Advantages of Distributed Operating System:
Failure of one will not affect the other network communication, as all systems are
independent from each other
Electronic mail increases the data exchange speed
Since resources are being shared, computation is highly fast and durable
Load on host computer reduces
These systems are easily scalable as many systems can be easily added to the network
Delay in data processing reduces
Disadvantages of Distributed Operating System:
Failure of the main network will stop the entire communication
To establish distributed systems the language which is used are not well defined yet
These types of systems are not readily available as they are very expensive. Not only that
the underlying software is highly complex and not understood well yet
Examples of Distributed Operating System are- LOCUS, etc.
4. Network Operating System –
These systems run on a server and provide the capability to manage data, users, groups,
security, applications, and other networking functions. These types of operating systems allow
shared access of files, printers, security, applications, and other networking functions over a
small private network. One more important aspect of Network Operating Systems is that all the
users are well aware of the underlying configuration, of all other users within the network,
their individual connections, etc. and that’s why these computers are popularly known
as tightly coupled systems.
7
5. Real-Time Operating System –
These types of OSs serve real-time systems. The time interval required to process and respond
to inputs is very small. This time interval is called response time.
Real-time systems are used when there are time requirements that are very strict like missile
systems, air traffic control systems, robots, etc.
Two types of Real-Time Operating System which are as follows:
Hard Real-Time Systems:
These OSs are meant for applications where time constraints are very strict and even the
shortest possible delay is not acceptable. These systems are built for saving life like
automatic parachutes or airbags which are required to be readily available in case of any
accident. Virtual memory is rarely found in these systems.
Soft Real-Time Systems:
These OSs are for applications where for time-constraint is less strict.
Advantages of RTOS:
Maximum Consumption: Maximum utilization of devices and system, thus more output
from all the resources
Task Shifting: The time assigned for shifting tasks in these systems are very less. For
example, in older systems, it takes about 10 microseconds in shifting one task to another,
and in the latest systems, it takes 3 microseconds.
Focus on Application: Focus on running applications and less importance to applications
which are in the queue.
Real-time operating system in the embedded system: Since the size of programs are small,
RTOS can also be used in embedded systems like in transport and others.
Error Free: These types of systems are error-free.
Memory Allocation: Memory allocation is best managed in these types of systems.
Disadvantages of RTOS:
Limited Tasks: Very few tasks run at the same time and their concentration is very less on
few applications to avoid errors.
Use heavy system resources: Sometimes the system resources are not so good and they are
expensive as well.
8
Complex Algorithms: The algorithms are very complex and difficult for the designer to
write on.
Device driver and interrupt signals: It needs specific device drivers and interrupts signals
to respond earliest to interrupts.
Thread Priority: It is not good to set thread priority as these systems are very less prone to
switching tasks.
Examples of Real-Time Operating Systems are: Scientific experiments, medical imaging
systems, industrial control systems, weapon systems, robots, air traffic control systems, etc.
An Operating System acts as a communication bridge (interface) between the user and
computer hardware. The purpose of an operating system is to provide a platform on which a
user can execute programs in a convenient and efficient manner.
An operating system is a piece of software that manages the allocation of computer hardware.
The coordination of the hardware must be appropriate to ensure the correct working of the
computer system and to prevent user programs from interfering with the proper working of the
system.
Example: Just like a boss gives orders to his employee, in a similar way we request or pass our
orders to the Operating System. The main goal of the Operating System is to make the
computer environment more convenient to use and Secondary goal is to use the resources in
the most efficient manner.
What is an Operating System?
An operating system is a program on which application programs are executed and acts as a
communication bridge (interface) between the user and the computer hardware.
The main task an operating system carries out is the allocation of resources and services, such
as the allocation of memory, devices, processors, and information. The operating system also
includes programs to manage these resources, such as a traffic controller, a scheduler, memory
management module, I/O programs, and a file system.
Important functions of an operating System:
1. Security –
The operating system uses password protection to protect user data and similar other
techniques. it also prevents unauthorized access to programs and user data.
3. Job accounting –
Operating system Keeps track of time and resources used by various tasks and users, this
information can be used to track resource usage for a particular user or group of users.
9
malfunctioning of a computer system.
6. Memory Management –
The operating system manages the Primary Memory or Main Memory. Main memory is
made up of a large array of bytes or words where each byte or word is assigned a certain
address. Main memory is fast storage and it can be accessed directly by the CPU. For a
program to be executed, it should be first loaded in the main memory. An Operating System
performs the following activities for memory management:
It keeps track of primary memory, i.e., which bytes of memory are used by which user
program. The memory addresses that have already been allocated and the memory addresses
of the memory that has not yet been used. In multiprogramming, the OS decides the order in
which processes are granted access to memory, and for how long. It Allocates the memory
to a process when the process requests it and deallocates the memory when the process has
terminated or is performing an I/O operation.
7. Processor Management –
In a multi-programming environment, the OS decides the order in which processes have
access to the processor, and how much processing time each process has. This function of
OS is called process scheduling. An Operating System performs the following activities for
processor management.
Keeps track of the status of processes. The program which performs this task is known as a
traffic controller. Allocates the CPU that is a processor to a process. De-allocates processor
when a process is no more required.
8. Device Management –
An OS manages device communication via their respective drivers. It performs the
following activities for device management. Keeps track of all devices connected to the
system. designates a program responsible for every device known as the Input/Output
controller. Decides which process gets access to a certain device and for how long.
Allocates devices in an effective and efficient way. Deallocates devices when they are no
longer required.
9. File Management –
A file system is organized into directories for efficient or easy navigation and usage. These
directories may contain other directories and other files. An Operating System carries out
the following file management activities. It keeps track of where information is stored, user
access settings and status of every file, and more… These facilities are collectively known
as the file system.
Moreover, Operating System also provides certain services to the computer system in one form
or the other.
The Operating System provides certain services to the users which can be listed in the
following manner:
10
1. Program Execution: The Operating System is responsible for the execution of all types of
programs whether it be user programs or system programs. The Operating System utilizes
various resources available for the efficient running of all types of functionalities.
2. Handling Input/Output Operations: The Operating System is responsible for handling all
sorts of inputs, i.e, from the keyboard, mouse, desktop, etc. The Operating System does all
interfacing in the most appropriate manner regarding all kinds of Inputs and Outputs.
For example, there is a difference in the nature of all types of peripheral devices such as
mice or keyboards, the Operating System is responsible for handling data between them.
3. Manipulation of File System: The Operating System is responsible for making decisions
regarding the storage of all types of data or files, i.e, floppy disk/hard disk/pen drive, etc.
The Operating System decides how the data should be manipulated and stored.
4. Error Detection and Handling: The Operating System is responsible for the detection of
any type of error or bugs that can occur while any task. The well-secured OS sometimes also
acts as a countermeasure for preventing any sort of breach to the Computer System from any
external source and probably handling them.
5. Resource Allocation: The Operating System ensures the proper use of all the resources
available by deciding which resource to be used by whom for how much time. All the
decisions are taken by the Operating System.
6. Accounting: The Operating System tracks an account of all the functionalities taking place
in the computer system at a time. All the details such as the types of errors that occurred are
recorded by the Operating System.
7. Information and Resource Protection: The Operating System is responsible for using all
the information and resources available on the machine in the most protected way. The
Operating System must foil an attempt from any external resource to hamper any sort of
data or information.
All these services are ensured by the Operating System for the convenience of the users to
make the programming task easier. All different kinds of Operating systems more or less
provide the same services.
11
Need for Operating System:
OS as a platform for Application programs:
The operating system provides a platform, on top of which, other programs, called
application programs can run. These application programs help the users to perform a
specific task easily. It acts as an interface between the computer and the user. It is designed
in such a manner that it operates, controls, and executes various applications on the
computer.
Multitasking:
Operating System manages memory and allows multiple programs to run in their own space
and even communicate with each other through shared memory. Multitasking gives users a
good experience as they can perform several tasks on a computer at a time.
Processor Management: This deals with the management of the Central Processing Unit
(CPU). The operating system takes care of the allotment of CPU time to different processes.
When a process finishes its CPU processing after executing for the allotted time period, this
12
is called scheduling. There is various type of scheduling techniques that are used by the
operating systems:
1. Shortest Job First(SJF): The process which needs the shortest CPU time is scheduled
first.
2. Round Robin Scheduling: Each process is assigned a fixed CPU execution time in a
cyclic way.
3. Priority Based Scheduling (Non-Preemptive): In this scheduling, processes are
scheduled according to their priorities, i.e., the highest priority process is scheduled first.
If the priorities of the two processes match, then schedule according to arrival time.
Context Switching: In most multitasking OSs, multiple running processes on the system
may need a change of state in execution. Even if there are multiple processes being executed
at any one point in time, only one task is executed in the foreground, while the others are put
in the background. So the process that is in the foreground is determined by the priority-
based scheduling, and the OS saves the execution state of the previous process before
switching to the current one. This is known as context switching.
Device Management:
The Operating System communicates with the hardware and the attached devices and
maintains a balance between them and the CPU. This is all the more important because the
CPU processing speed is much higher than that of I/O devices. In order to optimize the CPU
time, the operating system employs two techniques – Buffering and Spooling.
Buffering:
In this technique, input and output data are temporarily stored in Input Buffer and Output
Buffer. Once the signal for input or output is sent to or from the CPU respectively, the
operating system through the device controller moves the data from the input device to the
input buffer and from the output buffer to the output device. In the case of input, if the
buffer is full, the operating system sends a signal to the program which processes the data
stored in the buffer. When the buffer becomes empty, the program informs the operating
system which reloads the buffer and the input operation continues.
Memory management:
In a computer, both the CPU and the I/O devices interact with the memory. When a program
needs to be executed it is loaded onto the main memory till the execution is completed.
Thereafter that memory space is freed and is available for other programs. The common
memory management techniques used by the operating system are Partitioning and Virtual
13
Memory.
Partitioning:
The total memory is divided into various partitions of the same size or different sizes. This
helps to accommodate a number of programs in the memory. The partition can be fixed i.e.
remains the same for all the programs in the memory or variable i.e. memory is allocated
when a program is loaded onto the memory. The latter approach causes less wastage of
memory but in due course of time, it may become fragmented.
Virtual Memory:
This is a technique used by the operating systems which allows the user can load programs
that are larger than the main memory of the computer. In this technique, the program is
executed even if the complete program can not be loaded inside the main memory leading to
efficient memory utilization.
File Management:
The operating system manages the files, folders, and directory systems on a computer. Any
data on a computer is stored in the form of files and the operating system keeps the
information about all of them using the File Allocation Table (FAT), or a data structure
called an inode in Linux. The FAT stores general information about files like filename, type
(text or binary), size, starting address, and access mode (sequential/indexed
sequential/direct/relative). The file manager of the operating system helps to create, edit,
copy, allocate memory to the files and also updates the FAT. The operating system also
takes care that files are opened with proper access rights to read or edit them.
14
Introduction of Process Management
Program vs Process: A process is a program in execution. For example, when we write a
program in C or C++ and compile it, the compiler creates binary code. The original code and
binary code are both programs. When we actually run the binary code, it becomes a process.
A process is an ‘active’ entity instead of a program, which is considered a ‘passive’ entity. A
single program can create many processes when run multiple times; for example, when we
open a .exe or binary file multiple times, multiple instances begin (multiple processes are
created).
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.
Stack: The stack contains temporary data, such as function parameters, returns addresses, and
local variables.
Data Section: Contains the global variable.
Heap Section: Dynamically allocated memory to process during its run time.
15
may have different priorities, for example
a shorter process assigned high priority
in the shortest job first scheduling)
All of the above attributes of a process are also known as the context of the process. Every
process has its own process control block (PCB), i.e each process will have a unique PCB. All
of the above attributes are part of the PCB.
6. Suspended Ready: When the ready queue becomes full, some processes
are moved to suspended ready state
Context Switching: The process of saving the context of one process and loading the context
of another process is known as Context Switching. In simple terms, it is like loading and
unloading the process from the running state to the ready state.
16
When does context switching happen?
1. When a high-priority process comes to a ready state (i.e. with higher priority than the
running process)
2. An Interrupt occurs
3. User and kernel-mode switch (It is not necessary though)
4. Preemptive CPU scheduling used.
Context Switch vs Mode Switch: A mode switch occurs when the CPU privilege level is
changed, for example when a system call is made or a fault occurs. The kernel works in more a
privileged mode than a standard user task. If a user process wants to access things that are only
accessible to the kernel, a mode switch must occur. The currently executing process need not
be changed during a mode switch.
A mode switch typically occurs for a process context switch to occur. Only the kernel can
cause a context switch.
CPU-Bound vs I/O-Bound Processes: A CPU-bound process requires more CPU time or
spends more time in the running state.
An I/O-bound process requires more I/O time and less CPU time. An I/O-bound process
spends more time in the waiting state.
Exercise:
1. Which of the following need not necessarily be saved on a context switch between
processes? (GATE-CS-2000)
(A) General purpose registers
(B) Translation lookaside buffer
(C) Program counter
(D) All of the above
Answer (B)
Explanation: In a process context switch, the state of the first process must be saved
somehow, so that when the scheduler gets back to the execution of the first process, it can
restore this state and continue. The state of the process includes all the registers that the
process may be using, especially the program counter, plus any other operating system-specific
data that may be necessary. A translation look-aside buffer (TLB) is a CPU cache that memory
management hardware uses to improve virtual address translation speed. A TLB has a fixed
number of slots that contain page table entries, which map virtual addresses to physical
addresses. On a context switch, some TLB entries can become invalid, since the virtual-to-
physical mapping is different. The simplest strategy to deal with this is to completely flush the
TLB.
2. The time taken to switch between user and kernel modes of execution is t1 while the time
taken to switch between two processes is t2. Which of the following is TRUE? (GATE-CS-
2011)
(A) t1 > t2
(B) t1 = t2
(C) t1 < t2
(D) nothing can be said about the relation between t1 and t2.
Answer: (C)
17
Explanation: Process switching involves a mode switch. Context switching can occur only in
kernel mode.
New (Create) – In this step, the process is about to be created but not yet created, it is the
program which is present in secondary memory that will be picked up by OS to create the
process.
Ready – New -> Ready to run. After the creation of a process, the process enters the ready
state i.e. the process is loaded into the main memory. The process here is ready to run and is
waiting to get the CPU time for its execution. Processes that are ready for execution by the
CPU are maintained in a queue for ready processes.
Run – The process is chosen by CPU for execution and the instructions within the process are
executed by any one of the available CPU cores.
Blocked or wait – Whenever the process requests access to I/O or needs input from the user
or needs access to a critical region(the lock for which is already acquired) it enters the blocked
or wait state. The process continues to wait in the main memory and does not require CPU.
Once the I/O operation is completed the process goes to the ready state.
Terminated or completed – Process is killed as well as PCB is deleted.
Suspend ready – Process that was initially in the ready state but was swapped out of main
memory(refer Virtual Memory topic) and placed onto external storage by scheduler is said to
be in suspend ready state. The process will transition back to ready state whenever the process
is again brought onto the main memory.
Suspend wait or suspend blocked – Similar to suspend ready but uses the process which was
performing I/O operation and lack of main memory caused them to move to secondary
memory. When work is finished it may go to suspend ready.
18
CPU and I/O 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 I/O bound process.
Types of schedulers:
1. Long term – performance – Makes a decision about how many processes should be made to
stay in the ready state, this decides the degree of multiprogramming. Once a decision is taken
it lasts for a long time hence called long term scheduler.
2. Short term – Context switching time – Short term scheduler will decide which process to be
executed next and then it will call dispatcher. A dispatcher is a software that moves process
from ready to run and vice versa. In other words, it is context switching.
3. Medium term – Swapping time – Suspension decision is taken by medium term scheduler.
Medium term scheduler is used for swapping that is moving the process from main memory to
secondary and vice versa.
Multiprogramming – We have many processes ready to run. There are two types of
multiprogramming:
1. Pre-emption – Process is forcefully removed from CPU. Pre-emption is also called as time
sharing or multitasking.
2. Non pre-emption – Processes are not removed until they complete the execution.
Degree of multiprogramming – The number of processes that can reside in the ready state at
maximum decides the degree of multiprogramming, e.g., if the degree of programming = 100,
this means 100 processes can reside in the ready state at maximum.
19
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. A dispatcher does the
following:
1. Switching context.
2. Switching to user mode.
3. Jumping to the proper location in the newly loaded program.
3. Medium-Term Scheduler:
It is responsible for suspending and resuming the process. It mainly does swapping (moving
processes from main memory to disk and vice versa). Swapping may be necessary to improve
the process mix or because a change in memory requirements has overcommitted available
memory, requiring memory to be freed up. It is helpful in maintaining a perfect balance
between the I/O bound and the CPU bound. It reduces the degree of multiprogramming.
Pointer – It is a stack pointer which is required to be saved when the process is switched from
one state to another to retain the current position of the process.
Process state – It stores the respective state of the process.
20
Process number – Every process is assigned with a unique id known as process ID or PID
which stores the process identifier.
Program counter – It stores the counter which contains the address of the next instruction
that is to be executed for the process.
Register – These are the CPU registers which includes: accumulator, base, registers and
general purpose registers.
Memory limits – This field contains the information about memory management system used
by operating system. This may include the page tables, segment tables etc.
Open files list – This information includes the list of files opened for a process.
Miscellaneous accounting and status data – This field includes information about the
amount of CPU used, time constraints, jobs or process number, etc.
The process control block stores the register content also known as execution content of the
processor when it was blocked from running. This execution content architecture enables the
operating system to restore a process’s execution context when the process returns to the
running state. When the process makes a transition from one state to another, the operating
system updates its information in the process’s PCB. The operating system maintains pointers
to each process’s PCB in a process table so that it can access the PCB quickly.
Interrupts
The interrupt is a signal emitted by hardware or software when a process or an event needs
immediate attention. It alerts the processor to a high-priority process requiring interruption of the
current working process. In I/O devices one of the bus control lines is dedicated for this purpose
and is called the Interrupt Service Routine (ISR).
When a device raises an interrupt at let’s say process i, the processor first completes the
execution of instruction i. Then it loads the Program Counter (PC) with the address of the first
instruction of the ISR. Before loading the Program Counter with the address, the address of the
interrupted instruction is moved to a temporary location. Therefore, after handling the interrupt
the processor can continue with process i+1.
While the processor is handling the interrupts, it must inform the device that its request has been
recognized so that it stops sending the interrupt request signal. Also, saving the registers so that
the interrupted process can be restored in the future, increases the delay between the time an
interrupt is received and the start of the execution of the ISR. This is called Interrupt Latency.
Hardware Interrupts:
In a hardware interrupt, all the devices are connected to the Interrupt Request Line. A single
request line is used for all the n devices. To request an interrupt, a device closes its associated
switch. When a device requests an interrupt, the value of INTR is the logical OR of the requests
from individual devices.
The sequence of events involved in handling an IRQ:
1. Devices raise an IRQ.
2. The processor interrupts the program currently being executed.
3. The device is informed that its request has been recognized and the device deactivates the
request signal.
21
4. The requested action is performed.
5. An interrupt is enabled and the interrupted program is resumed.
Handling Multiple Devices:
When more than one device raises an interrupt request signal, then additional information is
needed to decide which device to be considered first. The following methods are used to decide
which device to select: Polling, Vectored Interrupts, and Interrupt Nesting. These are explained
as following below.
1. Polling: In polling, the first device encountered with the IRQ bit set is the device that is to be
serviced first. Appropriate ISR is called to service the same. It is easy to implement but a lot
of time is wasted by interrogating the IRQ bit of all devices.
2. Vectored Interrupts: In vectored interrupts, a device requesting an interrupt identifies itself
directly by sending a special code to the processor over the bus. This enables the processor to
identify the device that generated the interrupt. The special code can be the starting address of
the ISR or where the ISR is located in memory and is called the interrupt vector.
3. Interrupt Nesting: In this method, the I/O device is organized in a priority structure.
Therefore, an interrupt request from a higher priority device is recognized whereas a request
from a lower priority device is not. The processor accepts interrupts only from
devices/processes having priority.
Processors’ priority is encoded in a few bits of PS (Process Status register). It can be changed by
program instructions that write into the PS. The processor is in supervised mode only while
executing OS routines. It switches to user mode before executing application programs.
Thread in Operating System
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 lightweight process. The idea is to achieve parallelism by dividing a
process into multiple threads. For example, in a browser, multiple tabs can be different threads.
MS Word uses multiple threads: one thread to format the text, another thread to process inputs,
etc. More advantages of multithreading are discussed below
Process vs Thread?
The primary difference is that threads within the same process run in a shared memory space,
while processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result threads share with
other threads their code section, data section, and OS resources (like open files and signals). But,
like process, a thread has its own program counter (PC), register set, and stack space.
Advantages of Thread over Process
1. Responsiveness: If the process is divided into multiple threads, if one thread completes its
execution, then its output can be immediately returned.
2. Faster context switch: Context switch time between threads is lower compared to process
context switch. Process context switching requires more overhead from the CPU.
3. Effective utilization of multiprocessor system: If we have multiple threads in a single process,
then we can schedule multiple threads on multiple processor. This will make process execution
faster.
4. Resource sharing: Resources like code, data, and files can be shared among all threads within
a process.
22
Note: stack and registers can’t be shared among the threads. Each thread has its own stack and
registers.
5. Communication: Communication between multiple threads is easier, as the threads shares
common address space. while in process we have to follow some specific communication
technique for communication between two process.
6. Enhanced throughput of the system: If a process is divided into multiple threads, and each
thread function is considered as one job, then the number of jobs completed per unit of time is
increased, thus increasing the throughput of the system.
Types of Threads
There are two types of threads.
User Level Thread
Kernel Level Thread
Threads and its types in Operating System
Thread is a single sequence stream within a process. Threads have same properties as of the
process so they are called as light weight processes. Threads are executed one after another but
gives the illusion as if they are executing in parallel. Each thread has different states. Each thread
has
1. A program counter
2. A register set
3. A stack space
Threads are not independent of each other as they share the code, data, OS resources etc.
Similarity between Threads and Processes –
Only one thread or process is active at a time
Within process both execute sequential
Both can create children
Differences between Threads and Processes –
Threads are not independent, processes are.
Threads are designed to assist each other, processes may or may not do it
Types of Threads:
1. User Level thread (ULT) – Is implemented in the user level library, they are not created
using the system calls. Thread switching does not need to call OS and to cause interrupt to
Kernel. Kernel doesn’t know about the user level thread and manages them as if they were
single-threaded processes.
Advantages of ULT –
Can be implemented on an OS that doesn’t support multithreading.
Simple representation since thread has only program counter, register set, stack
space.
Simple to create since no intervention of kernel.
Thread switching is fast since no OS calls need to be made.
Limitations of ULT –
No or less co-ordination among the threads and Kernel.
If one thread causes a page fault, the entire process blocks.
2. Kernel Level Thread (KLT) – Kernel knows and manages the threads. Instead of thread
table in each process, the kernel itself has thread table (a master one) that keeps track of all the
23
threads in the system. In addition kernel also maintains the traditional process table to keep
track of the processes. OS kernel provides system call to create and manage threads.
Advantages of KLT –
Since kernel has full knowledge about the threads in the system, scheduler may
decide to give more time to processes having large number of threads.
Good for applications that frequently block.
Limitations of KLT –
Slow and inefficient.
It requires thread control block so it is an overhead.
Summary:
1. Each ULT has a process that keeps track of the thread using the Thread table.
2. Each KLT has Thread Table (TCB) as well as the Process Table (PCB).
do not isolate.
24
Process vs Thread
3. It takes more time for creation. It takes less time for creation.
10. If one process is blocked then it If a user-level thread is blocked, then all other user-
25
S.N
O Process Thread
13. A system call is involved in it. No system call is involved, it is created using APIs.
Note: In some cases where the thread is processing a bigger workload compared to a process’s
workload then the thread may take more time to terminate. But this is an extremely rare situation
and has fewer chances to occur.
26
In computing, a process is the instance of a computer program that is being executed by one
or many threads. It contains the program code and its activity. Depending on the operating
system (OS), a process may be made up of multiple threads of execution that execute instructions
concurrently.
How is process memory used for efficient operation?
The process memory is divided into four sections for efficient operation:
The text category is composed of integrated program code, which is read from fixed storage
when the program is launched.
The data class is made up of global and static variables, distributed and executed before the
main action.
Heap is used for flexible, or dynamic memory allocation and is managed by calls to new,
delete, malloc, free, etc.
The stack is used for local variables. The space in the stack is reserved for local variables
when it is announced.
27
Process Scheduling is an integral part of Multi-programming applications. Such operating
systems allow more than one process to be loaded into usable memory at a time and the loaded
shared CPU process uses repetition time.
There are three types of process schedulers:
Long term or Job Scheduler
Short term or CPU Scheduler
Medium-term Scheduler
Why do we need to schedule processes?
Scheduling is important in many different computer environments. One of the most important
areas is scheduling which programs will work on the CPU. This task is handled by the
Operating System (OS) of the computer and there are many different ways in which we can
choose to configure programs.
Process Scheduling allows the OS to allocate CPU time for each process. Another important
reason to use a process scheduling system is that it keeps the CPU busy at all times. This
allows you to get less response time for programs.
Considering that there may be hundreds of programs that need to work, the OS must launch
the program, stop it, switch to another program, etc. The way the OS configures the system to
run another in the CPU is called “context switching”. If the OS keeps context-switching
programs in and out of the provided CPUs, it can give the user a tricky idea that he or she can
run any programs he or she wants to run, all at once.
So now that we know we can run 1 program at a given CPU, and we know we can change the
operating system and remove another one using the context switch, how do we choose which
programs we need. run, and with what program?
That’s where scheduling comes in! First, you determine the metrics, saying something like
“the amount of time until the end”. We will define this metric as “the time interval between
which a function enters the system until it is completed”. Second, you decide on a metrics that
reduces metrics. We want our tasks to end as soon as possible.
What is the need for CPU scheduling algorithm?
CPU scheduling is the process of deciding which process will own the CPU to use while another
process is suspended. The main function of the CPU scheduling is to ensure that whenever the
CPU remains idle, the OS has at least selected one of the processes available in the ready-to-use
line.
In Multiprogramming, if the long-term scheduler selects multiple I / O binding processes then
most of the time, the CPU remains an idle. The function of an effective program is to improve
resource utilization.
If most operating systems change their status from performance to waiting then there may always
be a chance of failure in the system. So in order to minimize this excess, the OS needs to
schedule tasks in order to make full use of the CPU and avoid the possibility of deadlock.
Objectives of Process Scheduling Algorithm:
Utilization of CPU at maximum level. Keep CPU as busy as possible.
Allocation of CPU should be fair.
Throughput should be Maximum. i.e. Number of processes that complete their execution
per time unit should be maximized.
Minimum turnaround time, i.e. time taken by a process to finish execution should be the
least.
28
There should be a minimum waiting time and the process should not starve in the ready
queue.
Minimum response time. It means that the time when a process produces the first response
should be as less as possible.
What are the different terminologies to take care of in any CPU Scheduling algorithm?
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
Things to take care while designing a CPU Scheduling algorithm?
Different CPU Scheduling algorithms have different structures and the choice of a particular
algorithm depends on a variety of factors. Many conditions have been raised to compare CPU
scheduling algorithms.
The criteria include the following:
CPU utilization: The main purpose of any CPU algorithm is to keep the CPU as busy as
possible. Theoretically, CPU usage can range from 0 to 100 but in a real-time system, it varies
from 40 to 90 percent depending on the system load.
Throughput: The average CPU performance is the number of processes performed and
completed during each unit. This is called throughput. The output may vary depending on the
length or duration of the processes.
Turn round Time: For a particular process, the important conditions are how long it takes to
perform that process. The time elapsed from the time of process delivery to the time of
completion is known as the conversion time. Conversion time is the amount of time spent
waiting for memory access, waiting in line, using CPU, and waiting for I / O.
Waiting Time: The Scheduling algorithm does not affect the time required to complete the
process once it has started performing. It only affects the waiting time of the process i.e. the
time spent in the waiting process in the ready queue.
Response Time: In a collaborative system, turn around time is not the best option. The
process may produce something early and continue to computing the new results while the
previous results are released to the user. Therefore another method is the time taken in the
submission of the application process until the first response is issued. This measure is called
response time.
What are the different types of CPU Scheduling Algorithms?
There are mainly two types of scheduling methods:
Preemptive Scheduling: Preemptive scheduling is used when a process switches from
running state to ready state or from the waiting state to the ready state.
Non-Preemptive Scheduling: Non-Preemptive scheduling is used when a process
terminates , or when a process switches from running state to waiting state.
29
Different types of CPU Scheduling Algorithms
Let us now learn about these CPU scheduling algorithms in operating systems one by one:
1. First Come First Serve:
FCFS considered to be the simplest of all operating system scheduling algorithms. First come
first serve scheduling algorithm states that the process that requests the CPU first is allocated the
CPU first and is implemented by using FIFO queue.
Characteristics of FCFS:
FCFS supports non-preemptive and preemptive CPU scheduling algorithms.
Tasks are always executed on a First-come, First-serve concept.
FCFS is easy to implement and use.
This algorithm is not much efficient in performance, and the wait time is quite high.
Advantages of FCFS:
Easy to implement
First come, first serve method
Disadvantages of FCFS:
FCFS suffers from Convoy effect.
The average waiting time is much higher than the other algorithms.
FCFS is very simple and easy to implement and hence not much efficient.
To learn about how to implement this CPU scheduling algorithm, please refer to our detailed
article on First come, First serve Scheduling.
2. Shortest Job First(SJF):
Shortest job first (SJF) is a scheduling process that selects the waiting process with the smallest
execution time to execute next. This scheduling method may or may not be preemptive.
Significantly reduces the average waiting time for other processes waiting to be executed. The
full form of SJF is Shortest Job First.
30
Characteristics of SJF:
Shortest Job first has the advantage of having a minimum average waiting time among
all operating system scheduling algorithms.
It is associated with each task as a unit of time to complete.
It may cause starvation if shorter processes keep coming. This problem can be solved using
the concept of ageing.
Advantages of Shortest Job first:
As SJF reduces the average waiting time thus, it is better than the first come first serve
scheduling algorithm.
SJF is generally used for long term scheduling
Disadvantages of SJF:
One of the demerit SJF has is starvation.
Many times it becomes complicated to predict the length of the upcoming CPU request
31
This may lead to convoy effect.
To learn about how to implement this CPU scheduling algorithm, please refer to our detailed
article on the Longest job first scheduling.
4. Priority Scheduling:
Preemptive Priority CPU Scheduling Algorithm is a pre-emptive method of CPU scheduling
algorithm that works based on the priority of a process. In this algorithm, the editor sets the
functions to be as important, meaning that the most important process must be done first. In the
case of any conflict, that is, where there are more than one processor with equal value, then the
most important CPU planning algorithm works on the basis of the FCFS (First Come First Serve)
algorithm.
Characteristics of Priority Scheduling:
Schedules tasks based on priority.
When the higher priority work arrives while a task with less priority is executed, the higher
priority work takes the place of the less priority one and
The latter is suspended until the execution is complete.
Lower is the number assigned, higher is the priority level of a process.
Advantages of Priority Scheduling:
The average waiting time is less than FCFS
Less complex
Disadvantages of Priority Scheduling:
One of the most common demerits of the Preemptive priority CPU scheduling algorithm is
the Starvation Problem. This is the problem in which a process has to wait for a longer amount
of time to get scheduled into the CPU. This condition is called the starvation problem.
To learn about how to implement this CPU scheduling algorithm, please refer to our detailed
article on Priority Preemptive Scheduling algorithm.
5. Round robin:
Round Robin is a CPU scheduling algorithm where each process is cyclically assigned a fixed
time slot. It is the preemptive version of First come First Serve CPU Scheduling algorithm.
Round Robin CPU Algorithm generally focuses on Time Sharing technique.
Characteristics of Round robin:
It’s simple, easy to use, and starvation-free as all processes get the balanced CPU allocation.
One of the most widely used methods in CPU scheduling as a core.
It is considered preemptive as the processes are given to the CPU for a very limited time.
Advantages of Round robin:
Round robin seems to be fair as every process gets an equal share of CPU.
The newly created process is added to the end of the ready queue.
To learn about how to implement this CPU scheduling algorithm, please refer to our detailed
article on the Round robin Scheduling algorithm.
Shortest remaining time first is the preemptive version of the Shortest job first which we have
discussed earlier where the processor is allocated to the job closest to completion. In SRTF the
process with the smallest amount of time remaining until completion is selected to execute.
Characteristics of Shortest remaining time first:
SRTF algorithm makes the processing of the jobs faster than SJF algorithm, given it’s
overhead charges are not counted.
32
The context switch is done a lot more times in SRTF than in SJF and consumes the CPU’s
valuable time for processing. This adds up to its processing time and diminishes its advantage
of fast processing.
Advantages of SRTF:
In SRTF the short processes are handled very fast.
The system also requires very little overhead since it only makes a decision when a process
completes or a new process is added.
Disadvantages of SRTF:
Like the shortest job first, it also has the potential for process starvation.
Long processes may be held off indefinitely if short processes are continually added.
To learn about how to implement this CPU scheduling algorithm, please refer to our detailed
article on the shortest remaining time first.
7. Longest Remaining Time First:
The longest remaining time first is a preemptive version of the longest job first scheduling
algorithm. This scheduling algorithm is used by the operating system to program incoming
processes for use in a systematic way. This algorithm schedules those processes first which have
the longest processing time remaining for completion.
Characteristics of longest remaining time first:
Among all the processes waiting in a waiting queue, the CPU is always assigned to the
process having the largest burst time.
If two processes have the same burst time then the tie is broken using FCFS i.e. the process
that arrived first is processed first.
LJF CPU Scheduling can be of both preemptive and non-preemptive types.
Advantages of LRTF:
No other process can execute until the longest task executes completely.
All the jobs or processes finish at the same time approximately.
Disadvantages of LRTF:
This algorithm gives a very high average waiting time and average turn-around time for a
given set of processes.
This may lead to a convoy effect.
To learn about how to implement this CPU scheduling algorithm, please refer to our detailed
article on the longest remaining time first.
8. Highest Response Ratio Next:
Highest Response Ratio Next is a non-preemptive CPU Scheduling algorithm and it is
considered as one of the most optimal scheduling algorithms. The name itself states that we need
to find the response ratio of all available processes and select the one with the highest Response
Ratio. A process once selected will run till completion.
Characteristics of Highest Response Ratio Next:
The criteria for HRRN is Response Ratio, and the mode is Non-Preemptive.
HRRN is considered as the modification of Shortest Job First to reduce the problem
of starvation.
In comparison with SJF, during the HRRN scheduling algorithm, the CPU is allotted to the
next process which has the highest response ratio and not to the process having less burst
time.
Response Ratio = (W + S)/S
Here, W is the waiting time of the process so far and S is the Burst time of the process.
33
Advantages of HRRN:
HRRN Scheduling algorithm generally gives better performance than the shortest job
first Scheduling.
There is a reduction in waiting time for longer jobs and also it encourages shorter jobs.
Disadvantages of HRRN:
The implementation of HRRN scheduling is not possible as it is not possible to know the burst
time of every job in advance.
In this scheduling, there may occur an overload on the CPU.
To learn about how to implement this CPU scheduling algorithm, please refer to our detailed
article on Highest Response Ratio Next.
9. Multiple Queue Scheduling:
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
a background (batch) process. These two classes have different scheduling needs. For this kind
of situation Multilevel Queue Scheduling is used.
34
As the processes are permanently assigned to the queue, this setup has the advantage of low
scheduling overhead,
But on the other hand disadvantage of being inflexible.
Advantages of Multilevel feedback queue scheduling:
It is more flexible
It allows different processes to move between different queues
Disadvantages of Multilevel feedback queue scheduling:
It also produces CPU overheads
It is the most complex algorithm.
To learn about how to implement this CPU scheduling algorithm, please refer to our detailed
article on Multilevel Feedback Queue Scheduling.
Comparison between various CPU Scheduling algorithms
Here is a brief comparison between different CPU scheduling algorithms:
Average
waiting
Allocation time Preemptio
Algorithm is Complexity (AWT) n Starvation Performance
According
to the
arrival time
of the
processes, Simple and
the CPU is easy to Slow
FCFS allocated. implement Large. No No performance
Based on Minimum
the lowest More Average
CPU burst complex Smaller Waiting
SJF time (BT). than FCFS than FCFS No Yes Time
Depending
on some
measures
Based on e.g., arrival
the highest More time,
CPU burst complex process Big turn-
LJFS time (BT) than FCFS size, etc. No Yes around time
35
Average
waiting
Allocation time Preemptio
Algorithm is Complexity (AWT) n Starvation Performance
CPU burst
time (BT).
But it is
preemptive size, etc.
Same as
SJF the
allocation
of the CPU Depending
is based on on some
the lowest measures
CPU burst e.g., arrival The
time (BT). More time, preference is
But it is complex process given to the
SRTF preemptive. than FCFS size, etc Yes Yes short jobs
According
to the order
of the The
process complexity Large as Each
arrives with depends on compared process has
fixed time Time to SJF and given a
quantum Quantum Priority fairly fixed
RR (TQ) size scheduling. Yes No time
According
to the
priority. Well
The bigger performance
priority task This type is but contain a
Priority Pre- executes less Smaller starvation
emptive first complex than FCFS Yes Yes problem
36
Average
waiting
Allocation time Preemptio
Algorithm is Complexity (AWT) n Starvation Performance
higher
priority
jobs
According
to the More
process that complex Good
resides in than the performance
the bigger priority but contain a
queue scheduling Smaller starvation
MLQ priority algorithms than FCFS No Yes problem
It is the most
According Complex but
to the its Smaller
process of a complexity than all
bigger rate depends scheduling
priority on the TQ types in Good
MFLQ queue. size many cases No No performance
Exercise:
1. Consider a system which requires 40-time units of burst time. The Multilevel feedback queue
scheduling is used and time quantum is 2 unit for the top queue and is incremented by 5 unit at
each level, then in what queue the process will terminate the execution?
2. Which of the following is false about SJF? S1: It causes minimum average waiting time S2: It
can cause starvation (A) Only S1 (B) Only S2 (C) Both S1 and S2 (D) Neither S1 nor S2
Answer (D) S1 is true SJF will always give minimum average waiting time. S2 is true SJF can
cause starvation.
3. Consider the following table of arrival time and burst time for three processes P0, P1 and P2.
(GATE-CS-2011)
P0 0 ms 9 ms
P1 1 ms 4 ms
37
P2 2 ms 9 ms
1. The pre-emptive shortest job first scheduling algorithm is used. Scheduling is carried out only
at arrival or completion of processes. What is the average waiting time for the three
processes? (A) 5.0 ms (B) 4.33 ms (C) 6.33 (D) 7.33 Solution : Answer: – (A) Process P0 is
allocated processor at 0 ms as there is no other process in the ready queue. P0 is preempted
after 1 ms as P1 arrives at 1 ms and burst time for P1 is less than remaining time of P0. P1
runs for 4ms. P2 arrived at 2 ms but P1 continued as burst time of P2 is longer than P1. After
P1 completes, P0 is scheduled again as the remaining time for P0 is less than the burst time of
P2. P0 waits for 4 ms, P1 waits for 0 ms and P2 waits for 11 ms. So average waiting time is
(0+4+11)/3 = 5.
2. Consider the following set of processes, with the arrival times and the CPU-burst times given
in milliseconds (GATE-CS-2004)
P1 0 ms 5 ms
P2 1 ms 3 ms
P3 2 ms 3 ms
P4 4 ms 1 ms
1. What is the average turnaround time for these processes with the preemptive shortest
remaining processing time first (SRPT) algorithm ? (A) 5.50 (B) 5.75 (C) 6.00 (D) 6.25
Answer (A) Solution: The following is Gantt Chart of execution
P1 P2 P4 P3 P1
1 4 5 8 12
1. Turn Around Time = Completion Time – Arrival Time Avg Turn Around Time = (12 + 3 +
6+ 1)/4 = 5.50
2. An operating system uses the Shortest Remaining Time First (SRTF) process scheduling
algorithm. Consider the arrival times and execution times for the following processes:
38
P1 20 ms 0 ms
P2 25 ms 15 ms
P3 10 ms 30 ms
P4 15 ms 45 ms
1. What is the total waiting time for process P2? (A) 5 (B) 15 (C) 40 (D) 55 Answer (B) At time
0, P1 is the only process, P1 runs for 15 time units. At time 15, P2 arrives, but P1 has the
shortest remaining time. So P1 continues for 5 more time units. At time 20, P2 is the only
process. So it runs for 10 time units At time 30, P3 is the shortest remaining time process. So
it runs for 10 time units At time 40, P2 runs as it is the only process. P2 runs for 5 time units.
At time 45, P3 arrives, but P2 has the shortest remaining time. So P2 continues for 10 more
time units. P2 completes its execution at time 55
Total waiting time for P2
= Completion time – (Arrival time + Execution time)
= 55 – (15 + 25)
= 15
FCFS Scheduling Full Form
FCFS stands for First Come First Serve. In the FCFS scheduling algorithm, the job that arrived
first in the ready queue is allocated to the CPU and then the job that came second, and so on. We
can say that the ready queue acts as a FIFO (First In First Out) queue thus the arriving
jobs/processes are placed at the end of the queue.
FCFS is a non-preemptive scheduling algorithm as a process holds the CPU until it either
terminates or performs I/O. Thus, if a longer job has been assigned to the CPU then many shorter
jobs after it will have to wait. This algorithm is used in most batch operating systems.
Characteristics:
It follows the non-preemptive approach i.e. once a process has control over the CPU it will not
preempt until it terminates.
The criteria for the selection of processes is arrival time. The dispatcher selects the first job in
the ready queue and this job runs to completion of its CPU burst.
The average waiting time is very high so not optimal and thus gives poor performance.
Advantages:
39
FCFS algorithm is simple, easy to implement into any preexisting system, and easy to
understand.
Better for processes with large burst time as there is no context switch involved between
processes.
It is a fair algorithm as priority is not involved, processes that arrive first get served first.
Disadvantages:
Convoy effect occurs i.e. all small processes have to wait for one large process to get off the
CPU.
It is non-preemptive, the process will not release the CPU until it finishes its task and
terminates.
It is not appropriate for interactive systems as it cannot guarantee a short response time.
Average waiting time is high and the turnaround time is unpredictable which leads to poor
performance.
40
Examples to show working of Non-Preemptive First come first serve CPU Scheduling
Algorithm:
Example-1: Consider the following table of arrival time and burst time for five processes P1,
P2, P3, P4 and P5.
Processes Arrival Time Burst Time
P1 0 4
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 arrival time 0
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
41
Remaining
Time Arrival Waiting Execution Initial 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
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
Step 5: At time = 4,
The process P1 completes its execution
Process P5 arrives in waiting queue while process P2 starts executing
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
42
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
Step 7: At time = 7,
Process P3 starts executing, it has burst time of 1 thus, it completes execution at time interval
8
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
Step 8: At time 8,
The process of P3 completes its execution
Process P4 starts executing, it has burst time of 2 thus, it completes execution at time interval
10.
43
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
44
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
45
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
Complexity Analysis:
Time Complexity: O(N)
Auxiliary Space: O(N)
Advantages of FCFS:
The simplest and basic form of CPU Scheduling algorithm
Easy to implement
First come first serve method
Disadvantages of FCFS:
As it is a Non-preemptive CPU Scheduling Algorithm, hence it will run till it finishes the
execution.
46
Average waiting time in the FCFS is much higher than the others
It suffers from Convoy effect.
Not very efficient due to its simplicity
Processes which are at the end of the queue, have to wait longer to finish.
Shortest Job First (or SJF) CPU Scheduling Non-preemptive algorithm using Segment
Tree
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process
with the smallest execution time to execute next. SJN is a non-preemptive algorithm.
Shortest Job first has the advantage of having a minimum average waiting time among
all scheduling algorithms.
It is a Greedy Algorithm.
It may cause starvation if shorter processes keep coming. This problem can be solved using
the concept of ageing.
It is practically infeasible as Operating System may not know burst time and therefore may
not sort them. While it is not possible to predict execution time, several methods can be used
to estimate the execution time for a job, such as a weighted average of previous execution
times. SJF can be used in specialized environments where accurate estimates of running time
are available.
For example:
In the above example, since the arrival time of all the processes is 0, the execution order of the
process is the ascending order of the burst time of the processes. The burst time is given by the
column duration. Therefore, the execution order of the processes is given by:
47
Approach: The following is the approach used for the implementation of the shortest job first:
1. As the name suggests, the shortest job first algorithm is an algorithm which executes the
process whose burst time is least and has arrived before the current time. Therefore, in order
to find the process which needs to be executed, sort all the processes from the given set of
processes according to their arrival time. This ensures that the process with the shortest burst
time which has arrived first is executed first.
2. Instead of finding the minimum burst time process among all the arrived processes by
iterating the
whole struct array, the range minimum of the burst time of all the arrived processes upto the
current time is calculated using segment tree.
3. After selecting a process which needs to be executed, the completion time, turn around
time and waiting time is calculated by using arrival time and burst time of the process. The
formulae to calculate the respective times are:
Completion Time: Time at which process completes its execution.
Examples to show working of Preemptive Shortest Job First CPU Scheduling Algorithm:
Example-1: Consider the following table of arrival time and burst time for five processes P1,
P2, P3, P4 and P5.
Process Burst Time Arrival Time
P1 6 ms 2 ms
48
Process Burst Time Arrival Time
P2 2 ms 5 ms
P3 8 ms 1 ms
P4 3 ms 0 ms
P5 4 ms 4 ms
The Shortest Job First CPU Scheduling Algorithm will work on the basis of steps as mentioned
below:
At time = 0,
Process P4 arrives and starts executing
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time= 1,
Process P3 arrives.
But, as P4 has a shorter burst time. It will continue execution.
Thus, P3 will wait till P4 gets executed.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time =2,
Process P1 arrives with burst time = 6
As the burst time of P1 is more than that of P4
Thus, P4 will continue its execution.
49
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 3,
Process P4 will finish its execution.
Then, the burst time of P3 and P1 is compared.
Process P1 is executed because its burst time is less as compared to P3.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 4,
Process P5 arrives.
Then the burst time of P3, P5, and P1 is compared.
Process P5 gets executed first among them because its burst time is lowest, and process P1
is preempted.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
50
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 5,
Process P2 arrives.
The burst time of all processes are compared,
Process P2 gets executed as its burst time is lowest among all.
Process P5 is preempted.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
P3, P5,
5-6ms P2 5ms P1 1ms 2ms 1ms
At time = 6,
Process P2 will keep executing.
It will execute till time = 8 as the burst time of P2 is 2ms
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
51
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time=7,
The process P2 finishes its execution.
Then again the burst time of all remaining processes is compared.
The Process P5 gets executed because its burst time is lesser than the others.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 10,
The process P5 will finish its execution.
Then the burst time of the remaining processes P1 and P3 is compared.
Thus, process P1 is executed as its burst time is less than P3
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 15,
The process P1 finishes its execution and P3 is the only process left.
P3 will start executing.
52
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 23,
Process P3 will finish its execution.
The overall execution of the processes will be as shown below:
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
53
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
P3, P5,
5-6ms P2 5ms P1 1ms 2ms 1ms
P3, P5,
6-7ms P2 5ms P1 1ms 1ms 0ms
54
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
Now, lets calculate average waiting time and turn around time:
As we know,
Turn Around time = Completion time – arrival time
Waiting Time = Turn around time – burst time
Completion
Process Time Turn Around Time Waiting Time
P1 15 15-2 = 13 13-6 = 7
P2 7 7-5 = 2 2-2 = 0
P3 23 23-1 = 22 22-8 = 14
P4 3 3-0 = 3 3-3 = 0
P5 10 10-4 = 6 6-4 = 2
Now,
Average Turn around time = (13 + 2 + 22 + 3 + 6)/5 = 9.2
Average waiting time = (7 + 0 + 14 + 0 + 2)/5 = 23/5 = 4.6
55
Longest Job First (LJF) is a non-preemptive scheduling algorithm. This algorithm is based on
the burst time of the processes. The processes are put into the ready queue based on their burst
times i.e., in descending order of the burst times. As the name suggests this algorithm is based on
the fact that the process with the largest burst time is processed first. The burst time of only those
processes is considered that have arrived in the system until that time. Its preemptive version is
called Longest Remaining Time First (LRTF) algorithm.
56
Step 2: Choose the process having the highest Burst Time among all the processes that have
arrived till that time.
Step 3: Then process it for its burst time. Check if any other process arrives until this process
completes execution.
Step 4: Repeat the above three steps until all the processes are executed.
P1
1 ms 2 ms
P2
2 ms 4 ms
P3
3 ms 6 ms
P4
4 ms 8 ms
The Longest Job First CPU Scheduling Algorithm will work on the basis of steps as
mentioned below:
At time = 1, Available Process : P1. So, select P1 and start executing.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 2,
Process P2 arrives
As P1 is executing thus, Process P2 will wait in the waiting queue.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
57
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 3,
P1 gets executed, and process P3 arrives
Available Process: P2, P3. So, select P3 and execute 6 ms (since B.T(P3)=6 which is higher
than B.T(P2) = 4)
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 4,
Process P4 arrives,
As P3 is executing thus, Process P4 will wait in the waiting queue
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 5,
Process P3 is executing and P2 and P4 are in the waiting Table.
58
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 9,
Process P3 completes its execution,
Available Process : P2, P4. So, select P4 and execute 8 ms (since, B.T(P4) = 8, B.T(P2) = 4)
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 17,
Finally execute the process P2 for 4 ms.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 21,
Process P2 will finish its execution.
The overall execution of the processes will be as shown below:
59
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
60
Note –
CPU will be idle for 0 to 1 unit time since there is no process available in the given interval.
Gantt chart will be as following below:
Since, completion time (C.T) can be directly determined by Gantt chart, and
Turn Around Time (TAT)
= (Completion Time) – (Arrival Time)
Also, Waiting Time (WT)
= (Turn Around Time) – (Burst Time)
Therefore, final table look like,
Output :
Total Turn Around Time = 40 ms
So, Average Turn Around Time = 40/4 = 10.00 ms
And, Total Waiting Time = 20 ms
So, Average Waiting Time = 20/4 = 5.00 ms
Example-2: Consider the following table of arrival time and burst time for four processes P1,
P2, P3, P4 and P5.
Processes Arrival Time Burst Time
P1 0ms 2ms
P2 0ms 3ms
P3 2ms 2ms
P4 3ms 5ms
P5 4ms 4ms
Gantt chart for this example:
61
Since, completion time (C.T) can be directly determined by Gantt chart, and
Turn Around Time (TAT)
= (Completion Time) – (Arrival Time)
Also, Waiting Time (WT)
= (Turn Around Time) – (Burst Time)
Therefore, final table look like,
Output :
Total Turn Around Time = 44 ms
So, Average Turn Around Time = 44/5 = 8.8 ms
And, Total Waiting Time = 28 ms
So, Average Waiting Time = 28/5 = 5.6 ms
Longest Remaining Time First (LRTF) or Preemptive Longest Job First CPU Scheduling
Algorithm
Longest Remaining Time First (LRTF) is a preemptive version of Longest Job First (LJF)
scheduling algorithm. In this scheduling algorithm, we find the process with the maximum
remaining time and then process it, i.e. check for the maximum remaining time after some
interval of time(say 1 unit each) to check if another process having more Burst Time arrived up
to that time.
Characteristics of Longest Remaining Time First (LRTF)
Among all the processes waiting in a waiting queue, CPU is always assigned to the process
having largest burst time.
If two processes have the same burst time then the tie is broken using FCFS i.e. the process
that arrived first is processed first.
LJF CPU Scheduling can be of both preemptive and non-preemptive type.
Advantages of Longest Remaining Time First (LRTF)
No other process can execute until the longest job or process executes completely.
All the jobs or processes finishes at the same time approximately.
Disadvantages of Longest Remaining Time First (LRTF)
62
This algorithm gives very high average waiting time and average turn-around time for a given
set of processes.
This may lead to convoy effect.
It may happen that a short process may never get executed and the system keeps on executing
the longer processes.
It reduces the processing speed and thus reduces the efficiency and utilization of the system.
Longest Remaining Time First (LRTF) CPU Scheduling Algorithm
Step-1: First, sort the processes in increasing order of their Arrival Time.
Step-2: Choose the process having least arrival time but with most Burst Time.
Step-3: Then process it for 1 unit. Check if any other process arrives upto that time of
execution or not.
Step-4: Repeat the above both steps until execute all the processes.
Examples to show working of Preemptive Longest Job First CPU Scheduling Algorithm:
Example-1: Consider the following table of arrival time and burst time for four processes P1,
P2, P3 and P4.
P1
1 ms 2 ms
P2
2 ms 4 ms
P3
3 ms 6 ms
P4
4 ms 8 ms
The Longest Remaining Time First CPU Scheduling Algorithm will work on the basis of
steps as mentioned below:
At time = 1,
Available Process : P1. So, select P1 and execute 1 ms.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 2,
63
Available Process : P1, P2.
So, select P2 and execute 1 ms (since B.T(P1) = 1 which is less than B.T(P2) = 4)
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 3,
Available Process : P1, P2, P3.
So, select P3 and execute 1 ms (since, B.T(P1) = 1 , B.T(P2) = 3 , B.T(P3) = 6).
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 4,
Available processes: P1, P2, P3, P4.
So, select P4 (as burst time of P4 is largest) and execute 1 ms (since, B.T(P1) = 1 , B.T(P2) =
3 , B.T(P3) = 6, B.T(P4) = 8).
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
64
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 5,
Available processes: P1, P2, P3, P4,
Process P4 will continue its execution as no other process has burst time larger than the
Process P4
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 7,
The processes P3 and P4 have same remaining burst time,
hence If two processes have the same burst time then the tie is broken using FCFS i.e. the
process that arrived first is processed first.
Therefore P3 will get executed for 1ms
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
65
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 8,
Available processes: P1, P2, P3, P4,
Process P4 will again continue its execution as no other process has burst time larger than the
Process P4
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 9,
Available processes: P1, P2, P3, P4,
Process P3 continue its execution on the basis of FCFS rule.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
66
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 10,
Available processes: P1, P2, P3, P4,
Now again the burst time of P4 is largest, thus it will execute further.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 11,
Available processes: P1, P2, P3, P4,
Process P2 will continue its execution as the burst time of P2, P3, P4 is same
Thus, in this case the further execution will be decided on the basis of FCFS i.e. the process
that arrived first is processed first.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
67
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 12,
Available processes: P1, P2, P3, P4,
Process P3 continue its execution on the basis of above explanation.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 13,
Available processes: P1, P2, P3, P4,
Now again the burst time of P4 is largest, thus it will execute further.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
68
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 14,
Available processes: P1, P2, P3, P4
Now, the process P2 will again begin to execute first among all
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 15,
Available processes: P1, P2, P3, P4, now P3 will execute
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
69
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 16,
Available processes: P1, P2, P3, P4,
here, P4 will execute as it has the largest Burst time among all
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 17,
Available processes: P1, P2, P3, P4,
Process P1 will execute here on the basis of above explanation
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
70
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 18,
Available processes: P2, P3, P4,
Process P2 will execute.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 19,
Available processes: P3, P4,
Process P3 will execute.
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 20,
Process P4 will execute at the end.
71
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
At time = 22,
Process P4 will finish its execution.
The overall execution of the processes will be as shown below
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
72
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
73
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
74
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
75
Remaining
Time Arrival Waiting Execution Initial Burst Burst
Instance Process Time Table Time Time Time
Note: CPU will be idle for 0 to 1 unit time since there is no process available in the given
interval.
Gantt chart will be as following below:
76
Since, completion time (C.T) can be directly determined by Gantt chart, and
P1 0ms 2ms
P2 0ms 3ms
P3 2ms 2ms
P4 3ms 5ms
P5 4ms 4ms
77
Since, completion time (CT) can be directly determined by Gantt chart, and
Turn Around Time (TAT)
= (Completion Time) – (Arrival Time)
Also, Waiting Time (WT)
= (Turn Around Time) – (Burst Time)
Therefore, final table look like,
LRTF is a preemptive scheduling algorithm. Its tie-breaker is FCFS and if FCFS does not breaks
the tie then, we use process id as the tie-breaker.
Example: Consider the following table of arrival time and burst time for four processes P1, P2,
P3, and P4.
Process Arrival time Burst Time
P1 1 ms 2 ms
P2 2 ms 4 ms
P3 3 ms 6 ms
p4 4 ms 8 ms
78
Gantt chart will be as following below,
Since completion time (CT) can be directly determined by Gantt chart, and
Turn Around Time (TAT)
= (Completion Time) - (Arrival Time)
P1 0 5 12 12 7
79
Burst Turn Around
Process Arrival Time Time Completion time Time Waiting time
P2 1 4 11 10 6
P3 2 2 6 4 2
P4 3 1 9 6 5
Quantum time is 2 this means each process is only executing for 2 units of time at a time.
How to compute these process requests:-
1. Take the process which occurs first and start executing the process(for quantum time only).
2. Check if any other process request has arrived. If a process request arrives during the quantum
time in which another process is executing, then add the new process to the Ready queue
3. After the quantum time has passed, check for any processes in the Ready queue. If the ready
queue is empty then continue the current process. If the queue not empty and the current
process is not complete, then add the current process to the end of the ready queue.
4. Take the first process from the Ready queue and start executing it (same rules)
5. Repeat all steps above from 2-4
6. If the process is complete and the ready queue is empty then the task is complete
After all these we get the three times which are:
1. Completion Time: the time taken for a process to complete.
2. Turn Around Time: total time the process exists in the system. (completion time – arrival
time).
3. Waiting Time: total time waiting for their complete execution. (turn around time – burst
time ).
Round Robin is a CPU scheduling algorithm where each process is assigned a fixed time slot
in a cyclic way. It is basically the preemptive version of 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.
80
It is simple, easy to implement, and starvation-free as all processes get fair share of CPU.
One of the most commonly used technique in CPU scheduling as 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.
Advantages of Round Robin CPU Scheduling Algorithm:
There is fairness since every process gets equal share of CPU.
The newly created process is added to end of ready queue.
A round-robin scheduler generally employs time-sharing, giving each job a time slot or
quantum.
While performing a round-robin scheduling, a particular time quantum is allotted to
different jobs.
Each process get a chance to reschedule after a particular quantum time in this scheduling.
Disadvantages of Round Robin CPU Scheduling Algorithm:
There is Larger waiting time and Response time.
There is Low throughput.
There is Context Switches.
Gantt chart seems to come too big (if quantum time is less for scheduling. For Example:1
ms for big scheduling.)
Time consuming scheduling for small quantum.
P1 5 ms 0 ms
P2 4 ms 1 ms
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.
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
81
The processes P2 and P3 arrives in the ready queue and P2 starts executing for TQ period
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
82
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
83
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
P1 0 5 12 12-0 = 12 12-5 = 7
P2 1 4 11 11-1 = 10 10-4 = 6
P3 2 2 6 6-2 = 4 4-2 = 2
P4 4 1 9 9-4 = 5 5-1 = 4
Now,
84
Average Turn around time = (12 + 10 + 4 + 5)/4 = 31/4 = 7.7
Average waiting time = (7 + 6 + 2 + 4)/4 = 19/4 = 4.7
Example 2: Consider the following table of arrival time and burst time for three processes P1,
P2 and P3 and given Time Quantum = 2
Process Burst Time Arrival Time
P1 10 ms 0 ms
P2 5 ms 0 ms
P3 8 ms 0 ms
Similarly, Gantt chart for this example:
Now, lets calculate average waiting time and turn around time:
Processes AT BT CT TAT WT
P1 0 10 23 23-0 = 23 23-10 = 13
P2 0 5 15 15-0 = 15 15-5 = 10
P3 0 8 21 21-0 = 21 21-8 = 13
Total Turn Around Time = 59 ms
So, Average Turn Around Time = 59/3 = 19.667 ms
And, Total Waiting Time = 36 ms
So, Average Waiting Time = 36/3 = 12.00 ms
Selfish Round Robin CPU Scheduling
In the traditional Round Robin scheduling algorithm, all processes were treated equally for
processing. The objective of the Selfish Round Robin is to give better service to processes that
have been executing for a while than to newcomers. It’s a more logical and superior
implementation compared to the normal Round Robin algorithm.
Implementation:-
Processes in the ready list are partitioned into two lists: NEW and ACCEPTED.
The New processes wait while Accepted processes are serviced by the Round Robin.
Priority of a new process increases at rate ‘a’ while the priority of an accepted process
increases at rate ‘b’.
When the priority of a new process reaches the priority of an accepted process, that new
process becomes accepted.
If all accepted processes finish, the highest priority new process is accepted.
Let’s trace out the general working of this algorithm:-
85
STEP 1: Assume that initially there are no ready processes, when the first one, A, arrives. It has
priority 0, to begin with. Since there are no other accepted processes, A is accepted immediately.
STEP 2: After a while, another process, B, arrives. As long as b / a < 1, B’s priority will
eventually catch up to A’s, so it is accepted; now both A and B have the same priority.
STEP 3: All accepted processes share a common priority (which rises at rate b ); that makes this
policy easy to implement i.e any new process’s priority is bound to get accepted at some point.
So no process has to experience starvation.
STEP 4: Even if b / a > 1, A will eventually finish, and then B can be accepted.
Explanation –
Process A gets accepted as soon as it comes at time t = 0. So its priority is increased only by ‘b’
i.e ‘1’ after each second. B enters at time t = 1 and goes to the waiting queue. So its priority gets
increased by ‘a’ i.e. ‘2’ at time t = 2. At this point priority of A = priority of B = 2.
So now both processes A & B are in the accepted queue and are executed in a round-robin
fashion. At time t = 3 process C enters the waiting queue. At time t = 6 the priority of process C
86
catches up to the priority of process B and then they start executing in a Round Robin manner.
When B finishes execution at time t = 10, D is automatically promoted to the accepted queue.
Similarly, when D finishes execution at time t = 15, E is automatically promoted to the accepted
queue.
Multilevel Queue (MLQ) CPU Scheduling
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 a background (batch) process. These two classes have different
scheduling needs. For this kind of situation Multilevel Queue Scheduling is used.
Now, let us see how it works.
Advantages of Multilevel Queue CPU Scheduling:
The processes are permanently assigned to the queue, so it has advantage of low scheduling
overhead.
Disadvantages of Multilevel Queue CPU Scheduling:
Some processes may starve for CPU if some higher priority queues are never becoming
empty.
It is inflexible in nature.
Ready Queue is divided into separate queues for each class of processes. For example, let us
take three different types of processes System processes, Interactive processes, and Batch
Processes. All three processes have their own queue. Now, look at the below figure.
Priority of queue 1 is greater than queue 2. queue 1 uses Round Robin (Time Quantum = 2) and
queue 2 uses FCFS.
Below is the gantt chart of the problem :
Working:
At starting, both queues have process so process in queue 1 (P1, P2) runs first (because of
higher priority) in the round robin fashion and completes after 7 units
Then process in queue 2 (P3) starts running (as there is no process in queue 1) but while it is
running P4 comes in queue 1 and interrupts P3 and start running for 5 second and
After its completion P3 takes the CPU and completes its execution.
88
In a multilevel queue-scheduling algorithm, processes are permanently assigned to a queue on
entry to the system and processes are not allowed to move between queues.
As the processes are permanently assigned to the queue, this setup has the advantage of low
scheduling overhead,
But on the other hand disadvantage of being inflexible.
Advantages of Multilevel Feedback Queue Scheduling:
It is more flexible.
It allows different processes to move between different queues.
It prevents starvation by moving a process that waits too long for the lower priority queue to
the higher priority queue.
Disadvantages of Multilevel Feedback Queue Scheduling:
For the selection of the best scheduler, it requires some other means to select the values.
It produces more CPU overheads.
It is the most complex algorithm.
Multilevel feedback queue scheduling, however, allows a process to move between queues.
Multilevel Feedback Queue Scheduling (MLFQ) keeps 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 queues 1 and 2 follow round robin with time quantum 4 and 8
respectively and queue 3 follow FCFS.
Implementation of MFQS is given below –
When a process starts executing the operating system can insert it into any of the above three
queues depending upon its priority. For example, if it is some background process, then the
operating system would not like it to be given to higher priority queues such as queue 1 and 2.
It will directly assign it to a lower priority queue i.e. queue 3. Let’s say our current process for
consideration is of significant priority so it will be given queue 1.
In queue 1 process executes for 4 units and if it completes in these 4 units or it gives CPU for
I/O operation in these 4 units then the priority of this process does not change and if it again
comes in the ready queue then it again starts its execution in Queue 1.
If a process in queue 1 does not complete in 4 units then its priority gets reduced and it shifted
to queue 2.
89
Above points 2 and 3 are also true for queue 2 processes but the time quantum is 8 units. In a
general case if a process does not complete in a time quantum then it is shifted to the lower
priority queue.
In the last queue, processes are scheduled in an FCFS manner.
A process in a lower priority queue can only execute only when higher priority queues are
empty.
A process running in the lower priority queue is interrupted by a process arriving in the higher
priority queue.
Well, the above implementation may differ for example the last queue can also follow Round-
robin Scheduling.
Problems in the above implementation: A process in the lower priority queue can suffer from
starvation due to some short processes taking all the CPU time.
Solution: A simple solution can be to boost the priority of all the processes after regular
intervals and place them all in the highest priority queue.
What is the need for such complex Scheduling?
Firstly, it is more flexible than multilevel queue scheduling.
To optimize turnaround time algorithms like SJF are needed which require the running time of
processes to schedule them. But the running time of the process is not known in advance.
MFQS runs a process for a time quantum and then it can change its priority(if it is a long
process). Thus it learns from past behavior of the process and then predicts its future behavior.
This way it tries to run a shorter process first thus optimizing turnaround time.
MFQS also reduces the response time.
Example: Consider a system that has a CPU-bound process, which requires a burst time of 40
seconds. The multilevel Feed Back Queue scheduling algorithm is used and the queue time
quantum ‘2’ seconds and in each level it is incremented by ‘5’ seconds. Then how many times
the process will be interrupted and in which queue the process will terminate the execution?
Solution:
Process P needs 40 Seconds for total execution.
At Queue 1 it is executed for 2 seconds and then interrupted and shifted to queue 2.
At Queue 2 it is executed for 7 seconds and then interrupted and shifted to queue 3.
At Queue 3 it is executed for 12 seconds and then interrupted and shifted to queue 4.
At Queue 4 it is executed for 17 seconds and then interrupted and shifted to queue 5.
At Queue 5 it executes for 2 seconds and then it completes.
Hence the process is interrupted 4 times and completed on queue 5
90
Procedure for Algorithm
It starts.
It only consider the processes with Arrival time, Burst Time and priority.
It fills the ready Queue according to arrival times.
Highest hybrid priority process is executed first using formula Hp = 0.5 * Ep + 0.5 * R
Steps 4 and 5 repeated until queue becomes empty.
Now, Calculate average waiting time, average turnaround time, number of context switches.
It stop.
Advantages
It is Non-preemptive.
It prevents indefinite postponement (process of starvation).
It improves the performance of shortest process first scheduling.
It considers how long process has been waiting and increases its priority.
Disadvantages
Burst time of the processes can not be known in advance so it can not be implemented
practically.
Processes are scheduled by internal priority system because it does not support external
priority system.
Highest Response Ratio Next (HRRN) CPU Scheduling
Given N processes with their Arrival times and Burst times, the task is to find average waiting
time and an average turn around time using HRRN scheduling algorithm.
The name itself states that we need to find the response ratio of all available processes and select
the one with the highest Response Ratio. A process once selected will run till completion.
Characteristics of HRRN CPU Scheduling:
Highest Response Ratio Next is a non-preemptive CPU Scheduling algorithm and it is
considered as one of the most optimal scheduling algorithm.
The criteria for HRRN is Response Ratio, and the mode is Non-Preemptive.
HRRN is basically considered as the modification of Shortest Job First in order to reduce the
problem of starvation.
In comparison with SJF, during HRRN scheduling algorithm, the CPU is allotted to the next
process which has the highest response ratio and not to the process having less burst time.
Response Ratio = (W + S)/S
Here, W is the waiting time of the process so far and S is the Burst time of the process.
Advantages of HRRN CPU Scheduling
HRRN Scheduling algorithm generally gives better performance than the shortest job
first Scheduling.
There is a reduction in waiting time for longer jobs and also it encourages shorter jobs.
Disadvantages of HRRN CPU Scheduling
The on ground implementation of HRRN scheduling is not possible as it is not possible know
the burst time of every job in advance.
In this scheduling, there may occur overload on the CPU.
91
Performance of HRRN –
Shorter Processes are favoured.
Aging without service increases ratio, longer jobs can get past shorter jobs.
Let us consider the following examples.
Example-1: Consider the following table of arrival time and burst time for four processes P1,
P2, P3, P4 and P5.
Processes Arrival time Burst Time
P1 0ms 3ms
P2 2ms 6ms
P3 4ms 4ms
P4 6ms 5ms
P5 8ms 2ms
The Highest Response Ratio Next CPU Scheduling Algorithm will work on the basis of
steps as mentioned below:
At time= 0,
Available Processes: P1, Hence P1 starts executing till its completion.
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
At time = 2,
Available Processes: P1, P2
But P1 keep executing as HRRN is a non-preemptive algorithm and thus it will finish its
execution
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
92
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
At time = 3,
Process P1 finish its execution
Process P2 starts executing as it is only process available.
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
At time = 4,
Process P3 arrives and wait for process P2 to execute
Process P2 continue its execution
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
At time = 6,
Process P4 arrives and wait for the process P2 to execute
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
93
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
At time = 8,
Process P5 arrives and wait for its execution in the ready queue
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
P3, P4,
8-9ms P5 8ms P5 P2 0ms 2ms 2ms
At time = 9,
Process P2 completes its execution
Now there are 3 processes available, P3, P4 and P5. Since, P3, P4, P5 were available after 4, 6
and 8 units respectively.
Therefore, waiting time for P3, P4 and P5 are (9 – 4 =)5, (9 – 6 =)3, and (9 – 8 =)1 unit
respectively.
Using the formula given above, (Response Ratio = (W + S)/S) calculate the Response Ratios
of P3, P4 and P5 respectively as 2.25, 1.6 and 1.5.
Clearly, P3 has the highest Response Ratio and so it gets scheduled
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
94
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
At time = 13,
Available Processes: P4 and P5
Response Ratios of P4 and P5 are 2.4 and 3.5 respectively using the above formula.
Clearly, P5 has the highest Response Ratio and so it gets scheduled
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
At time = 15,
After completion of process P5, Process P4 is selected at last and execute till it gets finished
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
At time = 20,
Process P4 will finish its execution.
The overall execution of the processes will be as shown below:
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
95
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
P3, P4,
8-9ms P5 8ms P5 P2 0ms 2ms 2ms
96
Initial Remaining
Time Arrival Ready Running Execution Burst Burst
Instance Process Time Queue Queue Time Time Time
Gantt Chart –
Since, completion time (C.T) can be directly determined by Gantt chart, and
Turn Around Time (TAT)
= (Completion Time) – (Arrival Time)
Also, Waiting Time (WT)
= (Turn Around Time) – (Burst Time)
Therefore, final table look like,
97
A C
Processes T BT T TAT WT
P1 0 3 3 3-0 = 3 3-3 = 0
P2 2 6 9 9-2 = 7 7-6 = 1
P3 4 4 13 13-4 = 9 9-4 = 5
P4 6 5 20 20-6 = 14 14-5 = 9
P5 8 2 15 15-8 = 7 7-2 = 5
Output:
Total Turn Around Time = 40 ms
So, Average Turn Around Time = 40/5 = 8.00 ms
And, Total Waiting Time = 20 ms
So, Average Waiting Time = 20/5 = 4.00 ms
Implementation of HRRN Scheduling –
Input the number of processes, their arrival times and burst times.
Sort them according to their arrival times.
At any given time calculate the response ratios and select the appropriate process to be
scheduled.
Calculate the turn around time as completion time – arrival time.
Calculate the waiting time as turn around time – burst time.
Turn around time divided by the burst time gives the normalized turn around time.
Sum up the waiting and turn around times of all processes and divide by the number of
processes to get the average waiting and turn around time.
98
highest priority is executed first. Priorities can be defined internally as well as externally.
Internal priorities are decided by the system depending upon the number of resources required,
time needed etc. whereas external priorities are based upon the time in which the work is needed
or the amount being paid for the work done or the importance of process. Priority scheduling can
be preemptive or non- preemptive.
Note:
If two processes have the same priority then tie is broken using FCFS.
The waiting time for the highest priority process is always zero in preemptive mode while it
may not be zero in case of non preemptive mode.
Disadvantages: The major problem is the starvation or indefinite blocking. It may so happen
that in stream of processes, the system keeps executing the high priority processes and the low
priority processes never get executed. Solution: A solution to the problem is aging. Aging refers
to gradually increasing the priority of processes waiting in the system by a fixed number after a
fixed interval, say by 1 in every 10 minutes. This will ensure that a low priority process also gets
executed by slowly increasing its priority with the time.
Differences between FCFS and Priority scheduling algorithm are as follows:
It executes the processes in the order in It executes the processes based upon their prioritied
which they arrive i.e., the process that i.e., in descending order of their priorities. A process
arrives first is executed first. with higher priority is executed first.
99
Comparison of Different CPU Scheduling Algorithms in OS
A scheduling algorithm is used to estimate the CPU time required to allocate to the processes and
threads. The prime goal of any CPU scheduling algorithm is to keep the CPU as busy as possible
for improving CPU utilization.
Scheduling Algorithms
1. First Come First Serve(FCFS): As the name implies that the jobs are executed on a first
come first serve basis. It is a simple algorithm based on FIFO that’s first in first out. The process
that comes first in the ready queue can access the CPU first. If the arrival time is less, then the
process will get the CPU soon. It can suffer from the convoy effect if the burst time of the first
process is the longest among all the jobs.
2. Shortest Job First (SJF): Also known as the shortest job first or shortest job next is a non-
preemptive type algorithm that is easy to implement in batch systems and is best in minimizing
the waiting time. It follows the strategies where the process that is having the lowest execution
time is chosen for the next execution.
3. Longest Job First Scheduling(LJF): The longest job first (LJF) is the non-preemptive
version. This algorithm is also based upon the burst time of the processes. The processes are put
into the ready queue according to their burst times and the process with the largest burst time is
processed first.
4. Longest Remaining Time First Scheduling (LRTF): The preemptive version of LJF is
LRTF. Here the process with the maximum remaining CPU time will be considered first and
then processed. And after some time interval, it will check if another process having more Burst
Time arrived up to that time or not. If any other process has more remaining burst time, so the
running process will get pre-empted by that process.
5. Shortest Remaining Time First(SRTF): This algorithm is based on SJF and this is the
preemptive version of SJF. In this scheduling algorithm, the process with the smallest remaining
burst time is executed first and it may be preempted with a new job that has arrived with a
shorter execution time.
6. Round Robin(RR): It is a preemptive scheduling algorithm in which each process is given a
fixed time called quantum to execute. At this time one process is allowed to execute for a
quantum and then preempts and then another process is executed. In this way, there is context
switching between processes to save states of these preempted processes.
7. Priority Scheduling: It is a non-preemptive algorithm that works in batch systems and, each
process is assigned with a priority and the process with the highest priority is executed first. This
can lead to starvation for other processes.
8. Multiple Levels Queues Scheduling: In this scheduling, multiple queues have their
scheduling Algorithms and are maintained with the processes that possess the same
characteristics. For this, priorities are assigned to each queue for the jobs to be executed.
9. Multilevel-Feedback-Queue Scheduler: It defines several queues and the scheduling
algorithms for each queue. This algorithm is used to determine when to upgrade a process when
to demote a process, and also determine the queue in which a process will enter and when that
process needs service.
Note: The SJF scheduling algorithm is hypothetical and un-implementable, as it is impossible to
determine the burst time of any process without running it. SJF is a benchmarking algorithm as
it provides minimum waiting time than any other scheduling algorithm
100
Here is a brief comparison between different CPU scheduling algorithms:
Average
waiting
Allocation time Preemptio
Algorithm is Complexity (AWT) n Starvation Performance
According
to the
arrival time
of the
processes,
the CPU is Not Slow
FCFS allocated. complex Large. No No performance
Based on Minimum
the lowest More Average
CPU burst complex Smaller Waiting
SJF time (BT). than FCFS than FCFS No Yes Time
Depending
on some
measures
Based on e.g., arrival
the highest More time,
CPU burst complex process Big turn-
LJFS time (BT) than FCFS size, etc. No Yes around time
Same as
LJFS the
allocation
of the CPU Depending
is based on on some
the highest measures
CPU burst e.g., arrival The
time (BT). More time, preference is
But it is complex process given to the
LRTF preemptive than FCFS size, etc. Yes Yes longer jobs
101
Average
waiting
Allocation time Preemptio
Algorithm is Complexity (AWT) n Starvation Performance
Same as
SJF the
allocation
of the CPU Depending
is based on on some
the lowest measures
CPU burst e.g., arrival The
time (BT). More time, preference is
But it is complex process given to the
SRTF preemptive. than FCFS size, etc Yes Yes short jobs
According
to the order
of the
process Large as Each
arrives with The compared process has
fixed time complexity to SJF and given a
quantum depends on Priority fairly fixed
RR (TQ) TQ size scheduling. No No time
According
to the
priority. Well
The bigger performance
priority task This type is but contain a
Priority Pre- executes less Smaller starvation
emptive first complex than FCFS Yes Yes problem
102
Average
waiting
Allocation time Preemptio
Algorithm is Complexity (AWT) n Starvation Performance
monitoring
the new
incoming
higher
priority
jobs
According
to the More
process that complex Good
resides in than the performance
the bigger priority but contain a
queue scheduling Smaller starvation
MLQ priority algorithms than FCFS No Yes problem
It is the most
According Complex but
to the its Smaller
process of a complexity than all
bigger rate depends scheduling
priority on the TQ types in Good
MFLQ queue. size many cases No No performance
103
Non-Preemptive Scheduling: In this, once a process enters the running state it cannot be
preempted until it completes it’s allocation time. When scheduling takes place only under the
below circumstances we say that the scheduling scheme is non-preemptive or cooperative.
When a process switches from the running state to the waiting state (for example, as the
result of an I/O request or an invocation of wait() for the termination of a child process).
When a process terminates.
Let us see the difference between Preemptive Scheduling and Non-Preemptive Scheduling:
The executing process here is interrupted in the The executing process here is not
middle of execution. interrupted in the middle of execution.
104
Preemptive scheduling is better than non-preemptive scheduling or vice-versa can’t be said
definitely. It depends on how scheduling minimizes the average waiting time of the processes
and maximizes CPU utilization.
Difference between Turn Around Time (TAT) and Waiting Time (WT) in CPU Scheduling
In CPU Scheduling, we often need to find the average Turnaround and Waiting Time with the
help of Arrival, Burst and Completion Time. Let’s have a brief look of them: Turnaround Time
(TAT):
1. It is the time interval from the time of submission of a process to the time of the completion of
the process.
2. The difference b/w Completion Time and Arrival Time is called Turnaround Time.
Completion Time (CT): This is the time when the process completes its execution. Arrival
Time (AT): This is the time when the process has arrived in the ready state.
TAT = CT - AT
Waiting Time (WT):
1. The time spent by a process waiting in the ready queue for getting the CPU.
2. The time difference b/w Turnaround Time and Burst Time is called Waiting Time.
Burst Time (BT): This is the time required by the process for its execution.
WT = TAT - BT
Now with Waiting Time and Burst Time, we can also calculate Turn Around Time via:
TAT = BT + WT
Example:
Process Burst Time (in sec.)
P1 24
P2 3
P3 4
105
Turn Around Time Waiting Time
1 0 3
2 1 2
3 2 4
4 3 5
106
Process AT BT
5 4 6
Process AT BT
1 0 3
2 1 2
3 2 4
4 3 5
5 4 6
107
LJF LRJF
108
Similarities:
Both SJF and SRJF are practically not feasible as it is not possible to predict the burst time of
the processes.
Both SJF and SRJF may lead to process starvation as long processes may be held off
indefinitely if short processes are continually added.
Both SJF and SRJF are considered same once all the processes are available in the ready
queue. This is because after the processes are added in the ready queue, no preemption is
carried out.
Differences:
Shortest Job First: Shortest Remaining Job First:
It involves less overheads than SRJF. It involves more overheads than SJF.
It minimizes the average waiting time for each It may or may not minimize the average waiting
process. time for each process.
It may suffer from priority inversion. It may suffer from convoy effect.
109
It involves lesser number of context switching. It involves higher number of context switching.
Short processes are executed first and then Shorter processes run fast and longer processes
followed by longer processes. show poor response time.
First Come First Served (FCFS) executes the Shortest Job First (SJF) executes the
processes in the order in which they arrive i.e. processes based upon their burst time i.e. in
the process that arrives first is executed first. ascending order of their burst times.
110
First Come First Served (FCFS) Shortest Job First (SJF)
FCFS leads to the convoy effect. It does not lead to the convoy effect.
FCFS algorithm is the easiest to implement in The real difficulty with SJF is knowing the
any system. length of the next CPU request or burst.
A process may have to wait for quite long to get A long process may never get executed and
executed depending on the burst time of the the system may keep executing the short
processes that have arrived first. processes.
FCFS lead to lower device and CPU utilization SJF leads to higher effectiveness of the
thereby decreasing the efficiency of the system. system due to lower average waiting time.
FCFS does not suffers from starvation SJF suffers from starvation.
111
2. Burst Time (BT) :
Burst Time refers to the time required in milli seconds by a process for its execution. The Burst
Time takes into consideration the CPU time of a process. The I/O time is not taken into
consideration. It is called as the execution time or running time of the process. The process
makes a transition from the Running state to the Completion State during this time frame.
Burst time can be calculated as the difference of the Completion Time of the process and the
Waiting Time, that is,
Burst Time (B.T.)
= Completion Time (C.T.) - Waiting Time (W.T.)
The following table illustrates the Arrival and Burst time of three processes P1, P2 and P3. A
single CPU is allocated for the execution of these processes.
P1 0 3
P2 4 2
P3 6 4
If we compute the Gantt chart, based on FCFS scheduling where the process that comes first in
the ready queue is executed first. The processes arrival decides the order of execution of the
process for time equal to its Burst time.
Since, the process P2 arrives at 4ms, and process P1 requires 3ms for its execution (=Burst
Time), CPU waits for 1ms, that is the idle time for the CPU, where it doesn’t perform any
process execution. The last process to get executed is P3.
The following table illustrates the key differences in the Arrival and Burst Time respectively :
112
Arrival Time Burst Time
Marks the entry point of the process in Marks the exit point of the process in the
the queue. queue.
Difference between Priority Scheduling and Round Robin (RR) CPU scheduling
1. Priority Scheduling Algorithm :
Priority scheduling algorithm executes the processes depending upon their priority. Each process
is allocated a priority and the process with the highest priority is executed first. Priorities can be
defined internally as well as externally. Internal priorities are decided by the system depending
upon the number of resources required, time needed etc. whereas external priorities are based
upon the time in which the work is needed or the amount being paid for the work done or the
importance of process. Priority scheduling can be preemptive or non- preemptive.
Note –
If two processes have the same priority then tie is broken using FCFS.
The waiting time for the highest priority process is always zero in preemptive mode while it
may not be zero in case of non preemptive mode.
Disadvantages:
The major problem is the starvation or indefinite blocking. It may so happen that in stream of
processes, the system keeps executing the high priority processes and the low priority processes
never get executed.
2. Round-Robin (RR) :
Round-Robin (RR) Scheduling Algorithm is particularly designed for time sharing systems. The
processes are put into the ready queue which is a circular queue in this case. In this case a small
unit of time known as time quantum is defined. The algorithm selects the first process from the
queue and executes it for the time defined by the time quantum. If the process has burst time less
than the time quantum then the CPU executes the next process but if it has burst time higher than
the time quantum then the process is interrupted and next process is executed for same time
quantum. If a process is interrupted then a context switch happens and the process is put back at
the tail of the queue. It is preemptive in nature.
This algorithm mainly depends on the time quantum. Very large time quantum makes RR same
as the FCFS while a very small time quantum will lead to the overhead as context switch will
happen again and again after very small intervals.
The major advantage of this algorithm is that all processes get executed one after the other which
does not lead to starvation of processes or waiting by process for quite long time to get executed.
113
The difference between Priority Scheduling and Round-Robin (RR) scheduling algorithm are as
follows:
The average waiting time and The average waiting time for given
average response time is set of processes is quite small and
unknown beforehand. depends on the time quantum.
Courses@Sale
Discuss
Practice
Video
114
1. Earliest Deadline First (EDF) :
In Earliest Deadline First scheduling algorithm, at every scheduling point the task having the
shortest deadline is scheduled for the execution. It is an optimal dynamic priority-driven
scheduling algorithm used in real-time systems. It uses priorities of the tasks for scheduling. In
EDF, priorities to the task are assigned according to the absolute deadline. The task having
shortest deadline gets the highest priority.
Example –
Suppose here are two processes P1 and P2.
Let the period of P1 be p1 = 50
Let the processing time of P1 be t1 = 25
Let the period of P2 be p2 = 75
Let the processing time of P2 be t2 = 30
Explanation :
1. Deadline pf P1 is earlier, so priority of P1>P2.
2. Initially P1 runs and completes its execution of 25 time.
3. After 25 times, P2 starts to execute until 50 times, when P1 is able to execute.
4. Now, comparing the deadline of (P1, P2) = (100, 75), P2 continues to execute.
5. P2 completes its processing at time 55.
6. P1 starts to execute until time 75, when P2 is able to execute.
7. Now, again comparing the deadline of (P1, P2) = (100, 150), P1 continues to execute.
8. Repeat the above steps.
9. Finally at time 150, both P1 and P2 have the same deadline, so P2 will continue to execute till
its processing time after which P1 starts to execute.
2. Least Slack Time (LST) :
In Least Slack Time scheduling algorithm, at every scheduling point the task having the
minimum laxity is executed first. It is also a dynamic priority-driven scheduling algorithm used
in real-time systems. It assigns some priority to all the tasks in the system according to their
slack time. The task having the least slack time (laxity) gets the highest priority.
Example –
Process P1:
Arrival Time=0, Duration=10, Deadline=33
Process P2:
Arrival Time=4, Duration=3, Deadline=28
Process P3:
Arrival Time=5, Duration=10, Deadline=29
Explanation :
At time t=0:
Only process P1 has arrived.
P1 is executed till time t=4.
At time t=4: P2 has arrived.
Slack time of P1: 33-4-6=23
Slack time of P2: 28-4-3=21
Hence P2 starts to execute till time t=5 when P3 arrives.
At time t=5:
Slack Time of P1: 33-5-6=22
Slack Time of P2: 28-5-2=21
115
Slack Time of P3: 29-5-10=12
Hence P3 starts to execute till time t=13
At time t=13:
Slack Time of P1: 33-13-6=14
Slack Time of P2: 28-13-2=13
Slack Time of P3: 29-13-2=14
Hence P2 starts to execute till time t=15
At time t=15:
Slack Time of P1: 33-15-6=12
Slack Time of P3: 29-15-2=12
Hence P3 starts to execute till time t=16
At time t=16:
Slack Time of P1: 33-16-6=11
Slack Time of P3:29-16-=12
Hence P1 starts to execute till time t=18 and so on.
116
EDF LST
Difference between Priority scheduling and Shortest Job First (SJF) CPU scheduling
1. Priority Scheduling Algorithm :
Priority scheduling algorithm executes the processes depending upon their priority. Each process
is allocated a priority and the process with the highest priority is executed first. Priorities can be
defined internally as well as externally. Internal priorities are decided by the system depending
upon the number of resources required, time needed etc. whereas external priorities are based
upon the time in which the work is needed or the amount being paid for the work done or the
importance of process. Priority scheduling can be preemptive or non- preemptive.
Note –
If two processes have the same priority then tie is broken using FCFS.
The waiting time for the highest priority process is always zero in preemptive mode while it
may not be zero in case of non preemptive mode.
Disadvantages:
The major problem is the starvation or indefinite blocking. It may so happen that in stream of
processes, the system keeps executing the high priority processes and the low priority processes
never get executed.
2. Shortest Job First (SJF) :
Shortest Job First (SJF) Scheduling Algorithm is based upon the burst time of the process. The
processes are put into the ready queue based on their burst times. In this algorithm, the process
with the least burst time is processed first. The burst time of only those processes is compared
that are present or have arrived until that time. It is also non-preemptive in nature. Its preemptive
version is called Shortest Remaining Time First (SRTF) algorithm.
The major advantage of this algorithm is that it gives the minimum waiting time for a given set
of processes and thus reduces the average waiting time. The disadvantage of this algorithm is
that long processes may never be processed by the system and may remain in the queue for very
long time leading to starvation of processes.
Note –
If two processes have same burst time then the tie is broken using FCFS, i.e., the process that
arrived first is processed first.
The difference between Shortest job first (SJF) and Priority Scheduling algorithm are as follows:
117
Shortest job first (SJF) Priority scheduling
Difference between First Come First Served (FCFS) and Round Robin (RR) Scheduling
Algorithm
First Come First Served Scheduling Algorithm:
First Come First Served (FCFS) is the simplest and non-preemptive scheduling algorithm. In
First Come First Served (FCFS), the process is allocated to the CPU in the order of their arrival.
A queue data structure is used to implement the FCFS scheduling algorithm. The process which
is at the head of the ready queue is allocated to the CPU, when CPU is free. Then the process
which is running is removed from the queue. When a new process enters into the ready queue, it
is placed onto the tail of the ready queue.
118
Round Robin Scheduling Algorithm:
Round Robin (RR) Scheduling Algorithm is design for the time sharing system. This algorithm
is the preemptive scheduling algorithm. In Round Robin Scheduling Algorithm a small unit of
time called as time quantum or time slice for which the CPU is provided to each job. CPU is
allocated to the each job for the duration equal to the time quantum in cyclic order. This time
quantum, time slice or time interval is generally of the order of 10 to 100 milliseconds. Ready
queue in the Round Robin Scheduling Algorithm is treated as the circular queue.
The difference between First Come First Served (FCFS) and Round Robin(RR) scheduling
algorithm are as follows:
First Come First Served (FCFS) is the Round Robin(RR) is the preemptive scheduling
1. non-preemptive scheduling algorithm. algorithm.
FCFS is inconvenient to use in the It is mainly designed for the time sharing
3. time sharing system. system and hence convenient to use.
The process is simply processed in the It is similar like FCFS in processing but uses
6. order of their arrival in FCFS. time quantum.
Difference between Shortest Job First (SJF) and Round-Robin (RR) scheduling algorithms
1.Shortest Job First (SJF) :
Shortest Job First (SJF) Scheduling Algorithm is based upon the burst time of the process. The
119
processes are put into the ready queue based on their burst times. In this algorithm, the process
with the least burst time is processed first. The burst time of only those processes is compared
that are present or have arrived until that time. It is also non-preemptive in nature. Its preemptive
version is called Shortest Remaining Time First (SRTF) algorithm.
The major advantage of this algorithm is that it gives the minimum waiting time for a given set
of processes and thus reduces the average waiting time. The disadvantage of this algorithm is
that long processes may never be processed by the system and may remain in the queue for very
long time leading to starvation of processes.
Note –
If two processes have same burst time then the tie is broken using FCFS, i.e., the process that
arrived first is processed first.
2.Round-Robin (RR) :
Round-Robin (RR) Scheduling Algorithm is particularly designed for time sharing systems. The
processes are put into the ready queue which is a circular queue in this case. In this case a small
unit of time known as time quantum is defined. The algorithm selects the first process from the
queue and executes it for the time defined by the time quantum. If the process has burst time less
than the time quantum then the CPU executes the next process but if it has burst time higher than
the time quantum then the process is interrupted and next process is executed for same time
quantum. If a process is interrupted then a context switch happens and the process is put back at
the tail of the queue. It is preemptive in nature.
This algorithm mainly depends on the time quantum. Very large time quantum makes RR same
as the FCFS while a very small time quantum will lead to the overhead as context switch will
happen again and again after very small intervals.
The major advantage of this algorithm is that all processes get executed one after the other which
does not lead to starvation of processes or waiting by process for quite long time to get executed.
The difference between Shortest Job First (SJF) and Round-Robin (RR) scheduling algorithm are
as follows:
120
Shortest Job First (SJF) Round-Robin (RR)
The average waiting time for The average waiting time for given
given set of processes is set of processes is quite small and
minimum. depends on the time quantum.
A long process may never get Each process is executed and every
executed and the system may user feels that his work is being done
keep executing the short as the CPU gives equal amount of
processes. time to each process.
short processes are executed first and at Long processes are executed first and at
any instant if a process with shorter time any instant of time if a long process
121
Shortest Remaining Job First (SRJF) Longest Remaining Job First(LRJF)
More process are executed in less amount of Less process are executed in certain amount of
time time
P1 0 2
P2 0 4
P3 0 8
122
Turn Around
Processes Arrival Time Burst Time Completion Time Time Waiting Time
P1 0 2 12 12 10
P2 0 4 13 13 9
P3 0 8 14 14 6
P1 0 20
P2 15 25
P3 30 10
123
Processes Arrival Time Burst Time
P4 45 15
Turn Around
Processes Arrival Time Burst Time Completion Time Time Waiting Time
P1 0 20 20 20 0
P2 15 25 55 40 15
P3 30 10 40 10 0
P4 45 15 70 25 10
124
Average waiting time,
= [(20-20) + (40-25) + (10-10) + (25-15)] / 4
= (0 + 15 + 0 + 10) / 4
= 6.25
Difference between Multilevel Queue (MLQ) and Multi Level Feedback Queue (MLFQ)
CPU scheduling algorithms
Last Updated : 03 Nov, 2022
In multi programming environment, it often happens that more than one processes compete for
CPU resources at the same time. If only one CPU is available choice has to be made between
processes to run next. Part of the Operating System responsible for making choice of process is
called Scheduler and the algorithm it used is called Scheduling Algorithm.
The objective of multi programming is to maximize CPU utilization. Criteria like Turnaround
time, Response time, Waiting time, Throughput are suggested on basis of which scheduling
algorithms are judged. There are many CPU scheduling algorithms two of which are-
1. Multilevel Queue Scheduling
2. Multilevel Feedback Queue Scheduling
Difference between Multilevel Queue (MLQ) and Multi Level Feedback Queue (MLFQ)
CPU scheduling algorithms :
Multilevel feedback queue scheduling
Multilevel queue scheduling (MLQ) (MLFQ)
In this algorithm queue are classified into two Here, queues are classified as higher
groups, first containing background processes and priority queue and lower priority queues. If
second containing foreground processes. process takes longer time in execution it is
80% CPU time is given to foreground queue using moved to lower priority queue.
Round Robin Algorithm and 20% time is given to Thus, this algorithm leaves I/O bound and
background processes using First Come First Serve interactive processes in higher priority
Algorithm. queue.
The priority is fixed in this algorithm. When all The priority for process is dynamic as
125
Multilevel feedback queue scheduling
Multilevel queue scheduling (MLQ) (MLFQ)
126
1. System Process Queue
2. Interactive Processes Queue
3. Interactive Editing Processes Queue
4. Batch Processes Queue
5. Student Processes Queue
Here, all queues have their own scheduling algorithm, and process is chosen with highest
priority. Then it is executed preemptive or non-preemptively. No process in lower priority queue
can get executed until higher process queue are all empty.
For example, if batch process queue is running and interactive process comes in ready state batch
process is preempted and interactive process is allowed to execute.
2. Example for Multilevel Feedback Queue Scheduling (MLFQ) :
Now, let us consider multilevel feedback queue with three queues.
127
Difference Between Long-Term and Short-Term Scheduler:
Long-Term Scheduler takes the process from job Short-Term Scheduler takes the process
pool. from ready queue.
It regulates the more DOM (Degree of Multi- It regulates the less DOM (Degree of
programming). Multi-programming).
It regulates the programs which are selected to It ensures which program is suitable or
system for processing. important for processing.
Long-Term Scheduler changes the process state Short-Term Scheduler changes the
from New to Ready. process state from Ready to Running.
128
Long-Term Scheduler Short-Term Scheduler
It select a good process, mix of I/O bound and CPU It select a new process for a CPU quite
bound. frequently.
129
Shortest Job First (SJF) Longest Job First (LJF)
Short processes are executed first and then Longer processes are executed first and then
followed by longer processes. followed by shorter processes.
It does not lead to convoy effect. It might lead to the convoy effect.
Processes with longer burst may starve. Processes with shorter burst may starve.
P1 0 10
P2 1 5
P3 2 8
P4 2 15
Let’s try and solve this problem using both SJF and LJF to do a comparative study.
1. Longest Job First:
The Gantt chart will look like:
130
Average waiting time (AWT),
= ((0-0) + (33-1) + (25-2) + (10-2)) / 4
= 63/4
= 15.75
131
= 6.25
132
The basic difference between CPU and GPU is that CPU emphasis on low latency. Whereas,
GPU emphasis on high throughput.
Let’s see the difference between CPU and GPU:
133
S.NO CPU GPU
134
SR.NO. Preemptive Multitasking Cooperative Multitasking
135
Example –
Suppose there are two tasks that need to be executed.
Task1 has release time 0, period 7 units, execution time 2 units, and deadline of 6 units ( T1
( 0, 7, 2, 6 ) ). Task2 has release time 0, period 5 units, execution time 2 units, and deadline of
4 units ( T2 ( 0, 5, 2, 4 ) ).
136
At T=2 only T1 is available so T1 gets executed till T=4. Now T1 will be available at T=7.
At T=4 to T=5 CPU remains idle as not task is available for execution.
At T=5 only T2 is available so T2 gets executed till T=7. Now T2 will be available at T=10.
At T=7 only T1 is available so T1 gets executed till T=9. Now T1 will be available at T=14.
At T=9 to T=10 CPU remains idle as not task is available for execution.
At T=10 only T2 is available so T2 gets executed till T=12. Now T2 will be available at
T=15.
At T=12 to T=14 CPU remains idle as not task is available for execution.
At T=14 only T1 is available so T1 gets executed till T=15. Still 1 unit of work for T1 is
remaining.
At T=15 both T1 and T2 are available, but deadline (T2) < deadline (T1). So T2 gets CPU
and gets executed till T=17. Now T2 will be available at T=20.
Complete execution process is shown in figure below –
Advantages :
Optimal for Static Priority Scheduling.
Performs well in case of availability of tasks having longer period but shorter deadline.
Good performance in case of overload.
Disadvantages :
Implementation is complex.
It is a time taking process.
Rate-monotonic scheduling
Rate monotonic scheduling is a priority algorithm that belongs to the static priority scheduling
category of Real Time Operating Systems. It is preemptive in nature. The priority is decided
according to the cycle time of the processes that are involved. If the process has a small job
duration, then it has the highest priority. Thus if a process with highest priority starts execution,
it will preempt the other running processes. The priority of a process is inversely proportional to
the period it will run for.
A set of processes can be scheduled only if they satisfy the following equation :
137
Where n is the number of processes in the process set, Ci is the computation time of the process,
Ti is the Time period for the process to run and U is the processor utilization.
Example:
An example to understand the working of Rate monotonic scheduling algorithm.
Execution Time Time period
Processes (C) (T)
P1 3 20
P2 2 5
P3 2 10
138
Above figure says that, Process P2 will execute two times for every 5 time units, Process P3
will execute two times for every 10 time units and Process P1 will execute three times in 20
time units. This has to be kept in mind for understanding the entire execution of the algorithm
below.
Process P2 will run first for 2 time units because it has the highest priority. After completing
its two units, P3 will get the chance and thus it will run for 2 time units.
As we know that process P2 will run 2 times in the interval of 5 time units and process P3 will
run 2 times in the interval of 10 time units, they have fulfilled the criteria and thus now
process P1 which has the least priority will get the chance and it will run for 1 time. And here
the interval of five time units have completed. Because of its priority P2 will preempt P1 and
thus will run 2 times. As P3 have completed its 2 time units for its interval of 10 time units, P1
will get chance and it will run for the remaining 2 times, completing its execution which was
thrice in 20 time units.
Now 9-10 interval remains idle as no process needs it. At 10 time units, process P2 will run
for 2 times completing its criteria for the third interval ( 10-15 ). Process P3 will now run for
two times completing its execution. Interval 14-15 will again remain idle for the same reason
mentioned above. At 15 time unit, process P2 will execute for two times completing its
execution.This is how the rate monotonic scheduling works.
Conditions :
The analysis of Rate monotonic scheduling assumes few properties that every process should
possess. They are :
1. Processes involved should not share the resources with other processes.
2. Deadlines must be similar to the time periods. Deadlines are deterministic.
3. Process running with highest priority that needs to run, will preempt all the other processes.
4. Priorities must be assigned to all the processes according to the protocol of Rate monotonic
scheduling.
Advantages :
139
1. It is easy to implement.
2. If any static priority assignment algorithm can meet the deadlines then rate monotonic
scheduling can also do the same. It is optimal.
3. It consists of calculated copy of the time periods unlike other time-sharing algorithms as
Round robin which neglects the scheduling needs of the processes.
Disadvantages :
1. It is very difficult to support aperiodic and sporadic tasks under RMA.
2. RMA is not optimal when tasks period and deadline differ.
One approach is when all the scheduling decisions and I/O processing are handled by a single
processor which is called the Master Server and the other processors executes only the user
code. This is simple and reduces the need of data sharing. This entire scenario is
called Asymmetric Multiprocessing.
A second approach uses Symmetric Multiprocessing where each processor is self scheduling.
All processes may be in a common ready queue or each processor may have its own private
queue for ready processes. The scheduling proceeds further by having the scheduler for each
processor examine the ready queue and select a process to execute.
Processor Affinity –
Processor Affinity means a processes has an affinity for the processor on which it is currently
running.
When a process runs on a specific processor there are certain effects on the cache memory. The
data most recently accessed by the process populate the cache for the processor and as a result
successive memory access by the process are often satisfied in the cache memory. Now if the
process migrates to another processor, the contents of the cache memory must be invalidated for
the first processor and the cache for the second processor must be repopulated. Because of the
high cost of invalidating and repopulating caches, most of the SMP(symmetric multiprocessing)
systems try to avoid migration of processes from one processor to another and try to keep a
process running on the same processor. This is known as PROCESSOR AFFINITY.
There are two types of processor affinity:
1. Soft Affinity – When an operating system has a policy of attempting to keep a process
running on the same processor but not guaranteeing it will do so, this situation is called soft
affinity.
2. Hard Affinity – Hard Affinity allows a process to specify a subset of processors on which it
may run. Some systems such as Linux implements soft affinity but also provide some system
calls like sched_setaffinity() that supports hard affinity.
140
Load Balancing –
Load Balancing is the phenomena which keeps the workload evenly distributed across all
processors in an SMP system. Load balancing is necessary only on systems where each
processor has its own private queue of process which are eligible to execute. Load balancing is
unnecessary because once a processor becomes idle it immediately extracts a runnable process
from the common run queue. On SMP(symmetric multiprocessing), it is important to keep the
workload balanced among all processors to fully utilize the benefits of having more than one
processor else one or more processor will sit idle while other processors have high workloads
along with lists of processors awaiting the CPU.
There are two general approaches to load balancing :
1. Push Migration – In push migration a task routinely checks the load on each processor and if
it finds an imbalance then it evenly distributes load on each processors by moving the
processes from overloaded to idle or less busy processors.
2. Pull Migration – Pull Migration occurs when an idle processor pulls a waiting task from a
busy processor for its execution.
Multicore Processors –
In multicore processors multiple processor cores are places on the same physical chip. Each
core has a register set to maintain its architectural state and thus appears to the operating system
as a separate physical processor. SMP systems that use multicore processors are faster and
consume less power than systems in which each processor has its own physical chip.
However multicore processors may complicate the scheduling problems. When processor
accesses memory then it spends a significant amount of time waiting for the data to become
available. This situation is called MEMORY STALL. It occurs for various reasons such as
cache miss, which is accessing the data that is not in the cache memory. In such cases the
processor can spend upto fifty percent of its time waiting for data to become available from the
memory. To solve this problem recent hardware designs have implemented multithreaded
processor cores in which two or more hardware threads are assigned to each core. Therefore if
one thread stalls while waiting for the memory, core can switch to another thread.
There are two ways to multithread a processor :
1. Coarse-Grained Multithreading – In coarse grained multithreading a thread executes on a
processor until a long latency event such as a memory stall occurs, because of the delay
caused by the long latency event, the processor must switch to another thread to begin
execution. The cost of switching between threads is high as the instruction pipeline must be
terminated before the other thread can begin execution on the processor core. Once this new
thread begins execution it begins filling the pipeline with its instructions.
2. Fine-Grained Multithreading – This multithreading switches between threads at a much
finer level mainly at the boundary of an instruction cycle. The architectural design of fine
grained systems include logic for thread switching and as a result the cost of switching
between threads is small.
141
Virtualization and Threading –
In this type of multiple-processor scheduling even a single CPU system acts like a multiple-
processor system. In a system with Virtualization, the virtualization presents one or more virtual
CPU to each of virtual machines running on the system and then schedules the use of physical
CPU among the virtual machines. Most virtualized environments have one host operating system
and many guest operating systems. The host operating system creates and manages the virtual
machines. Each virtual machine has a guest operating system installed and applications run
within that guest.Each guest operating system may be assigned for specific use cases,applications
or users including time sharing or even real-time operation. Any guest operating-system
scheduling algorithm that assumes a certain amount of progress in a given amount of time will be
negatively impacted by the virtualization. A time sharing operating system tries to allot 100
milliseconds to each time slice to give users a reasonable response time. A given 100 millisecond
time slice may take much more than 100 milliseconds of virtual CPU time. Depending on how
busy the system is, the time slice may take a second or more which results in a very poor
response time for users logged into that virtual machine. The net effect of such scheduling
layering is that individual virtualized operating systems receive only a portion of the available
CPU cycles, even though they believe they are receiving all cycles and that they are scheduling
all of those cycles.Commonly, the time-of-day clocks in virtual machines are incorrect because
timers take no longer to trigger than they would on dedicated CPU’s.
Virtualizations can thus undo the good scheduling-algorithm efforts of the operating systems
within virtual machines.
142
The scheduler logically divides an equal amount even though, another layer of partition is added,
for example, if there were 3 groups present with different number of people in each group, the
algorithm would still divide the same time for those groups, 100%/3= 33.33%, this 33.33%
would be shared equally in the respective group depending on the number of users present in the
group.
To sum up, fair-share scheduling is an efficient strategy that creates a consistent user experience.
143
Earliest Deadline First (EDF) CPU scheduling algorithm
Earliest Deadline First (EDF) is an optimal dynamic priority scheduling algorithm used in real-
time systems.
It can be used for both static and dynamic real-time scheduling.
EDF uses priorities to the jobs for scheduling. It assigns priorities to the task according to the
absolute deadline. The task whose deadline is closest gets the highest priority. The priorities are
assigned and changed in a dynamic fashion. EDF is very efficient as compared to other
scheduling algorithms in real-time systems. It can make the CPU utilization to about 100% while
still guaranteeing the deadlines of all the tasks.
EDF includes the kernel overload. In EDF, if the CPU usage is less than 100%, then it means
that all the tasks have met the deadline. EDF finds an optimal feasible schedule. The feasible
schedule is one in which all the tasks in the system are executed within the deadline. If EDF is
not able to find a feasible schedule for all the tasks in the real-time system, then it means that no
other task scheduling algorithms in real-time systems can give a feasible schedule. All the tasks
which are ready for execution should announce their deadline to EDF when the task becomes
runnable.
EDF scheduling algorithm does not need the tasks or processes to be periodic and also the tasks
or processes require a fixed CPU burst time. In EDF, any executing task can be preempted if any
other periodic instance with an earlier deadline is ready for execution and becomes active.
Preemption is allowed in the Earliest Deadline First scheduling algorithm.
Example:
Consider two processes P1 and P2.
Let the period of P1 be p1 = 50
Let the processing time of P1 be t1 = 25
Let the period of P2 be period2 = 75
Let the processing time of P2 be t2 = 30
144
5. P2 completes its processing at time 55.
6. P1 starts to execute until time 75, when P2 is able to execute.
7. Now, again comparing the deadline of (P1, P2) = (100, 150), P1 continues to execute.
8. Repeat the above steps…
9. Finally at time 150, both P1 and P2 have the same deadline, so P2 will continue to execute till
its processing time after which P1 starts to execute.
Limitations of EDF scheduling algorithm:
Transient Overload Problem
Resource Sharing Problem
Efficient Implementation Problem
145
4. Priority Based (PB):
Advantages:
This provides a good mechanism where the relative importance of each process may be
precisely defined.
Disadvantages:
1. If high-priority processes use up a lot of CPU time, lower-priority processes may starve and
be postponed indefinitely. The situation where a process never gets scheduled to run is
called starvation.
2. Another problem is deciding which process gets which priority level assigned to it.
5. Multilevel Queue Scheduling (MQS):
Advantages:
Application of separate scheduling for various kinds of processes is possible.
System Process – FCFS
Interactive Process – SJF
Batch Process – RR
Student Process – PB
Disadvantages:
The lowest level process faces the starvation problem.
6. Multilevel Feedback Queue Scheduling (MFQS):
Advantages:
1. Low scheduling overhead.
2. Allows aging, thus no starvation.
Disadvantages:
1. It’s not flexible.
2. It also requires some means of selecting values for all the parameters to define the best
scheduler, thus it is also the most complex.
146
4. Waiting time: A scheduling algorithm does not affect the time required to complete the
process once it starts execution. It only affects the waiting time of a process i.e. time spent by
a process waiting in the ready queue. The formula for calculating Waiting Time = Turnaround
Time – Burst Time.
5. Response time: In an interactive system, turn-around time is not the best criteria. A process
may produce some output fairly early and continue computing new results while previous
results are being output to the user. Thus another criteria is the time taken from submission of
the process of request until the first response is produced. This measure is called response
time. The formula to calculate Response Time = CPU Allocation Time(when the CPU was
allocated for the first) – Arrival Time
6. Completion time: This is the time when the process completes its execution.
147
When process is allotted to CPU, clock timer is set corresponding to time slice.
If the process finishes its burst before time slice, CPU simply swaps it out like conventional
FCFS calculation.
If the time slice goes off first, CPU shifts it out to back of ongoing queue.
The ongoing queue is managed like circular queue, so, after all processes are executed once,
scheduler executes first process again and then second and so forth.
Example –
Process Required burst time by
Queue process(ms)
P1 1
P2 4
P3 5
148
We have three processes(P1, P2, P3) with their corresponding burst times(1ms, 4ms, 5ms). A
rule of thumb is that 80% of CPU bursts should be smaller than the time quantum. Considering
time slice of 2ms.
Here’s how CPU manages it by time slicing.
Advantages :
Fair allocation of CPU resources.
It deals all process with equal priority.
Easily implementable on the system.
Context switching method used to save states of preempted processes
gives best performance in terms of average processing time.
Disadvantages :
If the slicing time is short, processor output will be delayed.
It spends time on context switching.
Performance depends heavily on time quantum.
Priorities can’t be fixed for processes.
No priority to more important tasks.
Finding an appropriate time quantum is quite difficult.
149
FCFS algorithm is non-preemptive in nature, that is, once CPU time has been allocated to a
process, other processes can get CPU time only after the current process has finished. This
property of FCFS scheduling leads to the situation called Convoy Effect.
Suppose there is one CPU intensive (large burst time) process in the ready queue, and several
other processes with relatively less burst times but are Input/Output (I/O) bound (Need I/O
operations frequently).
Steps are as following below:
The I/O bound processes are first allocated CPU time. As they are less CPU intensive, they
quickly get executed and goto I/O queues.
Now, the CPU intensive process is allocated CPU time. As its burst time is high, it takes time
to complete.
While the CPU intensive process is being executed, the I/O bound processes complete their
I/O operations and are moved back to ready queue.
However, the I/O bound processes are made to wait as the CPU intensive process still hasn’t
finished. This leads to I/O devices being idle.
When the CPU intensive process gets over, it is sent to the I/O queue so that it can access an
I/O device.
Meanwhile, the I/O bound processes get their required CPU time and move back to I/O queue.
However, they are made to wait because the CPU intensive process is still accessing an I/O
device. As a result, the CPU is sitting idle now.
Hence in Convoy Effect, one slow process slows down the performance of the entire set of
processes, and leads to wastage of CPU time and other devices.
To avoid Convoy Effect, preemptive scheduling algorithms like Round Robin Scheduling can be
used – as the smaller processes don’t have to wait much for CPU time – making their execution
faster and leading to less resources sitting idle.
Introduction of Process Synchronization
On the basis of synchronization, processes are categorized as one of the following two types:
Independent Process: The execution of one process does not affect the execution of other
processes.
Cooperative Process: A process that can affect or be affected by other processes executing in
the system.
Process synchronization problem arises in the case of Cooperative process also because
resources are shared in Cooperative processes.
150
Race Condition:
When more than one process is executing the same code or accessing the same memory or any
shared variable in that condition there is a possibility that the output or the value of the shared
variable is wrong so for that all the processes doing the race to say that my output is correct this
condition known as a race condition. Several processes access and process the manipulations
over the same data concurrently, then the outcome depends on the particular order in which the
access takes place. A race condition is a situation that may occur inside a critical section. This
happens when the result of multiple thread execution in the critical section differs according to
the order in which the threads execute. Race conditions in critical sections can be avoided if the
critical section is treated as an atomic instruction. Also, proper thread synchronization using
locks or atomic variables can prevent race conditions.
A critical section is a code segment that can be accessed by only one process at a time. The
critical section contains shared variables that need to be synchronized to maintain the
consistency of data variables. So the critical section problem means designing a way for
cooperative processes to access shared resources without creating data inconsistencies.
In the entry section, the process requests for entry in the Critical Section.
Any solution to the critical section problem must satisfy three requirements:
Mutual Exclusion: If a process is executing in its critical section, then no other process is
allowed to execute in the critical section.
Progress: If no process is executing in the critical section and other processes are waiting
outside the critical section, then only those processes that are not executing in their remainder
151
section can participate in deciding which will enter in the critical section next, and the
selection can not be postponed indefinitely.
Bounded Waiting: A bound must exist on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to enter its critical
section and before that request is granted.
Peterson’s Solution:
152
It involves busy waiting.(In the Peterson’s solution, the code statement- “while(flag[j] &&
turn == j);” is responsible for this. Busy waiting is not favored because it wastes CPU cycles
that could be used to perform other tasks.)
It is limited to 2 processes.
Peterson’s solution cannot be used in modern CPU architectures.
Semaphores:
153
It could be visualized using the pseudo-code below:-
do{
flag=1;
while(flag); // (entry section)
// critical section
if (!flag)
// remainder section
} while(true);
A simple solution to the critical section can be thought of as shown below,
acquireLock();
Process Critical Section
releaseLock();
A thread must acquire a lock prior to executing a critical section. The lock can be acquired by
only one thread. There are various ways to implement locks in the above pseudo-code. Let us
discuss them in future articles. Please write comments if you find anything incorrect, or you want
to share more information about the topic discussed above.
154
An independent process is not affected by the execution of other processes while a co-
operating process can be affected by other executing processes. Though one can think that
those processes, which are running independently, will execute very efficiently, in reality,
there are many situations when co-operative nature can be utilized for increasing
computational speed, convenience, and modularity. Inter-process communication (IPC) is a
mechanism that allows processes to communicate with each other and synchronize their
actions. The communication between these processes can be seen as a method of co-operation
between them. Processes can communicate with each other through both:
1. Shared Memory
2. Message passing
Figure 1 below shows a basic structure of communication between processes via the shared
memory method and via the message passing method.
An operating system can implement both methods of communication. First, we will discuss the
shared memory methods of communication and then message passing. 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.
Let’s discuss an example of communication between processes using the shared memory
method.
155
i) Shared Memory Method
Ex: Producer-Consumer problem
There are two processes: Producer and Consumer. The producer produces some items and the
Consumer consumes that item. The two processes share a common space or memory location
known as a buffer where the item produced by the Producer is stored and from which the
Consumer consumes the item if needed. There are two versions of this problem: the first one is
known as the unbounded buffer problem in which the Producer can keep on producing items
and there is no limit on the size of the buffer, the second one is known as the bounded buffer
problem in which the Producer can produce up to a certain number of items before it starts
waiting for Consumer to consume it. We will discuss the bounded buffer problem. First, the
Producer and the Consumer will share some common memory, then the producer will start
producing items. If the total produced item is equal to the size of the buffer, the producer will
wait to get it consumed by the Consumer. Similarly, the consumer will first check for the
availability of the item. If no item is available, the Consumer will wait for the Producer to
produce it. If there are items available, Consumer will consume them. The pseudo-code to
demonstrate is provided below:
Shared Data between the two Processes
C
#define buff_max 25
#define mod %
struct item{
// or consumed data
---------
156
// This is the shared place which will be
int free_index = 0;
int full_index = 0;
C
item nextProduced;
while(1){
// for production.
157
// if so keep waiting.
shared_buff[free_index] = nextProduced;
C
item nextConsumed;
while(1){
while((free_index == full_index);
nextConsumed = shared_buff[full_index];
158
}
In the above code, the Producer will start producing again when the (free_index+1) mod buff
max will be free because if it it not free, this implies that there are still items that can be
consumed by the Consumer so there is no need to produce more. Similarly, if free index and
full index point to the same index, this implies that there are no items to consume.
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)
The message size can be of fixed size or of variable size. If it is of fixed size, it is easy for an
OS designer but complicated for a programmer and if it is of variable size then it is easy for a
programmer but complicated for the OS designer. A standard message can have two
parts: header and body.
The header part is used for storing message type, destination id, source id, message length,
and control information. The control information contains information like what to do if runs
out of buffer space, sequence number, priority. Generally, message is sent using FIFO style.
159
Message Passing through Communication Link.
Direct and Indirect Communication link
Now, We will start our discussion about the methods of implementing communication links.
While implementing the link, there are some questions that need to be kept in mind like :
160
Non-blocking send and Non-blocking receive
Non-blocking send and Blocking receive (Mostly used)
In Direct message passing, The process which wants to communicate must explicitly name
the recipient or sender of the communication.
e.g. send(p1, message) means send the message to p1.
Similarly, receive(p2, message) means to receive the message from p2.
In this method of communication, the communication link gets established automatically,
which can be either unidirectional or bidirectional, but one link can be used between one pair
of the sender and receiver and one pair of sender and receiver should not possess more than
one pair of links. Symmetry and asymmetry between sending and receiving can also be
implemented i.e. either both processes will name each other for sending and receiving the
messages or only the sender will name the receiver for sending the message and there is no
need for the receiver for naming the sender for receiving the message. The problem with this
method of communication is that if the name of one process changes, this method will not
work.
In Indirect message passing, processes use mailboxes (also referred to as ports) for sending
and receiving messages. Each mailbox has a unique id and processes can communicate only if
they share a mailbox. Link established only if processes share a common mailbox and a single
link can be associated with many processes. Each pair of processes can share several
communication links and these links may be unidirectional or bi-directional. Suppose two
processes want to communicate through Indirect message passing, the required operations are:
create a mailbox, use this mailbox for sending and receiving messages, then destroy the
mailbox. The standard primitives used are: send(A, message) which means send the message
to mailbox A. The primitive for the receiving the message also works in the same way
e.g. received (A, message). There is a problem with this mailbox implementation. Suppose
there are more than two processes sharing the same mailbox and suppose the process p1 sends
a message to the mailbox, which process will be the receiver? This can be solved by either
enforcing that only two processes can share a single mailbox or enforcing that only one process
is allowed to execute the receive at a given time or select any process randomly and notify the
sender about the receiver. A mailbox can be made private to a single sender/receiver pair and
can also be shared between multiple sender/receiver pairs. Port is an implementation of such
mailbox that can have multiple senders and a single receiver. It is used in client/server
applications (in this case the server is the receiver). The port is owned by the receiving process
and created by OS on the request of the receiver process and can be destroyed either on request
of the same receiver processor when the receiver terminates itself. Enforcing that only one
process is allowed to execute the receive can be done using the concept of mutual
exclusion. Mutex mailbox is created which is shared by n process. The sender is non-blocking
and sends the message. The first process which executes the receive will enter in the critical
section and all other processes will be blocking and will wait.
Now, let’s discuss the Producer-Consumer problem using the message passing concept. The
producer places items (inside messages) in the mailbox and the consumer can consume an item
when at least one message present in the mailbox. The code is given below:
Producer Code
C
161
void Producer(void){
int item;
Message m;
while(1){
receive(Consumer, &m);
item = produce();
build_message(&m , item ) ;
send(Consumer, &m);
Consumer Code
C
void Consumer(void){
int item;
Message m;
162
while(1){
receive(Producer, &m);
item = extracted_item();
send(Producer, &m);
consume_item(item);
Pipe
Socket
Remote Procedural calls (RPCs)
163
First, look at two operations that can be used to access and change the value of the semaphore
variable.
Now, let us see how it implements mutual exclusion. Let there be two processes P1 and P2 and a
semaphore s is initialized as 1. Now if suppose P1 enters in its critical section then the value of
semaphore s becomes 0. Now if P2 wants to enter its critical section then it will wait until s > 0,
this can only happen when P1 finishes its critical section and calls V operation on semaphore s.
This way mutual exclusion is achieved. Look at the below image for details which is Binary
semaphore.
164
Implementation: Binary semaphores
C++
struct semaphore {
Queue<process> q;
} P(semaphore s)
165
if (s.value == 1) {
s.value = 0;
else {
q.push(P) sleep();
V(Semaphore s)
if (s.q is empty) {
s.value = 1;
else {
Process p = q.front();
// sent for CS
q.pop();
166
wakeup(p);
The description above is for binary semaphore which can take only two values 0 and 1 and
ensure mutual exclusion. There is one other type of semaphore called counting semaphore which
can take values greater than one.
Now suppose there is a resource whose number of instances is 4. Now we initialize S = 4 and the
rest is the same as for binary semaphore. Whenever the process wants that resource it calls P or
waits for function and when it is done it calls V or signal function. If the value of S becomes zero
then a process has to wait until S becomes positive. For example, Suppose there are 4 processes
P1, P2, P3, P4, and they all call wait operation on S(initialized with 4). If another process P5
wants the resource then it should wait until one of the four processes calls the signal function and
the value of semaphore becomes positive.
Limitations :
1. One of the biggest limitations of semaphore is priority inversion.
2. Deadlock, suppose a process is trying to wake up another process which is not in a sleep state.
Therefore, a deadlock may block indefinitely.
3. The operating system has to keep track of all calls to wait and to signal the semaphore.
Problem in this implementation of semaphore :
The main problem with semaphores is that they require busy waiting, If a process is in the
critical section, then other processes trying to enter the critical section will be waiting until the
critical section is not occupied by any process. Whenever any process waits then it continuously
checks for semaphore value (look at this line while (s==0); in P operation) and waste CPU cycle.
There is also a chance of “spinlock” as the processes keep on spins while waiting for the lock. In
order to avoid this another implementation is provided below.
Implementation: Counting semaphore
CPP
struct Semaphore {
int value;
167
// q contains all Process Control Blocks(PCBs)
Queue<process> q;
} P(Semaphore s)
s.value = s.value - 1;
if (s.value < 0) {
q.push(p);
block();
else
return;
V(Semaphore s)
168
{
s.value = s.value + 1;
if (s.value <= 0) {
Process p = q.pop();
wakeup(p);
else
return;
In this implementation whenever the process waits it is added to a waiting queue of processes
associated with that semaphore. This is done through system call block() on that process. When a
process is completed it calls the signal function and one process in the queue is resumed. It uses
wakeup() system call.
Mutex vs Semaphore
What is the difference between a mutex and a semaphore? When should you use a mutex and
when should you use a semaphore?
A concrete understanding of Operating System concepts is required to design/develop smart
applications. Our objective is to educate the reader on these concepts and learn from other expert
geeks.
As per operating system terminology, mutexes and semaphores are kernel resources that provide
synchronization services (also called synchronization primitives). Why do we need such
synchronization primitives? Won’t only one be sufficient? To answer these questions, we need to
understand a few keywords. Please read the posts on atomicity and critical section. We will
illustrate with examples to understand these concepts well, rather than following the usual OS
textual description.
The producer-consumer problem:
Note that the content is a generalized explanation. Practical details vary with implementation.
Consider the standard producer-consumer problem. Assume, we have a buffer of 4096-byte
length. A producer thread collects the data and writes it to the buffer. A consumer thread
169
processes the collected data from the buffer. The objective is, both the threads should not run at
the same time.
Using Mutex:
A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and
proceed with their work. As long as the buffer is filled by the producer, the consumer needs to
wait, and vice versa.
At any point of time, only one thread can work with the entire buffer. The concept can be
generalized using semaphore.
Using Semaphore:
A semaphore is a generalized mutex. In lieu of a single buffer, we can split the 4 KB buffer into
four 1 KB buffers (identical resources). A semaphore can be associated with these four buffers.
The consumer and producer can work on different buffers at the same time.
Misconception:
There is an ambiguity between binary semaphore and mutex. We might have come across that a
mutex is a binary semaphore. But it is not! The purpose of mutex and semaphore are different.
Maybe, due to similarity in their implementation a mutex would be referred to as a binary
semaphore.
Strictly speaking, a mutex is a locking mechanism used to synchronize access to a resource.
Only one task (can be a thread or process based on OS abstraction) can acquire the mutex. It
means there is ownership associated with a mutex, and only the owner can release the lock
(mutex).
Semaphore is signaling mechanism (“I am done, you can carry on” kind of signal). For
example, if you are listening to songs (assume it as one task) on your mobile phone and at the
same time, your friend calls you, an interrupt is triggered upon which an interrupt service routine
(ISR) signals the call processing task to wakeup.
General Questions:
1. Can a thread acquire more than one lock (Mutex)?
Yes, it is possible that a thread is in need of more than one resource, hence the locks. If any lock
is not available the thread will wait (block) on the lock.
2. Can a mutex be locked more than once?
A mutex is a lock. Only one state (locked/unlocked) is associated with it. However, a recursive
mutex can be locked more than once (POSIX compliant systems), in which a count is associated
with it, yet retains only one state (locked/unlocked). The programmer must unlock the mutex as
many number times as it was locked.
3. What happens if a non-recursive mutex is locked more than once.
Deadlock. If a thread that had already locked a mutex, tries to lock the mutex again, it will enter
into the waiting list of that mutex, which results in a deadlock. It is because no other thread can
unlock the mutex. An operating system implementer can exercise care in identifying the owner
of the mutex and return if it is already locked by a same thread to prevent deadlocks.
4. Are binary semaphore and mutex same?
No. We suggest treating them separately, as it is explained in signaling vs locking mechanisms.
But a binary semaphore may experience the same critical issues (e.g. priority inversion)
associated with a mutex. We will cover these in a later article.
A programmer can prefer mutex rather than creating a semaphore with count 1.
170
5. What is a mutex and critical section?
Some operating systems use the same word critical section in the API. Usually a mutex is a
costly operation due to protection protocols associated with it. At last, the objective of mutex is
atomic access. There are other ways to achieve atomic access like disabling interrupts which can
be much faster but ruins responsiveness. The alternate API makes use of disabling interrupts.
6. What are events?
The semantics of mutex, semaphore, event, critical section, etc… are same. All are
synchronization primitives. Based on their cost in using them they are different. We should
consult the OS documentation for exact details.
7. Can we acquire mutex/semaphore in an Interrupt Service Routine?
An ISR will run asynchronously in the context of current running thread. It is not
recommended to query (blocking call) the availability of synchronization primitives in an ISR.
The ISR are meant be short, the call to mutex/semaphore may block the current running thread.
However, an ISR can signal a semaphore or unlock a mutex.
8. What we mean by “thread blocking on mutex/semaphore” when they are not available?
Every synchronization primitive has a waiting list associated with it. When the resource is not
available, the requesting thread will be moved from the running list of processors to the waiting
list of the synchronization primitive. When the resource is available, the higher priority thread on
the waiting list gets the resource (more precisely, it depends on the scheduling policies).
9. Is it necessary that a thread must block always when resource is not available?
Not necessary. If the design is sure ‘what has to be done when resource is not available‘, the
thread can take up that work (a different code branch). To support application requirements the
OS provides non-blocking API.
For example POSIX pthread_mutex_trylock() API. When the mutex is not available the function
returns immediately whereas the API pthread_mutex_lock() blocks the thread till the resource is
available.
Producer Consumer Problem using Semaphores | Set 1
Producer consumer problem is a classical synchronization problem. We can solve this problem
by using semaphores.
A semaphore S is an integer variable that can be accessed only through two standard
operations : wait() and signal().
The wait() operation reduces the value of semaphore by 1 and the signal() operation increases its
value by 1.
wait(S){
while(S<=0); // busy waiting
S--;
}
signal(S){
S++;
}
Semaphores are of two types:
171
1. Binary Semaphore – This is similar to mutex lock but not the same thing. It can have only
two values – 0 and 1. Its value is initialized to 1. It is used to implement the solution of critical
section problem with multiple processes.
2. Counting Semaphore – Its value can range over an unrestricted domain. It is used to control
access to a resource that has multiple instances.
Problem Statement – We have a buffer of fixed size. A producer can produce an item and can
place in the buffer. A consumer can pick items and can consume them. We need to ensure that
when a producer is placing an item in the buffer, then at the same time consumer should not
consume any item. In this problem, buffer is the critical section.
To solve this problem, we need two counting semaphores – Full and Empty. “Full” keeps track
of number of items in the buffer at any given time and “Empty” keeps track of number of
unoccupied slots.
Initialization of semaphores –
mutex = 1
Full = 0 // Initially, all slots are empty. Thus full slots are 0
Empty = n // All slots are empty initially
Solution for Producer –
do{
//produce an item
wait(empty);
wait(mutex);
//place in buffer
signal(mutex);
signal(full);
}while(true)
When producer produces an item then the value of “empty” is reduced by 1 because one slot will
be filled now. The value of mutex is also reduced to prevent consumer to access the buffer. Now,
the producer has placed the item and thus the value of “full” is increased by 1. The value of
mutex is also increased by 1 because the task of producer has been completed and consumer can
access the buffer.
Solution for Consumer –
do{
172
wait(full);
wait(mutex);
signal(mutex);
signal(empty);
// consumes item
}while(true)
As the consumer is removing an item from buffer, therefore the value of “full” is reduced by 1
and the value is mutex is also reduced so that the producer cannot access the buffer at this
moment. Now, the consumer has consumed the item, thus increasing the value of “empty” by 1.
The value of mutex is also increased so that producer can access the buffer now.
173
Semaphore Solution to Dining Philosopher –
Each philosopher is represented by the following pseudocode:
process P[i]
while true do
{ THINK;
PICKUP(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]);
EAT;
PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5])
}
There are three states of the philosopher: THINKING, HUNGRY, and EATING. Here there
are two semaphores: Mutex and a semaphore array for the philosophers. Mutex is used such that
no two philosophers may access the pickup or putdown at the same time. The array is used to
control the behavior of each philosopher. But, semaphores can result in deadlock due to
programming errors.
If one of the person tries editing the file, no other person should be reading or writing at the
same time, otherwise changes will not be visible to him/her.
However if some person is reading the file, then others may read it at the same time.
Precisely in OS we call this situation as the readers-writers problem
Problem parameters:
174
Case Process 1 Process 2 Allowed/Not Allowed
Here priority means, no reader should wait if the share is currently opened for reading.
Three variables are used: mutex, wrt, readcnt to implement solution
do {
// writer requests for critical section
wait(wrt);
} while(true);
Reader process:
175
it increments the count of number of readers inside the critical section. If this reader is the
first reader entering, it locks the wrt semaphore to restrict the entry of writers if any reader
is inside.
It then, signals mutex as any other reader is allowed to enter while others are already
reading.
After performing reading, it exits the critical section. When exiting, it checks if no more
reader is inside, it signals the semaphore “wrt” as now, writer can enter the critical section.
3. If not allowed, it keeps on waiting.
do {
readcnt--;
} while(true);
Thus, the semaphore ‘wrt‘ is queued on both readers and writers in a manner such that
preference is given to readers if writers are also there. Thus, no reader is waiting simply because
a writer has requested to enter the critical section.
176
Sleeping Barber problem in Process Synchronization
Problem : The analogy is based upon a hypothetical barber shop with one barber. There is a
barber shop which has one barber, one barber chair, and n chairs for waiting for customers if
there are any to sit on the chair.
If there is no customer, then the barber sleeps in his own chair.
When a customer arrives, he has to wake up the barber.
If there are many customers and the barber is cutting a customer’s hair, then the remaining
customers either wait if there are empty chairs in the waiting room or they leave if no chairs
are empty.
Solution : The solution to this problem includes three semaphores.First is for the customer
which counts the number of customers present in the waiting room (customer in the barber chair
is not included because he is not waiting). Second, the barber 0 or 1 is used to tell whether the
barber is idle or is working, And the third mutex is used to provide the mutual exclusion which is
required for the process to execute. In the solution, the customer has the record of the number of
customers waiting in the waiting room if the number of customers is equal to the number of
chairs in the waiting room then the upcoming customer leaves the barbershop.
When the barber shows up in the morning, he executes the procedure barber, causing him to
block on the semaphore customers because it is initially 0. Then the barber goes to sleep until the
first customer comes up.
When a customer arrives, he executes customer procedure the customer acquires the mutex for
entering the critical region, if another customer enters thereafter, the second one will not be able
to anything until the first one has released the mutex. The customer then checks the chairs in the
waiting room if waiting customers are less then the number of chairs then he sits otherwise he
leaves and releases the mutex.
If the chair is available then customer sits in the waiting room and increments the variable
waiting value and also increases the customer’s semaphore this wakes up the barber if he is
sleeping.
At this point, customer and barber are both awake and the barber is ready to give that person a
haircut. When the haircut is over, the customer exits the procedure and if there are no customers
in waiting room barber sleeps.
177
Algorithm for Sleeping Barber problem:
Semaphore Customers = 0;
Semaphore Barber = 0;
Mutex Seats = 1;
int FreeSeats = N;
Barber {
while(true) {
down(Customers);
down(Seats);
178
/* a chair gets free.*/
FreeSeats++;
up(Barber);
up(Seats);
Customer {
while(true) {
if(FreeSeats > 0) {
/* sitting down.*/
179
FreeSeats--;
up(Customers);
up(Seats);
down(Barber);
} else {
up(Seats);
// customer leaves
180
Deadlock is a situation where a set of processes are blocked because each process is holding a
resource and waiting for another resource acquired by some other process.
Consider an example when two trains are coming toward each other on the same track and there
is only one track, none of the trains can move once they are in front of each other. A similar
situation occurs in operating systems when there are two or more processes that hold some
resources and wait for resources held by other(s). For example, in the below diagram, Process 1
is holding Resource 1 and waiting for resource 2 which is acquired by process 2, and process 2 is
waiting for resource 1.
Deadlock can arise if the following four conditions hold simultaneously (Necessary
Conditions)
Mutual Exclusion: Two or more resources are non-shareable (Only one process can use at a
time)
Hold and Wait: A process is holding at least one resource and waiting for resources.
No Preemption: A resource cannot be taken from a process unless the process releases the
resource.
Circular Wait: A set of processes are waiting for each other in circular form.
Methods for handling deadlock
There are three ways to handle deadlock
1) Deadlock prevention or avoidance: The idea is to not let the system into a deadlock state.
One can zoom into each category individually, Prevention is done by negating one of above
mentioned necessary conditions for deadlock.
Avoidance is kind of futuristic in nature. By using strategy of “Avoidance”, we have to make an
assumption. We need to ensure that all information about resources which process will need are
known to us prior to execution of the process. We use Banker’s algorithm (Which is in-turn a gift
from Dijkstra) in order to avoid deadlock.
2) Deadlock detection and recovery: Let deadlock occur, then do preemption to handle it once
occurred.
3) Ignore the problem altogether: If deadlock is very rare, then let it happen and reboot the
system. This is the approach that both Windows and UNIX take.
181
Deadlock Detection Algorithm in Operating System
If a system does not employ either a deadlock prevention or deadlock avoidance algorithm then
a deadlock situation may occur. In this case-
Apply an algorithm to examine state of system to determine whether deadlock has occurred
or not.
Apply an algorithm to recover from the deadlock. For more refer- Deadlock Recovery
Deadlock Avoidance Algorithm/ Bankers Algorithm:
The algorithm employs several times varying data structures:
Available –
A vector of length m indicates the number of available resources of each type.
Allocation –
An n*m matrix defines the number of resources of each type currently allocated to a
process. Column represents resource and rows represent process.
Request –
An n*m matrix indicates the current request of each process. If request[i][j] equals k then
process Pi is requesting k more instances of resource type R j.
1. Let Work and Finish be vectors of length m and n respectively. Initialize Work= Available.
For i=0, 1, …., n-1, if Requesti = 0, then Finish[i] = true; otherwise, Finish[i]= false.
2. Find an index i such that both
a) Finish[i] == false
b) Requesti <= Work
If no such i exists go to step 4.
3. Work= Work+ Allocation i
Finish[i]= true
Go to Step 2.
4. If Finish[i]== false for some i, 0<=i<n, then the system is in a deadlocked state. Moreover,
if Finish[i]==false the process Pi is deadlocked.
For example,
182
1. In this, Work = [0, 0, 0] &
Finish = [false, false, false, false, false]
10. i=4 is selected as both Finish[4] = false and [0, 0, 2]<=[7, 2, 4].
12. Since Finish is a vector of all true it means there is no deadlock in this example.
183
2. In the above diagram, resource 1 and resource 2 have single instances. There is a cycle R1 →
P1 → R2 → P2. So, Deadlock is Confirmed.
1. Mutual Exclusion
2. Hold and Wait
3. No preemption
4. Circular wait
Deadlock Prevention
We can prevent Deadlock by eliminating any of the above four conditions.
184
Eliminate Mutual Exclusion
It is not possible to dis-satisfy the mutual exclusion because some resources, such as the tape
drive and printer, are inherently non-shareable.
Eliminate Hold and wait
1. Allocate all required resources to the process before the start of its execution, this way hold
and wait condition is eliminated but it will lead to low device utilization. for example, if a
process requires printer at a later time and we have allocated printer before the start of its
execution printer will remain blocked till it has completed its execution.
2. The process will make a new request for resources after releasing the current set of resources.
This solution may lead to starvation.
Eliminate No Preemption
Preempt resources from the process when resources required by other high priority processes.
Eliminate Circular Wait
Each resource will be assigned with a numerical number. A process can request the resources
increasing/decreasing. order of numbering.
For Example, if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3 lesser
than R5 such request will not be granted, only request for resources more than R5 will be
granted.
Deadlock Avoidance
Deadlock avoidance can be done with Banker’s Algorithm.
Banker’s Algorithm
Bankers’s Algorithm is resource allocation and deadlock avoidance algorithm which test all the
request made by processes for resources, it checks for the safe state, if after granting request
system remains in the safe state it allows the request and if there is no safe state it doesn’t allow
the request made by the process.
Inputs to Banker’s Algorithm:
185
Example:
Total resources in system:
ABCD
6576
186
Banker’s algorithm is named so because it is used in banking system to check whether loan can
be sanctioned to a person or not. Suppose there are n number of account holders in a bank and
the total sum of their money is S. If a person applies for a loan then the bank first subtracts the
loan amount from the total money that bank has and if the remaining amount is greater than S
then only the loan is sanctioned. It is done because if all the account holders comes to withdraw
their money then the bank can easily do it.
In other words, the bank would never allocate its money in such a way that it can no longer
satisfy the needs of all its customers. The bank would try to be in safe state always.
Following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available :
It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max :
It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.
Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation :
It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently
allocated to each process.
Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj
Need :
It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
Need [ i, j ] = k means process Pi currently need ‘k’ instances of resource type Rj
Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Allocationi specifies the resources currently allocated to process Pi and Needi specifies the
additional resources that process Pi may still request to complete its task.
Banker’s algorithm consists of Safety algorithm and Resource request algorithm
Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state can be described as
follows:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
187
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k
instances of resource type Rj. When a request for resources is made by process Pi, the following
actions are taken:
Example:
Considering a system with five processes P0 through P4 and three resources of type A, B, C.
Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at
time t0 following snapshot of the system has been taken:
188
Question2. Is the system in a safe state? If Yes, then what is the safe sequence?
Applying the Safety algorithm on the given system,
Question3. What will happen if process P1 requests one additional instance of resource type
A and two instances of resource type C?
189
We must determine whether this new system state is safe. To do so, we again execute Safety
algorithm on the above data structures.
Hence the new system state is safe, so we can immediately grant the request for process P1 .
190
Now coming to the edges of RAG.There are two types of edges in RAG –
1. Assign Edge – If you already assign a resource to a process then it is called Assign edge.
2. Request Edge – It means in future the process might want some resource to complete the
execution, that is called request edge.
So, if a process is using a resource, an arrow is drawn from the resource node to the process
node. If a process is requesting a resource, an arrow is drawn from the process node to the
resource node.
191
Example 1 (Single instances RAG) –
If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only
one instance, then the processes will be in deadlock. For example, if process P1 holds resource
R1, process P2 holds resource R2 and process P1 is waiting for R2 and process P2 is waiting for
R1, then process P1 and process P2 will be in deadlock.
Here’s another example, that shows Processes P1 and P2 acquiring resources R1 and R2 while
process P3 is waiting to acquire both resources. In this example, there is no deadlock because
there is no circular dependency.
So cycle in single-instance resource type is the sufficient condition for deadlock.
192
Example 2 (Multi-instances RAG) –
From the above example, it is not possible to say the RAG is in a safe state or in an unsafe state.
So to see the state of this RAG, let’s construct the allocation matrix and request matrix.
The total number of processes are three; P1, P2 & P3 and the total number of resources are
two; R1 & R2.
Allocation matrix –
For constructing the allocation matrix, just go to the resources and see to which process it is
allocated.
R1 is allocated to P1, therefore write 1 in allocation matrix and similarly, R2 is allocated to P2
as well as P3 and for the remaining element just write 0.
Request matrix –
In order to find out the request matrix, you have to go to the process and see the outgoing
edges.
P1 is requesting resource R2, so write 1 in the matrix and similarly, P2 requesting R1 and for
the remaining element write 0.
So now available resource is = (0, 0).
193
Checking deadlock (safe or not) –
So, there is no deadlock in this RAG.Even though there is a cycle, still there is no
deadlock.Therefore in multi-instance resource cycle is not sufficient condition for deadlock.
Above example is the same as the previous example except that, the process P3 requesting for
resource R1.
So the table becomes as shown in below.
194
So,the Available resource is = (0, 0), but requirement are (0, 1), (1, 0) and (1, 0).So you can’t
fulfill any one requirement.Therefore, it is in deadlock.
Therefore, every cycle in a multi-instance resource type graph is not a deadlock, if there has to
be a deadlock, there has to be a cycle.So, in case of RAG with multi-instance resource type, the
cycle is a necessary condition for deadlock, but not sufficient.
195
A word is a group of bits where a memory unit stores binary information. A word with a group
of 8 bits is called a byte.
A memory unit consists of data lines, address selection lines, and control lines that specify the
direction of transfer. The block diagram of a memory unit is shown below:
Data lines provide the information to be stored in memory. The control inputs specify the direct
transfer. The k-address lines specify the word chosen.
When there are k address lines, 2k memory words can be accessed.
Bit (Binary Units): bit is a logical representation of the electric state. It can be 1 or 0.
Nibble: it means the group of 4 bits.
Byte: a byte is a group of 8 bits.
Word: it is a fixed number of bits, it is different from computer to computer, but the same for
each device. Compute store information in the form of words.
196
Following are conversations of units:
197
gap increases between the CPU registers and Main Memory due to large difference in access
time. This results in lower performance of the system and thus, enhancement was required.
This enhancement was made in the form of Memory Hierarchy Design because of which the
performance of the system increases. One of the most significant ways to increase system
performance is minimizing how far down the memory hierarchy one has to go to manipulate
data.
4. Cost per bit:
As we move from bottom to top in the Hierarchy, the cost per bit increases i.e. Internal
Memory is costlier than External Memory.
198
So, size of partition for 18 KB process = 32 KB. It divides by 2, till possible to get minimum
block to fit 18 KB.
Fixed (or static) Partitioning in Operating System
In operating systems, Memory Management is the function responsible for allocating and
managing a computer’s main memory. Memory Management function keeps track of the status
of each memory location, either allocated or free to ensure effective and efficient use of Primary
Memory.
There are two Memory Management Techniques: Contiguous, and Non-Contiguous. In
Contiguous Technique, executing process must be loaded entirely in the main memory.
Contiguous Technique can be divided into:
1. Fixed (or static) partitioning
Fixed Partitioning:
This is the oldest and simplest technique used to put more than one process in the main memory.
In this partitioning, the number of partitions (non-overlapping) in RAM is fixed but the size of
each partition may or may not be the same. As it is a contiguous allocation, hence no spanning
is allowed. Here partitions are made before execution or during system configure.
199
As illustrated in above figure, first process is only consuming 1MB out of 4MB in the main
memory.
Hence, Internal Fragmentation in first block is (4-1) = 3MB.
Sum of Internal Fragmentation in every block = (4-1)+(8-7)+(8-7)+(16-14)= 3+1+1+2 = 7MB.
Suppose process P5 of size 7MB comes. But this process cannot be accommodated in spite of
available free space because of contiguous allocation (as spanning is not allowed). Hence, 7MB
becomes part of External Fragmentation.
There are some advantages and disadvantages of fixed partitioning.
Advantages of Fixed Partitioning –
1. Easy to implement:
Algorithms needed to implement Fixed Partitioning are easy to implement. It simply requires
putting a process into a certain partition without focusing on the emergence of Internal and
External Fragmentation.
2. Little OS overhead:
Processing of Fixed Partitioning requires lesser excess and indirect computational power.
2. External Fragmentation:
The total unused space (as stated above) of various partitions cannot be used to load the
processes even though there is space available but not in the contiguous form (as spanning is
200
not allowed).
201
There are some advantages and disadvantages of variable partitioning over fixed partitioning as
given below.
Advantages of Variable Partitioning –
1. No Internal Fragmentation:
In variable Partitioning, space in main memory is allocated strictly according to the need of
process, hence there is no case of internal fragmentation. There will be no unused space left in
the partition.
2. No restriction on Degree of Multiprogramming:
More number of processes can be accommodated due to absence of internal fragmentation. A
process can be loaded until the memory is empty.
3. No Limitation on the size of the process:
In Fixed partitioning, the process with the size greater than the size of the largest partition
could not be loaded and process can not be divided as it is invalid in contiguous allocation
technique. Here, In variable partitioning, the process size can’t be restricted since the partition
size is decided according to the process size.
Disadvantages of Variable Partitioning –
1. Difficult Implementation:
Implementing variable Partitioning is difficult as compared to Fixed Partitioning as it involves
allocation of memory during run-time rather than during system configure.
2. External Fragmentation:
There will be external fragmentation inspite of absence of internal fragmentation.
For example, suppose in above example- process P1(2MB) and process P3(1MB) completed
their execution. Hence two spaces are left i.e. 2MB and 1MB. Let’s suppose process P5 of
size 3MB comes. The empty space in memory cannot be allocated as no spanning is allowed
in contiguous allocation. The rule says that process must be contiguously present in main
memory to get executed. Hence it results in External Fragmentation.
202
Now P5 of size 3 MB cannot be accommodated in spite of required available space because in
contiguous no spanning is allowed.
203
manner. Suppose process P of size 4KB. Consider main memory has two empty slots each of
size 2KB. Hence total free space is, 2*2= 4 KB. In contiguous memory allocation, process P
cannot be accommodated as spanning is not allowed.
In contiguous allocation, space in memory should be allocated to the whole process. If not, then
that space remains unallocated. But in Non-Contiguous allocation, the process can be divided
into different parts and hence filling the space in the main memory. In this example, process P
can be divided into two parts of equal size – 2KB. Hence one part of process P can be allocated
to the first 2KB space of main memory and the other part of the process can be allocated to the
second 2KB space of main memory. The below diagram will explain in a better way:
But, in what manner we divide a process to allocate them into main memory is very important to
understand. The process is divided after analysing the number of empty spaces and their size in
the main memory. Then only we do divide our process. It is a very time-consuming process.
Their number as well as their sizes changing every time due to execution of already present
processes in main memory.
In order to avoid this time-consuming process, we divide our process in secondary memory in
advance before reaching the main memory for its execution. Every process is divided into
various parts of equal size called Pages. We also divide our main memory into different parts of
equal size called Frames. It is important to understand that:
204
Although their numbers can be different. Below diagram will make you understand it in a better
way: consider empty main memory having a size of each frame is 2 KB, and two processes P1
and P2 are 2 KB each.
205
Mapping virtual-address to physical-addresses
Comparison Chart:
Address Logical Address Space is set of all Physical Address is set of all physical
Space logical addresses generated by CPU in addresses mapped to the corresponding
206
Parameter LOGICAL ADDRESS PHYSICAL ADDRESS
User can view the logical address of a User can never view physical address of
Visibility program. program.
The user can use the logical address to The user can indirectly access physical
Access access the physical address. address but not directly.
Editable Logical address can be change. Physical address will not change.
207
Concluding, we can say that Paging allows the memory address space of a process to be non-
contiguous. Paging is more flexible as only pages of a process are moved. It allows more
processes to reside in main memory than Contiguous memory allocation.
Paging in Operating System
Paging is a memory management scheme that eliminates the need for contiguous allocation of
physical memory. The process of retrieving processes in the form of pages from the secondary
storage into the main memory is known as paging. The basic purpose of paging is to separate
each procedure into pages. Additionally, frames will be used to split the main memory.This
scheme permits the physical address space of a process to be non – contiguous.
Let us look some important terminologies:
Logical Address or Virtual Address (represented in bits): An address generated by the CPU
Logical Address Space or Virtual Address Space( represented in words or bytes): The set of
all logical addresses generated by a program
Physical Address (represented in bits): An address actually available on memory unit
Physical Address Space (represented in words or bytes): The set of all physical addresses
corresponding to the logical addresses
Example:
If Logical Address = 31 bit, then Logical Address Space = 231 words = 2 G words (1 G = 230)
If Logical Address Space = 128 M words = 27 * 220 words, then Logical Address = log2 227 =
27 bits
If Physical Address = 22 bit, then Physical Address Space = 222 words = 4 M words (1 M =
220)
If Physical Address Space = 16 M words = 24 * 220 words, then Physical Address = log2 224 =
24 bits
The mapping from virtual to physical address is done by the memory management unit (MMU)
which is a hardware device and this mapping is known as paging technique.
The Physical Address Space is conceptually divided into a number of fixed-size blocks,
called frames.
The Logical address Space is also splitted into fixed-size blocks, called pages.
Page Size = Frame Size
Let us consider an example:
Physical Address = 12 bits, then Physical Address Space = 4 K words
Logical Address = 13 bits, then Logical Address Space = 8 K words
Page size = frame size = 1 K words (assumption)
208
Address
generated by CPU is divided into
Page number(p): Number of bits required to represent the pages in Logical Address Space or
Page number
Page offset(d): Number of bits required to represent particular word in a page or page size of
Logical Address Space or word number of a page or page offset.
Physical Address is divided into
Frame number(f): Number of bits required to represent the frame of Physical Address Space
or Frame number.
Frame offset(d): Number of bits required to represent particular word in a frame or frame
size of Physical Address Space or word number of a frame or frame offset.
The hardware implementation of page table can be done by using dedicated registers. But the
usage of register for the page table is satisfactory only if page table is small. If page table contain
large number of entries then we can use TLB(translation Look-aside buffer), a special, small, fast
look up hardware cache.
The TLB is associative, high speed memory.
Each entry in TLB consists of two parts: a tag and a value.
When this memory is used, then an item is compared with all tags simultaneously.If the item
is found, then corresponding value is returned.
Page Table Entries in Page Table
Page table has page table entries where each page table entry stores a frame number and optional
status (like protection) bits. Many of status bits used in the virtual memory system. The
most important thing in PTE is frame Number.
Page table entry has the following information –
209
1. Frame Number – It gives the frame number in which the current page you are looking for is
present. The number of bits required depends on the number of frames.Frame bit is also
known as address translation bit.
2. Number of bits for frame = Size of physical memory/frame size
3. Present/Absent bit – Present or absent bit says whether a particular page you are looking for
is present or absent. In case if it is not present, that is called Page Fault. It is set to 0 if the
corresponding page is not in memory. Used to control page fault by the operating system to
support virtual memory. Sometimes this bit is also known as valid/invalid bits.
4. Protection bit – Protection bit says that what kind of protection you want on that page. So,
these bit for the protection of the page frame (read, write etc).
5. Referenced bit – Referenced bit will say whether this page has been referred in the last clock
cycle or not. It is set to 1 by hardware when the page is accessed.
6. Caching enabled/disabled – Some times we need the fresh data. Let us say the user is typing
some information from the keyboard and your program should run according to the input
given by the user. In that case, the information will come into the main memory. Therefore
main memory contains the latest information which is typed by the user. Now if you try to put
that page in the cache, that cache will show the old information. So whenever freshness is
required, we don’t want to go for caching or many levels of the memory.The information
present in the closest level to the CPU and the information present in the closest level to the
user might be different. So we want the information has to be consistency, which means
whatever information user has given, CPU should be able to see it as first as possible. That is
the reason we want to disable caching. So, this bit enables or disable caching of the page.
7. Modified bit – Modified bit says whether the page has been modified or not. Modified means
sometimes you might try to write something on to the page. If a page is modified, then
whenever you should replace that page with some other page, then the modified information
should be kept on the hard disk or it has to be written back or it has to be saved back. It is set
to 1 by hardware on write-access to page which is used to avoid writing when swapped out.
Sometimes this modified bit is also called as the Dirty bit.
210
Page size = 512 Bytes
Size of page table entry = 4 Bytes, then
Number of pages in the process = 2 GB / 512 B = 222
PageTable Size = 222 * 22 = 224 bytes
Through this example, it can be concluded that for multiple processes running simultaneously in
an OS, a considerable part of memory is occupied by page tables only. Operating Systems also
incorporate multilevel paging schemes which further increase the space required for storing the
page tables and a large amount of memory is invested in storing them. The amount of memory
occupied by the page tables can turn out to be a huge overhead and is always unacceptable as
main memory is always a scarce resource. Various efforts are made to utilize the memory
efficiently and to maintain a good balance in the level of multiprogramming and efficient CPU
utilization.
Inverted Page Table – An alternate approach is to use the Inverted Page Table structure that
consists of a one-page table entry for every frame of the main memory. So the number of page
table entries in the Inverted Page Table reduces to the number of frames in physical memory and
a single page table is used to represent the paging information of all the processes. Through the
inverted page table, the overhead of storing an individual page table for every process gets
eliminated and only a fixed portion of memory is required to store the paging information of all
the processes together. This technique is called inverted paging as the indexing is done with
respect to the frame number instead of the logical page number. Each entry in the page table
contains the following fields.
Page number – It specifies the page number range of the logical address.
Process id – An inverted page table contains the address space information of all the
processes in execution. Since two different processes can have a similar set of virtual
addresses, it becomes necessary in the Inverted Page Table to store a process Id of each
process to identify its address space uniquely. This is done by using the combination of PId
and Page Number. So this Process Id acts as an address space identifier and ensures that a
virtual page for a particular process is mapped correctly to the corresponding physical frame.
Control bits – These bits are used to store extra paging-related information. These include the
valid bit, dirty bit, reference bits, protection, and locking information bits.
Chained pointer – It may be possible sometimes that two or more processes share a part of
the main memory. In this case, two or more logical pages map to the same Page Table Entry
then a chaining pointer is used to map the details of these logical pages to the root page table.
Working – The operation of an inverted page table is shown below. The virtual address
generated by the CPU contains the fields and each page table entry contains the other relevant
information required in paging related mechanism. When a memory reference takes place, this
virtual address is matched by the Memory-Mapping unit(MMU), the Inverted Page table is
searched and the corresponding frame number is obtained. If the match is found at the ith entry
then the physical address of the process is sent as the real address otherwise if no match is found
then Segmentation Fault is generated. Note: Number of Entries in Inverted page table =
Number of frames in Physical address Space(PAS).
Examples – The Inverted Page table and its variations are implemented in various systems like
PowerPC, UltraSPARC, and the IA-64 architecture. An implementation of the Mach operating
system on the RT-PC also uses this technique.
Advantages and Disadvantages:
211
Reduced memory space – Inverted Pagetables typically reduce the amount of memory
required to store the page tables to a size bound of physical memory. The maximum number
of entries could be the number of page frames in the physical memory.
Longer lookup time – Inverted Page tables are sorted in order of frame number but the
memory look-up takes place with respect to the virtual address, so, it usually takes a longer
time to find the appropriate entry but often these page tables are implemented using hash data
structures for a faster lookup.
Difficult shared memory implementation – As the Inverted Page Table stores a single entry
for each frame, it becomes difficult to implement the shared memory in the page tables.
Chaining techniques are used to map more than one virtual address to the entry specified in
the order of frame number.
212
Segmentation in Operating System
A process is divided into Segments. The chunks that a program is divided into which are not
necessarily all of the same sizes are called segments. Segmentation gives user’s view of the
process which paging does not give. Here the user’s view is mapped to physical memory.
There are types of segmentation:
1. Virtual memory segmentation –
Each process is divided into a number of segments, not all of which are resident at any one
point in time.
2. Simple segmentation –
Each process is divided into a number of segments, all of which are loaded into memory at run
time, though not necessarily contiguously.
There is no simple relationship between logical addresses and physical addresses in
segmentation. A table stores the information about all such segments and is called Segment
Table.
Segment Table – It maps two-dimensional Logical address into one-dimensional Physical
address. It’s each table entry has:
Base Address: It contains the starting physical address where the segments reside in memory.
Limit: It specifies the length of the segment.
213
1. All memory references within a process are logical addresses that are dynamically translated
into physical addresses at run time. This means that a process can be swapped in and out of
the main memory such that it occupies different places in the main memory at different times
during the course of execution.
2. A process may be broken into a number of pieces and these pieces need not be continuously
located in the main memory during execution. The combination of dynamic run-time address
translation and use of page or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or segments are
present in the main memory during execution. This means that the required pages need to be
loaded into memory whenever required. Virtual memory is implemented using Demand Paging
or Demand Segmentation.
Demand Paging :
The process of loading the page into memory on demand (whenever page fault occurs) is known
as demand paging.
The process includes the following steps :
1. If the CPU tries to refer to a page that is currently not available in the main memory, it
generates an interrupt indicating a memory access fault.
2. The OS puts the interrupted process in a blocking state. For the execution to proceed the OS
must bring the required page into the memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical address space. The
page replacement algorithms are used for the decision-making of replacing the page in
physical address space.
5. The page table will be updated accordingly.
6. The signal will be sent to the CPU to continue the program execution and it will place the
process back into the ready state.
Hence whenever a page fault occurs these steps are followed by the operating system and the
required page is brought into memory.
Advantages :
More processes may be maintained in the main memory: Because we are going to load only
some of the pages of any particular process, there is room for more processes. This leads to
more efficient utilization of the processor because it is more likely that at least one of the more
numerous processes will be in the ready state at any particular time.
A process may be larger than all of the main memory: One of the most fundamental
restrictions in programming is lifted. A process larger than the main memory can be executed
because of demand paging. The OS itself loads pages of a process in the main memory as
required.
It allows greater multiprogramming levels by using less of the available (primary) memory for
each process.
214
Page Fault Service Time :
The time taken to service the page fault is called page fault service time. The page fault service
time includes the time taken to perform all the above six steps.
Let Main memory access time is: m
Page fault service time is: s
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m
Swapping:
Swapping a process out means removing all of its pages from memory, or marking them so that
they will be removed by the normal page replacement process. Suspending a process ensures that
it is not runnable while it is swapped out. At some later time, the system swaps back the process
from the secondary storage to the main memory. When a process is busy swapping pages in and
out then this situation is called thrashing.
Thrashing :
At any given time, only a few pages of any process are in the main memory and therefore more
processes can be maintained in memory. Furthermore, time is saved because unused pages are
not swapped in and out of memory. However, the OS must be clever about how it manages this
scheme. In the steady-state practically, all of the main memory will be occupied with process
pages, so that the processor and OS have direct access to as many processes as possible. Thus
when the OS brings one page in, it must throw another out. If it throws out a page just before it is
used, then it will just have to get that page again almost immediately. Too much of this leads to a
condition called Thrashing. The system spends most of its time swapping pages rather than
executing instructions. So a good page replacement algorithm is required.
In the given diagram, the initial degree of multiprogramming up to some extent of point(lambda),
the CPU utilization is very high and the system resources are utilized 100%. But if we further
increase the degree of multiprogramming the CPU utilization will drastically fall down and the
system will spend more time only on the page replacement and the time is taken to complete the
execution of the process will increase. This situation in the system is called thrashing.
Causes of Thrashing :
1. High degree of multiprogramming : If the number of processes keeps on increasing in the
memory then the number of frames allocated to each process will be decreased. So, fewer
frames will be available for each process. Due to this, a page fault will occur more frequently
and more CPU time will be wasted in just swapping in and out of pages and the utilization
will keep on decreasing.
For example:
Let free frames = 400
Case 1: Number of process = 100
Then, each process will get 4 frames.
Case 2: Number of processes = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes is increased, frames per process
are decreased. Hence CPU time will be consumed in just swapping pages.
215
2. Lacks of Frames: If a process has fewer frames then fewer pages of that process will be able
to reside in memory and hence more frequent swapping in and out will be required. This may
lead to thrashing. Hence sufficient amount of frames must be allocated to each process in
order to prevent thrashing.
Recovery of Thrashing :
Do not allow the system to go into thrashing by instructing the long-term scheduler not to
bring the processes into memory after the threshold.
If the system is already thrashing then instruct the mid-term scheduler to suspend some of the
processes so that we can recover the system from thrashing.
1. First In First Out (FIFO): This is the simplest page replacement algorithm. In this algorithm,
the operating system keeps track of all pages in the memory in a queue, the oldest page is in the
front of the queue. When a page needs to be replaced page in the front of the queue is selected
for removal.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames.Find the number
of page faults.
Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3
Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes, it is not available in
memory so it replaces the oldest page slot i.e 1. —>1 Page Fault. 6 comes, it is also not
available in memory so it replaces the oldest page slot i.e 3 —>1 Page Fault. Finally, when 3
come it is not available so it replaces 0 1 page fault.
Belady’s anomaly proves that it is possible to have more page faults when increasing the
number of page frames while using the First in First Out (FIFO) page replacement algorithm.
For example, if we consider reference strings 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we get 9
total page faults, but if we increase slots to 4, we get 10-page faults.
2. Optimal Page replacement: In this algorithm, pages are replaced which would not be used
for the longest duration of time in the future.
Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page frame.
Find number of page fault.
216
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7 because it is not
used for the longest duration of time in the future.—>1 Page fault. 0 is already there so —> 0
Page fault. 4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already available in
the memory.
Optimal page replacement is perfect, but not possible in practice as the operating system cannot
know future requests. The use of Optimal Page replacement is to set up a benchmark so that
other replacement algorithms can be analyzed against it.
3. Least Recently Used: In this algorithm, page will be replaced which is least recently used.
Example-3: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page
frames. Find number of page faults.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault. when 3 came it will take the place of 7 because it is least
recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in
the memory.
4. Most Recently Used (MRU): In this algorithm, page will be replaced which has been used
recently. Belady’s anomaly can occur in this algorithm.
217
Reduce time requirement
Disadvantage –
Overlap map must be specified by programmer
Programmer must know memory requirement
Overlapped module must be completely disjoint
Programming design of overlays structure is complex and not possible in all cases
Example –
The best example of overlays is assembler. Consider the assembler has 2 passes, 2 pass means at
any time it will be doing only one thing, either the 1st pass or the 2nd pass. This means it will
finish 1st pass first and then 2nd pass.Let assume that available main memory size is 150KB and
total code size is 200KB
Pass 1.......................70KB
Pass 2.......................80KB
Symbol table.................30KB
Common routine...............20KB
As the total code size is 200KB and main memory size is 150KB, it is not possible to use 2
passes together.So, in this case, we should go with the overlays technique.According to the
overlays concept at any time only one pass will be used and both the passes always need symbol
table and common routine.Now the question is if overlays-driver* is 10KB, then what is the
minimum partition size required?For pass 1 total memory needed is = (70KB + 30KB + 20KB +
10KB) = 130KB and for pass 2 total memory needed is = (80KB + 30KB + 20KB + 10KB) =
140KB.So if we have minimum 140KB size partition then we can run this code very easily.
*Overlays driver:-It is the user responsibility to take care of overlaying, the operating system
will not provide anything.Which means the user should write even what part is required in the
1st pass and once the 1st pass is over, the user should write the code to pull out the pass 1 and
load the pass 2.That is what is the responsibility of the user, that is known as the Overlays
driver.Overlays driver will just help us to move out and move in the various part of the code.
File Systems in Operating System
A file is a collection of related information that is recorded on secondary storage. Or file is a
collection of logically related entities. From user’s perspective a file is the smallest allotment of
logical secondary storage.
The name of the file is divided into two parts as shown below:
name
extension, separated by a period.
218
Attributes Types Operations
Author C Append
Last
Modified Java Truncate
Close
Word Processor wp, tex, rrf, doc Various word processor formats
Archive arc, zip, tar Related files grouped into one compressed file
Print or View gif, pdf, jpg It is a format for printing or viewing a ASCII or binary file.
219
FILE DIRECTORIES:
Collection of files is a file directory. The directory contains information about the files, including
attributes, location and ownership. Much of this information, especially that is concerned with
storage, is managed by the operating system. The directory is itself a file, accessible by various
file management routines.
Information contained in a device directory are:
Name
Type
Address
Current length
Maximum length
Date last accessed
Date last updated
Owner id
Protection information
Operation performed on directory are:
Search for a file
Create a file
Delete a file
List a directory
Rename a file
Traverse the file system
Advantages of maintaining directories are:
Efficiency: A file can be located more quickly.
Naming: It becomes convenient for users as two users can have same name for different files
or may have different name for same file.
Grouping: Logical grouping of files can be done by properties e.g. all java programs, all
games etc.
SINGLE-LEVEL DIRECTORY
In this a single directory is maintained for all the users.
Naming problem: Users cannot have same name for two files.
Grouping problem: Users cannot group files according to their need.
220
TWO-LEVEL DIRECTORY
In this separate directories for each user is maintained.
Path name:Due to two levels there is a path name for every file to locate that file.
Now,we can have same file name for different user.
Searching is efficient in this method.
TREE-STRUCTURED DIRECTORY :
Directory is maintained in the form of a tree. Searching is efficient and also there is grouping
capability. We have absolute or relative path name for a file.
1. Continuous Allocation –
A single continuous set of blocks is allocated to a file at the time of file creation. Thus, this is a
pre-allocation strategy, using variable size portions. The file allocation table needs just a single
entry for each file, showing the starting block and the length of the file. This method is best from
the point of view of the individual sequential file. Multiple blocks can be read in at a time to
improve I/O performance for sequential processing. It is also easy to retrieve a single block. For
example, if a file starts at block b, and the ith block of the file is wanted, its location on
221
secondary storage is simply b+i-1.
Disadvantage –
External fragmentation will occur, making it difficult to find contiguous blocks of space of
sufficient length. Compaction algorithm will be necessary to free up additional space on disk.
Also, with pre-allocation, it is necessary to declare the size of the file at the time of creation.
2. Linked Allocation(Non-contiguous allocation) –
Allocation is on an individual block basis. Each block contains a pointer to the next block in the
chain. Again the file table needs just a single entry for each file, showing the starting block and
the length of the file. Although pre-allocation is possible, it is more common simply to allocate
blocks as needed. Any free block can be added to the chain. The blocks need not be continuous.
Increase in file size is always possible if free disk block is available. There is no external
fragmentation because only one block at a time is needed but there can be internal fragmentation
but it exists only in the last disk block of file.
Disadvantage –
Internal fragmentation exists in last disk block of file.
There is an overhead of maintaining the pointer in every disk block.
If the pointer of any disk block is lost, the file will be truncated.
It supports only the sequential access of files.
3. Indexed Allocation –
It addresses many of the problems of contiguous and chained allocation. In this case, the file
allocation table contains a separate one-level index for each file: The index has one entry for
each block allocated to the file. Allocation may be on the basis of fixed-size blocks or variable-
sized blocks. Allocation by blocks eliminates external fragmentation, whereas allocation by
variable-size blocks improves locality. This allocation technique supports both sequential and
222
direct access to the file and thus is the most popular form of file allocation.
1. Bit Tables : This method uses a vector containing one bit for each block on the disk. Each
entry for a 0 corresponds to a free block and each 1 corresponds to a block in use.
For example: 00011010111100110001
In this vector every bit correspond to a particular block and 0 implies that, that particular
block is free and 1 implies that the block is already occupied. A bit table has the advantage
that it is relatively easy to find one or a contiguous group of free blocks. Thus, a bit table
works well with any of the file allocation methods. Another advantage is that it is as small as
possible.
2. Free Block List : In this method, each block is assigned a number sequentially and the list of
the numbers of all free blocks is maintained in a reserved block of the disk.
223
Structures of Directory in Operating System
A directory is a container that is used to contain folders and files. It organizes files and folders
in a hierarchical manner.
There are several logical structures of a directory, these are given below.
Single-level directory –
The single-level directory is the simplest directory structure. In it, all files are contained in the
same directory which makes it easy to support and understand.
A single level directory has a significant limitation, however, when the number of files
increases or when the system has more than one user. Since all the files are in the same
directory, they must have a unique name. if two users call their dataset test, then the unique
name rule violated.
224
Advantages:
Since it is a single directory, so its implementation is very easy.
If the files are smaller in size, searching will become faster.
The operations like file creation, searching, deletion, updating are very easy in such a
directory structure.
Disadvantages:
There may chance of name collision because two files can have the same name.
Searching will become time taking if the directory is large.
This can not group the same type of files together.
Two-level directory –
As we have seen, a single level directory often leads to confusion of files names among
different users. the solution to this problem is to create a separate directory for each user.
In the two-level directory structure, each user has their own user files directory (UFD). The
UFDs have similar structures, but each lists only the files of a single user. system’s master file
directory (MFD) is searches whenever a new user id=s logged in. The MFD is indexed by
username or account number, and each entry points to the UFD for that user.
Advantages:
We can give full path like /User-name/directory-name/.
Different users can have the same directory as well as the file name.
Searching of files becomes easier due to pathname and user-grouping.
Disadvantages:
A user is not allowed to share files with other users.
225
Still, it not very scalable, two files of the same type cannot be grouped together in the same
user.
Tree-structured directory –
Once we have seen a two-level directory as a tree of height 2, the natural generalization is to
extend the directory structure to a tree of arbitrary height.
This generalization allows the user to create their own subdirectories and to organize their
files accordingly.
A tree structure is the most common directory structure. The tree has a root directory, and every
file in the system has a unique path.
Advantages:
Very general, since full pathname can be given.
Very scalable, the probability of name collision is less.
Searching becomes very easy, we can use both absolute paths as well as relative.
Disadvantages:
Every file does not fit into the hierarchical model, files may be saved into multiple directories.
We can not share files.
It is inefficient, because accessing a file may go under multiple directories.
226
Acyclic graph directory –
An acyclic graph is a graph with no cycle and allows us to share subdirectories and files. The
same file or subdirectories may be in two different directories. It is a natural generalization of
the tree-structured directory.
It is used in the situation like when two programmers are working on a joint project and they
need to access files. The associated files are stored in a subdirectory, separating them from
other projects and files of other programmers since they are working on a joint project so they
want the subdirectories to be into their own directories. The common subdirectories should be
shared. So here we use Acyclic directories.
It is the point to note that the shared file is not the same as the copy file. If any programmer
makes some changes in the subdirectory it will reflect in both subdirectories.
Advantages:
We can share files.
Searching is easy due to different-different paths.
Disadvantages:
We share the files via linking, in case deleting it may create the problem,
If the link is a soft link then after deleting the file we left with a dangling pointer.
In the case of a hard link, to delete a file we have to delete all the references associated with
it.
227
that has been taken by the files and directories.
Advantages:
It allows cycles.
It is more flexible than other directories structure.
Disadvantages:
It is more costly than others.
It needs garbage collection.
228
Advantages:
Both the Sequential and Direct Accesses are supported by this. For direct access, the address
of the kth block of the file which starts at block b can easily be obtained as (b+k).
This is extremely fast since the number of seeks are minimal because of contiguous allocation
of file blocks.
Disadvantages:
This method suffers from both internal and external fragmentation. This makes it inefficient in
terms of memory utilization.
Increasing file size is difficult because it depends on the availability of contiguous memory at
a particular instance.
2. Linked List Allocation
In this scheme, each file is a linked list of disk blocks which need not be contiguous. The disk
blocks can be scattered anywhere on the disk.
The directory entry contains a pointer to the starting and the ending file block. Each block
contains a pointer to the next block occupied by the file.
229
The file ‘jeep’ in following image shows how the blocks are randomly distributed. The last block
(25) contains -1 indicating a null pointer and does not point to any other block.
Advantages:
This is very flexible in terms of file size. File size can be increased easily since the system
does not have to look for a contiguous chunk of memory.
This method does not suffer from external fragmentation. This makes it relatively better in
terms of memory utilization.
Disadvantages:
Because the file blocks are distributed randomly on the disk, a large number of seeks are
needed to access every block individually. This makes linked allocation slower.
It does not support random or direct access. We can not directly access the blocks of a file. A
block k of a file can be accessed by traversing k blocks sequentially (sequential access ) from
the starting block of the file via block pointers.
Pointers required in the linked allocation incur some extra overhead.
3. Indexed Allocation
In this scheme, a special block known as the Index block contains the pointers to all the blocks
occupied by a file. Each file has its own index block. The ith entry in the index block contains
the disk address of the ith file block. The directory entry contains the address of the index block
as shown in the image:
230
Advantages:
This supports direct access to the blocks occupied by the file and therefore provides fast
access to the file blocks.
It overcomes the problem of external fragmentation.
Disadvantages:
The pointer overhead for indexed allocation is greater than linked allocation.
For very small files, say files that expand only 2-3 blocks, the indexed allocation would keep
one entire block (index block) for the pointers which is inefficient in terms of memory
utilization. However, in linked allocation we lose the space of only 1 pointer per block.
For files that are very large, single index block may not be able to hold all the pointers.
Following mechanisms can be used to resolve this:
1. Linked scheme: This scheme links two or more index blocks together for holding the
pointers. Every index block would then contain a pointer or the address to the next index
block.
2. Multilevel index: In this policy, a first level index block is used to point to the second level
index blocks which inturn points to the disk blocks occupied by the file. This can be extended
to 3 or more levels depending on the maximum file size.
3. Combined Scheme: In this scheme, a special block called the Inode (information
Node) contains all the information about the file such as the name, size, authority, etc and the
remaining space of Inode is used to store the Disk Block addresses which contain the actual
file as shown in the image below. The first few of these pointers in Inode point to the direct
blocks i.e the pointers contain the addresses of the disk blocks that contain data of the file.
The next few pointers point to indirect blocks. Indirect blocks may be single indirect, double
indirect or triple indirect. Single Indirect block is the disk block that does not contain the file
data but the disk address of the blocks that contain the file data. Similarly, double indirect
blocks do not contain the file data but the disk address of the blocks that contain the address
231
of the blocks containing the file data.
Advantages –
Simple to understand.
Finding the first free block is efficient. It requires scanning the words (a group of 8 bits)
in a bitmap for a non-zero word. (A 0-valued word has all bits 0). The first free block is
then found by scanning for the first 1 bit in the non-zero word.
232
The block number can be calculated as:
(number of bits per word) *(number of 0-values words) + offset of bit first bit 1 in the non-
zero word .
For the Figure-1, we scan the bitmap sequentially for the first non-zero word.
The first group of 8 bits (00001110) constitute a non-zero word since all bits are not 0. After
the non-0 word is found, we look for the first 1 bit. This is the 5th bit of the non-zero word.
So, offset = 5.
Therefore, the first free block number = 8*0+5 = 5.
2. Linked List –
In this approach, the free disk blocks are linked together i.e. a free block contains a pointer
to the next free block. The block number of the very first disk block is stored at a separate
location on disk and is also cached in memory.
In Figure-2, the free space list head points to Block 5 which points to Block 6, the next free
block and so on. The last free block would contain a null pointer indicating the end of free
list.
A drawback of this method is the I/O required for free space list traversal.
3. Grouping –
This approach stores the address of the free blocks in the first free block. The first free block
stores the address of some, say n free blocks. Out of these n blocks, the first n-1 blocks are
actually free and the last block contains the address of next free n blocks.
An advantage of this approach is that the addresses of a group of free disk blocks can be
found easily.
4. Counting –
This approach stores the address of the first free disk block and a number n of free
contiguous disk blocks that follow the first block.
Every entry in the list would contain:
233
1. Address of first free disk block
2. A number n
For example, in Figure-1, the first entry of the free space list would be: ([Address of Block
5], 2), because 2 contiguous free blocks follow block 5.
Multiple I/O requests may arrive by different processes and only one I/O request can be
served at a time by the disk controller. Thus other I/O requests need to wait in the waiting
queue and need to be scheduled.
Two or more request may be far from each other so can result in greater disk arm movement.
Hard drives are one of the slowest parts of the computer system and thus need to be accessed
in an efficient manner.
There are many Disk Scheduling Algorithms but before discussing them let’s have a quick look
at some of the important terms:
Seek Time:Seek time is the time taken to locate the disk arm to a specified track where the
data is to be read or write. So the disk scheduling algorithm that gives minimum average seek
time is better.
Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to
rotate into a position so that it can access the read/write heads. So the disk scheduling
algorithm that gives minimum rotational latency is better.
Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating speed
of the disk and number of bytes to be transferred.
Disk Access Time: Disk Access Time is:
Rotational Latency +
Transfer Time
234
Disk Response Time: Response Time is the average of time spent by a request waiting to
perform its I/O operation. Average Response time is the response time of the all
requests. Variance Response Time is measure of how individual request are serviced with
respect to average response time. So the disk scheduling algorithm that gives minimum
variance response time is better.
Disk Scheduling Algorithms
1. FCFS: FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are
addressed in the order they arrive in the disk queue.Let us understand this with the help of an
example.
Example:
1. Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50
Advantages:
1. SSTF: In SSTF (Shortest Seek Time First), requests having shortest seek time are executed
first. So, the seek time of every request is calculated in advance in the queue and then they are
scheduled according to their calculated seek time. As a result, the request near the disk arm
will get executed first. SSTF is certainly an improvement over FCFS as it decreases the
average response time and increases the throughput of system.Let us understand this with the
help of an example.
235
Example:
1. Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50
1.
So, total seek time:
1. =(50-43)+(43-24)+(24-16)+(82-16)+(140-82)+(170-140)+(190-170)
=208
Advantages:
1. SCAN: In SCAN algorithm the disk arm moves into a particular direction and services the
requests coming in its path and after reaching the end of disk, it reverses its direction and
again services the request arriving in its path. So, this algorithm works as an elevator and
hence also known as elevator algorithm. As a result, the requests at the midrange are
serviced more and those arriving behind the disk arm will have to wait.
Example:
1. Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is
at 50, and it is also given that the disk arm should move “towards the larger value”.
236
1.
Therefore, the seek time is calculated as:
1. =(199-50)+(199-16)
=332
Advantages:
High throughput
Low variance of response time
Average response time
Disadvantages:
Long waiting time for requests for locations just visited by disk arm
1. CSCAN: In SCAN algorithm, the disk arm again scans the path that has been scanned, after
reversing its direction. So, it may be possible that too many requests are waiting at the other
end or there may be zero or few requests pending at the scanned area.
These situations are avoided in CSCAN algorithm in which the disk arm instead of reversing its
direction goes to the other end of the disk and starts servicing the requests from there. So, the
disk arm moves in a circular fashion and this algorithm is also similar to SCAN algorithm and
hence it is known as C-SCAN (Circular SCAN).
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”.
237
Seek time is calculated as:
=(199-50)+(199-0)+(43-0)
=391
Advantages:
1. LOOK: It is similar to the SCAN disk scheduling algorithm except for the difference that the
disk arm in spite of going to the end of the disk goes only to the last request to be serviced in
front of the head and then reverses its direction from there only. Thus it prevents the extra
delay which occurred due to unnecessary traversal to the end of the disk.
Example:
1. Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is
at 50, and it is also given that the disk arm should move “towards the larger value”.
238
1.
So, the seek time is calculated as:
1. =(190-50)+(190-16)
=314
Example:
1. Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is
at 50, and it is also given that the disk arm should move “towards the larger value”
1.
So, the seek time is calculated as:
1. =(190-50)+(190-16)+(43-16)
=341
2. RSS– It stands for random scheduling and just like its name it is nature. It is used in situations
where scheduling involves random attributes such as random processing time, random due
dates, random weights, and stochastic machine breakdowns this algorithm sits perfect. Which
is why it is usually used for and analysis and simulation.
3. LIFO– In LIFO (Last In, First Out) algorithm, newest jobs are serviced before the existing
ones i.e. in order of requests that get serviced the job that is newest or last entered is serviced
first and then the rest in the same order.
Advantages
Maximizes locality and resource utilization
Can seem a little unfair to other requests and if new requests keep coming in, it cause
starvation to the old and existing ones.
239
4. N-STEP SCAN – It is also known as N-STEP LOOK algorithm. In this a buffer is created for
N requests. All requests belonging to a buffer will be serviced in one go. Also once the buffer
is full no new requests are kept in this buffer and are sent to another one. Now, when these N
requests are serviced, the time comes for another top N requests and this way all get requests
get a guaranteed service
Advantages
It eliminates starvation of requests completely
5. FSCAN– This algorithm uses two sub-queues. During the scan all requests in the first queue
are serviced and the new incoming requests are added to the second queue. All new requests
are kept on halt until the existing requests in the first queue are serviced.
Advantages
FSCAN along with N-Step-SCAN prevents “arm stickiness” (phenomena in I/O scheduling
where the scheduling algorithm continues to service requests at or near the current sector
and thus prevents any seeking)
Each algorithm is unique in its own way. Overall Performance depends on the number and type
of requests.
Note:Average Rotational latency is generally taken as 1/2(Rotational latency).
240
Example –
Request sequence = {176, 79, 34, 60, 92, 11, 41, 114}
Initial head position = 50
The following chart shows the sequence in which requested tracks are serviced using SSTF.
241
5. Increment the total seek count with this distance.
6. Currently serviced track position now becomes the new head position.
7. Go to step 3 until we reach at one of the ends of the disk.
8. If we reach at the end of the disk reverse the direction and go to step 2 until all tracks in
request array have not been serviced.
Example:
Input:
Request sequence = {176, 79, 34, 60, 92, 11, 41, 114}
Initial head position = 50
Direction = left (We are moving from right to left)
Output:
Total number of seek operations = 226
Seek Sequence is
41
34
11
0
60
79
92
114
176
The following chart shows the sequence in which requested tracks are serviced using SCAN.
242
C-SCAN (Circular Elevator) Disk Scheduling Algorithm
The circular SCAN (C-SCAN) scheduling algorithm is a modified version of the SCAN disk
scheduling algorithm that deals with the inefficiency of the SCAN algorithm by servicing the
requests more uniformly. Like SCAN (Elevator Algorithm) C-SCAN moves the head from one
end servicing all the requests to the other end. However, as soon as the head reaches the other
end, it immediately returns to the beginning of the disk without servicing any requests on the
return trip (see chart below) and starts servicing again once reaches the beginning. This is also
known as the “Circular Elevator Algorithm” as it essentially treats the cylinders as a circular list
that wraps around from the final cylinder to the first one.
Advantages of C-SCAN (Circular Elevator) Disk Scheduling Algorithm:
Works well with moderate to heavy loads.
It provides better response time and uniform waiting time.
Disadvantages of C-SCAN (Circular Elevator) Disk Scheduling Algorithm:
May not be fair to service requests for tracks at the extreme end.
It has more seek movements as compared to the SCAN Algorithm.
Algorithm:
1. Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‘head’ is the position of disk head.
2. The head services only in the right direction from 0 to the size of the disk.
3. While moving in the left direction do not service any of the tracks.
4. When we reach the beginning(left end) reverse the direction.
5. While moving in the right direction it services all tracks one by one.
6. While moving in the right direction calculate the absolute distance of the track from the head.
7. Increment the total seek count with this distance.
8. Currently serviced track position now becomes the new head position.
9. Go to step 6 until we reach the right end of the disk.
10. If we reach the right end of the disk reverse the direction and go to step 3 until all tracks in the
request array have not been serviced.
Examples:
Input:
Request sequence = {176, 79, 34, 60, 92, 11, 41, 114}
Initial head position = 50
Direction = right(We are moving from left to right)
Output:
Initial position of head: 50
Total number of seek operations = 389
Seek Sequence is
60
79
92
114
176
199
0
11
243
34
41
The following chart shows the sequence in which requested tracks are serviced using SCAN.
= (60-50)+(79-60)+(92-79)
+(114-92)+(176-114)+(199-176)+(199-0)
+(11-0)+(34-11)+(41-34)
= 389
LOOK Disk Scheduling Algorithm
Given an array of disk track numbers and initial head position, our task is to find the total
number of seek operations done to access all the requested tracks if LOOK disk scheduling
algorithm is used. Also, write a program to find the seek sequence using LOOK disk scheduling
algorithm.
LOOK Disk Scheduling Algorithm:
LOOK is the advanced version of SCAN (elevator) disk scheduling algorithm which gives
slightly better seek time than any other algorithm in the hierarchy (FCFS->SRTF->SCAN->C-
SCAN->LOOK). The LOOK algorithm services request similarly as SCAN algorithm meanwhile
it also “looks” ahead as if there are more tracks that are needed to be serviced in the same
direction. If there are no pending requests in the moving direction the head reverses the direction
and start servicing requests in the opposite direction.
The main reason behind the better performance of LOOK algorithm in comparison to SCAN is
because in this algorithm the head is not allowed to move till the end of the disk.
Algorithm:
1. Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‘head’ is the position of disk head.
2. The initial direction in which head is moving is given and it services in the same direction.
3. The head services all the requests one by one in the direction head is moving.
4. The head continues to move in the same direction until all the request in this direction are
finished.
5. While moving in this direction calculate the absolute distance of the track from the head.
6. Increment the total seek count with this distance.
244
7. Currently serviced track position now becomes the new head position.
8. Go to step 5 until we reach at last request in this direction.
9. If we reach where no requests are needed to be serviced in this direction reverse the direction
and go to step 3 until all tracks in request array have not been serviced.
Examples:
Input:
Request sequence = {176, 79, 34, 60, 92, 11, 41, 114}
Initial head position = 50
Direction = right (We are moving from left to right)
Output:
Initial position of head: 50
Total number of seek operations = 291
Seek Sequence is
60
79
92
114
176
41
34
11
The following chart shows the sequence in which requested tracks are serviced using LOOK.
245
C-LOOK (Circular LOOK) Disk Scheduling Algorithm:
C-LOOK is an enhanced version of both SCAN as well as LOOK disk scheduling algorithms.
This algorithm also uses the idea of wrapping the tracks as a circular cylinder as C-SCAN
algorithm but the seek time is better than C-SCAN algorithm. We know that C-SCAN is used to
avoid starvation and services all the requests more uniformly, the same goes for C-LOOK.
In this algorithm, the head services requests only in one direction(either left or right) until all the
requests in this direction are not serviced and then jumps back to the farthest request on the other
direction and service the remaining requests which gives a better uniform servicing as well as
avoids wasting seek time for going till the end of the disk.
Algorithm-
1. Let Request array represents an array storing indexes of the tracks that have been requested in
ascending order of their time of arrival and head is the position of the disk head.
2. The initial direction in which the head is moving is given and it services in the same direction.
3. The head services all the requests one by one in the direction it is moving.
4. The head continues to move in the same direction until all the requests in this direction have
been serviced.
5. While moving in this direction, calculate the absolute distance of the tracks from the head.
6. Increment the total seek count with this distance.
7. Currently serviced track position now becomes the new head position.
8. Go to step 5 until we reach the last request in this direction.
9. If we reach the last request in the current direction then reverse the direction and move the
head in this direction until we reach the last request that is needed to be serviced in this
direction without servicing the intermediate requests.
10. Reverse the direction and go to step 3 until all the requests have not been serviced.
Examples:
Input:
Request sequence = {176, 79, 34, 60, 92, 11, 41, 114}
Initial head position = 50
Direction = right (Moving from left to right)
Output:
Initial position of head: 50
Total number of seek operations = 156
Seek Sequence is
60
79
92
114
176
11
34
41
The following chart shows the sequence in which requested tracks are serviced using C-LOOK.
246
Therefore, the total seek count = (60 – 50) + (79 – 60) + (92 – 79) + (114 – 92) + (176 – 114) +
(176 – 11) + (34 – 11) + (41 – 34) = 321
247
How it works?
FSCAN makes use of two queues, one of queues stores old r/w requests and other queue stores
new r/w requests. When old requests are handled then only new requests are processed.
Variations of FSCAN algorithm can also consist of N queues which in turn will make response
time faster.
How it handles issue of “high variance in SSTF” ?
FSCAN addresses above mentioned issue by “freezing” queue once scan starts, requests that
arrive after scan starts are processed in the next scan.
Performance analysis :
Citing theoretical analysis, it can be seen that SCAN results in lower average response time than
FSCAN and higher average response time than shortest seek time first (SSTF). FSCAN
algorithm has nice performance due to high throughput and low average response times. FSCAN
removes problem of indefinite postponement.
Example : How requests are processed
248