Operating System
Operating System
UNIT: 1
Introduction: Operating system and function, the evolution of OS, Operating System services, OS
Components. Operating Systems Types: Batch, Time Sharing, Multiprogramming, Multitasking,
Multiprocessor, Distributed, Real Time, Network.
UNIT: 2
CPU Scheduling: Process concept, Process state transitions, schedulers (long term, short term, mid
term), Scheduling concept, Performance criteria, Scheduling algorithms, multiple processor scheduling.
UNIT: 3
Deadlocks: System model, Deadlock characterization, prevention, avoidance detection and recovery
from deadlock.
UNIT: 4
UNIT: 5
File System: File support, access methods, allocation methods (Contiguous, linked and index allocation),
Directory system (Single level, tree structured, acyclic graph and general graph directory), file
protection.
SUGGESTED READINGS:-
An operating system (OS) is a crucial layer of software that manages computer hardware and software
resources, serving as an essential intermediary between computer users and the physical machine. The
primary role of an OS is to control and coordinate the use of hardware among various application
programs, ensuring efficient resource allocation and system stability.
The key functions of an operating system include managing the CPU, memory, input/output devices, and
security protocols. By handling these diverse tasks, the OS acts much like a traffic controller, facilitating
safe and efficient data flow and processing across the system components. Without an operating
system, users would find it challenging to interact with the system except through complex machine-
level instructions.
Operating systems provide a broad range of essential functions that allow computers to operate
efficiently and securely. Foremost among these is resource management. The OS orchestrates CPU
scheduling, allocating processor time efficiently to various processes while managing memory use and
overseeing peripheral devices to ensure smooth operation.
Operating System manages external and internal devices for example, printers, scanners, and
other.
Operating System provides interfaces and drivers for proper communication between system
and hardware devices.
Allows multiple applications to run simultaneously.
Manages the execution of processes, ensuring that the system remains responsive.
Organizes and manages files on storage devices.
Operating system allocates resources to various applications and ensures their efficient
utilization.
If an error occurred in your operating system, then there may be a chance that your data may
not be recovered therefore always have a backup of your data.
Threats and viruses can attack our operating system at any time, making it challenging for the
OS to keep the system protected from these dangers.
For learning about new operating system can be a time-consuming and challenging, Specially for
those who using particular Operating system for example switching from Windows OS to Linux is
difficult.
Keeping an operating system up-to-date requires regular maintenance, which can be time-
consuming.
Operating systems consume system resources, including CPU, memory, and storage, which can
affect the performance of other applications.
System Softwares − System softwares are the programs which interact with Kernal and provides
interface for security managment, memory management and other low level activities.
Application Programs − Application softwares/Programs are the one using which a user
interacts with the operating system. For example a word processor to create a document and
save it on the file system, a notepad to create notes etc.
There are mainly 8 functions of an operating system that are given below :-
1. File Management
The operating system provides an interface for users to create, delete, and modify files. It also manages
the storage of files on secondary storage devices, such as hard disks and optical drives. The operating
system keeps track of the location of files on secondary storage devices and provides mechanisms for
users to access files.
Scheduling: The OS decides which process to execute next and allocates CPU time to each
process.
Process Synchronization: The OS ensures that multiple processes can access shared resources
without conflicts.
Deadlock Handling: The OS prevents and resolves deadlocks, which occur when two or more
processes are waiting for each other to release resources.
2. Memory Management
The operating system manages the allocation and deallocation of memory to processes. It keeps track of
which areas of memory are in use and which areas are free. The operating system also manages the
swapping of processes between main memory and secondary storage.
Memory Allocation: The OS allocates memory to processes and manages the allocation and
deallocation of memory space.
Virtual Memory: The OS uses virtual memory to allow processes to access more memory than is
physically available.
3. Process Management
The operating system creates and terminates processes. It also schedules processes to run on the CPU.
The operating system manages the resources that are allocated to each process, such as memory, CPU
time, and I/O devices.
File Organization: The OS organizes files into a hierarchical file system, making it easy to find
and access files.
File Access Control: The OS controls access to files and ensures that only authorized users can
access specific files.
Backup and Recovery: The OS provides mechanisms for backing up files and recovering them in
case of system failure.
4. Device Management
The operating system manages the devices that are connected to the computer. It provides an interface
for users to access devices and it also handles the interrupts that are generated by devices. The
operating system also manages the allocation and deallocation of devices to processes.
Device Drivers: The OS provides device drivers that allow hardware devices to communicate
with the computer.
Device Scheduling: The OS schedules access to devices and ensures that devices are used
efficiently.
Error Handling: The OS handles device errors and ensures that the system continues to function
properly.
5. Networking
The operating system provides support for networking. It allows processes to communicate with each
other over a network. The operating system also manages the routing of network traffic.
Internet Access: The OS provides support for accessing the Internet and using network services.
Network Security: The OS provides mechanisms for securing network communications and
protecting the system from network attacks.
6. Security
The operating system provides security features to protect the computer from unauthorized access. It
also provides mechanisms for users to authenticate themselves to the operating system. The operating
system also manages the access control to files and other resources.
User Authentication: The OS provides mechanisms for user authentication, such as passwords
and biometric data.
Access Control: The OS controls access to system resources and ensures that only authorized
users can access specific resources.
7. User Interface
The operating system provides a user interface that allows users to interact with the computer. The user
interface can be a command-line interface (CLI) or a graphical user interface (GUI). The operating system
also provides mechanisms for users to customize the user interface.
Command-Line Interface (CLI): The OS provides a CLI that allows users to interact with the
system using text commands.
Graphical User Interface (GUI): The OS provides a GUI that allows users to interact with the
system using graphical elements such as icons, menus, and windows.
The history of the operating system has four generations now. Let us understand each of them in detail.
1. First Generation (1945-1955) In this generation, operating systems were not introduced
therefore the instruction was directly given to the computer systems. All the code was included
to communicate with the connected hardware and the system.
Electronic computers were introduced during the 1940s when there were no operating systems
therefore the code was written in machine language where they were just used to solve simple
mathematic problems.
Example - to control the machine functions plug boards were used using the wiring.
2. Second Generation (1955-1965) GMOS (General Motos operating system) was the first
operating system that came into the picture in the 1950s which was developed for IBM
computers. IBM was the first one that bought an operating system into the world. In the second
generation, around the 1960s the first UNIX Operating system was developed that was available
for free for a few years. Also, the batch processing system, where all the similar jobs are
collected in groups by the system, and then all the jobs are submitted to the operating system
using a punch card to execute all jobs in a machine.
Example - Type of operating system and Batch OS.
3. Third Generation (1965-1980) In the third generation, the concept of multiprogramming was
introduced in which multiple tasks could be performed in a single computer i.e., operating
system. Due to performing multiple tasks at a time, multiprogramming allows the CPU to be
busy every time multiple tasks are performed on the same computer. With the DEC PDP-
1 in 1961, the development of minicomputers' phenomenal growth was introduced.
Example - Concept of multiprogramming and types of operating systems used in
multiprogramming.
4. Fourth Generation (1980-now) The evolution of computers aka operating systems came under
the fourth generation. Every user is using their personal computers in this generation. The
concept of personal computers is similar to the minicomputer that was introduced in the third
generation. The birth of the Microsoft Windows operating system was in 1975 and then Bill
Gates took the personal computers to next level by launching MS-DOS in 1981, but due to the
cryptic commands, it was difficult for a user to get hold of the commands. In this generation,
people were also introduced to Graphic User Interface(GUI). Today, Windows is the most
popular operating system and has evolved from Windows 95, Windows 98, Windows XP, and
Windows 7. The most used Windows operating system was Windows 7 and Windows 10. In
2021, Windows 11 was introduced with major changes. Apple also introduced its operating
system known as MacOS which is also as popular as Windows these days.
Question : Explain about the Services of Operating System ?
The operating system works as a resource manager for the system. The various services of the operating
system for efficient working of the system are:
1. Program execution
The operating system loads the program into the memory and takes care of the memory
allocation for the program. Program execution is one of the operating system services which
also ensures that the program that is started can also end its execution
either normally or forcefully.The program is first loaded into the RAM and then the CPU is being
assigned for program execution through various CPU scheduling algorithms provided by the
operating system.After the program execution, the operating system also takes care of
process synchronization, inter-process communication, and deadlock handling.
2. Control Input/output devices
The programs running in the system need input and output devices access for performing the
input/output operations. The access to the input and output devices is given by the operating
system to the program for I/O operations. I/O operations mean writing or reading operations
performed over any file or any input/output device.
3. Program creation
In order to create, modify and debug programs the operating system provides tools like editors
and debuggers to make the task of programmers easy.
4. Error Detection and Response
Handling and detecting error is one of the crucial operating system services which ensures the
smooth working of the system.
The error can occur in the system in the following devices or programs -
Network connection error, loose connection of I/O devices, and restricted network calls are
some of the error that occur in input/output devices.
The program run by the user in the system can also cause errors such as accessing illegal
memory, undefifned operations such as division by zero, excess use of CPU by a program
etc.
5. Accounting
The operating system keeps track of all the data of performance parameters and response time
of the system in order to make it more robust. This data is used to improve the performance of
the operating system and minimize the response time of the system.
6. Security and Protection
If a user downloads a program from the internet there are chances that the program can contain
malicious code which can affect other programs in the system. The operating system takes care
that such a program is checked for any malicious code before downloading it to the system.
7. File Management
File management is one of the operating system services that handles the memory
management for the programs running in the system. The operating system knows the
information of all the types of different files and properties of different storage devices and
ensures the proper management and safety of the files stored in the secondary storage devices.
8. Communication
The processes running in the system need to communicate with each other and also the
computers or systems connected over a network need to exchange data with each other over a
secure network. Operating system uses message passing and shared memory to keep
communication effective and safe.
9. User Interface
The user interacts with the system either by command-line interface or Graphical user interface.
The command-line interface uses text commands entered by the user to interact with the
system. These commands can also be given using a terminal emulator, or remote shell client. A
graphical user interface is a more user-friendly way to interact with the system. The GUI
provides icons, widgets, texts, labels, and text navigation. The user can easily interact with these
icons and widgets with just a click of a mouse or keyboard.
10. Resource allocation
The processes running in the system require resources to complete their execution. The
operating system uses CPU scheduling to allocate resources effectively among the processes
ensuring better utilization of the CPU. The resources used by the processes can be CPU cycles,
primary memory storage, file storage, and I/O devices.
11. Command Interpretation
The user interacts with the system through commands and the operating system interprets
these commands and inputs and provides appropriate outputs accordingly. If the interpreter is
separate from the kernel then the user can modify the interpreter and prevent
any unauthorized access to the system
There are different users, and each user prepares their work in a standalone device, such as punchcards
in batch operating systems and sends them to a computer operator. The various systems split and
distribute similar tasks in batches to facilitate computing and faster responses. A single operator takes
similar jobs with similar needs and requirements and then groups them into various batches. Similar
kinds of jobs that share similar needs and requirements. These types of operating systems are not used
nowadays.
Advantages
The overall time the system takes to execute all the programs will be reduced.
It can give specific time to the computer, and when a computer is idle can process the jobs.
Disadvantages
The CPU utilization is low because the time taken in loading and unloading batches is very high
compared to execution time.
Meanwhile, if one job takes too much time, other jobs must wait.
2. Client/server network OS
Client/server network operating systems are those networks that contain two types of nodes: the
servers and clients. The servers host the applications or services for users while clients use these
applications. In a client/server system, both the server and client computers must have certain software
installed to connect to each other securely over a network connection.
Client-server networks are a type of computer network in which two or more computer systems are
linked through a telecommunications network. Clients are the computers that use the network to access
services provided by the server. Servers are the computers that provide the services to the network.
Client/server networks are commonly used in business and government applications.
Advantages
Allows companies to scale their computing resources to handle increased demand without
having to buy new hardware.
Client-server systems can be quickly reconfigured to meet the changing needs of an
organization.
They are also more reliable and easier to maintain than dedicated server systems.
Lower operating cost.
More reliable and easier to maintain than dedicated server systems
Disadvantages
These OS need more sophisticated management and networking technologies, longer startup
times, and increased vulnerability to attack.
Less secure than dedicated server systems.
More challenging to scale than dedicated server systems.
3. Time-sharing OS
A time-sharing operating system is an application that provides a shared user interface with multiple
users logged in simultaneously. It allows multiple users to access the same resources, such as files and
applications, as long as they are logged in simultaneously. This operating system type is most commonly
used in businesses, especially those that involve many simultaneous users. Time-sharing operating
systems enable users to finish their jobs on a system at once. The time-sharing OS is the latest
advancement in the computer science world; it is being accepted worldwide, also at an increasing rate.
4. Distributed OS
Advantages
It is more reliable as a failure of one system will not impact the other computers or the
overall system.
All computers work independently.
Resources are shared, so there is less cost overall.
The system works at a higher speed as resources are shared
The host system has less load.
Computers can be easily added to the system.
Disadvantages
Costly setup.
If the server fails, then the whole system will fail.
Complex software is used for such a system
5. Multiprocessor OS
A multiprocessor operating system is an operating system that uses multiple processors to improve
performance. This operating system is commonly found on computers with more than one CPU.
Multiprocessor systems improve system performance by allowing the execution of tasks on multiple
processors simultaneously. Overall reduces the time it takes to complete specific tasks.
Advantages
Disadvantages
They require additional hardware, such as processors and memory, making a system more
expensive.
6. Multi-programming OS
The operating system which can run multiple processes on a single processor is called a
multiprogramming operating system. There are different programs that want to get executed. So these
programs are kept in the ready queue. And are assigned to the CPU one by one. If one process gets
blocked then other processes from the ready queue are assigned to the CPU. The aim of this is optimal
resource utilization and more CPU utilization. In the below figure, different processes are there in
RAM(main memory). Some processes are waiting for the CPU, and process 2(which was previously
executing) is now doing I/O operations. So CPU shifted to execute process 1.
Multitasking OS
Multi-tasking operating systems are designed to enable multiple applications to run simultaneously.
Multi-tasking operating systems allow multiple users to work on the same document or application
simultaneously. For example, a user running antivirus software, searching the internet, and playing a
song simultaneously. Then the user is using a multitasking OS.
UNIT: 2 CPU Scheduling
Process concept, Process state transitions, schedulers (long term, short term, mid term), Scheduling
concept, Performance criteria, Scheduling algorithms, multiple processor scheduling.
A process is defined as a sequence of instructions executed in a predefined order. In simple words, any
program that is executed is termed as a process. Processes change their state as they execute and can
be either new, ready, running, waiting or terminated. A process in OS is managed by the Process Control
Block (PCB).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.
The process life cycle is the sequence of states that a process goes through during its existence. The
process life cycle can be divided into the following stages:
1. New: The process has been created but has not yet been scheduled for execution.
2. Ready: The process is ready to be executed.
3. Running: The process is currently being executed.
4. Waiting: The process is waiting for an event to occur, such as the completion of an I/O
operation.
5. Terminated: The process has finished executing.
In the new state, the process has been created but has not yet been allocated any resources. The
process is moved to the runnable state when it is scheduled for execution. The process is moved to the
running state when it is given the CPU. The process is moved to the blocked state when it is waiting for
an event to occur. The process is moved to the terminated state when it has finished executing.
An OS serves in the creation, scheduling and termination of processes. The OS consists of Process
Control Block (PCB) that helps control the functioning of processes. Every process in OS has a PCB
associated with it. A PCB keeps track of processes by storing information about various things like their
state, I/O status and CPU Scheduling.
Understanding the components of a PCB will help us get to know the process information better. A PCB
comprises of the following:
1. New: When a program is started, the operating system creates a new process for it. The
process is then placed in the new state. In the new state, the process has been created but
has not yet been allocated any resources.
2. Runnable: When a process is in the runnable state, it is ready to be executed. The operating
system maintains a queue of runnable processes. The scheduler selects a process from the
queue and moves it to the running state.
3. Running: When a process is in the running state, it is currently being executed. The process
has been given the CPU and is executing its instructions.
4. Blocked: When a process is in the blocked state, it is waiting for an event to occur. The
event could be the completion of an I/O operation, the arrival of a signal, or the availability
of a resource. When the event occurs, the process is moved to the runnable state.
5. Terminated: When a process has finished executing, it is moved to the terminated state.
The operating system then releases all of the resources that were allocated to the process.
Existence Dynamic entity that exists for a limited time. Static entity that exists until it
is deleted.
State Can be in one of several states, such as new, Always in the same state.
runnable, running, blocked, or terminated.
Control Has a process control block (PCB) that Does not have a control block.
block contains information about the process.
Execution Can be executed concurrently with other Can only be executed one at a
processes. time.
CPU scheduling is a process used by the operating system to decide which task or process gets to use
the CPU at a particular time. This is important because a CPU can only handle one task at a time, but
there are usually many tasks that need to be processed. The following are different purposes of a CPU
scheduling time.
As you can see in the diagram above, we have processes that come from the job queue to the ready
queue (in primary memory) that are one by one, in some manner given resources, and then their
execution is completed.
CPU scheduling in OS is a method by which one process is allowed to use the CPU while the other
processes are kept on hold or are kept in the waiting state. This hold or waiting state is implemented
due to the unavailability of any of the system resources like I/O etc. Thus, the purpose of CPU scheduling
in OS is to result in an efficient, faster and fairer system.
2. Completion Time in OS: It is the time at which the selected process completes its execution.
3. Burst Time in OS: This is the time required by a process for its execution by the CPU.
4. Turn Around Time in OS: Turnaround time for each process is the difference in completion time
and the time of arrival of that process. Turn around time = Completion time – Arrival time
5. Waiting time in OS: The waiting time for each and every process is the difference between the
turnaround time and burst time of that process.
To understand the terms given above let us consider the following process table with the processes
scheduled according to First Come First Serve algorithm:
Process Arrival Time Burst Time Completion Time Turnaround Time Waiting Time
P1 0 6 (0+6) = 6 (6 – 0) = 6 (6 – 6) = 0
P2 2 3 (6 +3) = 9 (9 – 2) = 7 (7 – 3) = 4
P3 4 5 (9 + 5) = 14 (14 – 4) = 10 (10 – 5) = 5
There are many different CPU scheduling algorithms and choosing a particular one depends on a
number of factors. The following conditions may be considered before selecting the most appropriate
CPU scheduling algorithm:
CPU Utilization
A CPU algorithm must keep a CPU busy to the maximum level. Theoretically, CPUs are considered to be
working in the range of 0% to 100%, where 100 is the ideal; but practically, depending on the load the
range of CPU usage is 40% to 90%.
Throughput
Throughput refers to the average performance of a CPU, i.e. the number of processes processed and
executed to completion during each unit of time. The throughput of a CPU varies on the basis of the
length or duration of a process.
Turnaround Time
Turnaround time is the time taken for the execution of a particular process. It is the duration in which a
process is performed, i.e. time elapsed between the time of submission of the process to its time of
completion.
Waiting Time
Once a process has started its execution is not affected much by any scheduling algorithm. The
scheduling algorithms have a definite effect on the waiting time of the processes that are already in the
ready queue and need to acquire the CPU control.
Load Average
Load average refers to the average number of processes that are already present in the ready queue and
are waiting to acquire control of the CPU.
Response Time
Response time is measured as the duration between submission of the process for execution till the
time the first response is produced. Response time is the time spent when the process is in the ready
state and gets the CPU for the first time. summarize the effect of the factors listed above, it can be said
that for optimal performance the CPU utilization and Throughput be maximized with the minimization of
the other factors.
The types of scheduling in OS are primarily categorized into two main categories:
o Preemptive Scheduling
o Non-Preemptive Scheduling
1. Preemptive Scheduling:
In preemptive scheduling, the operating system can interrupt a currently running process and
allocate the CPU to another process with higher priority or a more urgent task.
The CPU can be preempted (taken away) from a process even if it has not completed its
execution.
Examples of preemptive scheduling algorithms include Shortest Remaining Time First (SRTF),
Round Robin, and Priority Scheduling with preemption.
Preemptive scheduling is used when a process switches from running state to ready state or
from waiting state to ready state.
The resources (mainly CPU cycles) are allocated to the process for the limited amount of time
and then is taken away, and the process is again placed back in the ready queue if that process
still has CPU burst time remaining.
That process stays in ready queue till it gets next chance to execute.
2. Non-Preemptive Scheduling:
Once a process starts executing, it retains control of the CPU until it finishes or explicitly gives up
control.
Non-preemptive scheduling is simpler to implement and may be suitable for systems where
fairness is less critical, such as batch processing environments.
In this scheduling, once the resources (CPU cycles) is allocated to a process, the process holds
the CPU till it gets terminated or it reaches a waiting state.
In case of non-preemptive scheduling does not interrupt a process running CPU in middle of the
execution.
Instead, it waits till the process complete its CPU burst time and then it can allocate the CPU to
another process.
Question : What are the Different types of CPU Scheduling Algorithms in OS ?
Computer scientists have developed some algorithms that can decide the order of execution of
processes in a way that shall help achieve maximum utilization of the CPU. These CPU Scheduling
algorithms in operating systems can be classified as follows:
Of all the scheduling algorithms it is one of the simplest and easiest to implement. As the name suggests
the First Come First Serve scheduling algorithm means that the process that requests the CPU first is
allocated the CPU first. It is basically implemented using a First In First Out queue. It supports both non-
preemptive and preemptive CPU scheduling algorithms.
In FCFS Scheduling
The process which arrives first in the ready queue is firstly assigned the CPU.
In case of a tie, process with smaller process id is executed first.
It is always non-preemptive in nature.
Jobs are executed on first come, first serve basis.
It is a non-preemptive, pre-emptive scheduling algorithm.
Easy to understand and implement.
Its implementation is based on FIFO queue.
Poor in performance as average wait time is high.
Advantages-
Disadvantages-
Shortest job first (SJF) is a scheduling algorithm that selects the waiting process with the smallest
execution time to be executed next. The method followed by the SJF scheduling algorithm may or may
not be preemptive. SJF reduces the average waiting time of other waiting processes significantly.
Process which have the shortest burst time are scheduled first.
If two processes have the same bust time, then FCFS is used to break the tie.
This is a non-pre-emptive, pre-emptive scheduling algorithm.
Best approach to minimize waiting time.
Easy to implement in Batch systems where required CPU time is known in advance.
Impossible to implement in interactive systems where required CPU time is not known.
The processer should know in advance how much time process will take.
Pre-emptive mode of Shortest Job First is called as Shortest Remaining Time First (SRTF).
Advantages-
Disadvantages-
It can not be implemented practically since burst time of the processes can not be known in
advance.
It leads to starvation for processes with larger burst time.
Priorities can not be set for the processes.
Processes with larger burst time have poor response time.
The working of the Longest Job First(LJF) scheduling process is the opposite of the shortest job first (SJF),
Here, the process with the largest burst time is processed first. Longest Job First is one of the non-
preemptive algorithms.
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
No other process can execute until the longest job or process executes completely.
All the jobs or processes finish at the same time approximately.
Disadvantages
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.
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.
4. Priority Scheduling:
The Priority CPU Scheduling Algorithm is one of the pre-emptive methods of CPU scheduling. Here, the
most important process must be done first. In the case there are more than one processes with the
same priority value then the FCFS algorithm is used to resolve the situation.
Priority Scheduling
Out of all the available processes, CPU is assigned to the process having the highest priority.
In case of a tie, it is broken by FCFS Scheduling.
Priority Scheduling can be used in both preemptive and non-preemptive mode.
The waiting time for the process having the highest priority will always be zero in preemptive
mode.
The waiting time for the process having the highest priority may not be zero in nonpreemptive
mode.
Priority scheduling in preemptive and non-preemptive mode behaves exactly same under following
conditions-
Advantages-
It considers the priority of the processes and allows the important processes to run first.
Priority scheduling in pre-emptive mode is best suited for real time operating system.
Disadvantages-
5. Round Robin:
In the Round Robin CPU scheduling algorithm the processes are allocated CPU time in a cyclic order with
a fixed time slot for each process. It is said to be a pre-emptive version of FCFS. With the cyclic allocation
of equal CPU time, it works on the principle of time-sharing.
CPU is assigned to the process on the basis of FCFS for a fixed amount of time.
This fixed amount of time is called as time quantum or time slice.
After the time quantum expires, the running process is preempted and sent to the
ready queue.
Then, the processor is assigned to the next arrived process.
It is always preemptive in nature.
Advantages-
Disadvantages-
It leads to starvation for processes with larger burst time as they have to repeat the cycle many
times.
Its performance heavily depends on time quantum.
Priorities can not be set for the processes.
The Shortest remaining time first CPU scheduling algorithm is a preemptive version of the Shortest job
first scheduling algorithm. Here the CPU is allocated to the process that needs the smallest amount of
time for its completion. Here the short processes are handled very fast but the time taking processes
keep waiting.
Advantages:
SRTF algorithm makes the processing of the jobs faster than SJN algorithm, given it's
overhead charges are not counted.
Allows for easier management of library updates or replacements without recompiling the
program.
Enables efficient memory usage, as libraries can be shared among multiple instances of the
program.
Provides better portability, as the program can be executed on different systems with
compatible libraries available at runtime.
Disadvantages:
The longest remaining time first CPU scheduling algorithm is a preemptive CPU scheduling algorithm.
This algorithm selects those processes first which have the longest processing time remaining for
completion i.e. processes with the largest burst time are allocated the CPU time first.
No other process can execute until the longest job or process executes completely.
All the jobs or processes finishes at the same time approximately.
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.
The Highest Response Ratio Next is one of the non-preemptive CPU Scheduling algorithms. It has the
distinction of being recognised as one of the most optimal scheduling algorithms. As the name suggests
here the CPU time is allocated on the basis of the response ratio of all the available processes where it
selects the process that has the highest Response Ratio. The selected process will run till its execution is
complete.
Advantages of HRRN
Fairness: HRRN provides fairness by considering both the waiting time and burst time of
processes. It ensures that processes with longer waiting times are given priority, which helps
prevent starvation.
Better Response Time: Since HRRN prioritizes processes that have been waiting for a longer
period, it can lead to better response times for these processes compared to simpler algorithms
like FCFS.
No Starvation: Unlike some other scheduling algorithms like Shortest Job First (SJF), HRRN
reduces the risk of starvation. Even processes that need more CPU time will eventually get
executed.
Simple to Implement: The algorithm is easy to understand and implement since it only requires
calculating the response ratio and selecting the process with the highest ratio.
Dynamic Prioritization: The HRRN algorithm dynamically adjusts priorities meaning that
processes which have been waiting longer (but are not necessarily the shortest jobs) will
eventually be given higher priority.
Disadvantages of HRRN
HRRN is not practically feasible to implement the HRRN scheduling algorithm because we cannot
predict the burst time of every process in advance.
HRRN requires the calculation of the response ratio for each process, which adds some
computational overhead, especially in systems with a large number of processes.
Question : Discuss the Difference between Short-Term, Medium Term, and Long-Term Scheduler?
Short-Term Scheduler
The short-term scheduler selects processes from the ready queue that are residing in the main memory
and allocates CPU to one of them. Thus, it plans the scheduling of the processes that are in a ready
state. It is also known as a CPU scheduler. As compared to long-term schedulers, a short-term scheduler
has to be used very often i. e. the frequency of execution of short-term schedulers is high. The short-
term scheduler is invoked whenever an event occurs. Such an event may lead to the interruption of the
current process or it may provide an opportunity to preempt the currently running process in favor of
another. The example of such events are:
Medium-Term Scheduler
The medium-term scheduler is required at the times when a suspended or swapped-out process is to be
brought into a pool of ready processes. A running process may be suspended because of an I/O request
or by a system call. Such a suspended process is then removed from the main memory and is stored in a
swapped queue in the secondary memory in order to create a space for some other process in the main
memory. This is done because there is a limit on the number of active processes that can reside in the
main memory. The medium-term scheduler is in charge of handling the swapped-out process. It has
nothing to do with when a process remains suspended. However, once the suspending condition is
removed, the medium terms scheduler attempts to allocate the required amount of main memory and
swap the process in and make it ready. Thus, the medium-term scheduler plans the CPU scheduling for
processes that have been waiting for the completion of another process or an I/O task.
Long-Term Scheduler
The long-term scheduler works with the batch queue and selects the next batch job to be executed.
Thus it plans the CPU scheduling for batch jobs. Processes, which are resource intensive and have a low
priority are called batch jobs. These jobs are executed in a group or bunch. For example, a user requests
for printing a bunch of files. We can also say that a long-term scheduler selects the processes or jobs
from secondary storage device eg, a disk and loads them into the memory for execution. It is also known
as a job scheduler. The long-term scheduler is called "long-term" because the time for which the
scheduling is valid is long. This scheduler shows the best performance by selecting a good process mix of
I/O-bound and CPU-bound processes. I/O bound processes are those that spend most of their time in
I/O than computing. A CPU-bound process is one that spends most of its time in computations rather
than generating I/O requests.
Shedulers Function
1. Alternate It is also called a CPU It is also called a process It is also called a job
Name scheduler. swapping scheduler. scheduler.
2. Degree in It provides lesser It reduces the control over It controls the degree of
programming control over the degree the degree multiprogramming.
of multiprogramming. of multiprogramming.
3. Speed The speed of the short- Speed of medium scheduler The speed of a long-
term scheduler is very between the short-term and term term scheduler is
fast. long-term scheduler more than medium-
term scheduler.
5. Purpose It selects the processes It can reintroduce the from It selects processes
from among the among the process into from the pool and loads
process that is ready to memory that executes and them into memory for
execute. its execution can be execution.
continued.
6. Process state Process state is ready Process state is not present Process state is new to
to running ready.
7. Selection of Select a new process Select that process, which is Select a good process ,
process for a CPU quite currently not need to load mix of I/O bound and
frequently. fully on RAM, so it swap it CPU bound.
into swap partition.
Schedulers are a special type of operating system software that manages process scheduling in a variety
of ways. Its main function is to select the jobs that are to be submitted to the system and decide which
process will run.
1. Long-Term (job) Scheduler: Due to the small size of the main memory, initially all the programs
are stored in secondary memory. When they are loaded or stored in main memory, then they
are known as processes. The long-term scheduler decides how many processes will remain in
the ready queue. So, in simple words, the long-term scheduler decides the degree of
multiprogramming of the system.
2. Medium-Term Scheduler: Often a running process needs an I/0 operation that does not require
CPU time. That is why when a process needs I/O operation during its execution, the operating
system sends that process to a blocked queue. When that process completes its I/O operation,
then it is again shifted to the ready queue . All these decisions are taken by the medium-term
scheduler. Medium-term scheduling is part of swapping.
3. Short-Term (CPU) Scheduler: When there are many processes initially in the main memory, all
are present in the ready queue. Out of all these processes, only one is selected for execution.
This decision is in the hands of the short-term scheduler or CPU scheduler.
Advantages of a Scheduler
Optimized CPU Utilization: It helps to check that the CPU is loaded to the maximum possible
extent by frequently choosing tasks to execute.
Fair Process Handling: Demands a fixed amount of CPU time for each process like FCFS, SJF, and
RR in order to offer equal opportunity for processes in the system.
Process Management: Oversees the processes in different states (for instance, ready, blocked,
or running).
Disadvantages of a Scheduler
Complexity: They may also not be well suited for all system designs and can also be very hard to
actually schedule.
Overhead: There are some drawbacks in maintaining the scheduler, as the latter could present
some overhead to the system, and this could be especially so in a real-time environment where
decisions require to be made rapidly.
Dispatcher is a special type of program whose work starts after the scheduler. When the scheduler
completes its task of selecting a process, it is the dispatcher that moves the process to the queue it
needs to go to.
The dispatcher is the module that hands over control of the CPU to the process that has been selected
by the short-term scheduler.
1. switching context: Cores the current process and restores the state of the process to be run
next.
2. Switching to User Mode: Makes sure that it runs in the user mode and not kernel mode,
which is for security and privilege break-
3. Jumps to the correct location in the user program from where the program can be restarted.
Advantages of a Dispatcher
Fast Process Switching: Evaluates circumstances where a procedure shifts from the ready
queue to the execution phase in a way that causes less delay.
Efficient CPU Time Allocation: Is important in making sure that processes receive CPU time,
hence giving the necessary support for multitasking to occur.
Disadvantages of a Dispatcher
Dispatch Latency: Although the time taken is considerably small, this lateness in dispatching
the requests can slow down the system.
Dependent on Scheduler: The dispatcher hence cannot work on her own since she is reliant
on the decisions made by the scheduler.
Consider a situation, where various processes are residing in the ready queue waiting to be executed.
The CPU cannot execute all of these processes simultaneously, so the operating system has to choose a
particular process on the basis of the scheduling algorithm used. So, this procedure of selecting a
process among various processes is done by the scheduler. Once the scheduler has selected a process
from the queue, the dispatcher comes into the picture, and it is the dispatcher who takes that process
from the ready queue and moves it into the running state. Therefore, the scheduler gives the dispatcher
an ordered list of processes which the dispatcher moves to the CPU over time.
Example - There are 4 processes in the ready queue, P1, P2, P3, P4; Their arrival times are t0, t1, t2, t3
respectively. A First in First out (FIFO) scheduling algorithm is used. Because P1 arrived first, the
scheduler will decide it is the first process that should be executed, and the dispatcher will remove P1
from the ready queue and give it to the CPU. The scheduler will then determine P2 to be the next
process that should be executed, so when the dispatcher returns to the queue for a new process, it will
take P2 and give it to the CPU. This continues in the same way for P3, and then P4.
There are no different types in There are 3 types of scheduler i.e. Long-
Types
dispatcher.It is just a code segment. term, Short-term, Medium-term
Deadlocks: System model, Deadlock characterization, prevention, avoidance detection and recovery
from deadlock.
A deadlock is a situation where a set of processes is blocked because each process is holding a resource
and waiting for another resource acquired by some other process. In this article, we will discuss
deadlock, its necessary conditions, etc. in detail.
Deadlock is a situation in computing where two or more processes are unable to proceed
because each is waiting for the other to release resources.
Key concepts include mutual exclusion, resource holding, circular wait, and no preemption.
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. This is a practical example of
deadlock.
Before going into detail about how deadlock occurs in the Operating System, let's first discuss how the
Operating System uses the resources present. A process in an operating system uses resources in the
following way.
Requests a resource
A 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.
Neccessary Conditions for Deadlock:
1. Mutual Exclusion: Resources cannot be shared; only one process can use a resource at a
time.
2. Hold and Wait: A process holds allocated resources while waiting for additional resources
that are currently held by other processes.
3. No Preemption: Resources cannot be forcibly taken away from a process; they must be
released voluntarily.
4. Circular Wait: A set of waiting processes must exist such that each process is waiting for a
resource held by the next process in the set.
1. Mutual Exclusion
Mutual Exclusion condition requires that at least one resource be held in a non-shareable mode, which
means that only one process can use the resource at any given time. Both Resource 1 and Resource 2
are non-shareable in our scenario, and only one process can have exclusive access to each resource at
any given time. As an example:
The hold and wait condition specifies that a process must be holding at least one resource while waiting
for other processes to release resources that are currently held by other processes. In our example,
Both processes hold one resource while waiting for the other, satisfying the hold and wait
condition.
3. No Preemption
Preemption is the act of taking a resource from a process before it has finished its task. According to the
no preemption condition, resources cannot be taken forcibly from a process a process can only release
resources voluntarily after completing its task.
For example – Process p1 have resource r1 and requesting for r2 that is hold by process p2. then
process p1 can not preempt resource r2 until process p2 can finish his execution. After some time it try
to restart by requesting both r1 and r2 resources.
4. Circular Wait
Circular wait is a condition in which a set of processes are waiting for resources in such a way that there
is a circular chain, with each process in the chain holding a resource that the next process needs. This is
one of the necessary conditions for a deadlock to occur in a system.
Example: Imagine four processes—P1, P2, P3, and P4—and four resources—R1, R2, R3, and R4.
This forms a circular chain where every process is waiting for a resource held by another, creating a
situation where no process can proceed, leading to a deadlock.
1. Deadlock Prevention:
This approach aims to prevent deadlocks by ensuring that at least one of the necessary conditions for
deadlock cannot occur.The strategy of deadlock prevention is to design the system in such a way that
the possibility of deadlock is excluded. The indirect methods prevent the occurrence of one of three
necessary conditions of deadlock i.e., mutual exclusion, no pre-emption, and hold and wait. The direct
method prevents the occurrence of circular wait. Prevention techniques -Mutual exclusion - are
supported by the OS. Hold and Wait - the condition can be prevented by requiring that a process
requests all its required resources at one time and blocking the process until all of its requests can be
granted at the same time simultaneously.
Mutual Exclusion: Ensure that resources are not held exclusively by a single process.
Hold and Wait: Require processes to request and be allocated all their resources before
execution, or allow them to request resources only when they hold none.
No Preemption: If a process holding resources requests another, and the request cannot be
granted immediately, the process must release all its currently held resources.
Circular Wait: Prevent the occurrence of circular wait by imposing a total ordering on
resources and requiring processes to request them in a specific order.
2. Deadlock Avoidance:
This method involves making dynamic decisions about resource allocation to ensure that the system
remains in a safe state where no deadlock can occur. The deadlock avoidance Algorithm works by
proactively looking for potential deadlock situations before they occur. It does this by tracking the
resource usage of each process and identifying conflicts that could potentially lead to a deadlock. If a
potential deadlock is identified, the algorithm will take steps to resolve the conflict, such as rolling back
one of the processes or pre-emptively allocating resources to other processes. The Deadlock Avoidance
Algorithm is designed to minimize the chances of a deadlock occurring, although it cannot guarantee
that a deadlock will never occur. This approach allows the three necessary conditions of deadlock but
makes judicious choices to assure that the deadlock point is never reached. It allows more concurrency
than avoidance detection A decision is made dynamically whether the current resource allocation
request will, if granted, potentially lead to deadlock. It requires knowledge of future process requests.
Two techniques to avoid deadlock :
Advantages
Disadvantages
This approach allows the system to enter a deadlock state, but then detects and recovers from
it. Deadlock detection is used by employing an algorithm that tracks the circular waiting and kills one or
more processes so that the deadlock is removed. The system state is examined periodically to determine
if a set of processes is deadlocked. A deadlock is resolved by aborting and restarting a process,
relinquishing all the resources that the process held.
This technique does not limit resource access or restrict process action.
Deadlock recovery :
Recovery from Deadlock in Operating Systems" refers to the set of techniques and algorithms designed
to detect, resolve, or mitigate deadlock situations. These methods ensure that the system can continue
processing tasks efficiently without being trapped in an eternal standstill. Let's take a closer look at
some of the key strategies employed.
There is no mechanism implemented by the OS to avoid or prevent deadlocks. The system, therefore,
assumes that a deadlock will undoubtedly occur. The OS periodically checks the system for any
deadlocks in an effort to break them. The OS will use various recovery techniques to restore the system
if it encounters any deadlocks. When a Deadlock Detection Algorithm determines that a deadlock has
occurred in the system, the system must recover from that deadlock.
In the Deadlock ignorance method the OS acts like the deadlock never occurs and completely ignores it
even if the deadlock occurs. This method only applies if the deadlock occurs very rarely. The algorithm is
very simple. It says, " if the deadlock occurs, simply reboot the system and act like the deadlock never
occurred." That's why the algorithm is called the Ostrich Algorithm.
Advantages
Disadvantages
Ostrich Algorithm does not provide any information about the deadlock situation.
It can lead to reduced performance of the system as the system may be blocked for a long time.
UNIT: 4
The term memory can be defined as a collection of data in a specific format. It is used to store
instructions and process data. The memory comprises a large array or group of words or bytes, each
with its own location. The primary purpose of a computer system is to execute programs. These
programs, along with the information they access, should be in the main memory during execution. The
CPU fetches instructions from memory according to the value of the program counter.
The main memory is central to the operation of a Modern Computer. Main Memory is a large array of
words or bytes, ranging in size from hundreds of thousands to billions. Main memory is a repository of
rapidly available information shared by the CPU and I/O devices. Main memory is the place where
programs and information are kept when the processor is effectively utilizing them. Main memory is
associated with the processor, so moving instructions and information into and out of the processor is
extremely fast. Main memory is also known as RAM (Random Access Memory). This memory is volatile.
RAM loses its data when a power interruption occurs.
Memory management keeps track of the status of each memory location, whether it is allocated
or free.
Memory management enables computer systems to run programs that require more main
memory than the amount of free main memory available on the system. This is achieved by
moving data between primary and secondary memory.
Memory management addresses the system’s primary memory by providing abstractions such
that the programs running on the system perceive a large memory is allocated to them.
It is the job of memory management to protect the memory allocated to all the processes from
being corrupted by other processes. If this is not done, the computer may exhibit
unexpected/faulty behavior.
Memory management enables sharing of memory spaces among processes, with the help of
which, multiple programs can reside at the same memory location (although only one at a time).
1. Memory Hierarchy: Computer memory is organized into a hierarchy, ranging from high-speed,
low-capacity registers and cache to slower, high-capacity RAM and even slower but high-
capacity storage devices like hard drives. Memory management ensures that data is moved
efficiently between these levels, with the most critical and frequently accessed data residing in
faster memory for quicker access.
2. Address Spaces: An address space is the range of valid addresses that can be used to identify
memory locations. Each process in an OS has its own address space, preventing interference
between processes and providing a degree of isolation. Memory management is responsible for
allocating and deallocating these address spaces as needed.
3. Memory Allocation and Deallocation: Memory allocation involves assigning a portion of the
available memory to a process, enabling it to store data and execute instructions. Conversely,
memory deallocation releases memory once it is no longer required, preventing wastage of
resources. Common allocation strategies include contiguous and non-contiguous allocation,
each with its own advantages and drawbacks.
Memory management keeps track of the status of each memory location, whether it is allocated or free.
It allocates the memory dynamically to the programs at their request and frees it for reuse when it is no
longer needed. Memory management meant to satisfy some requirements that we should keep in mind.
These Requirements of memory management are:
2. Protection - There is always a danger when we have multiple programs at the same time as one
program may write to the address space of another program. So every process must be
protected against unwanted interference when other process tries to write in a process whether
accidental or incidental. Between relocation and protection requirement a trade-off occurs as
the satisfaction of relocation requirement increases the difficulty of satisfying the protection
requirement.
Prediction of the location of a program in main memory is not possible, that's why it is
impossible to check the absolute address at compile time to assure protection. Most of the
programming language allows the dynamic calculation of address at run time. The memory
protection requirement must be satisfied by the processor rather than the operating system
because the operating system can hardly control a process when it occupies the processor. Thus
it is possible to check the validity of memory references.
3. Sharing - A protection mechanism must have to allow several processes to access the same
portion of main memory. Allowing each processes access to the same copy of the program
rather than have their own separate copy has an advantage.
For example, multiple processes may use the same system file and it is natural to load one copy
of the file in main memory and let it shared by those processes. It is the task of Memory
management to allow controlled access to the shared areas of memory without compromising
the protection. Mechanisms are used to support relocation supported sharing capabilities.
Modules are written and compiled independently and all the references from one
module to another module are resolved by `the system at run time.
There are mechanisms by which modules can be shared among processes. Sharing can
be provided on a module level that lets the user specify the sharing that is desired.
5. Physical organization - The structure of computer memory has two levels referred to as main
memory and secondary memory. Main memory is relatively very fast and costly as compared to
the secondary memory. Main memory is volatile. Thus secondary memory is provided for
storage of data on a long-term basis while the main memory holds currently used programs. The
major system concern between main memory and secondary memory is the flow of information
and it is impractical for programmers to understand this for two reasons:
The programmer may engage in a practice known as overlaying when the main memory
available for a program and its data may be insufficient. It allows different modules to
be assigned to the same region of memory. One disadvantage is that it is time-
consuming for the programmer.
In a multiprogramming environment, the programmer does not know how much space
will be available at the time of coding and where that space will be located inside the
memory.
Resident Monitor
The Resident Monitor is program that runs on Bare Machines. The resident monitor works like a
primitive operating system that controls the instructions and performs all necessary functions such as
job scheduling, memory management, and interrupt processing.
Control Language Interpreter: The first part of the Resident monitor is control language
interpreter which is used to read and carry out the instruction from one level to the next level.
Loader: The second part of the Resident monitor which is the main part of the Resident Monitor
is Loader which Loads all the necessary system and application programs into the main memory.
Device Driver: The third part of the Resident monitor is Device Driver which is used to manage
the connecting input-output devices to the system. So basically it is the interface between the
user and the system. it works as an interface between the request and response. request which
user made, Device driver responds that the system produces to fulfill these requests.
Interrupt Processing: The fourth part as the name suggests, it processes the all occurred
interrupt to the system.
It works like job sequencer as it sequences job and sends them to the processor.
After scheduling the job Resident monitors loads the programs one by one into the main
memory according to their sequences.
It controls the execution of instructions and ensures that programs are executed without any
interruption in between.
The Interrupt Processing feature allows the system to handle signals or requests from the
hardware during execution, ensuring smooth operations.
1. Job Sequencing: The Resident Monitor handles job sequencing and memory management.
2. Faster Execution: There is no time gap between job scheduling and execution, making the
system faster compared to manual job handling.
3. No OS Overhead: The Resident Monitor allows program execution without the overhead of a
operating system, making it lighter in terms of resource consumption.
4. Basic System Control: It provides a minimal, yet effective level of control over hardware
resources.
1. Limited Functionality: The Resident Monitor provides only basic control over the system, and
lacks the extensive features of a modern operating system.
2. No Multitasking: Like the Bare Machine, Resident Monitors cannot handle multiple programs
running simultaneously, which limits their use in more complex environments.
3. Not Scalable: The Resident Monitor is not scalable for larger applications or systems that require
more advanced management.
4. Error Handling: As with Bare Machines, error handling is more difficult because there is no
higher-level operating system to catch and handle errors gracefully.
Fragmentation
Fragmentation is an unwanted problem in the operating system in which the processes are loaded and
unloaded from memory, and free memory space is fragmented. Processes can't be assigned to memory
blocks due to their small size, and the memory blocks stay unused. It is also necessary to understand
that as programs are loaded and deleted from memory, they generate free space or a hole in the
memory. These small blocks cannot be allotted to new arriving processes, resulting in inefficient
memory use. The conditions of fragmentation depend on the memory allocation system. As the process
is loaded and unloaded from memory, these areas are fragmented into small pieces of memory that
cannot be allocated to incoming processes. It is called fragmentation.
Types of Fragmentation
1. External Fragmentation:
Occurs when there is enough total free memory, but it's not contiguous, preventing allocation to a
process. It's like having several small, unusable spaces left behind after processes are removed from
memory. External fragmentation is a result of free space being dispersed in a non-contiguous manner.
As files are added or removed, small pockets of unused space accumulate, making it challenging to
allocate large, contiguous spaces for new files. This can lead to inefficient use of storage resources and a
slowdown in performance.
2. Internal Fragmentation:
Occurs when a process is allocated more memory than it needs, leaving unused space within that
allocated block. This unused space is essentially wasted, even though it's part of the memory allocated
to the process. Internal fragmentation occurs within allocated storage blocks. When a file does not
perfectly fit into a given block, the remaining space is wasted. Over time, this can accumulate, leading to
a considerable amount of storage being underutilized.
Fixed partitioning is the simplest and the oldest technique that puts multiple processes in the main
memory. The total number of partitions (the non-overlapping ones) present in the RAM is fixed in this
type of partitioning, but the sizes of all the partitions might or might not be similar. The OS resides in the
first partition, and all the other partitions help in storing the user processes. The memory here is
assigned to all the processes in a contiguous manner. No spamming is allowed since it is a contiguous
allocation. In this case, partitions are supposed to be made during system configuration or before
execution.
This method allows multiple processes to execute simultaneously. Here memory is divided into fixed
sized partitions. Size can be equal or unequal for different partitions. Generally unequal partitions are
used for better utilizations. Each partition can accommodate exactly one process, means only single
process can be placed in one partition. The partition boundaries are not movable. Whenever any
program needs to be loaded in memory, a free partition big enough to hold the program is found. This
partition will be allocated to that program or process. If there is no free partition available of required
size, then the process needs to wait. Such process will be put in a queue.
The disadvantage of sorting the incoming jobs into separate queues becomes apparent when the queue
for a large partition is empty but the queue for a small partition is full, as is the case for partitions 1 and
3 in given Fig. Here small jobs have to wait to get into memory, even though plenty of memory is free.
An alternative organization is to maintain a single queue as in Fig. Whenever a partition becomes free,
the job closest to the front of the queue that fits in it could be loaded into the empty partition and run.
Internal Fragmentation: Main memory use is inefficient. Any program, no matter how small,
occupies an entire partition. This can cause internal fragmentation.
Limit process size: Process of size greater than the size of the partition in Main Memory
cannot be accommodated. The partition size cannot be varied according to the size of the
incoming process size. Hence, the process size of 32MB in the above-stated example is
invalid.
Limitation on Degree of Multiprogramming: Partitions in Main Memory are made before
execution or during system configure. Main Memory is divided into a fixed number of
partitions. Suppose if there are partitions in RAM and are the number of processes,
then n2<=n1 n2<=n1 condition must be fulfilled. Number of processes greater than
the number of partitions in RAM is invalid in Fixed Partitioning.
Variable partitioning is part of the contiguous allocation technique. It is used to alleviate the problem
faced by fixed partitioning. As opposed to fixed partitioning, in variable partitioning, partitions are not
created until a process executes. At the time it is read into main memory, the process is given exactly
the amount of memory needed. This technique, like the fixed partitioning scheme previously discussed
have been replaced by more complex and efficient techniques.
Initially RAM is empty and partitions are made during the run-time according to process’s need
instead of partitioning during system configure.
The partition size varies according to the need of the process so that the internal fragmentation
can be avoided to ensure efficient utilisation of RAM.
Number of partitions in RAM is not fixed and depends on the number of incoming process and
Main Memory’s size.
No Limitation on the Size of the Process: In Fixed partitioning, the process with a size greater
than the size of the largest partition could not be loaded and the process can not be divided as it
is invalid in the 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.
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.
External Fragmentation:
There will be external fragmentation inspite of absence of internal fragmentation.
The First Fit method works by searching through memory blocks from the beginning, looking for the first
block that can fit the process.
For any process Pn, the operating system looks through the available memory blocks.
It allocates the first block that is free and large enough to hold the process.
In simple terms, First Fit simply finds the first available block that can fit the process and assigns it there.
It's a straightforward and fast way to allocate memory.
1. It is straightforward to implement and easy to understand, making it ideal for systems with
limited computational power.
2. Memory can be allocated quickly when a suitable free block is found at the start.
3. When processes have similar memory sizes, First Fit can help minimize fragmentation by
utilizing the first available block that fits the process.
Despite its advantages, the First Fit algorithm has a few downsides:
1. Over time, First Fit can lead to both external fragmentation, where small free memory blocks
are scattered, and internal fragmentation, where allocated memory exceeds the process’s
requirement, wasting space.
2. It may not always allocate memory in the most efficient manner, leading to suboptimal use of
available memory.
3. For large processes, First Fit can be less efficient, as it may need to search through numerous
smaller blocks before finding an appropriate one, which can slow down memory allocation.
Memory Efficient. The operating system allocates the job minimum possible space in the
memory, making memory management very efficient.
To save memory from getting wasted, it is the best method.
Improved memory utilization
Reduced memory fragmentation
Minimizes external fragmentation
It is a Slow Process. Checking the whole memory for each job makes the working of the
operating system very slow. It takes a lot of time to complete the work.
Increased computational overhead
May lead to increased internal fragmentation
Can result in slow memory allocation times.
In Worst-Fit Memory Allocation allocation technique, the process traverses the whole memory and
always search for the largest hole/partition, and then the process is placed in that hole/partition. It is a
slow process because it has to traverse the entire memory to search the largest hole.
Reduces Chances of Small Fragments: By allocating the largest available block, worst fit leaves
larger leftover fragments, which are more likely to be useful for future allocations.
Simple to Implement: The logic is straightforward: find the largest block that can fit the process
and allocate it.
Efficient for Large Processes: It always looks for the biggest space, large processes may find
better fits compared to other strategies like Best Fit or First Fit.
Inefficient Memory Usage: Allocating the largest block to a process, even if it's much larger than
required, can lead to inefficient memory usage.
Lower Allocation Time: Worst-Fit requires the operating system to search for the largest
available block, which can be slower than methods like First-Fit or Next-Fit, especially if there
are many free blocks.
1. Swapping
When the process is to be executed, then that process is taken from secondary memory to stored in
RAM.But RAM have limited space so we have to take out and take in the process from RAM time to
time. This process is called swapping. The purpose is to make a free space for other processes. And later
on, that process is swapped back to the main memory.
The Round Robin algorithm is executing in which the quantum process is supposed to preempt
after running for some time. In that case, that process is swapped out, and the new process is
swapped in.
When there is a priority assigned to each process, the process with low priority is swapped out,
and the higher priority process is swapped in. After its execution, the lower priority process is
again swapped in, and this process is so fast that users will not know anything about it.
In the shortest time remaining first algorithm, when the next process(which arrives in the ready
queue) has less burst time, then the executing process is preempted.
When a process has to do I/O operations, then that process temporarily swapped out.
Swap-in: Swap-in means removing a program from the hard disk and putting it back in the RAM.
Swap-out: Swap-out means removing a program from the RAM and putting it into the hard disk.
2. Paging
Paging is the memory management technique in which secondary memory is divided into fixed-size
blocks called pages, and main memory is divided into fixed-size blocks called frames. The Frame has the
same size as that of a Page. The processes are initially in secondary memory, from where the processes
are shifted to main memory(RAM) when there is a requirement. Each process is mainly divided into
parts where the size of each part is the same as the page size. One page of a process is mainly stored in
one of the memory frames. Paging follows no contiguous memory allocation. That means pages in the
main memory can be stored at different locations in the memory.
3. Compaction
Compaction is a memory management technique in which the free space of a running system is
compacted, to reduce fragmentation problem and improve memory allocation efficiency. Compaction is
used by many modern operating systems, such as Windows, Linux, and Mac OS X. As in the fig we have
some used memory(black color) and some unused memory(white color).The used memory is
combined.All the empty spaces are combined together.This process is called compaction.This is done to
prevent to solve the problem of fragmentation, but it requires too much of CPU time.
By compacting memory, the operating system can reduce or eliminate fragmentation and make it easier
for programs to allocate and use memory.
Copying all pages that are not in use to one large contiguous area.
Then, write the pages that are in use into the newly freed space.
4. Segmentation
Segmentation is another memory management technique used by operating systems. The process is
divided into segments of different sizes and then put in the main memory. The program/process is
divided into modules, unlike paging, in which the process was divided into fixed-size pages or frames.
The corresponding segments are loaded into the main memory when the process is executed. Segments
contain the program’s utility functions, main function, and so on.
Paging is a technique that divides memory into fixed-sized blocks. The main memory is divided into
blocks known as Frames and the logical memory is divided into blocks known as Pages. Paging requires
extra time for the address conversion, so we use a special hardware cache memory known as TLB.
This concept of Paging in OS includes dividing each process in the form of pages of equal size and also,
the main memory is divided in the form of frames of fixed size. Now, each page of the process when
retrieved into the main memory, is stored in one frame of the memory, and hence, it is also important
to have the pages and frames of equal size for mapping and maximum utilization of the memory.
Its main advantage is that the pages can be stored at different locations of the memory and not
necessarily in a contiguous manner, though priority is always set to firstly find the contiguous frames for
allocating the pages.
If a process has n pages in the secondary memory then there must be n frames available in the main
memory for mapping.
As we can see in the above image, we have main memory divided into 16 frames of the size of 1KB each.
Also, there are 4 processes available in the secondary (local) memory: P1, P2, P3, and P4 of a size
of 4KB each. Clearly, each process needs to be further subdivided into pages of size of 1KB each, so that
one page can be easily mapped to one frame of the main memory. This divides each process
into 4 pages and the total for 4 processes gives 16 pages of 1KB each. Initially, all the frames were empty
and therefore, pages will be allocated here in a contiguous manner.
CASE-2 (Non-Contiguous Allocation of Pages)
Let us assume that in Case-1, processes P2 and P4 are moved to the waiting state after some time and
leave behind the empty space of 8 frames. In Case-2, we have another process P5 of size 8KB (8 pages)
waiting inside the ready queue to be allocated. We know that with Paging, we can store the pages at
different locations of the memory, and here, we have8non-contiguous frames available. Therefore, we
can easily load the 8 pages of the process P5 in the place of P2 and P4 which we can observe in the
above image.
MMU is a computer hardware component that responsible for all memory and caching operations which
are associated with the CPU. MMU usually exist in CP but in some cases it operates on separate
integrated circuit (IC) chip. Here's an overview of how the MMU, specifically in the context of paging,
functions in an operating system:
Virtual Memory Translation: The MMU translates virtual addresses generated by a process into
physical addresses in RAM (Random Access Memory). This translation allows processes to run as
if they have access to a large, continuous block of memory, even if the physical memory is
fragmented or limited.
Paging: Paging is a memory management scheme where physical memory is divided into fixed-
size blocks called "frames," and logical memory is divided into equally sized blocks called
"pages." The MMU maps pages to frames, enabling efficient allocation and management of
memory.
Page Tables: The MMU uses page tables to maintain the mapping between virtual pages and
physical frames. Each process has its own page table, which keeps track of the virtual-to-physical
address translations.
Page Faults: When a process accesses a page that is not in physical memory, a page fault occurs.
The MMU triggers an interrupt, and the operating system must load the required page from
secondary storage (usually a hard disk) into a free frame in RAM.
Page Replacement: If all frames in physical memory are in use, the operating system must
choose a page to evict. This process, known as page replacement, is often managed using
algorithms like the Least Recently Used (LRU) or the Second Chance algorithm.
Advantages of Paging in OS
It is a memory management technique as we can store the pages of a single process in a non-
contiguous manner as well which saves the memory.
The problem of external fragmentation is solved with the help of the Paging technique.
Allocating the pages within equal and fixed-size frames is easy and simple. Swapping is also easy
between the pages and the page frames.
Disadvantages of Paging in OS
Internal Fragmentation may occur especially during the allocation of the last page of the
process.
Page tables that are separate for each process in the secondary memory may consume extra
memory.
If we are not using TLB (Translation of look-aside buffer), then the time taken to fetch an
instruction and the element byte is high because we need to access the memory two times (for
page entry and for actual element within the frame).
Address translation of logical to physical address lengthens the memory cycle times. Also, it
needs specialized hardware.
Segmentation divides processes into smaller subparts known as modules. The divided segments
need not be placed in contiguous memory. Since there is no contiguous memory allocation, internal
fragmentation does not take place. The length of the segments of the program and memory is
decided by the purpose of the segment in the user program.
We can say that logical address space or the main memory is a collection of segments.
Types of Segmentation
2. Simple Segmentation: Simple Segmentation also divides the processes into n number of
segments but the segmentation is done all together at once. Simple segmentation takes place at
the run time of a program. Simple segmentation may scatter the segments into the memory
such that one segment of the process can be at a different location than the other(in a
noncontinuous manner).
Segmentation came into existence because of the problems in the paging technique. In the case of
the paging technique, a function or piece of code is divided into pages without considering that the
relative parts of code can also get divided. Hence, for the process in execution, the CPU must load more
than one page into the frames so that the complete related code is there for execution. Paging took
more pages for a process to be loaded into the main memory. Hence, segmentation was introduced in
which the code is divided into modules so that related code can be combined in one single block.
Other memory management techniques have also an important drawback - the actual view of physical
memory is separated from the user's view of physical memory. Segmentation helps in overcoming the
problem by dividing the user's program into segments according to the specific need.
Advantages of Segmentation in OS
Segmentation is near to the user's view of physical memory. Segmentation allows users to
partition the user programs into modules. These modules are nothing but the independent
codes of the current process.
The Segment size is specified by the user but in Paging, the hardware decides the page size.
Disadvantages of Segmentation in OS
During the swapping of processes the free memory space is broken into small pieces, which is a
major problem in the segmentation technique.
When a process is completed, it is removed from the main memory. After the execution of the
current process, the unevenly sized segments of the process are removed from the main
memory. Since the segments are of uneven length it creates unevenly sized holes in the main
memory. These holes in the main memory may remain unused due to their very small size.
Sr.
Paging Segmentation
No.
The address space of a process is broken The address space of a process is broken into
1
into blocks of fixed size called pages. blocks of different sizes.
The memory is divided into pages by the The segment size, virtual address, and actual
2
Operating System. address are calculated by the compiler.
6 The logical address gets split into page The logical address gets split into section offset
Sr.
Paging Segmentation
No.
To calculate the absolute address, the To calculate the full address, the processor
11
processor needs page number and offset. needs segment number and offset.
Page replacement is needed in the operating systems that use virtual memory using Demand Paging. As
we know in Demand paging, only a set of pages of a process is loaded into the memory. This is done so
that we can have more processes in the memory at the same time.
When a page that is residing in virtual memory is requested by a process for its execution, the Operating
System needs to decide which page will be replaced by this requested page. This process is known as
page replacement and is a vital component in virtual memory management.
The FIFO algorithm is the simplest of all the page replacement algorithms. In this, we maintain a queue
of all the pages that are in the memory currently. The oldest page in the memory is at the front end of
the queue and the most recent page is at the back or rear end of the queue.
Whenever a page fault occurs, the operating system looks at the front end of the queue to know the
page to be replaced by the newly requested page. It also adds this newly requested page at the rear end
and removes the oldest page from the front end of the queue.
Example: Consider the page reference string as 3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames. Let’s try to find
the number of page faults:
Initially, all of the slots are empty so page faults occur at 3,1,2.
Page faults = 3
Page faults = 3
When page 6 comes, it is not present and a page fault occurs. Since there are no empty slots,
we remove the front of the queue, i.e 3.
Page faults = 4
When page 5 comes, it is also not present, and hence a page fault occurs. The front of the queue
i.e. 1 is removed.
Page faults = 5
When page 1 comes, it is not found in memory and again a page fault occurs. The front of the
queue i.e. 2 is removed.
Page faults = 6
When page 3 comes, it is again not found in memory, a page fault occurs, and page 6 is
removed being on top of the queue
Belady's anomaly: Generally if we increase the number of frames in the memory, the number of page
faults should decrease due to obvious reasons. Belady’s anomaly refers to the phenomena where
increasing the number of frames in memory, increases the page faults as well.
Advantages
Poor performance
Doesn’t use the frequency of the last used time and just simply replaces the oldest page.
Suffers from Belady’s anomaly.