OS R-23
OS R-23
OPERATING SYSTEMS
(2024 – 2025)
The OS acts as the manager of the resources (such as CPU time, memory space, files
storage space, I/O devices) and allocates them to specific programs and users as necessary
for tasks.
Views of an OS:-
The operating system from two view points:
o User View
o System View
User view:-
o In user view the operating system is designed for only fulfilling the requirement of the
user.
o These systems are designed for the single-user experience.
o Some operating systems are designed to maximize resource utilization to assure that all available
CPUtime, memory, and I/O are used efficiently and no individual user takes more than his share.
o These are multi-user systems where terminals are connected to mainframe or
minicomputers.
o users share resources and may exchange information.
System view:-
o In this view OS is designed to be a resource allocator or resource manager.
o The program that is most intimately involved with the hardware.
o The operating system is a resource allocator.
o The following resources may be required to solve a problem:
o CPU time
o memory space
o file-storage space
o I/O devices
o etc.
o The operating system acts as the manager of these resources.
Each task is given some time to execute so that all the tasks work smoothly. Each user gets the time of CPU
as they use a single system. These systems are also known as Multitasking Systems. The task can be from a
single user or different users also. The time that each task gets to execute is called quantum. After this time
interval is over OS switches over to the next task.
These types of the operating system are 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 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.
• Soft Real-Time Systems: These OSs are for applications where for time-constraint is less strict
The Simple structured operating systems do not have a well-defined structure. These systems will be
simple, small and limited systems. Example: MS-DOS.
In MS-DOS, the interfaces and levels of functionality are not well separated.
In MS-DOS application programs are able to access the basic I/O routines. This causes the entire
systems to be crashed when user programs fail.
Advantages
Easy Development - In simple operation system, being very few interfaces, development is easy
especially when only limited functionalities are to be delivered.
Better Performance - Such a sytem, as have few layers and directly interects with hardware, can
provide a better performance as compared to other types of operating systems.
Monolith Structure
In monolith structured operating system, a central piece of code called kernel is responsible for all
major operations of an operating system.
Such operations include file management, memory management, device management and so on.
The kernel is the main component of an operating system and it provides all the services of an
operating system to the application programs and system programs.
The kernel has access to the all the resources and it acts as an interface with application programs
and the underlying hardware.
A monolithic kernel structure promotes timesharing, multiprogramming model and was used in
old banking systems.
Mr.B V CHANDRA SEKHAR CSE(R-23) Page 11
Advantages
Easy Development - As kernel is the only layer to develop with all major functionalities, it is easier
to design and develop.
Performance - As Kernel is responsible for memory management, other operations and have direct
access to the hardware, it performs better.
Disadvantages
Crash Prone - As Kernel is responsible for all functions, if one function fails entire operating system
fails.
Difficult to enhance - It is very difficult to add a new service without impacting other services of a
monolith operating system.
Micro-Kernel Structure
Modular Structure
Modular structure operating system works on the similar principle as a monolith but with better
design.
A central kernel is responsible for all major operations of operating system.
This kernel has set of core functionality and other services are loaded as modules dynamically to
the kernel at boot time or at runtime.
Advantages
High Customizable - Being modular, each module implementation can be customized easily. A new
functionality can be added without impacting other modules as well.
Verifiable - Being modular, each layer can be verified and debugged easily.
Disadvantages
Less Performance - A modular structured operating system is less performance as compared to
basic structured operating system.
Complex designing - Each module is to planned carefully as each module communicates with
kernal. A communication API is to be devised to facilitate the communication.
Views of an OS:-
There are two views related to an OS, i.e.
i) User view
ii) System view.
User Interfaces
Program execution
I/O operations
File System manipulation
Communication
Error Detection
Resource Allocation
Accounting
Protection & security
User Interfaces – Operating System is interface between user and computer .It Means
users can provide interface for accessing the computer.we have different type of
interfaces
In GUI systems - windows with pointing device to get inputs and keyboard
to enter the text
Program Execution - The OS must be able to load a program into RAM, run the
program, and terminate the program, either normally or abnormally.
I/O Operations - The OS is responsible for transferring data to and from I/O devices,
including keyboards, terminals, printers, and files. For specific devices, special
functions are provided (device drivers) by OS.
File-System Manipulation – Programs need to read and write files or directories. The
services required to create or delete files, search for a file, list the contents of a file and
change the file permissions are provided by OS.
Communications - Inter-process communications, IPC, either between processes
running on the same processor, or between processes running on separate processors or
Resource Allocation – Resources like CPU cycles, main memory, storage space, and
I/Odevices must be allocated to multiple users and multiple jobs at the same time.
Accounting – There are services in OS to keep track of system activity and resource
usage, either for billing purposes or for statistical record keeping that can be used to
optimize future performance.
Protection and Security – The owners of information (file) in multiuser or networked
computer system may want to control the use of that information. When several separate
processes execute concurrently, one process should not interfere with other or with OS.
Protection involves ensuring that all access to system resources is controlled. Security of
the system from outsiders must also be done, by means of a password.
An example to illustrate how system calls are used: writing a simple program to read data from
one file and copy them to another file.
There are number of system calls used to finish this task. The first system call is to
write a message on the screen (monitor). Then to accept the input filename. Then another
system call to write message on the screen, then to accept the output filename.
When the program tries to open the input file, it may find that there is no file of that
name or that the file is protected against access. In these cases, the program should print
a message on the console (another system call) and then terminate abnormally (another
system call) and create a new one (another system call).
Now that both the files are opened, we enter a loop that reads from the input file
Each system call has a specific numbered system call. The system call table (consisting of
system call number and address of the particular service) invokes a particular service
routine for a specific system call.
The caller need know nothing about how the system call is implemented or what it
doesduring execution.
Three general methods used to pass parameters to OS are –
To pass parameters in registers.
If parameters are large blocks, address of block (where parameters are stored in
memory) issent to OS in the register. (Linux & Solaris).
Parameters can be pushed onto the stack by program and popped off the stack by OS.
1. Process Control
These system calls are used for process management tasks such as creating, terminating, and controlling
processes. Process control refers to the management and manipulation of processes within an operating
system. System calls related to process control allow for the creation, termination, and manipulation of
processes, as well as providing mechanisms for process synchronization and communication.
Examples include fork(), exec(), wait(), and exit().
fork(): Creates a new process by duplicating the calling process. The new process, known as the child
process, has its own address space but shares the code and data of the parent process.
exec(): Replaces the current process with a new process loaded from an executable file. It loads a new
program into the current process's address space and starts its execution.
wait(): Suspends the calling process until one of its child processes terminates. It allows the parent
process to synchronize its execution with the termination of child processes.
exit(): Terminates the calling process and releases all associated resources. It also returns an exit status to
the parent process or the operating system.
2. File Management
File management in an operating system involves the creation, deletion, manipulation, and
organization of files and directories. System calls related to file management provide interfaces for
performing these operations, such as opening, closing, reading from, and writing to files, as well as
navigating directory structures.
System calls in this category are used for file-related operations such as creating, opening, reading,
writing, closing, and deleting files. Examples include open(), close(), read(), write(), and unlink().
3. Device Management
These system calls manage devices such as printers, disk drives, and network interfaces. They allow
processes to perform operations like reading from and writing to devices.
Examples include read(), write(), ioctl(), and open().
Information Maintenance Get Current Process ID(), Set Timer() getpid(), alarm()
System programs
System Programs are the collection of software required for program execution. They are also known as
system utilities. System programs can be simple, involving only system calls as well as complex ones.
System Programming can be defined as the act of building Systems Software using System Programming
Languages. According to Computer Hierarchy, Hardware comes first then is Operating System, System
Programs, and finally Application Programs.
It is quite complicated to define all the goals and specifications of the operating system while
designing it.
The design changes depending on the type of the operating system i.e if it is batch system, time
shared system, single user system, multi user system, distributed system etc.
There are basically two types of goals while designing an operating system. These are
User Goals
System Goals
User Goals
Design goals are the objectives of the operating system. They must be met to fulfill design
requirements and they can be used to evaluate the design.
The operating system should be convenient, easy to use, reliable, safe and fast according to the
users.
However, these specifications are not very useful as there is no set method to achieve these goals.
System Goals
The operating system should be easy to design, implement and maintain.
These are specifications required by those who create, maintain and operate the operating system.
It should be flexible, reliable and error free& efficient.
o The separation of policy from mechanism is a very important principle, it allows maximum
flexibility if policy decisions are to be changed later
o Policy may changes over time and this would be lead to change in mechanism
What is Booting?
Booting – starting a computer by loading the kernel (or) The operating system is loaded via
bootstrapping procedure, often known as booting.
When a computer system is started, there is a mechanism in the system that loads the operating
system from the secondary storage into the main memory, or RAM, of the system. This is
called the booting process of the system.
What happens in the Process of Booting?
Booting happens when you start the computer. This happens when we turned ON the power or
the computer restarts. The system BIOS (Basic Input/Output System) makes the peripheral devices
active. Further, it requires that the boot device loads the operating system into the main memory.
Boot Devices
Booting can be done either through hardware (pressing the start button) or by
giving software commands. Therefore, a boot device is a device that loads the operating system.
Examples are the hard drive, floppy disk drive, CD drive, etc. Among them, the hard drive is the
most used one.
Types of Booting
There are two types of booting:
Cold Booting
A cold boot is also called a hard boot. It is the process when we first start the computer. In other
words, when the computer is started from its initial state by pressing the power button it is called
cold boot. The instructions are read from the ROM and the operating system is loaded in the main
memory.
Processes Concept:
A process is a program under execution.
Its current activity is indicated by PC (Program Counter) and the contents of the
processor's registers.
The Process
Process memory is divided into four sections as shown in the figure below:
The stack is used to store temporary data such as local variables, functionparameters,
function return values, return address etc.
The heap which is memory that is dynamically allocated during process run time
The data section stores global variables.
The text section comprises the compiled program code.
Note that, there is a free space between the stack and the heap. When the stack is full, it
grows downwards and when the heap is full, it grows upwards.
Process State
A Process has 5 states. Each process may be in one of the following states –
1. New - The process is in the stage of being created.
2. Ready - The process has all the resources it needs to run. It is waiting to be assigned to
the processor.
Threads
In a process, a thread refers to a single sequential activity being executed. These activities are also known as
thread of execution or thread control. Now, any operating system process can execute a thread. We can say
that a process can have multiple threads.
Process Scheduling
The objective of multiprogramming is to have some process running at all times, so as to
maximize CPU utilization. Maintains scheduling queues of processes
There are 3 types of scheduling queues .
They are :
1. Job Queue
2. Ready Queue
3. Device Queue
As processes enter the system, they are put into a job queue.
The processes that are residing in main memory and are ready and waiting to
execute are kept on a list called the ready queue.
The list of processes waiting for an I/O device is kept in a device queue for
that particular device.
Processes migrate among the various queues
A common representation of process scheduling is a queueing diagram, such as that in Figure .Each
rectangular box represents a queue. Two types of queues are present: the ready queue and a set of
device queues. The circles represent the resources that serve the queues, and the arrows indicate the
flow of processes in the system.
A new process is initially put in the ready queue. It waits there until it is selected for
execution, or dispatched. Once the process is allocated the CPU and is executing, one of
several events could occur:
• The process could issue an I/O request and then be placed in an I/O queue.
• The process could create a new child process and wait for the child’s termination.
• The process could be removed forcibly from the CPU, as a result of an interrupt, and be
put back in the ready queue.
Schedulers
A process migrates between the various scheduling queues throughout its lifetime.
The operating system must select, for scheduling purposes, processes from these queues in
some fashion.
The selection process is carried out by the appropriate scheduler.
There are three different types of schedulers. They are:
1. Long-term Scheduler or Job Scheduler
2. Short-term Scheduler or CPU Scheduler
3. Medium term Scheduler
Long-term scheduler(or job scheduler) – selects which processes should be
brought into the ready queue
Short-term scheduler(or CPU scheduler) – selects which process should be
executed next and allocates CPU
Sometimes the only scheduler in a system
Short-term scheduler is invoked very frequently (milliseconds)=> (must be fast)
Long-term scheduler is invoked very infrequently (seconds, minutes)=>(may be
slow)
The main difference between these two schedulers is The long-term scheduler
executes much less frequently; minutes may separate the creation of one new process
and the next.
The long-term scheduler controls the degree of multiprogramming (the number of
processes in memory).
Processes can be described as either:
I/O-bound process– spends more time doing I/O than computations, many short
CPU bursts
CPU-bound process– spends more time doing computations; few very long CPU
bursts
Medium term Scheduler
Some operating systems, such as time-sharing systems, may introduce an additional,
intermediate level of scheduling.
The key idea is medium-term scheduler, removes processes from memoryand thus
reduces the degree of multiprogramming.
At some later time, the process can be reintroduced into memory and its execution can be
continued where it left off. This scheme is called swapping
Context Switch
Context switch = saving the state of the old process and switching the CPU to another process
The context of a process is represented in the PCB of a process
(It includes the value of the CPU registers, process state, and memory-management information)
Context-switch time is pure overhead, because the system does no useful work while switching
Context-switch time is highly dependent on hardware support (e.g. some processors
provide multiple sets of registers)
CPU Switch from Process to Process
Operations on Processes
The processes in most systems can execute concurrently, and they may be created and deleted
dynamically
1. Process Creation:
During the course of execution, a process may create several new processes.
As mentioned earlier, the creating process is called a parent process, and the new processes
are called the children of that process.
Each of these new processes may in turn create other processes, forming a tree of processes.
Most operating systems (including UNIX, Linux, and Windows) identify processes
according to a unique process identifier (or pid), which is typically an integer number.
The ‘pid’ provides a unique value for each process in the system, and it can be used as an
index to access various attributes of a process within the kernel.
UNIX example
fork system call creates new process
exec system call used after a fork to replace the process’ memory space with a new program
Process Termination
A process terminates when it finishes executing its last statement and asks the operating
system to delete it, by using the exit () system call. All of the resources assigned to the
process like memory, open files, and I/O buffers, are de-allocated by the operating
system.
A process can cause the termination of another process by using appropriate system call.
The parent process can terminate its child processes by knowing of the PID of the child.
A parent may terminate the execution of children for a variety of reasons, such as:
The child has exceeded its usage of the resources, it has been allocated.
The task assigned to the child is no longer required.
The parent is exiting, and the operating system terminates all the children. This
is called cascading termination.
A process that has terminated, but whose parent has not yet called wait(), is
known as a zombie process. All processes transition to this state when they
terminate, but generally they exist as zombies only briefly.
1) Shared-Memory Systems
A region of shared-memory is created within the address space of a process, which needs
to communicate. Other process that needs to communicate uses this shared memory.
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.
Message Passing
When two or more processes participate in inter-process communication, each process sends messages to the
others via Kernel. Here is an example of sending messages between two processes: – Here, the process sends a
message like “M” to the OS kernel. This message is then read by Process B. A communication link is required
between the two processes for successful message exchange. There are several ways to create these links.
If the process has multiple threads of control,it can do more than one task at a time
Benefits of multithreaded programming
Responsiveness
Resource Sharing
Economy
Scalability
TYPES OF THREADS:
User Level Threads − User managed threads. User threads are supported above the kernel
and are managed without kernel support i.e., they are implemented by thread library at the
user level.
Kernel Level Threads − Operating System managed threads acting on kernel, an operating
system core.
User Threads:
When a user thread performs a blocking system call, it will cause the entire process to block
even if other threads are available to run within the application.
Kernel Threads:
When a kernel thread performs a blocking system call ,the kernel schedules another thread in the
application for execution.
Examples
o Windows XP/2000
o Solaris
o Linux
o Tru64 UNIX
o Mac OS X
Multithreading Models:
Many-to-One
One-to-One
Many-to-Many
1. Many-to-One
Disadvantages:
The entire process will block if a thread makes a blocking system-call.
Multiple threads are unable to run in parallel on multiprocessors.
2. One-to-One:
3. Many-to-Many Model:
Many user-level threads are multiplexed to a smaller / equal number of kernel threads
When a thread performs a blocking system call, the kernel can schedule another thread for
execution
Advantages:
PThreads
Java Threads:
Windows XP Threads
Implements the one-to-one mapping
Each thread contains
o A thread id
o Register set
o Separate user and kernel stacks
o Private data storage area
Threading Issues:
Semantics of fork() and exec() system calls
Thread cancellation
Signal handling
Thread pools
Thread specific data
If a thread invoke exec() system call ,the program specified in the parameter to exec will replace
the entire process.
2. Thread Cancellation:
A thread that is to be cancelled is called a target thread. There are two types of cancellation
namely
2. Deferred Cancellation – The target thread can periodically check if it should terminate, and
does so in an orderly fashion.
3. Signal handling
CPU Scheduling:
Basic Concepts
CPU scheduling is the task of selecting a waiting process from the ready queue and
allocating the CPU to it
o The CPU is allocated to the selected process by the dispatcher
In a uni-processor system, only one process may run at a time; any other process must wait
until the CPU is rescheduled
The objective of multiprogramming is to have some process running at all times, in order to
maximize CPU utilization
CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O
wait
CPU burst distribution
In choosing which algorithm to use in a particular situation, we must consider the properties of the various
algorithms. Many criteria have been suggested for comparing CPU scheduling algorithms.
Which characteristics are used for comparison can make a substantial difference in which algorithm is
judged to be best. The criteria include the following:
• CPU utilization. We want to keep the CPU as busy as possible. Conceptually, CPU utilization can range
from 0 to 100 percent. In a real system, it should range from 40 percent (for a lightly loaded system) to 90
percent (for a heavily used system).
• Throughput. The number of processes completed per time unit
• Turnaround time.
The time interval from process submission to completion
Formula: Time of completion – Time of submission
Formula: CPU burst time + Waiting time (includes I/O)
• Waiting time.
The sum of the periods spent waiting in the ready queue
Formula: Turnaround time – CPU burst time
• Response time. The amount of time it takes to start responding, but not the time it takes to output that
response
We want to maximize CPU utilization, and minimize turnaround, waiting & response time
Scheduling Algorithms
Preemptive Scheduling:
Preemptive scheduling is used when a process switches from running state to ready state or
from waiting state to ready state.
There sources (mainly CPU cycles)are allocated to the process for the limited amount
oftimeandthenistakenaway,andtheprocessisagainplacedbackinthereadyqueue if that process
still has CPU burst time remaining.
That process stays in ready queue till it gets next chance to execute.
Non-Preemptive Scheduling:
Basis for
Preemptive Scheduling Non Preemptive Scheduling
Comparison
Once resources are allocated to a
There sources are allocated to a process, the process holds it till it
Basic
process for a limited time. completes its burst time or switches to
waiting state.
Process can be interrupted in Process can not be interrupted till it
Interrupt
between. terminates or switches to waiting state.
If a high priority process
If a process with long burst time is
frequently arrives in the ready
Starvation running CPU ,then an other process with
queue ,low priority process may
less CPU burst time may starve.
starve.
Preemptive scheduling has
Non-preemptive scheduling does not
Overhead overhead so f scheduling the
have overheads.
processes.
Flexibility Preemptive scheduling is flexible. Non-preemptive scheduling is rigid.
Preemptive scheduling is cost Non-preemptive scheduling is not cost
Cost
associated. associative.
OperatingSystemHandout
Unit II–CPUSchedulingandAlgorithmSection
Scheduling types
SchedulingObjectives
BeFairwhileallocatingresourcestothe processes
Maximizethroughputof the system
Maximizenumberofusers receivingacceptableresponse times.
Bepredictable
Balanceresourceuse
Avoidindefinitepostponement
EnforcePriorities
Givepreferenceto processes holdingkeyresources
Givebetterservicetoprocesses thathavedesirablebehaviour patterns
CPUandI/OBurst Cycle:
Processexecutionconsistsofacycleof CPUexecutionandI/Owait.
Processesalternatebetweenthesetwostates.
ProcessexecutionbeginswithaCPUburst,followedbyanI/Oburst,thenanotherCPU burst
... etc
The last CPU burst will end with a system request to terminate execution rather than
with another I/O burst.
Theduration oftheseCPU bursthavebeen measured.
An I/O-bound program would typically have many short CPU bursts, A CPU-bound
program might have a few very long CPU bursts.
Thiscanhelptoselect anappropriateCPU-schedulingalgorithm.
Page1of 16
OperatingSystemHandout
PreemptiveScheduling:
Preemptive scheduling is used when a process switches from running state to ready
state or from waiting state to ready state.
Theresources (mainlyCPUcycles)areallocated to theprocess forthelimitedamount
oftimeandthenistakenaway,andtheprocessisagainplacedbackinthereadyqueue if that
process still has CPU burst time remaining.
Thatprocess staysinreadyqueuetillit getsnext chanceto execute.
Non-PreemptiveScheduling:
Non-preemptive Scheduling is used when a process terminates, or a process switches
from running to waiting state.
Inthisscheduling,oncetheresources(CPUcycles)isallocatedtoaprocess,theprocess holds
the CPU till it gets terminated or it reaches a waiting state.
Incaseofnon-preemptiveschedulingdoesnotinterruptaprocessrunningCPUin middle of
the execution.
Instead,itwaitstilltheprocesscompleteitsCPUbursttimeandthenitcanallocatethe CPU to
another process.
Basis for
PreemptiveScheduling NonPreemptiveScheduling
Comparison
Once resources are allocated to a
Theresourcesareallocatedtoa process, the process holds it till it
Basic
process for a limited time. completesitsbursttimeorswitchesto
waiting state.
Processcanbeinterruptedin Process can not be interrupted till it
Interrupt
between. terminatesorswitchestowaitingstate.
If a high priority process
If a process with long burst time is
frequently arrives in the ready
Starvation runningCPU,thenanotherprocesswith
queue,lowpriorityprocessmay
less CPU burst time may starve.
starve.
Preemptive scheduling has
Non-preemptiveschedulingdoesnot have
Overhead overheadsofschedulingthe
overheads.
processes.
Flexibility Preemptiveschedulingisflexible. Non-preemptiveschedulingis rigid.
Preemptiveschedulingiscost Non-preemptiveschedulingisnotcost
Cost
associated. associative.
SchedulingCriteria
There are several different criteria to consider when trying to select the "best"
scheduling algorithm for a particular situation and environment, including:
o CPU utilization - Ideally the CPU would be busy 100% of the time, so
astowaste0CPUcycles.OnarealsystemCPUusageshouldrangefrom 40% (
lightly loaded ) to 90% ( heavily loaded. )
o Throughput- Number of processescompletedperunittime. Mayrange
from 10 / second to 1 / hour depending on the specific processes.
Page2of 16
OperatingSystemHandout
Inbrief:
ArrivalTime:Timeatwhichtheprocessarrivesinthereadyqueue.
CompletionTime:Timeatwhichprocesscompletesitsexecution.
BurstTime:TimerequiredbyaprocessforCPUexecution.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 turnaround time and burst time.
Waiting Time = Turn Around Time – Burst Time
4.2TypesofSchedulingAlgorithm
(a) FirstComeFirstServe(FCFS)
InFCFSScheduling
Theprocesswhicharrivesfirstinthereadyqueueisfirstlyassignedthe CPU.
Incaseofatie,processwithsmallerprocessidisexecutedfirst.
Itisalwaysnon-preemptiveinnature.
Jobsareexecutedon firstcome, firstservebasis.
Itisanon-preemptive,pre-emptiveschedulingalgorithm.
Easyto understand and implement.
ItsimplementationisbasedonFIFOqueue.
Poorinperformanceasaverage waittimeishigh.
Advantages-
Itissimpleandeasyto understand.
Itcanbeeasilyimplementedusingqueuedatastructure.
Itdoesnotleadtostarvation.
Disadvantages-
Itdoesnotconsiderthepriorityorbursttimeoftheprocesses.
Itsuffersfromconvoyeffecti.e.processeswithhigherbursttimearrivedbefore the
processes with smaller burst time.
Page3of 16
OperatingSystemHandout
Example1:
Example2:
Consider the processes P1, P2, P3 given in the below table, arrives for execution in
the same order, with Arrival Time 0, and given Burst Time,
PROCESS ARRIVALTIME BURSTTIME
P1 0 24
P2 0 3
P3 0 3
Ganttchart
P1 P2 P3
0 24 27 30
Page4of 16
OperatingSystemHandout
Around Time: 24 + 27 + 30 = 81 ms
Ganttchart
P1 P2 P3 P4
0 8 12 21 26
Around Time: 8 + 11 + 19 + 23 = 61 ms
AverageTurnAroundtime=(TotalTurnAroundTime)/(Totalnumberofprocesses) 61/4 =
15.25 ms
Page5of 16
OperatingSystemHandout
(b) ShortestJobFirst(SJF)
Processwhichhavetheshortestbursttimearescheduledfirst.
Iftwoprocesseshavethesamebusttime,thenFCFSisusedtobreakthetie.
Thisisanon-pre-emptive,pre-emptiveschedulingalgorithm.
Bestapproachtominimizewaiting time.
Easyto implementin Batchsystems where required CPUtimeis known inadvance.
ImpossibletoimplementininteractivesystemswhererequiredCPUtimeisnot
known.
Theprocessershouldknow inadvancehowmuchtimeprocess will take.
Pre-emptive mode of Shortest Job First is called as Shortest Remaining
TimeFirst (SRTF).
Advantages-
SRTFisoptimalandguaranteestheminimumaveragewaitingtime.
Itprovidesastandardforotheralgorithmssincenootheralgorithmperforms better
than it.
Disadvantages-
Itcannotbeimplementedpracticallysincebursttimeoftheprocessescannot be
known in advance.
Itleadstostarvationforprocesseswithlargerbursttime.
Prioritiescannotbesetfortheprocesses.
Processeswithlargerbursttimehavepoorresponsetime.
Example-01:
Considerthesetof5processeswhosearrivaltimeandbursttimearegivenbelow-
ProcessId Arrivaltime Bursttime
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
Solution-
IftheCPUschedulingpolicyisSJFnon-preemptive,calculatetheaveragewaiting time
and average turnaround time.
GanttChart-
Now,weknow-
TurnAroundtime=Exittime–Arrivaltime
Waitingtime=TurnAroundtime–Bursttime
Page6of 16
OperatingSystemHandout
Example-02:
Considerthesetof5processeswhosearrivaltimeandbursttimearegivenbelow-
ProcessId Arrivaltime Bursttime
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
IftheCPUschedulingpolicyisSJFpre-emptive,calculatetheaveragewaitingtimeand
average turnaround time.
Solution-
GanttChart-
Now,
Page7of 16
OperatingSystemHandout
Example-03:
Considerthesetof6processeswhosearrivaltimeandbursttimearegivenbelow-
Now,weknow-
TurnAroundtime=Exittime–Arrivaltime
Waitingtime=TurnAroundtime–Bursttime
Now,
AverageTurnAroundtime=(19+12+4+1+5+2)/6=43/6=7.17unit
Averagewaitingtime=(12+7+1+0+3+ 1)/6=24/6=4unit
Page8of 16
OperatingSystemHandout
Example-04:
Considerthesetof3processeswhosearrivaltimeandbursttimearegivenbelow-
IftheCPUschedulingpolicyisSRTF,calculatetheaveragewaitingtimeandaverage turn
around time.
Solution-
GanttChart-
Now,weknow-
TurnAroundtime=Exittime–Arrivaltime
Waitingtime=TurnAroundtime–Bursttime
Now,
AverageTurnAroundtime=(13+4+20)/3=37/3 =12.33 unit
Averagewaitingtime=(4+0+11)/3=15/3=5unit
Example-05:
Considerthesetof4processeswhosearrivaltimeandbursttimearegivenbelow-
Page9of 16
OperatingSystemHandout
IftheCPUschedulingpolicyisSRTF,calculatethewaitingtimeofprocessP2.
Solution-
GanttChart-
Now,weknow-
TurnAroundtime=Exittime–Arrivaltime
Waitingtime=TurnAroundtime–Bursttime
Thus,
TurnAroundTimeofprocessP2=55–15=40 unit
WaitingtimeofprocessP2=40–25=15 unit
(c) RoundRobinScheduling
CPUisassignedtotheprocessonthebasisofFCFSforafixedamountoftime.
Thisfixedamountoftimeiscalledastimequantumortimeslice.
Afterthetimequantumexpires,therunningprocessispreemptedandsenttothe ready
queue.
Then,theprocessorisassignedtothenextarrivedprocess.
Itisalwayspreemptiveinnature.
Page10of 16
OperatingSystemHandout
Advantages-
Itgivesthebestperformanceintermsofaverageresponsetime.
Itisbestsuitedfortimesharingsystem,clientserverarchitectureand
interactive system.
Disadvantages-
Itleadstostarvationforprocesseswithlargerbursttimeastheyhavetorepeat the
cycle many times.
Itsperformanceheavilydependsontimequantum.
Prioritiescannotbesetfortheprocesses.
Withdecreasingvalueoftimequantum,
Numberofcontextswitchincreases
Responsetimedecreases
Chancesofstarvationdecreases
Thus,smallervalueoftimequantumisbetterintermsofresponsetime.
Withincreasingvalueoftimequantum,
Numberofcontextswitchdecreases
Responsetimeincreases
Chancesofstarvationincreases
Thus,highervalueoftimequantumisbetterintermsofnumberofcontextswitch.
Withincreasingvalueoftimequantum,RoundRobinSchedulingtendsto
become FCFS Scheduling.
Whentimequantumtendstoinfinity,RoundRobinSchedulingbecomesFCFS
Scheduling.
TheperformanceofRoundRobinschedulingheavilydependsonthevalueof time
quantum.
Thevalueoftimequantumshouldbesuchthatitisneithertoobignortoo small.
Example-01:
Considerthesetof5processeswhosearrivaltimeandbursttimearegivenbelow-
Page11of 16
OperatingSystemHandout
IftheCPUschedulingpolicyisRoundRobinwithtimequantum=2unit,calculate the
average waiting time and average turnaround time.
Solution-
ReadyQueue- P5,P1,P2,P5,P4,P1,P3,P2,P1
GanttChart-
Now,weknow-
TurnAroundtime=Exittime –Arrival time
Waitingtime =Turn Around time – Burst time
ProcessId Exit time TurnAroundtime Waiting time
P1 13 13 – 0 =13 13 – 5 =8
P2 12 12 – 1 =11 11 – 3 =8
P3 5 5 – 2 =3 3 – 1 =2
P4 9 9 – 3 =6 6 – 2 =4
P5 14 14 – 4 =10 10 – 3 =7
Now,
AverageTurn Around time =(13 +11+3 +6+10) / 5 =43 / 5 = 8.6 unit
Averagewaitingtime =(8 +8 +2 +4+7) / 5 =29 / 5 = 5.8unit
Problem-02:
Considerthesetof6processeswhosearrivaltimeandbursttimearegivenbelow-
ProcessId Arrivaltime Bursttime
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
IftheCPUschedulingpolicyisRoundRobinwithtimequantum=2, calculatetheaverage waiting
time and average turnaround time.
Solution-
Ready Queue- P5,P6,P2,P5, P6,P2,P5,P4, P1,P3,P2, P1
Ganttchart-
Now,weknow-
TurnAroundtime=Exittime –Arrival time
Waitingtime =Turn Around time – Burst time
Page12of 16
OperatingSystemHandout
Now,weknow-
TurnAroundtime=Exittime –Arrival time
Waitingtime =Turn Around time – Burst time
ProcessId Exit time TurnAroundtime Waiting time
P1 32 32 – 5 =27 27 – 5 =22
P2 27 27 – 4 =23 23 – 6 =17
P3 33 33 – 3 =30 30 – 7 =23
P4 30 30 – 1 =29 29 – 9 =20
P5 6 6 – 2 =4 4 – 2 =2
P6 21 21 – 6 =15 15 – 3 =12
Page13of 16
OperatingSystemHandout
Now,
(d) PriorityScheduling
Outofalltheavailableprocesses,CPUisassignedtotheprocesshavingthe highest
priority.
Incaseofatie,itis brokenbyFCFSScheduling.
PrioritySchedulingcanbeusedinbothpreemptiveandnon-preemptivemode.
Thewaitingtimefortheprocesshavingthehighestprioritywillalwaysbezeroin
preemptive mode.
Thewaitingtimefortheprocesshavingthehighestprioritymaynotbezeroinnon-
preemptive mode.
Priorityschedulinginpreemptiveandnon-preemptivemodebehavesexactlysameunder following
conditions-
Thearrivaltimeofalltheprocessesissame
Alltheprocessesbecome available
Advantages-
Itconsidersthepriorityoftheprocessesandallowstheimportantprocessesto run
first.
Priorityschedulinginpre-emptivemodeisbestsuitedforrealtimeoperating
system.
Disadvantages-
ProcesseswithlesserprioritymaystarveforCPU.
Thereisnoideaofresponsetimeandwaitingtime.
Problem-01:
Considerthesetof5processeswhosearrivaltimeandbursttimearegivenbelow-
ProcessId Arrivaltime Bursttime Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority)
Page14of 16
OperatingSystemHandout
Solution-
GanttChart-
Now,weknow-
TurnAroundtime=Exittime –Arrival time
Waitingtime =Turn Around time – Burst time
ProcessId Exit time TurnAroundtime Waiting time
P1 4 4 – 0 =4 4 – 4 =0
P2 15 15 – 1 =14 14 – 3 =11
P3 12 12 – 2 =10 10 – 1 =9
P4 9 9 – 3 =6 6 – 5 =1
P5 11 11 – 4 =7 7 – 2 =5
Now,
AverageTurn Aroundtime =(4 +14+10 +6+7) / 5 =41 / 5 = 8.2 unit
Averagewaitingtime =(0 +11 +9 +1+5) / 5 = 26 / 5 = 5.2 unit
Problem-02:Considerthesetof5processeswhosearrivaltimeandbursttimeare given
below-
ProcessId Arrivaltime Bursttime Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
IftheCPUschedulingpolicyisprioritypreemptive,calculatetheaveragewaiting time
and average turn around time. (Higher number represents higher priority).
Solution-
GanttChart-
Now,weknow-
TurnAroundtime=Exittime –Arrival time
Waitingtime =Turn Around time – Burst time
ProcessId Exit time TurnAroundtime Waiting time
P1 15 15 – 0 =15 15 – 4 =11
P2 12 12 – 1 =11 11 – 3 =8
P3 3 3 – 2 =1 1 – 1 =0
P4 8 8 – 3 =5 5 – 5 =0
P5 10 10 – 4 =6 6 – 2 =4
Page15of 16
OperatingSystemHandout
Now,
AverageTurn Aroundtime =(15 +11+1 +5+6) / 5 =38 / 5 = 7.6 unit
Averagewaitingtime =(11 +8 +0 +0+4) / 5 = 23 / 5 = 4.6 unit
(d) MultilevelQueueScheduling
A multi-level queue scheduling algorithm partitions the ready queue into several separate
queues. The processes are permanently assigned to one queue, generally based on some
propertyoftheprocess,suchasmemorysize,processpriority,orprocesstype.Eachqueuehas its own
scheduling algorithm.
Letus consideranexampleofamultilevelqueue-schedulingalgorithmwithfivequeues:
1. System Processes
2. InteractiveProcesses
3. InteractiveEditingProcesses
4. BatchProcesses
5. Student Processes
Each queue has absolute priority over lower-priority queues. No process in the batch queue,
for example, could run unless the queues for system processes, interactive processes, and
interactiveeditingprocesseswereallempty.Ifaninteractiveeditingprocessenteredtheready queue
while a batch process was running, the batch process will be pre-empted.
Page16of 16
UNIT – III
Synchronization Tools: The Critical Section Problem, Peterson’s Solution, Mutex Locks, Semaphores,
Monitors, Classic problems of Synchronization. Deadlocks: system Model, Deadlock characterization,
Methods for handling Deadlocks, Deadlock prevention, Deadlock avoidance, Deadlock detection,
Recovery from Deadlock.
Introduction:
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.
Race Condition
A race condition is an undesirable situation that occurs when a device or system attempts to perform
two or more operations at the same time
Critical Section
The regions of a program that try to access shared resources and may cause race conditions are called
critical section. To avoid race condition among the processes, we need to assure that only one process
at a time can execute within the critical section.
Each process must request permission to enter its critical section. The section of code
implementing this request is the entry section.
CSE-II-II(R-23) Page 1
The critical section may be followed by an exit section. The remaining code is the reminder
section.
A solution to the critical-section problem must satisfy the following three requirements:
1. Mutual exclusion: If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.
2. Progress: If no process is executing in its critical section and some processes wish to enter their
critical sections, then only those processes that are not executing in their remainder sections can
participate in deciding which will enter its critical section next, and this selection cannot be postponed
indefinitely.
3. Bounded waiting: There exists a bound, or limit, 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
Peterson's solution is restricted to two processes that alternate execution between their critical sections
and remainder sections. The processes are numbered Po and P1 or Pi and Pj where j = 1-i Peterson's
solution requires the two processes to share two data items:
int turn;
boolean flag[2];
CSE-II-II(R-23) Page 2
turn: The variable turn indicates whose turn it is to enter its critical section. Ex: if turn == i, then process
Pi is allowed to execute in its critical section
flag: Initialized to FALSE, initially no one is interested in entering the critical section .The flag array is
used to indicate if a process is ready to enter its critical section. Ex: if flag [i] is true, this value indicates
that Pi is ready to enter its critical section.
1. To enter the critical section, process Pi first sets flag [i] to be true and then sets turn to the value j,
thereby asserting that if the other process wishes to enter the critical section, it can do so.
2. If both processes try to enter at the same time, turn will be set to both i and j at roughly the same
time. Only one of these assignments will last, the other will occur but will be over written immediately.
3. The eventual value of turn determines which of the two processes is allowed to enter its critical
section first.
Mutual Exclusion is assured as only one process can access the critical section at any time.
Progress is also assured, as a process outside the critical section does not block other processes
from entering the critical section.
Bounded Waiting is preserved as every process gets a fair chance.
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.)
Itislimitedto2processes.
Peterson’s solution cannot be used in modern CPU architectures.
CSE-II-II(R-23) Page 3
SYNCHRONIZATION HARDWARE
Both these instructions are atomic instructions which mean that when a process is executing
any of these instructions it cannot be preempted until the instruction is complete.
TestAndSet() and Swap() instruction Instructions:
Many modern computer systems provide special hardware instructions that allow to test and
modify the content of a word or to swap the contents of two words atomically, that is, as one
uninterruptible unit. Two such solutions are:
1. TestAndSet() instruction
2. Swap() instruction
Special instructions such as test_and_set () and swap() instructions are used to solve the critical-
section problem.
The test_and_set () instruction can be defined as shown in Figure. The important characteristic
of this instruction is that it is executed atomically
TestAndSet() and Swap() instruction Instructions: TestAndSet() instruction uses a boolean variable lock.
The initial value of the lock is false. The variable lock ensures mutual exclusion. If the value of lock is
false, this means that no process is in its critical section. Hence, the value true means that some process
is running in its critical section.
TestAndSet Instruction
Definition:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
CSE-II-II(R-23) Page 4
}
Solution using TestAndSet
Shared boolean variable lock., initialized to false.
Solution:
do {
while ( TestAndSet (&lock ))
; // do nothing
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Swap Instruction
Definition:
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
Solution using Swap
Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key
Solution:
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Swap() it is executed atomically. If the machine supports the Swap() instruction, then mutual
exclusion can be provided as follows.
A global Boolean variable lock is declared and is initialized to false. In addition, each process has
a local Boolean variable key. The structure of process Pi is shown in below.
SEMAPHORE
A semaphore is a synchronization tool is used solve various synchronization problem and can be
implemented efficiently.
A semaphore is a signaling mechanism and a thread that is waiting on a semaphore can be
signaled by another thread.
CSE-II-II(R-23) Page 5
This is different than a mutex as the mutex can be signaled only by the thread that is called the
wait function.
Semaphores do not require busy waiting.
A semaphore S is an integer variable that is accessed only through two standard atomic
operations: wait () and signal (). The wait () operation was originally termed P and signal() was
called V.
Definition of wait():
wait(S){
whileS<=0;//busywait
S--;
}
Definition of signal():
signal(S){
S++;}
All modifications to the integer value of the semaphore in the wait () and signal()
operations must be executed indivisibly. That is, when one process modifies the
semaphore value, no other process can simultaneously modify that same
semaphore value.
PROPERTIES OF SEMAPHORES
It's simple and always have a non-negative Integer value.
Works with many processes.
Can have many different critical sections with different semaphores.
Each critical section has unique access semaphores.
TYPES OF SEMAPHORES
1. BINARY SEMAPHORE:
The value of a binary semaphore can range only between 0 and 1.
Binary semaphores are known as mutex locks, as they are locks that provide mutual exclusion.
Binary semaphores to deal with the critical-section problem for multiple processes.
Then processes share a semaphore, mutex, initialized to 1.
Each process Pi is organized as shown in below figure
do {
wait(mutex);
//CriticalSectionsignal
(mutex);
//remaindersection
CSE-II-II(R-23) Page 6
}while (TRUE);
2. Counting semaphore
CSE-II-II(R-23) Page 7
Monitor
Monitors in Process Synchronization:
The monitor is one of the ways to achieve Process synchronization.
The monitor is supported by programming languages to achieve mutual exclusion between
processes.
For example Java Synchronized methods. Java provides wait() and notify() constructs.
1. It is the collection of condition variables and procedures combined together in a special kind
of module or a package.
2. The processes running outside the monitor can’t access the internal variable of the monitor
but can call procedures of the monitor.
3. Only one process at a time can execute code inside monitors.
Syntax:
Condition Variables:
Two different operations are performed on the condition variables of the monitor.
1. Wait.
2. signal.
Let say we have 2 condition variables
condition x, y; // Declaring variable
Wait operation x.wait() : Process performing wait operation on any condition variable are suspended.
The suspended processes are placed in block queue of that condition variable.
Note: Each condition variable has its unique block queue.
Signal operation x.signal(): When a process performs signal operation on condition variable, one of the
blocked processes is given chance.
If (x block queue empty)
// Ignore signal
else
CSE-II-II(R-23) Page 8
// Resume a process from block queue.
Problem Statement:
CSE-II-II(R-23) Page 9
There is a buffer of n slots and each slot is capable of storing one unit of data. There are two processes
running, namely, producer and consumer, which are operating on the buffer.
A producer tries to insert data into an empty slot of the buffer. A consumer tries to remove data from a
filled slot in the buffer. As you might have guessed by now, those two processes won’t produce the
expected output if they are being executed concurrently.
There needs to be a way to make the producer and consumer work in an independent manner.
Solution:
One solution of this problem is to use semaphores. The semaphores which will be used here are:
At any instant, the current value of empty represents the number of empty slots in the buffer and full
represents the number of occupied slots in the buffer.
Producer Operation:
The pseudocode of the producer function looks like this:
do {
wait(empty);//waituntilempty>0andthendecrement ‘empty’
wait(mutex);//acquirelock
/* performthe
insert operation in a slot */
signal(mutex); // release lock
signal(full);//increment‘full’
} while(TRUE)
CSE-II-II(R-23) Page 10
Looking at the above code for a producer, we can see that a producer first waits until there is
atleast one empty slot.
Then it decrements the empty semaphore because, there will now be one less empty slot, since
the producer is going to insert data in one of those slots.
Then, it acquires lock on the buffer, so that the consumer cannot access the buffer until
producer completes its operation.
After performing the insert operation, the lock is released and the value of full is incremented
because the producer has just filled a slot in the buffer.
Consumer Operation:
The pseudocode of the consumer function looks like this:
do {
wait(full); // wait until full>0 and then decrement ‘full’
wait(mutex); // acquire the lock
/* perform the remove operation
in a slot */
signal(mutex); // release the lock
signal(empty); // increment ‘empty’
} while(TRUE);
The consumer waits until there is atleast one full slot in the buffer.
Then it decrements the full semaphore because the number of occupied slots will be decreased
by one, after the consumer completes its operation.
After that, the consumer acquires lock on the buffer.
Following that, the consumer completes the removal operation so that the data from one of the
full slots is removed.
Then, the consumer releases the lock.
Finally, the empty semaphore is incremented by 1, because the consumer has just removed data
from an occupied slot, thus making it empty.
2. READERS WRITER PROBLEM:
Readers writer problem is another example of a classic synchronization problem. There are many
variants of this problem, one of which is examined below.
A data set is shared among a number of concurrent processes
Readers – only read the data set; they do not perform any updates
Writers – can both read and write.
Problem – allow multiple readers to read at the same time. Only one single writer can access the
shared data at the same time.
The Problem Statement
There is a shared resource which should be accessed by multiple processes. There are two types of
processes in this context. They are reader and writer. Any number of readers can read from the shared
resource simultaneously, but only one writer can write to the shared resource. When a writer is writing
data to the resource, no other process can access the resource. A writer cannot write to the resource if
there are non zero number of readers accessing the resource at that time.
The Solution
From the above problem statement, it is evident that readers have higher priority than writer. If
a writer wants to write to the resource, it must wait until there are no readers currently
accessing that resource.
CSE-II-II(R-23) Page 11
Here, we use one mutex m and a semaphore w. An integer variable read_count is used to
maintain the number of readers currently accessing the resource.
The variable read_count is initialized to 0. A value of 1 is given initially to m and w.
Instead of having the process to acquire lock on the shared resource, we use the mutex m to
make the process to acquire and release lock whenever it is updating the read_count variable
The code for the writer process looks like this:
while(TRUE)
{
wait(w);
/* perform the write operation */
signal(w);
}
And, the code for the reader process looks like this:
while(TRUE)
{
//acquire lock
wait(m);
read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
/* perform the reading operation */
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
}
Explanation:
As seen above in the code for the writer, the writer just waits on the w semaphore until it gets a
chance to write to the resource.
After performing the write operation, it increments w so that the next writer can access the
resource.
On the other hand, in the code for the reader, the lock is acquired whenever the read_count is
updated by a process.
When a reader wants to access the resource, first it increments the read_count value, then
accesses the resource and then decrements the read_count value.
The semaphore w is used by the first reader which enters the critical section and the last reader
which exits the critical section.
The reason for this is, when the first readers enters the critical section, the writer is blocked
from the resource. Only new readers can access the resource now.
Similarly, when the last reader exits the critical section, it signals the writer using the w
semaphore because there are zero readers now and a writer can have the chance to access the
resource.
CSE-II-II(R-23) Page 12
3. DINING PHILOSOPHERS PROBLEM (DPP)
The dining philosopher's problem is the classical problem of synchronization
Which says that Five philosophers are sitting around a circular table and their job is to think and
eat alternatively.
There is a bowl of rice for each of the philosophers and 5 chopsticks.
A philosopher needs both their right and left chopstick to eat.
A hungry philosopher may only eat if there are both chopsticks available. Otherwise a
philosopher puts down their chopstick and begins thinking again.
CSE-II-II(R-23) Page 13
Allow a philosopher to pickup her chopsticks only if both chopsticks are available.
Use an asymmetric solution—that is, an odd-numbered philosopher picks up first her left
chopstick and then her right chopstick, whereas an even numbered philosopher picks up her
right chopstick and then her left chopstick.
CSE-II-II(R-23) Page 14
Deadlocks
Deadlocks: system Model
INTRODUCTION TO DEADLOCK:-
Def:-Deadlocks are a set of blocked processes each holding a resource and waiting to acquire a resource
held by another process.
Or
A Deadlock is a situation where each of the computer process waits for a resource which is being
assigned to some another process. In this situation, none of the process gets executed since the
resource it needs, is held by some other process which is also waiting for some other resource to be
released.
Or
When processes request a resource and if the resources are not available at that time the process enters
into waiting state. Waiting process may not change its state because the resources they are requested
are held by other process. This situation is called deadlock. The situation where the process waiting for
the resource i.e., not available is called deadlock.
Bridge Crossing Example
CSE-II-II(R-23) Page 15
A process may utilize the resources in only the following sequences:
Request:-If the request is not granted immediately then the requesting process must wait it can acquire
the resources.
Use:-The process can operate on the resource.
Release:-The process releases the resource after using it.
Let us assume that there are three processes P1, P2 and P3. There are three different resources R1, R2
and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned to P3.
After some time, P1 demands for R2 which is being used by P2. P1 halts its execution since it can't
complete without R2. P2 also demands for R3 which is being used by P3. P2 also stops its execution
because it can't continue without R3. P3 also demands for R1 which is being used by P1 therefore P3
also stops its execution.
In this scenario, a cycle is being formed among the three processes. None of the process is progressing
and they are all waiting. The computer becomes unresponsive since all the processes got blocked
CSE-II-II(R-23) Page 16
1. Mutual Exclusion: only one process at a time can use a resource
2. Hold and wait: a process holding at least one resource is waiting to acquire additional resources
held by other processes
3. No preemption: a resource can be released only voluntarily by the process holding it, after that
process has completed its task
4. Circular wait: there exists a set {P0 , P1 , …, Pn } of waiting processes such that P0 is waiting for a
resource that is held by P1 , P1 is waiting for a resource that is held by P2 , …, Pn–1 is waiting for
a resource that is held by Pn , and Pn is waiting for a resource that is held by P0 .
RESOURCEALLOCATIONGRAPH
Resource Allocation Graph (RAG) is a graph that represents the state of a Directed Graph.
Components Of RAG-There are two major components of a Resource Allocation Graph-
1. Vertices
2. Edges
1. Vertices-
There are following types of vertices in a Resource Allocation Graph
Process Vertices
Resource Vertices
Resource Vertices
Resource vertices represent the resources. Depending on the number of instances that exists in the
system, resource vertices may be single instance or multiple instances. They are drawn as a rectangle by
mentioning the dots inside the rectangle. The number of dots inside the rectangle indicates the number
of instances of that resource existing in the system.
2. Edges
There are two types of edges in a Resource Allocation Graph
1. Assign Edges
2. Request Edges
CSE-II-II(R-23) Page 17
Assign Edges: Assign edges represent the assignment of resources to the processes. They are drawn as
an arrow where the head of the arrow points to the process and tail of the process points to the
instance of the resource.
Request Edges: Request edges represent the waiting state of processes for the resources. They are
drawn as an arrow where the head of the arrow points to the instance of the resource and tail of the
process points to the process. If a process requires ‘n’ instances of a resource type, then ‘n’ assign edges
will be drawn.
CSE-II-II(R-23) Page 18
Basic Facts
If a graph contain no cycle=> no dead lock
If graph contains a cycle
o if only one instance per resource type, then deadlock
o if several instances per resource type, possibility of deadlock
Methods for Handling Deadlocks
There are three ways of handling deadlocks:
1. Deadlock prevention
2. Deadlock avoidance
3. Deadlock detection and recovery.
1. Deadlock prevention:
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and circular wait
holds simultaneously. If it is possible to violate one of the four conditions at any time then the
deadlock can never occur in the system.
The idea behind the approach is very simple that we have to fail one of the four conditions but
there can be a big argument on its physical implementation in the system.
2. Deadlock avoidance:
In deadlock avoidance, the operating system checks whether the system is in safe state or in
unsafe state at every step which the operating system performs.
The process continues until the system is in safe state. Once the system moves to unsafe state,
the OS has to backtrack one step.
3. Deadlock detection and recovery:
This approach let the processes fall in deadlock and then periodically check whether deadlock occur in
the system or not. If it occurs then it applies some of the recovery methods to the system to get rid of
deadlock
CSE-II-II(R-23) Page 19
Deadlock Prevention
To prevent the system from deadlocks, one of the four discussed conditions that may create a deadlock
should be discarded.
• Deadlocks can be eliminated by preventing at least one of the four required conditions:
1) Mutual exclusion
2) Hold-and-wait
3) No preemption
4) Circular-wait.
Mutual Exclusion: In general, we do not have systems with all resources being sharable. Some resources
like printers, processing units are non-sharable. So it is not possible to prevent deadlocks by denying
mutual exclusion.
• This condition must hold for non-sharable resources.
For example: A printer cannot be simultaneously shared by several processes.
• On the other hand, shared resources do not lead to deadlocks.
For example: Simultaneous access can be granted for read-only file.
• A process never waits for accessing a sharable resource.
• In general, we cannot prevent deadlocks by denying the mutual-exclusion condition because some
resources are non-sharable by default.
Hold and Wait
Whenever a process requests a resource, it does not hold any other resources
Require process to request and be allocated all its resources before it begins execution, or allow
process to request resources only when the process has none
Low resource utilization; starvation possible
No Preemption –
If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released
Preempted resources are added to the list of resources for which the process is waiting
Process will be restarted only when it can regain its old resources, as well as the new ones that it
is requesting
Circular Wait –
One protocol to ensure that the circular wait condition never holds is “Impose a linear ordering of all
resource types.” Then, each process can only request resources in an increasing order of priority.
For example, set priorities for r1 = 1, r2 = 2, r3 = 3, and r4 = 4. With these priorities, if process P wants to
use r1 and r3, it should first request r1, then r3.
Another protocol is “Whenever a process requests a resource rj, it must have released all resources rk
with priority (rk) ≥ priority (rj).
CSE-II-II(R-23) Page 20
Deadlock Avoidance
Given some additional information on how each process will request resources, it is possible to
construct an algorithm that will avoid deadlock states. The algorithm will dynamically examine
the resource allocation operations to ensure that there won't be a circular wait on resources.
When a process requests a resource that is already available, the system must decide whether
that resource can immediately be allocated or not. The resource is immediately allocated only if
it leaves the system in a safe state.
A state is safe if the system can allocate resources to each process in some order avoiding a
deadlock. A deadlock state is an unsafe state
Safe State
• A state is safe if the system can allocate all resources requested by all processes without entering a
deadlock state.
• A state is safe if there exists a safe sequence of processes {P0, P1, P2, ..., PN} such that the requests of
each process(Pi) can be satisfied by the currently available resources.
• If a safe sequence does not exist, then the system is in an unsafe state, which may lead to deadlock. •
All safe states are deadlock free, but not all unsafe states lead to deadlocks.
Basic Facts
If a system is in safe stateno deadlocks
If a system is in unsafe state possibility of deadlock
Avoidanceensures that a system will never enter an unsafe state.
Avoidance algorithms
Single instance of a resource type
o Use a resource-allocation graph
Multiple instances of a resource type
o Use the banker’s algorithm
Resource-Allocation-Graph Algorithm
If resource categories have only single instances of their resources, then deadlock states can be
detected by cycles in the resource-allocation graphs.
In this case, unsafe states can be recognized and avoided by augmenting the resource-allocation
graph with claim edges (denoted by a dashed line).
Claim edge Pi → Rj indicated that process Pi may request resource Rj at some time in future.
The important steps are as below:
CSE-II-II(R-23) Page 21
Deadlock Detection using RAG
• If a cycle is being formed in a Resource allocation graph where all the resources have the single
instance then the system is deadlocked.
• In Case of Resource allocation graph with multi-instanced resource types, Cycle is a necessary
condition of deadlock but not the sufficient condition.
• The following example contains three processes P1, P2, P3 and three resources R2, R2, R3. All the
resources are having single instances each.
• If we analyze the graph then we can find out that there is a cycle formed in the graph since the system
is satisfying all the four conditions of deadlock.
CSE-II-II(R-23) Page 22
o Low resource utilization; starvation possible
No Preemption –
o If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released
o Preempted resources are added to the list of resources for which the process is waiting
o Process will be restarted only when it can regain its old resources, as well as the new
ones that it is requesting
o Circular Wait – impose a total ordering of all resource types, and require that each process
requests resources in an increasing order of enumeration
2. DEADLOCK AVOIDANCE
Requires that the system has some additional a priori information available
Simplest and most useful model requires that each process declare the maximum number of
resources of each type that it may need
The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure
that there can never be a circular-wait condition
Resource-allocation state is defined by the number of available and allocated resources, and
the maximum demands of the processes
Safe State
When a process requests an available resource, system must decide if immediate allocation
leaves the system in a safe state.
System is in safe state if there exists a safe sequence of all processes.
Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be
satisfied by currently available resources + resources held by all the Pj, with j<I.
– If Pi resource needs are not immediately available, then
Pi can wait until all Pj have finished.
– When Pj is finished, Pi can obtain needed resources,
execute, return allocated resources, and terminate.
– When Pi terminates, Pi+1 can obtain its needed
Basic Facts
If a system is in safe state ⇒ no deadlocks.
If a system is in unsafe state ⇒ possibility of deadlock.
Avoidance ⇒ ensure that a system will never enter an unsafe state.
Safe, Unsafe , Deadlock State
Avoidance algorithms
Single instance of a resource type
o Use a resource-allocation graph
Multiple instances of a resource type
CSE-II-II(R-23) Page 23
o Use the banker’s algorithm
Banker's Algorithm in Operating System
Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is
used in banking systems to determine whether a loan can be granted or not.
Consider there are n account holders in a bank and the sum of the money in all of their accounts
is S. Every time a loan has to be granted by the bank, it subtracts the loan amount from the total
money the bank has. Then it checks if that difference is greater than S. It is done because, only
then, the bank would have enough money even if all the n account holders draw all their money
at once.
Whenever a new process is created, it must specify the maximum instances of each resource type that it
needs, exactly.
This algorithm is applicable to the system with multiple instances of each resource types.
However, this algorithm is less efficient then the resource-allocation-graph algorithm.
When a process starts up, it must declare the maximum number of resources that it may need.
This number may not exceed the total number of resources in the system.
When a request is made, the system determines whether granting the request would leave the
system in a safe state.
If the system in a safe state, the resources are allocated;
else
the process must wait until some other process releases enough resources.
• Assumptions:
Let n = number of processes in the system
Let m = number of resources types.
1. Available
It is an array of length m. It represents the number of available resources of each type. If Available[j] = k,
then there are k instances available, of resource type Rj.
2. Max
It is an n x m matrix which represents the maximum number of instances of each resource that a process
can request. If Max[i][j] = k, then the process Pi can request atmost k instances of resource type Rj.
3) Allocation [n][m]
It is an n x m matrix which represents the number of resources of each type currently allocated to each
process. If Allocation[i][j] = k, then process Pi is currently allocated k instances of resource type Rj.
4) Need [n][m]
It is a two-dimensional array. It is an n x m matrix which indicates the remaining resource needs of each
process. If Need[i][j] = k, then process Pi may need k more instances of resource type Rj to complete its
task.
Need[i][j] = Max[i][j] - Allocation [i][j]
CSE-II-II(R-23) Page 24
Safety Algorithm
This algorithm is used for finding out whether a system is in safe state or not
Assumptions:
Work is a working copy of the available resources, which will be modified during the analysis.
Finish is a vector of boolean values indicating whether a particular process can finish.
Step 1:
Let Work and Finish be two vectors of length m and n respectively.
Initialize:
Work = Available
Finish[i] = false for i=1,2,3,…….n
Step 2:
Find an index(i) such that both
a) Finish[i] = false
b) Need i <= Work.
If no such i exist, then go to step 4
Step 3:
Set:
Work = Work + Allocation(i)
Finish[i] = true
Go to step 2
Step 4:
If Finish[i] = true for all i, then the system is in safe state.
Resource-Request Algorithm
Now the next algorithm is a resource-request algorithm and it is mainly used to determine whether
requests can be safely granted or not.
Let Request(i) be the request vector for the process Pi. If Requesti[j]==k, then process Pi wants k instance
of Resource type Rj. When a request for resources is made by the process Pi, the following are the actions
that will be taken:
1. If Request(i) <= Need(i), then go to step 2;else raise an error condition, since the process has
exceeded its maximum claim.
2. If Request(i) <= Available(i) then go to step 3; else Pi must have to wait as resources are not available.
3. Now we will assume that resources are assigned to process Pi and thus perform the following steps:
CSE-II-II(R-23) Page 25
Banker's Algorithm Example::
Let us consider the following snapshot for understanding the banker's algorithm:
Example:1 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:
CSE-II-II(R-23) Page 26
Apply the Banker's Algorithm:
Now we check if each type of resource request is available for each process.
Step 1: For Process P1:
Need <= Available
7, 4, 3 <= 3, 3, 2 condition is false.
So, we examine another process, P2.
Step 2: For Process P2:
Need <= Available
1, 2, 2 <= 3, 3, 2 condition true
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2
Similarly, we examine another process P3.
Step 3: For Process P3:
P3 Need <= Available
6, 0, 0 < = 5, 3, 2 condition is false.
Similarly, we examine another process, P4.
Step 4: For Process P4:
P4 Need <= Available
0, 1, 1 <= 5, 3, 2 condition is true
New Available resource = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3
Similarly, we examine another process P5.
Step 5: For Process P5:
P5 Need <= Available
4, 3, 1 <= 7, 4, 3 condition is true
New available resource = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5
Now, we again examine each type of resource request for processes P1 and P3.
Step 6: For Process P1:
P1 Need <= Available
7, 4, 3 <= 7, 4, 5 condition is true
New Available Resource = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5
So, we examine another process P2.
Step 7: For Process P3:
P3 Need <= Available
6, 0, 0 <= 7, 5, 5 condition is true
New Available Resource = Available + Allocation
7, 5, 5 + 3, 0, 2 => 10, 5, 7
Hence, we execute the banker's algorithm to find the safe state and the safe sequence like P2, P4, P5,
P1 and P3.
CSE-II-II(R-23) Page 27
Let us consider the following snapshot for understanding the banker's algorithm:
Example 2:
P1 212 322
P2 401 902
P3 020 753
P4 112 112
Solution:
1. The Content of the need matrix can be calculated by using the formula given below:
Need = Max – Allocation
Available = (2, 1, 0)
CSE-II-II(R-23) Page 28
So, the system will move to the next process.
Available = (2, 1, 0)
Request of P1 is granted.
= (2, 1, 0) + (2, 1, 2)
Available = (4, 2, 2)
Available = (4, 2, 2)
Available = (4, 2, 2)
Request of P4 is granted.
= (4, 2, 2) + (1, 1, 2)
CSE-II-II(R-23) Page 29
Available = (5, 3, 4)
Request of P2 is granted.
= (5, 3, 4) + (4, 0, 1)
Available = (9, 3, 5)
= Available (9, 5, 5)
The system allocates all the needed resources to each process. So, we can say that the system is in a
safe state.
= [8 5 7] + [2 1 0] = [10 6 7]
CSE-II-II(R-23) Page 30
Resource-Request Algorithm
Example:
Question: Consider the following snapshot of a system:
Answer the following questions using Banker's algorithm. i) What is the content of the matrix need? ii) Is
the system in a safe state? iii) If a request from process P1 arrives for (1 0 2) can the request be granted
immediately?
Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
CSE-II-II(R-23) Page 31
3. Deadlock Detection
• If a system does not use deadlock-prevention or deadlock-avoidance algorithm then a deadlock may
occur.
• In this environment, the system must provide
1) An algorithm to examine the system-state to determine whether a deadlock has occurred.
2) An algorithm to recover from the deadlock.
Single Instance of Each Resource Type
If all the resources have only a single instance then we can define deadlock detection algorithm
that uses a variant of resource allocation graph called a wait for graph.
This graph is obtained by removing the nodes of type resources and removing appropriate edges
If all the resources have only a single instance, then deadlock detection-algorithm can be
defined using a wait-for-graph.
The wait-for-graph is applicable to only a single instance of a resource type.
A wait-for-graph (WAG) is a variation of the resource-allocation-graph.
The wait-for-graph can be obtained from the resource-allocation-graph by
o → removing the resource nodes and
o → collapsing the appropriate edges.
• An edge from Pi to Pj implies that process Pi is waiting for process Pj to release a resource that Pi
needs.
• An edge Pi → Pj exists if and only if the corresponding graph contains two edges
1) Pi → Rq and
2) Rq → Pj.
• For example:
Consider resource-allocation-graph shown in Figure a
Corresponding wait-for-graph is shown in Figure b.
CSE-II-II(R-23) Page 32
Figure (a) Resource-allocation-graph Figure (b) Corresponding wait-for-graph.
• A deadlock exists in the system if and only if the wait-for-graph contains a cycle.
• To detect deadlocks, the system needs to
→ maintain the wait-for-graph and
→ periodically execute an algorithm that searches for a cycle in the graph.
Several Instances of a Resource Type
• The wait-for-graph is applicable to only a single instance of a resource type.
• Problem: However, the wait-for-graph is not applicable to a multiple instance of a resource type.
• Solution: The following detection-algorithm can be used for a multiple instance of a resource type.
• Assumptions:
Let ‘n’ be the number of processes in the system
Let ‘m’ be the number of resources types.
• Following data structures are used to implement this algorithm.
1) Available [m]
This vector indicates the no. of available resources of each type.
If Available[j]=k, then k instances of resource type Rj is available.
2) Allocation [n][m]
This matrix indicates no. of resources currently allocated to each process.
If Allocation[i,j]=k, then Pi is currently allocated k instances of Rj.
3) Request [n][m]
This matrix indicates the current request of each process.
If Request [i, j] = k, then process Pi is requesting k more instances of resource type Rj.
DETECTION ALGORITHM
CSE-II-II(R-23) Page 33
DETECTION-ALGORITHM USAGE
When, and how often, to invoke depends on:
How often a deadlock is likely to occur?
How many processes will need to be rolled back? one for each disjoint cycle If detection
algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we
would not be able to tell which of the many deadlocked processes ―caused the deadlock.
Recovery from deadlock
1. Process Termination:
Abort all deadlocked processes
Abort one process at a time until the deadlock cycle is eliminated
For process termination, following factors need to be considered:
1) The priority of process.
2) The time taken by the process for computation & the required time for complete execution.
3) The no. of resources used by the process.
4) The no. of extra resources required by the process for complete execution.
5) The no. of processes that need to be terminated for deadlock-free execution.
6) The process is interactive or batch.
2. Resource Preemption
Selecting a victim – minimize cost.
Rollback – return to some safe state, restart process for that state.
Starvation – same process may always be picked as victim, include number of rollback in cost
factor.
CSE-II-II(R-23) Page 34
UNIT-4
Memory-Management Strategies: Introduction, Contiguous memory allocation, Paging, Structure of the
Page Table, Swapping. Virtual Memory Management: Introduction, Demand paging, Copy-on-write,
Page replacement, Allocation of frames, Thrashing. Storage Management: Overview of Mass Storage
Structure, HDD Scheduling.
Introduction
Memory management is the functionality of an operating system which handles or manages
primary memory and moves processes back and forth between main memory and disk during
execution.
Memory management keeps track of each and every memory location, regardless of either it is
allocated to some process or it is free.
It checks how much memory is to be allocated to processes.
It decides which process will get memory at what time.
It tracks whenever some memory gets freed or unallocated and correspondingly it updates the
status.
Memory Management
Logical address – is reference to memory location independent of the current assignment of data to
memory; a translation must be made to physical address before the memory access can be achieved. It
is generated by the CPU; also referred to as virtual address.
Physical address or Absolute Address is actual memory location in memory. It is the address seen by the
memory unit (HW).
Logical and physical addresses are the same in compile-time and load-time address-binding schemes;
logical (virtual) and physical addresses differ in execution-time address-binding scheme.
A relative address is a particular example of logical address, in which the address is expressed as a
location relative to some known point, usually a value in a processor register.
Base and Limit Registers
A pair of base and limit registers define the logical address space
CPU must check every memory access generated in user mode to be sure it is between base and
limit for that user
WHAT IS MEMORY?
Computer memory can be defined as a collection of some data represented in the binary format. On the
basis of various functions, memory can be classified into various categories.
A computer device that is capable to store any information or data temporally or permanently is called
storage device.
The main memory can be broadly allocated in two ways:
Contiguous memory allocation
Non-contiguous memory allocation
MEMORY ALLOCATION STRATEGIES:
Contiguous Memory Allocation:
In contiguous memory allocation each process is contained in a single contiguous block of
memory.
Memory is divided into several fixed size partitions.
Each partition contains exactly one process.
When a partition is free, a process is selected from the input queue and loaded into it.
The free blocks of memory are known as holes.
The set of holes is searched to determine which hole is best to allocate.
Contiguous Technique can be divided into:
o 1. Fixed (or static) partitioning
o 2. Variable (or dynamic) partitioning
Fixed Partition Scheme:
In the fixed partition scheme, memory is divided into fixed number of partitions i.e. number of
partitions in RAM is fixed but the size of each partition may or may not be the same.
Degree of multi-programming is restricted by number of partitions in the memory.
In every partition only one process will be accommodated.
Here partitions are made before execution or during system configure.
Advantages of Fixed Partitioning –
1. Easy to implement:
2. Little OS overhead:
Disadvantages of Fixed Partitioning –
1. Internal Fragmentation
2. External Fragmentation
3. Limit process size
4. Limitation on Degree of Multiprogramming
Variable Partitioning
It is a part of Contiguous allocation technique.
It is used to alleviate the problem faced by Fixed Partitioning.
In contrast with fixed partitioning, partitions are not made before the execution or during
system configure.
Various features associated with variable Partitioning
1. Initially RAM is empty and partitions are made during the run-time according to
process’s need instead of partitioning during system configure.
2. The size of partition will be equal to incoming process.
3. The partition size varies according to the need of the process so that the internal
fragmentation can be avoided to ensure efficient utilization of RAM.
4. Number of partitions in RAM is not fixed and depends on the number of incoming
process and Main Memory’s size.
Example: As illustrated in above figure, first process is only consuming 1MB out of 4MB in 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.
Hence, 7 MB becomes part of external fragmentation.
1. Paging: Paging is a memory management technique that divides physical memory into fixed-size
pages and virtual memory into pages of the same size. The operating system loads pages of a
process into physical memory as needed, allowing the process to access a larger virtual address
space than is available in physical memory.
2. Segmentation: Segmentation is another memory management technique that divides memory
into segments or sections of varying sizes, which are used to store different parts of a program or
process.
3. Swapping: Swapping is a memory management technique that temporarily removes a process
from memory and stores it on disk, freeing up physical memory for other processes. When the
process needs to run again, it is swapped back into memory.
4. Fragmentation: Fragmentation occurs when memory becomes fragmented, or broken into small
pieces, making it difficult to allocate contiguous blocks of memory for processes. This can be
addressed by defragmenting memory or using dynamic memory allocation.
5. Caching: Caching is a memory management technique that stores frequently accessed data in
fast memory (cache) to reduce the number of times the data needs to be retrieved from slower
memory (such as RAM or disk).
6. Garbage Collection: Garbage collection is a memory management technique used in higher-
level programming languages to automatically deallocate memory that is no longer in use by a
program or process. This helps to prevent memory leaks and ensures that memory is used
efficiently.
Paging
Paging is a memory management scheme that eliminates the need for contiguous allocation of
physical memory. In paging, secondary memory and main memory are divided into equal fixed
size partitions.
The partitions of secondary memory are called as pages.
The partitions of main memory are called as frames.
Each process is divided into parts where size of each part is same as page size.
(Or)
Divide logical memory (programs) into blocks of same size called pages. Divide physical memory
into equal sized partitions called frames (size is power of 2, between ½K and 8K). The size of the
page and frame must be equal
When a process is to be executed, its pages are loaded into any available memory frames from the disk.
Page table is used to map the page number to corresponding frame number. The page table contains
the base address of the page (frame) in memory. Page number will be used as index for the page table.
Whenever program is loaded into memory the corresponding frame number is updated to page table. In
the fig, page 0 is loaded into frame 1, page 1 is loaded into frame 4, page 2 is loaded into frame 3 and
page 3 is loaded into frame 7. The frame numbers are updates to page table. If a frame is allocated to
one process it cannot be allocated to another process.
Paging Model of Logical and Physical Memory
Example
Let us consider the main memory size 16 Kb and Frame size is 1 KB therefore the main memory
will be divided into the collection of 16 frames of 1 KB each.
There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB each. Each process is
divided into pages of 1 KB each so that one page can be stored in one frame.
Initially, all the frames are empty therefore pages of the processes will get stored in the
contiguous way.
Frames, pages and the mapping between the two is shown in the image below.
Let us consider that, P2 and P4 are moved to waiting state after some time. Now, 8 frames
becomeemptyandthereforeotherpagescanbe loaded inthat emptyplace. TheprocessP5of size 8
KB (8 pages) is waiting inside the ready queue.
Given the fact that, we have 8 non contiguous frames available in the memoryand paging
providesthe flexibilityofstoringtheprocessatthedifferent places.Therefore,wecanloadthe pages
of process P5 in the place of P2 and P4.
In order to execute any instructions the CPU always generates a Logical Address. But, the Physical
Address is needed to access the main memory.
The page number is used as index for the page table. The page number is replaced with corresponding
frame number
The Logical Address generated by the CPU has two parts:
1. Page Number (p) - It is the number of bits required to represent the pages in the Logical Address
Space. It is used as an index in a page table that contains the base address of a page in the
physical memory.
2. Page Offset (d) - It denotes the page size or the number of bits required to represent a word on
a page. It is combined with the Page Number to get the Physical Address.
The Physical Address also consists of two parts:
1. Frame Number (f) - It is the number of bits required to represent a frame in the Physical Address
Space. It is the location of the required page inside the Main Memory.
2. Frame Offset (d) - It is the page size or the number of bits required to represent a word in a
frame. It is equal to the Page Offset.
Page Table
Def: - Page Table is a data structure used by the virtual memory system to store the mapping
between logical addresses and physical addresses.
Page table accepts the inputs of page numbers as that produce the output of the frame numbers
The Page Table contains the base address of each page inside the Physical Memory. It is then combined
with Page Offset to get the actual address of the required data in the main memory.
The Page Number is used as the index of the Page Table which contains the base address which is the
Frame Number. Page offset is then used to retrieve the required data from the main memory.
Example of Paging
If Logical Address = 2 bits, then Logical Address Space = 22 words and vice versa.
If Physical Address = 4 bits, then Physical Address Space = 24 words and vice versa.
Let Page Size = 1 bit, Logical Address = 2 bits, Physical Address= 4 bits.
Frame Size = Page Size = 21 = 2 Words. It means every page will contain 2 words.
Advantages of Paging
It is one of the easiest Memory Management Algorithms.
Paging helps in storing a process at non-contagious locations in the main memory.
Paging removes the problem of External Fragmentation.
Disadvantages of Paging
It may cause Internal Fragmentation.
More Memory is consumed by the Page Tables.
It faces a longer memory lookup time.
The concept of normal paging says that every process maintains its own page table which
includes the entries of all the pages belonging to the process. The large process may have a page
table with millions of entries. Such a page table consumes a large amount of memory.
Consider we have six processes in execution. So, six processes will have some or the other of
their page in the main memory which would compel their page tables also to be in the main
memory consuming a lot of space. This is the drawback of the paging concept.
The inverted page table is the solution to this wastage of memory. The concept of an inverted
page table involves the existence of single-page table which has entries of all the pages (may they
belong to different processes) in main memory along with the information of the process to which
they are associated. To get a better understanding consider the figure below of inverted page
table.
Segmentation:
Segmentation is a memory-management scheme that supports this user view of memory. The
user's view is mapped into the Physical Memory.
In which memory and process are divide into variable size portions
In Segmentation, a process is divided into multiple segments. The size of each segment is not
necessarily the same which is different from paging. In Paging, a process was divided into equal
partitions called pages. The module contained in a segment decides the size of the segment.
The partitions of secondary memory area units are known as segments.
The details concerning every segment are stored in a table known as segmentation table.
Segment table contains two main data concerning segment, one is Base and Limit
Base:- which is the bottom address of the segment and
Limit:-which is the length of the segment.
Now suppose the CPU calls for segment number 2 which is 400 bytes long and it resides at 4300
memory location. The CPU wants to refer the 53rd byte of segment 2. So, here the input we get from
CPU is segment number 2 and 53 as an offset.
Now the offset is in between 0 and limit of the segment 2 i.e. 400. So, the condition got verified and
the offset is being added to the base address of the segment 2 to reach the 53rd byte of the segment
2 in physical memory. In case you try to access the 453rd byte of segment 2 then it would result in a
trap to the operating system as offset value 453 is greater than the limit of segment 2.
Difference between Paging and Segmentation
Sr. No. Key Paging Segmentation
Compiler is responsible to
Operating System divides calculate the segment size,
2 Accountability
the memory into pages. the virtual address and actual
address.
Swapping
Swapping in OS is a memory management technique that temporarily swaps processes from main
memory to secondary memory or vice versa which helps to increase the degree of multi-
programming and increase main memory utilization.
There are two steps in the process of Swapping in the operating system:
1. Swap In: Swap-in is the process of bringing a process from secondary storage/hard disc to main
memory (RAM).
2. Swap Out: Swap-out takes the process out of the Main memory and puts it in the secondary
storage.
In the above diagram, process P1 is swap out so the process with more memory requirement or
higher priority will be executed to increase the overall efficiency of the operating system. While the
process P2 is swap in for its execution from the secondary memory to the main memory(RAM).
Example
Let's understand this with the help of an example. The process size of the user's process is 4096KB,
and the transfer rate is 1 Mbps. Now we'll find out how long it will take to move from main memory
to secondary memory.
User process size = 4096KB
Data transfer Rate = 1024 Kbps
Time = User process size / Data transfer Rate
4096 / 1024 = 4 seconds
4000 milliseconds
The process will now take 4000 milliseconds.
Advantages of Swapping in OS
Disadvantages of Swapping in OS
The swapping algorithm must be perfect; otherwise, the number of Page Faults will increase, and
performance will decrease.
Inefficiency will occur when there are common/shared resources between many processes.
The user will lose information if there is heavy swapping and the computer loses its power.
Virtual Memory is a technique that is implemented using both hardware and software. It maps
memory addresses used by a program, called virtual addresses, into physical addresses in
computer memory.
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.
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 the use of a page or segment table permit 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
A demand paging system is quite similar to a paging system with swapping where processes reside in
secondary memory and pages are loaded only on demand, not in advance. When a context switch occurs,
the operating system does not copy any of the old program’s pages out to the disk or any of the new
program’s pages into the main memory Instead, it just begins executing the new program after loading the
first page and fetches that program’s pages as they are referenced.
While executing a program, if the program references a page which is not available in the main memory
because it was swapped out a little ago, the processor treats this invalid memory reference as a page
fault and transfers control from the program to the operating system to demand the page back into the
memory.
Advantages
In paging, processes are divided into pages and main memory is divided into frames. Pages of a process are loaded
into frames of main memory when required.
Page Fault means the page referenced by the CPU is not present in the main memory.
When the CPU generates the reference of a page, if there is any vacant frame available in the main memory then
the page is loaded in that vacant frame. In another case, if there is no vacant frame available in the main memory,
it is required to replace one of the pages in the main memory with the page referenced by the CPU.
Page Replacement Algorithm is used to decide which page will be replaced to allocate memory to the current
referenced page.
First In First Out (FIFO) -This algorithm is similar to the operations of the queue. All the pages are stored in the
queue in the order they are allocated frames in the main memory. The one which is allocated first stays in the
front of the queue. The one which is allocated the memory first is replaced first. The one which is at the front of
the queue is removed at the time of replacement.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1
As in the above figure shown, Let there are 3 frames in the memory.
6, 7, 8 are allocated to the vacant slots as they are not in memory.
When 9 comes page fault occurs, it replaces 6 which is the oldest in memory or front element of the
queue.
Then 6 comes (Page Fault), it replaces 7 which is the oldest page in memory now.
Similarly, 7 replaces 8, 1 replaces 9.
Then 6 comes which is already in memory (Page Hit).
Then 7 comes (Page Hit).
Then 8 replaces 6, 9 replaces 7. Then 1 comes (Page Hit).
Number of Page Faults = 9
While using the First In First Out algorithm, the number of page faults increases by increasing the number
of frames. This phenomenon is called Belady's Anomaly.
Let's take the same above order of pages with 4 frames.
In the above picture shown, it can be seen that the number of page faults is 10.
There were 9 page faults with 3 frames and 10 page faults with 4 frames.
The number of page faults increased by increasing the number of frames.
Optimal Page Replacement - In this algorithm, the page which would be used after the longest interval is replaced.
In other words, the page which is farthest to come in the upcoming sequence is replaced.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1, 7, 9, 6
First, all the frames are empty. 6, 7, 8 are allocated to the frames (Page Fault).
Now, 9 comes and replaces 8 as it is the farthest in the upcoming sequence. 6 and 7 would come earlier
than that so not replaced.
Then, 6 comes which is already present (Page Hit).
Then 7 comes (Page Hit).
Then 1 replaces 9 similarly (Page Fault).
Then 6 comes (Page Hit), 7 comes (Page Hit).
Then 8 replaces 6 (Page Fault) and 9 replaces 8 (Page Fault).
Then 1, 7, 9 come respectively which are already present in the memory.
Then 6 replaces 9 (Page Fault), it can also replace 7 and 1 as no other page is present in the upcoming
sequence.
The number of Page Faults = 8
This is the most optimal algorithm but is impractical because it is impossible to predict the upcoming page
references.
Least Recently Used - This algorithm works on previous data. The page which is used the earliest is replaced or
which appears the earliest in the sequence is replaced.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1, 7, 9, 6
First, all the frames are empty. 6, 7, 8 are allocated to the frames (Page Fault).
Now, 9 comes and replaces 6 which is used the earliest (Page Fault).
Then, 6 replaces 7, 7 replaces 8, 1 replaces 9 (Page Fault).
Then 6 comes which is already present (Page Hit).
Then 7 comes (Page Hit).
Then 8 replaces 1, 9 replaces 6, 1 replaces 7, and 7 replaces 8 (Page Fault).
Then 9 comes (Page Hit).
Then 6 replaces 1 (Page Fault).
The number of Page Faults = 12
Copy on Write
Copy-on-Write(CoW) is mainly a resource management technique that allows the parent and child
process to share the same pages of the memory initially. If any process either parent or child modifies the
shared page, only then the page is copied.
The CoW is basically a technique of efficiently copying the data resources in the computer system. In this
case, if a unit of data is copied but is not modified then "copy" can mainly exist as a reference to the
original data.
But when the copied data is modified, then at that time its copy is created(where new bytes are actually
written )as suggested from the name of the technique.
The main use of this technique is in the implementation of the fork system call in which it shares the virtual
memory/pages of the Operating system.
Recall in the UNIX(OS), the fork() system call is used to create a duplicate process of the parent
process which is known as the child process.
The CoW technique is used by several Operating systems like Linux, Solaris, and Windows XP.
The CoW technique is an efficient process creation technique as only the pages that are modified
are copied.
Free pages in this technique are allocated from a pool of zeroed-out pages.
The Copy on Write(CoW) Technique
The main intention behind the CoW technique is that whenever a parent process creates a child process both parent
and child process initially will share the same pages in the memory.
These shared pages between parent and child process will be marked as copy-on-write which means that if
the parent or child process will attempt to modify the shared pages then a copy of these pages will be
created and the modifications will be done only on the copy of pages by that process and it will not affect
other processes.
Now its time to take a look at the basic example of this technique:
Let us take an example where Process A creates a new process that is Process B, initially both these processes will
share the same pages of the memory.
Figure: Above figure indicates parent and child process sharing the same pages
Now, let us assume that process A wants to modify a page in the memory. When the Copy-on-write(CoW) technique
is used, only those pages that are modified by either process are copied; all the unmodified pages can be easily
shared by the parent and child process.
Allocation of Frames in OS
The main memory of the operating system is divided into various frames.
The process is stored in these frames, and once the process is saved as a frame, the CPU may run it.
As a result, the operating system must set aside enough frames for each process.
As a result, the operating system uses various algorithms in order to assign the frame.
There are mainly five ways of frame allocation algorithms in the OS. These are as follows:
The proportional frame allocation technique assigns frames based on the size needed for execution and the total
number of frames in memory.
The allocated frames for a process pi of size si are ai = (si/S)*m, in which S represents the total of all process sizes,
and m represents the number of frames in the system.
Disadvantage
The only drawback of this algorithm is that it doesn't allocate frames based on priority. Priority frame allocation solves
this problem.
3. Priority Frame Allocation
Priority frame allocation assigns frames based on the number of frame allocations and the processes. Suppose a
process has a high priority and requires more frames that many frames will be allocated to it. Following that, lesser
priority processes are allocated.
4. Global Replacement Allocation
When a process requires a page that isn't currently in memory, it may put it in and select a frame from the all frames
sets, even if another process is already utilizing that frame. In other words, one process may take a frame from
another.
Advantages
Process performance is not hampered, resulting in higher system throughput.
Disadvantages
The process itself may not solely control the page fault ratio of a process. The paging behavior of other processes also
influences the number of pages in memory for a process.
5. Local Replacement Allocation
When a process requires a page that isn't already in memory, it can bring it in and assign it a frame from its set of
allocated frames.
Advantages
The paging behavior of a specific process has an effect on the pages in memory and the page fault ratio.
Disadvantages
A low priority process may obstruct a high priority process by refusing to share its frames.
Global Vs. Local Replacement Allocation
The number of frames assigned to a process does not change using a local replacement strategy. On the other hand,
using global replacement, a process can choose only frames granted to other processes and enhance the number of
frames allocated.
Thrashing
Thrashing in an operating system (OS) is when a system spends too much time swapping pages instead
of doing useful work. This can lead to poor performance, such as slow response times and decreased
system efficiency.
Causes of thrashing:
High degree of multiprogramming.
Lack of frames.
Page replacement policy.
Programs or workloads may cause thrashing, and it results in severe performance problems, such as:
If CPU utilization is too low, we increase the degree of multiprogramming by introducing a new
system. A global page replacement algorithm is used. The CPU scheduler sees the decreasing
CPU utilization and increases the degree of multiprogramming.
CPU utilization is plotted against the degree of multiprogramming.
As the degree of multiprogramming increases, CPU utilization also increases.
If the degree of multiprogramming is increased further, thrashing sets in, and CPU utilization
drops sharply.
So, at this point, to increase CPU utilization and to stop thrashing, we must decrease the degree
of multiprogramming.
Techniques to Prevent Thrashing
The Local Page replacement is better than the Global Page replacement, but local page replacement has many
disadvantages, so it is sometimes not helpful. Therefore below are some other techniques that are used to handle
thrashing:
1. Locality Model
A locality is a set of pages that are actively used together. The locality model states that as a process executes, it
moves from one locality to another. Thus, a program is generally composed of several different localities which may
overlap.
For example, when a function is called, it defines a new locality where memory references are made to the function
call instructions, local and global variables, etc. Similarly, when the function is exited, the process leaves this locality.
2. Working-Set Model
This model is based on the above-stated concept of the Locality Model.
The basic principle states that if we allocate enough frames to a process to accommodate its current locality, it will
only fault whenever it moves to some new locality. But if the allocated frames are lesser than the size of the current
locality, the process is bound to thrash.
According to this model, based on parameter A, the working set is defined as the set of pages in the most recent 'A'
page references. Hence, all the actively used pages would always end up being a part of the working set.
3. Page Fault Frequency
A more direct approach to handle thrashing is the one that uses the Page-Fault Frequency concept.
The problem associated with thrashing is the high page fault rate, and thus, the concept here is to control
the page fault rate.
If the page fault rate is too high, it indicates that the process has too few frames allocated to it. On the
contrary, a low page fault rate indicates that the process has too many frames.
Upper and lower limits can be established on the desired page fault rate, as shown in the diagram.
If the page fault rate falls below the lower limit, frames can be removed from the process. Similarly, if the
page faults rate exceeds the upper limit, more frames can be allocated to the process.
In other words, the graphical state of the system should be kept limited to the rectangular region formed in
the given diagram.
Storage Management: Overview of Mass-Storage Structure
Mass Storage System
• A storage device is any device used in a computer to store information. It will retain this information
when the computer is switched off.
• Type of storage device used varies based on the type of data and the rate at which it is created and
used.
• Examples of storage devices are hard disk, CD-ROM, magnetic tape and removable media etc.
• The computer has many types of data storage devices. Some of them can be classified as the
removable data storage devices and the others as the non removable data storage devices.
Magnetic Disks
Traditional magnetic disks have the following basic structure:
o One or more platters in the form of disks covered with magnetic media. Hard
disk platters are made of rigid metal, while "floppy" disks are made of more flexible
plastic.
o Each platter has two working surfaces. Older hard disk drives would sometimes not use
the very top or bottom surface of a stack of platters, as these surfaces were more
susceptible to potential damage.
o Each working surface is divided into a number of concentric rings called tracks. The
collection of all tracks that are the same distance from the edge of the platter, ( i.e. all
tracks immediately above one another in the following diagram ) is called a cylinder.
o Each track is further divided into sectors, traditionally containing 512 bytes of data each,
although some modern disks occasionally use larger sector sizes. ( Sectors also include a
header and a trailer, including checksum information among other things. Larger sector
sizes reduce the fraction of the disk consumed by headers and trailers, but increase internal
fragmentation and the amount of disk that must be marked bad in the case of errors. )
o The data on a hard drive is read by read-write heads. The standard configuration ( shown
below ) uses one head per surface, each on a separate arm, and controlled by a
common arm assembly which moves all heads simultaneously from one cylinder to
another. ( Other configurations, including independent read-write heads, may speed up disk
access, but involve serious technical difficulties. )
o The storage capacity of a traditional disk drive is equal to the number of heads ( i.e. the
number of working surfaces ), times the number of tracks per surface, times the number of
sectors per track, times the number of bytes per sector. A particular physical block of data
is specified by providing the head-sector-cylinder number at which it is located.
HDD SCHEDULING (Disk scheduling algorithms)
Types of Disk Scheduling Algorithm in OS
FCFS is the simplest of all 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:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is: 50
So, total overhead movement (total distance covered by the disk arm) =
(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16) =642
Advantages of FCFS
Here are some of the advantages of First Come First Serve.
Every request gets a fair chance
No indefinite postponement
Disadvantages of FCFS
Here are some of the disadvantages of First Come First Serve.
Does not try to optimize seek time
May not provide the best possible service
2. SSTF (Shortest Seek Time First)
In SSTF (Shortest Seek Time First), requests having the 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 the
system. Let us understand this with the help of an example.
Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is: 50
SCAN Algorithm
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”.
Therefore, the total overhead movement (total distance covered by the disk arm) is calculated as
= (199-50) + (199-16) = 332
Advantages of SCAN Algorithm
Here are some of the advantages of the SCAN Algorithm.
High throughput
Low variance of response time
Average response time
Disadvantages of SCAN Algorithm
Here are some of the disadvantages of the SCAN Algorithm.
Long waiting time for requests for locations just visited by disk arm
4. C-SCAN
In the 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 the 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 the SCAN algorithm hence it is known as C-SCAN
(Circular SCAN).
Example:
Circular SCAN
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”.
So, the total overhead movement (total distance covered by the disk arm) is calculated as:
=(199-50) + (199-0) + (43-0) = 391
Advantages of C-SCAN Algorithm
Here are some of the advantages of C-SCAN.
Provides more uniform wait time compared to SCAN.
5. LOOK
LOOK Algorithm 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:
LOOK Algorithm
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”.
So, the total overhead movement (total distance covered by the disk arm) is calculated as:
= (190-50) + (190-16) = 314
6. C-LOOK
As LOOK is similar to the SCAN algorithm, in a similar way, C-LOOK is similar to the CSCAN disk scheduling
algorithm. In CLOOK, the disk arm in spite of going to the end goes only to the last request to be serviced in
front of the head and then from there goes to the other end’s last request. Thus, it also 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”
C-LOOK
So, the total overhead movement (total distance covered by the disk arm) is calculated as
= (190-50) + (190-16) + (43-16) = 341
What is RAID?
RAID (redundant array of independent disks) is a way of storing the same data in different places on
multiple hard disks or solid-state drives (SSDs) to protect data in the case of a drive failure. There are
different RAID levels, however, and not all have the goal of providing redundancy.
RAID works by placing data on multiple disks and allowing input/output (I/O) operations to overlap in a
balanced way, improving performance. Because using multiple disks increases the mean time between
failures, storing data redundantly also increases fault tolerance.
RAID arrays appear to the operating system (OS) as a single logical drive.