0% found this document useful (0 votes)
8 views

Operating System

The document provides an overview of operating systems, detailing the distinction between system software and application software, and explaining the roles of operating systems in managing hardware resources and providing user interfaces. It discusses various types of operating systems, including single-user, multi-programmed, batch, time-sharing, and real-time systems, along with their functionalities and examples. Additionally, it covers process management, scheduling queues, and the concept of process control blocks in operating systems.

Uploaded by

kiracherub866
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Operating System

The document provides an overview of operating systems, detailing the distinction between system software and application software, and explaining the roles of operating systems in managing hardware resources and providing user interfaces. It discusses various types of operating systems, including single-user, multi-programmed, batch, time-sharing, and real-time systems, along with their functionalities and examples. Additionally, it covers process management, scheduling queues, and the concept of process control blocks in operating systems.

Uploaded by

kiracherub866
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

OPERATING SYSTEM

CHAPTER ONE application software. Application software are also


called end user software because they are
designed to use by users of the computer. An
INTRODUCTION TO OPERATING SYSTEM
application software may be a set of computer
SYSTEM SOFTWARE AND APPLICATION SOFTWARE programs or a single computer program , these
software provide the required functionality for a
specific purpose. Thus , a computer user uses
Those computer software that control and these software to accomplish a specific task.
monitor the computer hardware and provide
essential functionality to the computer are called The different types of application software can be
system software. Therefore , system software are developed to accomplish different tasks such as
essential part of a computer , which means a word processing , playing multimedia files ,
computer can not perform its functions with out database applications , spread sheets , accounting
the system software. System software are entirely , graphic designing etc ...
responsible for creating an interface between the
computer’s user and computer hardware. Examples of application software include :-

System software are the system-side computer ➢ word processing programs such as
programs that are required for the working of the Mircosoft Word and Google Docs
computer itself , although the user of the
computer does not need to know about the ➢ Spreadsheeet programs such as Microsoft
functionality of system software while using the Excel and Google sheets
computer. Examples of system software include :
operating systems , device drivers , language ➢ Database programs such as oracle and
processors , etc … Microsoft access

Some examples of system software include :- Definition of an operating system

Operating systems :- These are the core software An operating system (os) is a software program
programs that manage a computer’s resources , that manages and controls the hardware and the
including the CPU , memory and storage devices. software resources of a computer system. It acts
Popular operating systems include Windows , as an interface between the user and the
macOS , Linux , and Android. computer’s hardware and provides services and
tools for application programs to interact with the
Device Drivers :- These programs enable a computer.
computer to communicate with hardware devices
such as printers , scanners , and graphic cards. The operating system is responsible for managing
Device drivers act as a translator between the the computer’s memory , processing power ,
hardware and the operating system , allowing the input/output devices and other resources. It also
two to work together seamlessly. provides a platform for running other software
applications , manages the file system and
Utility programs :- These programs perform provides a user interface for interacting with the
system-level tasks such as managing memory , computer.
scheduling tasks , and controlling hardware
devices. Examples of utility programs include Some common examples of operating systems
antivirus software , disk de-fragments , and include :-
system back up tools.
Microsoft Windows :- This is the most widely
What is an Application Software used operating system for personal computers

A computer software which is developed to MacOs :- This is the operating system used on
perform a specific function is known as Apple’s Mac computers
Linux :- This is an open source operating system Batch systems typically use a queue to manage
that is widely used in servers and other systems. the jobs or tasks that are waiting to be
processed. Each job is executed in turn , without
Android :- This is an operating system designed any user interaction , and the system may print
for mobile devices such as smart phones and out a report or summary of the results once the
tablets. job has finished.

Types of operating system's One advantage of batch systems is that they can
efficiently process large volumes of similar jobs ,
Single-User systems such as printing reports or processing payroll ,
they can also help to maximize the utilization of
A single user operating system that is designed to system resources by allowing multiple jobs to be
be used by one user at a time. It is typically processed simultaneously.
found on personal computers , work stations and
other systems that are used by individual users Multi-programmed systems
rather than shared among multiple users. The
goals of such systems are maximizing user In operating systems, a multi–programmed
convenience and responsiveness , instead of system is a type of operating system that allows
maximizing the utilization of the CPU and multiple programs to run on a computer system
peripheral devices. simultaneously , with the goal of maximizing the
utilization of system resources and improving
Single user operating systems are designed to overall system performance.
provide a simple , user-friendly interface that
allows users to perform basic tasks such as file In multi programmed system , multiple programs
management , document creation and editing and are loaded in to the computer’s memory and are
web browsing. They typically provide a graphical executed concurrently , with the operating system
user interface (GUI) that allows users to interact controlling the execution of each program. The
with the computer system using icons , menus , operating system use techniques such as time-
and windows. sharing , in which each program is allocated a
small slice of time to execute and priority
Single user operating systems are generally easier scheduling , in which higher priority programs
to use and manage than multi–user operating are given more time to execute.
systems , which are designed to support multiple
users simultaneously , they are also less One advantage of multi-programmed systems is
complex , as they do not need to manage access that they can improve overall system performance
to system resources by multiple users. by allowing multiple programs to run
simultaneously and share system resources such
Batch systems as CPU time and memory. This can help to
reduce the idle time of the CPU and improve
In operating systems courses , batch systems refer overall system throughput.
to a type of operating system that is designed to
process a large number of similar jobs or tasks in Another advantage of multi-programmed systems
a batch mode , without any user interaction is that they can improve the response time of the
during the processing of each job. system for interactive applications , by allowing
the operating system to switch quickly between
Batch systems were popular in the early days of different programs as needed.
computing when computers were large and
expensive , and user has to share the system Time-sharing or Multi-tasking systems
resources. In this environment , users would
submit their jobs or tasks to a system operator , Time sharing or multi tasking systems in
who would then queue them up for processing in operating systems refer to a type of system that
batch mode. allows multiple users or tasks to run concurrently
on a single computer system. These systems
enable users to interact with the computer in real
time and provide a seamless experience for deadlines , even in the face of resource
running multiple applications simultaneously. contention or other system constraints.

In a time-sharing or multi tasking system , the Soft real time systems :- These systems have less
CPU is divided in to multiple time slots or time strick timing requirements and can tolerate
slices and each user or task is allocated a small occasional missed deadlines , as long as the
slice of CPU time to execute its instructions. The system can recover and continue to function
operating system manages the allocation of CPU properly.
time , and switches between tasks or users
rapidly to give the impression of simultaneous CHAPTER TWO
execution.
PROCESS AND PROCESS MANAGEMENT
Parallel or Multiprocessor systems

Early computer systems allowed only one


Parallel or multi processor systems in operating
program to be executed at a time. This program
systems refer to a type of system that uses
had complete control of the system , and had
multiple processors or CPUs to perform
access to all of the system’s resources. Current
computations and execute instructions in parallel.
day computer systems allow multiple programs to
These systems enable faster and more efficient
be loaded in to memory and to be executed
processing of complex tasks by distributing the
concurrently. This evolution required firmer
work load across processors.
control and more compartmentalization of various
programs. These needs resulted in the notion of a
In a parallel or multi processor system , each
process , which is a program in execution. A
processor has its own memory and I/O devices
system therefore consists of a collection of
and is capable of executing instructions
processes , all these processes can potentially
independently of the other processors. The
execute concurrently , with the CPU (or CPUs)
operating system manages the allocation of tasks
multiplexed among them. By switching the CPU
to different processors and ensures that the
between processes , the operating system can
processors are synchronized and communicating
make the computer more productive.
effectively.

The Process :- Informally , a process is a program


Real time operating systems
in execution. The execution of a process must
progress in a sequential fashion. That is at any
Real – time systems in operating systems refer to
time , at most one instruction is executed on
a type of system that is designed to respond to
behalf of the process. We emphasize that a
events or stimuli in a timely and predictable
program by itself is not a process , A program is
manner. These systems are used in applications
a passive entity , such as the contents of a file
where timing is critical , such as in process
store on a disk , where as a process is an active
control , industrial automation and mission –
entity , with a program counter specifying the
critical systems.
next instruction to execute and a set of associated
resources.
In real – time system , the operating system must
respond to events or stimuli with in a specified
time frame , known as deadline. If the system Process state
fails to respond with in the deadline , the
consequences can be severe , such as equipment As a process executes , it changes state. The state
damage or loss of life. of a process is defined in part by the current
activity of that process. Each process may be one
Real time systems can be classified in to two of the following states ….
types :-
New State :- The process is being created
Hardware real time systems :- These systems Running :- Instructions are being executed
have strict timing requirements and must meet all Waiting :- The process is waiting for some event
to occur (such as an IO completion or reception
of a signal)
Ready :- The process is waiting to be assigned to particular process , so we have lines of
a processor instructions that are being executed and at a
Terminated :- The process has finished execution particular moment what is the address of the
next line that has to be executed , that is given
by the program counter.

CPU registers :- It tells us the registers that are


being used by a particular process. The registers
vary in number and type depending on the
computer architecture , they include accumulators
, index registers , stack pointers and general
purpose registers and so on. So these CPU
registers will tell us the particular register that
are used by a particular process.

CPU Scheduling information :- we know


PROCESS CONTROL BLOCK that there are many processes that are waiting to
be executed , so scheduling determines which
Each process is represented in the operating process has to be executed first or it determines
system by a process control block (PCB). A the order in which the processes has to be
process control block is something that we used executed.
to represent a particular process in the operating
system. Accounting information :- Keeps an account
of certain things like the resources that are being
What are the things we have in the process used by the particular process like CPU , time
control block which helps us to represent the and memory. It keeps an account of all the
process in the operating system. information that are being used by a particular
process for its execution.

I/O status information :- represents the


input/output devices that are being assigned
for a particular process. A process during
execution may need to use some
input/output devices , so which are the
input/output devices assigned stored in the
input/output status information.

Process Scheduling :-
These block tell or represent about the process in
an operating system. The objective of multi programming is to have
some process running at all times , to maximize
Process number (process – id ) :- Shows a unique CPU utilization. The objective of time sharing is
id of a particular process , so every process has to switch the CPU among processes so frequently
to be represented by a unique id. that users can interact with each program while
it is running. For a uni-processor system , there
Process state :- tells us a particular state in which will never be more than one running process. If
a process is at that particular moment , from the there are more processes , the rest will have to
different states that we have , this tells In which wait until the CPU is free and can be
of those states the process is in at that particular rescheduled.
moment.
Process scheduler selects an available process
Program counter :- It indicates the address of the (possibly from a set of several available processes
next instruction that has to be executed for that ) for program execution on the CPU.
SCHEDULING QUEUES
In operating systems, scheduling queues refer to
the data structures used to organize and manage
the execution of processes or threads. These
queues store the processes or threads in different
states, allowing the scheduler to determine which
one should be executed next.

Job Queue :- As a process enter the system they


are put in to a job queue , which consists of all So let’s say that from the Job queue , the
processes in the system. So they may not be process came to the ready queue , so let’s say a
executing at this time but there are the lists or particular process is in the ready queue and it’s
the set of all processes that we have in the just waiting to get the CPU for its execution and
system , so all these processes they are put in once it gets the CPU and it begins its execution ,
this set or this queue known as the Job Queue , but at this stage there are several cases that can
where they will be waiting In order to go to the occur :-
ready state.
The first thing that can happen is , it received
Ready Queue :- The processes that are residing in the CPU and it completed its execution and then
main memory and are ready and waiting to the process just ends , that means it goes to the
execute are kept on a list called the ready terminated state. Another thing that can happen
queue , in ready queue we have those processes , is , it comes from the ready queue and it is
which are ready and waiting to be executed , assigned to the CPU and it’s holding the CPU for
they are just waiting to get the CPU , so that it’s execution but at the same time , let’s say
they can start executing. that there is another process that comes with a
higher priority , that means this process that
Waiting Queue: This queue stores the processes or comes now is having a higher priority and it has
threads that are waiting for a specific event or to be executed before this , so the process that
was previously executing , it will get swapped
resource to become available before they can
out and it goes to a list called the partially
proceed with their execution. Examples of events executed swapped out process , and after that it
or resources include I/O operations, signals, or gets swapped in to the ready queue again , so
synchronization primitives. the execution has not completed fully , it is just
partially executed , it still needs a cpu to
Device Queue: These queues are associated with
complete it’s executions.
specific devices in the system, such as printers or
disks. Processes or threads waiting for access to a Another thing that can happened again is from
particular device are placed in the corresponding the ready queue it is assigned to the CPU , and
device queue until the device becomes available. it begins its execution and at some point of
time , it needs some input / output devices , at
Priority Queue: This queue assigns different some point of time the process may need to use
priorities to processes or threads based on their some of the input / output devices , so it will go
importance or urgency. The scheduler uses these to the input/output waiting queue , where it will
priorities to determine the order of execution, wait for the input/output operation to occur ,
which is required by that particular process. If
giving higher priority to critical tasks or time-
the input/output devices are available at that
sensitive operations.
time , that operation can immediately occur and
it continue its execution but if the input output
devices are being held by some other processes
and if it has to wait then this process will come
to the input/output waiting queue and wait for
the availability of the input/output devices and
once the input/output devices are assigned to the
process it will execute the input/output operation There are three types of schedulers:
and it will go back to the ready queue again
because it needs the CPU again to resume its 1) Long Term Scheduler (Job Scheduler): This
execution. scheduler determines which programs or processes
should be admitted to the system for execution.
Its main objective is to create a balanced mix of
Context Switch different types of jobs, such as those that require
a lot of input/output operations (I/O bound) or
Interrupts cause the operating system to change a those that require a lot of CPU processing
CPU from its current task and to run a kernel (processor bound). The long term scheduler
routine. controls the degree of multiprogramming, which
refers to the number of processes that are
When a process are being executed , if an
interrupt occurs or a process of higher priority allowed to run concurrently. In some systems,
comes then that particular process has to stop its this scheduler may be minimal or absent,
execution and it has to allow the process that especially in time-sharing operating systems.
causing the interrupt to execute first and after
This scheduler is like a smart gatekeeper who
that is completed the previous process can
resume its execution. decides which programs or tasks should be
allowed to enter the computer for doing their
When processes are being executed , the CPU is work. It tries to make sure that different types of
being assigned to that particular process , so tasks, like ones that need a lot of reading or
when an interrupt occurs , the CPU has to be writing, or ones that need a lot of thinking, get a
assigned to that process or the thing which is
fair chance. It also keeps an eye on how many
causing the interrupt so that it will execute first
and when that is completed , the CPU re- tasks can be done at the same time.
assigned back to the process that was previously 2) Short Term Scheduler (CPU Scheduler): The
being executed.
short term scheduler aims to increase system
performance by selecting which process should be
So when an interrupt occurs , the system needs
to save the current context of the process executed next from the pool of processes that are
currently running on the CPU , so that it can ready to run. Its main job is to allocate the CPU
restore the context when its processing is done , to one of the processes in the ready queue. The
essentially suspending the process and then short term scheduler, also known as the
resuming it. dispatcher, makes frequent decisions about which
process to execute next. It operates at a faster
When an interrupt occurs , the process that was
currently running needs to save its context to the rate compared to the long term scheduler.
CPU , the reason we need it is because when the
This scheduler is like a quick decision-maker that
process resume its execution , it needs to know
picks the next task to be done from a list of
where it did it stop executing at that particular
moment so that when it resumes , it will know tasks waiting for their turn. It's in charge of
from where it has to resume its operation again. making sure that each task gets a fair chance to
use the computer's brain, which is called the
SCHEDULERS CPU. It's a very fast decision-maker and always
wants to keep things moving smoothly.

Schedulers in operating systems are responsible 3) Medium Term Scheduler: The medium term
for managing the execution of processes and scheduler is responsible for handling the
deciding which processes should be allocated swapping function, which involves moving
system resources such as the CPU and memory. processes out of main memory (swapping out) to
create space for other processes. This occurs
when a running process makes an I/O request
and becomes suspended, unable to make progress. Imagine you have a big task to do, like cleaning
To free up memory, the suspended process is your room. But instead of doing it all by
moved to secondary storage, which is usually a yourself, you decide to divide the work among
hard disk. This swapping process reduces the your siblings. You become the parent, and your
degree of multiprogramming, helping to improve siblings become the children.
the overall process mix in the system.
So, you start by assigning different parts of the
This scheduler helps manage the computer's room to each sibling. Each sibling becomes
memory, which is like its working space. responsible for their assigned area. Now, here's
Sometimes, when a task is waiting for something where it gets interesting. Each sibling can also
else, like when it needs to read from a hard disk, ask for help from their own friends or siblings.
it gets put aside temporarily to make room for They become parents themselves, and their
other tasks. The medium term scheduler helps friends or siblings become their children. This
with this, making sure that the right tasks are way, the work of cleaning the room is divided
moved out of the working space to make room and shared among everyone.
for new tasks. It helps keep things organized and
In the same way, in a computer operating
running well.
system, a process (like a program or task) can
In simpler terms, the long term scheduler decides create child processes under it. The original
which programs should be allowed to run, the process is the parent process, and the newly
short term scheduler chooses which program created processes are its children. Just like you
should run next on the CPU, and the medium assigned tasks to your siblings, the parent process
term scheduler handles the swapping of processes can assign specific tasks or share the workload
between memory and secondary storage. Each among its child processes.
scheduler has its own role in managing the
And similar to how your siblings can also create
execution of processes and optimizing system
their own children to help with the work, child
performance.
processes can create their own child processes as
well. This creates a tree-like structure where the
Operation On Processes parent process can have children, and those
children can have their own children, and so on.
Processes in an operating system are like tasks or
By creating these child processes, the computer
jobs that the computer needs to do. The
can perform multiple tasks simultaneously,
operating system has to manage the creation and
making the most efficient use of its resources and
deletion of processes. Here's a simplified
getting work done more quickly.
explanation of how this works:
So, process creation is like dividing a big task
Creating a Process: When the operating system
into smaller tasks, assigning them to different
creates a new process, it gives it a name and
processes, and allowing those processes to create
some characteristics. It's like giving a new task a
more processes if needed, forming a tree-like
name and some instructions. A process can also
structure of tasks being executed by the
create other processes, like a parent giving birth
computer.
to children. Each new process can create more
processes, forming a tree-like structure. When a When a process creates child processes, there are
process is created, the operating system prepares two possibilities for how they can execute:
a special block of information called the Process
The parent and children execute concurrently:
Control Block (PCB), which holds important
This means that the parent process and its child
details about the process. The PCB is then added
processes can execute at the same time. It's like a
to a list of ready processes, meaning it's ready to
parent working on their task while the children
run.
work on their own tasks simultaneously. They work and wants to be removed from the
can all make progress together, independently, system.
without waiting for each other.
2. Returning a Status Value: Upon
The parent waits for its children to complete: In termination, a process may provide a
this case, the parent process pauses its own status value or result back to its parent
execution and waits for its child processes to process. This status value is typically an
finish their tasks. It's like a parent waiting for integer that indicates the outcome or
their children to complete their work before relevant information about the process's
continuing with their own task. Once all the execution. It helps the parent process
child processes have finished, the parent process understand the result or status of its child
resumes its execution. process.

To better understand the concept of resources, 3. Resource Deallocation: When a process


let's imagine a scenario. Think of the parent terminates, all the resources it was using
process as a baker in a bakery. The baker has all are deallocated by the operating system.
the necessary resources like ingredients, tools, These resources include physical and
and equipment. When the baker creates child virtual memory, open files, and
processes, which could be other bakers, the child input/output buffers. Deallocation ensures
bakers may receive the same set of resources as that the resources are freed up and
the parent baker or a subset of those resources. available for other processes to utilize.
For example, they might have their own
Additionally, there are cases where one process
ingredients but share the same equipment.
can cause the termination of another process
So, in simpler terms, when a process creates through an appropriate system call. However,
child processes, the parent and children can such termination is usually limited to the parent
either work together at the same time, each process terminating its child processes. This
doing their own tasks, or the parent can pause mechanism prevents unauthorized termination of
and wait for the children to finish before unrelated processes and maintains control within
continuing with its own task. The resources the process hierarchy.
needed for the tasks can be shared or partially
The parent process may decide to terminate its
shared between the parent and children processes,
child process for various reasons, including:
depending on how they are created.
• Resource Overuse: The child process has
exceeded its allocated resources, such as
TERMINATE A PROCESS
memory or CPU usage. This termination
prevents resource exhaustion and allows
Process termination refers to the event when a
other processes to utilize those resources
process comes to an end and is stopped. There
effectively.
are several aspects to consider when
understanding process termination :- • Task Completion: The task assigned to the
child process is no longer required or
1. Process Completion: When a process
relevant. If the parent determines that the
reaches its final statement, it has finished
child's task is no longer necessary, it can
executing all the tasks or lines of code
terminate the child process to free up
assigned to it. At this point, the process
resources and eliminate unnecessary
requests the operating system to delete it
processing.
using an "exit" system call. This call
signals that the process has completed its • Parent Termination: In some cases, when
the parent process is exiting or
terminating, the operating system may not It's important to note that the distinction between
allow its child processes to continue independent and cooperating processes depends
running independently. Therefore, the on whether they share data with each other. If
parent process ensures that its children processes don't share any data, they are
processes also terminate to maintain independent. However, if they exchange
system integrity and prevent orphaned information or work together by sharing data,
processes. they are considered cooperating processes.

Overall, process termination involves the INTER PROCESS COMMUNICATION


completion of a process's tasks, potential
communication of status values, and the Interprocess Communication (IPC) refers to the
deallocation of resources. The termination can be mechanisms and techniques used by processes in
initiated by the process itself upon completion or an operating system to exchange information and
by the parent process due to resource coordinate their activities. IPC allows processes to
management or task requirements. communicate with each other, share data, and
synchronize their actions to work together
INDEPENDENT AND COOPERATING PROCESS effectively.

Processes in an operating system can run


When multiple processes are running in an
independently or cooperatively. When processes
operating system, they can be categorized as
need to collaborate or share information, IPC
either independent processes or cooperating
provides the means for them to do so. It enables
processes.
processes to send messages, share memory, or use
1. Independent Processes: Think of other forms of communication to exchange data
independent processes as separate and coordinate their activities.
individuals who are working on their own
IPC is crucial for various scenarios, such as:
tasks without any interaction or influence
from other processes. They don't share 1. Sharing Data: Processes may need to
any data with other processes. Each exchange data, such as passing messages
process is like a self-contained entity, or sharing files, to cooperate and
doing its own thing without being accomplish tasks collectively. IPC
affected by or affecting other processes. facilitates the transfer of information
between processes.
2. Cooperating Processes: In contrast,
cooperating processes are like a team or 2. Synchronization: Processes may need to
group of people working together. They synchronize their activities to maintain
can affect or be affected by other order or prevent conflicts. For example,
processes in the system. These processes one process might need to wait for
share data with each other, meaning they another process to finish its task before
exchange information and collaborate to proceeding. IPC provides synchronization
accomplish a task or solve a problem. The mechanisms to control the flow and
actions of one cooperating process can coordination of processes.
impact the behavior or outcome of other
3. Coordination: Processes may need to
cooperating processes.
coordinate their actions to accomplish
To put it simply, independent processes are like complex tasks together. IPC allows
individuals working independently, while processes to communicate and coordinate
cooperating processes are like team members their activities to achieve a common goal
working together and sharing information. efficiently.
There are different IPC mechanisms and need to establish proper sequencing and
techniques available, including message passing, synchronization between processes to
shared memory, pipes, sockets, and remote maintain the correct order of operations.
procedure calls (RPC). Each mechanism has its
It's important to note that two of these issues
advantages, and the choice depends on factors
also apply to threads, which are smaller units of
like the nature of the communication,
execution within a process. Threads can easily
performance requirements, and the level of
share information since they have a common
coordination needed between processes.
memory space. However, they still face
Overall, IPC enables processes to communicate, challenges in avoiding conflicts and sequencing
share information, synchronize their actions, and dependencies. So, many of the same problems
work together to accomplish tasks in an operating and solutions that apply to processes also apply
system. It is a fundamental aspect of modern to threads.
operating systems and plays a crucial role in
In the upcoming sections, we will delve into
facilitating collaboration and coordination
these IPC issues and explore different mechanisms
between processes.
to address them.
Processes often need to communicate with each
In simpler terms, interprocess communication is
other. Think of it like people talking to each
when processes need to talk to each other. They
other to share information or work together. In
need ways to share information, avoid conflicts,
the computer world, processes also need a way to
and maintain the right order of operations. These
pass information or work together in a structured
concepts also apply to threads within a process.
manner. This is what we call interprocess
We will explore these topics further to understand
communication or IPC.
how processes and threads can work together
There are three main issues related to IPC: effectively.

1. Passing Information: One process needs a THREADS


way to send information to another
process. For example, in a pipeline of A thread is a unit of execution within a program
commands, the output of one command or process. It is like a mini-program or a small
should go to the next command. So, worker that can perform tasks independently.
processes should have a way to exchange Threads are a way to divide the work in a
information without interrupting each program so that multiple things can happen at
other's work. the same time. Each thread has its own set of
instructions (program), a register set (which stores
2. Avoiding Conflicts: When multiple information about the thread's current state), and
processes are doing critical tasks, we want a stack (a small space to keep track of what the
to make sure they don't interfere with thread is doing).
each other. Imagine if two processes both
try to use the same memory at the same The advantage of using threads is that they can
time. We need mechanisms to prevent work together, allowing for parallelism and
such conflicts and ensure that processes improved performance. Threads can perform
work independently and efficiently. different tasks simultaneously, making programs
more efficient and responsive. For example, in a
3. Sequencing Dependencies: Sometimes, web browser, multiple threads can handle tasks
processes have dependencies or specific like downloading web pages, rendering graphics,
order requirements. For example, if one and handling user input, all happening
process produces data and another process concurrently.
needs to print it, the printing process
should wait until the data is ready. We
Threads can communicate and synchronize with switch between each other and keep the progress
each other through shared memory or other going smoothly.
mechanisms provided by the operating system,
However, because threads work together and
such as locks or semaphores. This coordination
share things, they need to be careful not to
ensures that threads work together effectively and
interfere with each other. They have to
avoid conflicts when accessing shared resources.
communicate well and make sure they're not
However, it's important to note that threads also doing the same thing at the same time. It's like
introduce challenges. Since threads share the your friends need to talk to each other and make
same memory space, they must be careful not to sure they don't accidentally take the same Lego
interfere with each other's work or modify shared piece or build the same part twice.
data simultaneously, which can lead to data
So, in simpler terms, a thread is like a friend
corruption or inconsistent results. Proper
who helps you with a specific task. They can
synchronization mechanisms, such as locks or
work together with other friends (threads) at the
semaphores, are used to control access to shared
same time, sharing things and getting things done
resources and prevent race conditions.
faster. But they also have to be careful not to
In summary, a thread is like a small worker or interfere with each other and communicate well
mini-program within a program or process. It can to avoid any problems.
execute tasks independently and concurrently with
SINGLE THREADED PROCESS AND MULTI
other threads, sharing the same memory space
THREADED PROCESS
and resources. Threads allow for parallelism,
improved performance, and coordination among Single-Threaded Process :- Imagine you have a
different tasks. However, careful synchronization task to complete, like baking a cake, and you're
is necessary to avoid conflicts and ensure data the only one working on it. In a single-threaded
integrity. process, it's like you're doing the task all by
yourself. You follow a step-by-step process,
THREAD IN SIMPLER TERMS :- Imagine you
completing one task at a time before moving on
have a big task to complete, like building a Lego
to the next. You can't do multiple things at once.
model. Now, instead of doing it all by yourself,
For example, you mix the ingredients, bake the
you have a team of friends who can help you.
cake, and then frost it. Each step needs to finish
Each friend in your team is like a thread.
before you can move on to the next. It's a linear
A thread is like a friend who can work on a process where you can only do one thing at a
specific part of the task independently. They have time.
their own set of instructions (program) and a
Multi-Threaded Process :- Now, imagine you have
little space to keep track of what they're doing
some friends helping you with the cake-making
(stack). They can also share the same Lego pieces
task. Each friend has their own specific job, like
and tools (code and data) with other friends in
mixing the ingredients, baking the cake, or
the team.
frosting it. In a multi-threaded process, it's like
The good thing about threads is that they can you and your friends are working together as a
work together at the same time. For example, team. Each friend represents a separate thread,
while one friend is building the base of the Lego and each thread can work on a specific part of
model, another friend can work on the roof. They the task independently. For example, one friend
can also talk to each other and help if needed. can mix the ingredients while another friend
preheats the oven. Threads can execute tasks
Threads are faster to switch between than
concurrently and communicate with each other to
separate tasks, like if you were building the Lego
coordinate their actions. It's like everyone is
model alone and had to keep changing between
doing their part simultaneously, making the cake-
different tasks. With threads, they can quickly
• User-Level Threads: You and your friends
handle everything in the game without
help from adults. It's fast and flexible, but
may not have access to advanced features.
• Kernel-Level Threads: A teacher oversees
the game and ensures fairness. It may be
slower to manage, but has advanced
features and stability.

In simple terms, user-level threads are like


playing a game with friends without adult
supervision, while kernel-level threads are like
playing a game with a teacher overseeing it for
TYPES OF THREADS fairness and stability.
• USER LEVEL THREAD
A SEMAPHORE
• KERNEL LEVEL THREAD
Imagine you are in a game where you need to
User-Level Threads: Imagine you and your friends take turns playing with a special toy. To make
are playing a game, and you're in charge of sure everyone gets a fair chance, there is a
managing everything yourselves. User-level special object called a "semaphore" that helps
threads are like playing the game where you and control who can play with the toy at any given
your friends handle all the tasks without any help time.
from adults. You decide who plays what role,
The semaphore keeps track of how many people
how to communicate, and when to take turns.
can play with the toy. Let's say the semaphore
Everything related to the threads is managed
starts with a certain number, like 5. Each time
within the game itself, without involving any
someone wants to play with the toy, they have
outside authority. User-level threads are fast and
to ask the semaphore for permission. If there are
flexible because you have control over them, but
still available spots (let's say the semaphore is not
they may not have access to certain advanced
zero), they can take a turn and play. But if all
features.
the spots are taken (semaphore is zero), they
Kernel-Level Threads: Now, let's imagine a have to wait for their turn until someone finishes
different scenario where a teacher oversees the playing and gives the spot back.
game you and your friends are playing. Kernel-
When someone finishes playing, they let the
level threads are like playing the game under the
semaphore know by saying they are done. This
supervision of the teacher. The teacher manages
increases the number of available spots, and now
the game, ensures everyone follows the rules, and
someone who was waiting can take their turn.
takes care of scheduling and coordination. Kernel-
level threads rely on the operating system to In simpler terms, a semaphore is like a special
handle them. The operating system provides object that keeps track of how many people can
support for managing threads and ensures fairness do something at the same time. It helps make
and stability. Kernel-level threads may be slower sure everyone gets a fair turn and prevents too
to create and manage because they involve the many people from doing something all at once.
operating system, but they have access to
A semaphore is like a special variable that helps
advanced features and are more reliable.
manage access to shared resources in a multi-
To summarize: processing environment. There are two common
types of semaphores:
Counting Semaphores: Think of a counting RACE CONDITION
semaphore as representing the available resources,
like the seating capacity at a restaurant. For Imagine you have two or more people trying to
example, if a restaurant has a capacity of 50 use the same thing at the same time. They don't
people, the counting semaphore would start at have a clear rule or order to follow. This can
50. As people arrive and take up seats, the lead to a race condition.
counting semaphore decreases. When the For example, imagine two friends, Alice and Bob,
maximum capacity is reached (semaphore reaches sharing a toy car. Both of them want to play
zero), no more people can enter until someone with it and start pushing it at the same time. But
leaves and increases the counting semaphore, since they aren't coordinating their actions, they
making a seat available again. may push the car in different directions, causing
Binary Semaphores: Binary semaphores are like a it to go off course or even crash into something.
simple on/off switch, indicating whether a In a computer system, a race condition is similar.
resource is locked or unlocked. It has only two It happens when multiple processes or parts of a
states: 0 (locked) or 1 (unlocked). It's commonly program are trying to access and modify the
used for situations where only one process can same resource or data simultaneously, without
access a resource at a time. proper coordination. The timing and sequence of
To access a semaphore, we use two operations: these actions become unpredictable, just like in
the case of Alice and Bob with the toy car.
➔ wait(): This operation is used when a
process wants to access a resource. It's When a race condition occurs, the outcome of
like a customer arriving at a restaurant the program can become incorrect or unexpected.
and trying to get an open table. If there's It can lead to errors, crashes, or data corruption
an open table (counting semaphore is because the processes or parts of the program
greater than zero), they can take it and interfere with each other's actions.
use the resource. But if all tables are To avoid race conditions, programmers use
occupied (semaphore is zero), they must techniques like synchronization and locking
wait until a table becomes available. mechanisms. These techniques ensure that only
one process or part of the program can access the
➔ signal(): This operation is used when a shared resource at a time, preventing conflicts
process is done using a resource. It's like and maintaining proper order.
a customer finishing their meal and
leaving the table. The signal() operation In simpler terms, a race condition happens when
increments the semaphore, indicating that multiple processes or parts of a program try to
the resource is available again. use the same thing at the same time without
following proper rules. Just like in a real race,
In simpler terms, a semaphore is like a counter without coordination, the outcome can be chaotic
that keeps track of available resources. Processes and unpredictable. To prevent race conditions,
can use the wait() operation to request access to programmers use techniques to ensure that
a resource, and if it's available, they can use it. processes take turns using shared resources,
Otherwise, they have to wait. When a process is leading to more reliable and correct program
done using a resource, it signals that it's available execution.
again using the signal() operation. Semaphores
help ensure that resources are used in a CRITICAL REGION
coordinated and controlled manner.
Imagine you and your friend both want to play
with the same toy. To avoid fights or accidents,
you need to take turns and not play with the toy
at the same time. This is similar to what we call vary depending on the hardware
a critical region. configuration.

In a computer system, a critical region refers to a • To design a robust and efficient solution,
part of a program where multiple processes or it's important to create algorithms or
parts of the program may access shared mechanisms that work reliably, regardless
resources, like memory or files. To prevent of the number of processors or their
problems, we want to ensure that only one speeds. We should not make assumptions
process can use the shared resource at a time. about the underlying hardware's specific
characteristics to ensure compatibility and
To avoid issues like race conditions, we need a
effectiveness across different systems.
way to provide mutual exclusion, which means
making sure that only one process can use the • By not assuming anything about the speed
shared resource while others are excluded. It's or number of processors, we create
like having a rule that only one person can play solutions that are flexible and can work
with the toy at a time. optimally in various computer
environments. It allows the operating
The part of the program where the shared
system or program to adapt to different
resource is accessed is called the critical region
hardware configurations without relying
or critical section. Think of it as the special area
on fixed assumptions.
where you have to take turns to use the toy. If
we can ensure that no two processes are inside 3. A process that is not in its critical region
their critical regions simultaneously, we can should not block or prevent other
prevent race conditions. processes from doing their work.
4. No process should have to wait
To make sure processes cooperate correctly and
indefinitely to enter its critical region. We
efficiently using shared resources, we need to
want everyone to get a fair chance to
follow four conditions:
play with the toy.
1. No two processes can be in their critical
By following these conditions, we create a system
regions at the same time. It's like only
where processes take turns using shared
one person playing with the toy at a
resources, like playing with the toy, ensuring that
time.
everyone gets a chance without conflicts or
2. We can't assume anything about the speed
waiting forever.
or number of processors in the computer
system. So, in simpler terms, a critical region is like a
special area where processes or parts of a
When it says, "We can't assume anything program need to take turns using shared
about the speed or number of processors resources. We want to avoid conflicts and make
in the computer system," it means that sure everyone gets a fair chance, just like sharing
we should not rely on specific a toy with a friend.
assumptions about how fast the processors
in the system are or how many processors CPU SCHEDULER
there are. In an operating system, scheduling algorithms
• In a computer system, the number of help manage the allocation of resources, such as
processors (also known as CPUs) can vary, the CPU (Central Processing Unit), to different
and their speeds can differ as well. Some processes or tasks that are running on a
systems may have multiple processors, computer. Preemptive and non-preemptive
while others may have a single processor. scheduling are two types of algorithms used for
Additionally, the speed of processors can this purpose.
Non-preemptive Scheduling: Non-preemptive how much work is being done by the CPU. For
scheduling is like taking turns. Imagine you and example, if the CPU can complete 10 processes in
your friends are playing a game, and you decide a second, the throughput is 10 processes per
that each person will take a turn to play for a second. Higher throughput indicates a higher rate
certain amount of time before the next person of work being accomplished.
gets a chance. In non-preemptive scheduling, a
Turnaround Time: Turnaround time focuses on
process is given control of the CPU and is
the perspective of an individual process. It is the
allowed to run until it completes or voluntarily
time taken for a process to complete from the
gives up the CPU.
moment it is submitted. Turnaround time includes
For example, if you're watching a video on your waiting to enter memory, waiting in the ready
computer, the non-preemptive scheduling queue, executing on the CPU, and performing I/O
algorithm will let the video play until it finishes operations. We aim to minimize turnaround time
or until you decide to pause or stop it. Other to ensure processes are completed quickly.
processes will have to wait for the video to finish
Waiting Time: Waiting time refers to the time
before they can use the CPU.
spent by a process waiting in the ready queue,
Preemptive Scheduling: Preemptive scheduling is waiting for its turn to be executed by the CPU. It
like interrupting or cutting in line. Imagine you does not include the time spent executing or
and your friends are waiting in line for a ride, performing I/O operations. Minimizing waiting
and suddenly someone jumps in front of you time helps in improving overall process efficiency.
without waiting for their turn. Preemptive
Response Time: In interactive systems, response
scheduling allows a process to be interrupted and
time is crucial. It measures the time it takes from
temporarily paused so that another process can
submitting a request until the first response is
use the CPU.
produced. For example, in a web application,
For example, if you're working on a document response time is the time it takes for the system
and suddenly a high-priority task comes up, the to start showing results to the user. Lower
preemptive scheduling algorithm will pause your response time leads to better user experience and
work and allow the high-priority task to use the interactivity.
CPU. After the high-priority task is done, your
The goal is to maximize CPU utilization and
work will resume. This way, time-critical or
throughput while minimizing turnaround time,
important tasks can be handled promptly.
waiting time, and response time. By optimizing
SCHEDULING CRITERIA these criteria, we can enhance the efficiency and
performance of the system.
When it comes to choosing a CPU scheduling
algorithm, we consider different criteria to In simpler terms, the criteria for comparing
compare them. These criteria help us determine scheduling algorithms include keeping the CPU
which algorithm is the best fit for a particular busy, completing tasks efficiently, minimizing the
situation. Here are the criteria: time processes take to finish, reducing waiting
time, and providing fast response to user
CPU Utilization: We want to keep the CPU busy
requests. The aim is to make the best use of the
and productive as much as possible. CPU
CPU's time and resources.
utilization refers to how much of the CPU's time
is being used for executing processes. We aim for
high CPU utilization, ideally close to 100 percent,
to ensure efficient utilization of system resources.

Throughput: Throughput measures the number of


processes completed per unit of time. It tells us
SCHEDULING ALGORITHMS
The average waiting time is (0 + 24 + 27) / 3 =
17 milliseconds
1) FIRST COME FIRST SERVED
The average turnaround time = (24 + 27 + 30) /3
First-Come, First-Served (FCFS) Scheduling is a
= 27milliseconds
simple scheduling algorithm used in operating
systems. It works on the principle of serving In simpler terms, turnaround time represents the
processes in the order they arrive. entire duration that a process experiences from its
arrival in the system until its completion. It
In FCFS scheduling, the process that arrives first
includes the time spent waiting for execution in
is given the CPU first and is allowed to run until the ready queue, as well as the time spent
it completes or voluntarily gives up the CPU. The actively running on the CPU.
next process in the queue is then selected and
given the CPU, and this process continues until Turnaround Time = Completion Time - Arrival
all the processes have been executed. Time

FCFS scheduling is a non-preemptive scheduling The components of the formula are defined as
algorithm. Once a process starts running, it is not follows:
interrupted until it finishes or voluntarily releases • Completion Time: The time at which a
the CPU. This means that if a process with a process finishes execution.
longer execution time arrives before a process • Arrival Time: The time at which a process
with a shorter execution time, the longer process arrives in the system.
will continue to run, and the shorter process will
have to wait.

Process Burst Time Example 2 :- If the processes arrive in the order


P2 , P3 , P1 however , the results will be shown
P1 24
in the following Gantt chart
P2 3
P3 3 P2 P3 P1
0 3 6 30
If the process arrive in the order p1 , p2 , p3
and are served in FCFS order , we get the result The following table shows the Arrival time , start
shown in the following Gantt chart. time , waiting time , finish time and Turn
around time of the given processes.
P1 P2 P3
Process Burst Arrival Start Waiting Finish Turn
0 24 27 30 Time Time Time Time Time around
Time

p2 3 0 0 0 3 3
The following table shows the arrival time , start
time , waiting time , finish time and Turnaround p3 3 0 3 3 6 6
time of the given processes. p1 24 0 6 6 30 30

The average waiting time = (6 + 0 + 3) / 3 = 3


Process Burst Arrival Start Waiti Finish Turn
milliseconds
Time Time Time ng Time around
Time Time
The average turnaround time = (3 + 6 + 30) /3
p1 24 0 0 0 24 24
= 13 milliseconds
p2 3 0 24 24 27 27
This reduction is substantial. Thus, the average
p3 3 0 27 27 30 30
waiting time under a FCFS policy is generally not
minimal, and may vary substantially if the completion. The OS schedules the process with
process CPU-burst times vary greatly. the smallest total execution time first.

2) SHORTEST JOB SCHEDULING Shortest Remaining Time First (SRTF) is a


preemptive version of SJF. In SRTF, the OS
The shortest job scheduling algorithm is a type of always ensures that the process with the smallest
scheduling algorithm that selects the process with remaining execution time is the one running. If a
the smallest execution time from the list of new process arrives with a shorter remaining time
available processes. This type of scheduling is than the currently executing process, the current
very intuitive and is often used in batch systems process is suspended (preempted), and the CPU is
for minimizing job flow time. However, this given to the new process.
algorithm does not account for process priority, For example, if process P1 is currently executing
which can result in priority inversion or with an estimated remaining time of 5 units and
indefinite postponement (also known as a new process P2 arrives with an estimated total
starvation). execution time of 3 units, the operating system
The shortest job scheduling algorithm can be will suspend P1 and start executing P2 because it
categorized as: has a shorter remaining time. This is what we
mean by preemptive scheduling in the context of
1. Shortest Job Next (SJN) or Shortest Job the shortest job scheduling algorithm.
First (SJF): In SJN or SJF, jobs with the
NON-PREEMPTIVE EXAMPLE :-
shortest burst time are scheduled first.

2. Shortest Remaining Time First (SRTF):


SRTF is the preemptive counterpart of
SJF, where the process with the smallest
remaining burst time gets scheduled.

The SJF algorithm can be either preemptive or


non-preemptive.

In the context of CPU scheduling, the term


"preemptive" means that the operating system can
take control of the CPU away from a process
before it completes execution. This is typically
PREEMPTIVE EXAMPLE :-
done if another, higher priority process becomes
ready to execute or in a round-robin scheduling
scenario where each process is given a fair share
of the CPU time (quantum).

On the other hand, "non-preemptive" scheduling


means that once a process starts execution, it
continues until it completes, yields the CPU
voluntarily, or blocks for some reason (like I/O
operations).

Shortest Job scheduling algorithms can be either


preemptive or non-preemptive:

Shortest Job Next (SJN), also known as Shortest


Job First (SJF), is a non-preemptive scheduling
algorithm. Once a job starts execution, it runs to
3) PRIORITY SCHEDULING ALGORITHM interpreting priority numbers. Some systems use
low numbers (e.g., 0) to indicate high priority,
Priority Scheduling is a method of scheduling while others use low numbers to represent low
processes that is based on priority. In this priority. This discrepancy can lead to confusion
scheduling method, the scheduler selects the tasks when discussing priorities across different systems
to work as per the priority, which is different or contexts.
from other types of scheduling, for example,
To avoid ambiguity we assumed that low
simple round robin which treats all processes
numbers represent high priority. This means that
equally.
in the context our discussion, a process with a
Each process is assigned a priority. The process lower priority number would be considered as
with the highest priority is to be executed first having a higher priority for execution.
and so on. Processes with the same priority are
By establishing this convention, we can ensure
executed on a first-come, first-served (FCFS) or
consistency within its own framework. However,
round-robin basis. Priority can be decided based
it's important to note that other sources or
on memory requirements, time requirements, or
systems might adopt the opposite convention,
any other resource requirement.
where high numbers represent high priority.
There are two types of priority scheduling: Therefore, when working with different systems
or referring to other materials, it's crucial to
1. Preemptive Priority Scheduling: In
understand and follow the specific convention
Preemptive Priority Scheduling, if a new
they employ for interpreting priority numbers.
process arrives with a higher priority than
the current executing process, then the NON-PREEMPTIVE EXAMPLE
current process is put back into the ready
queue and the new process is scheduled
on the CPU. That means that if a high
priority process frequently arrives in the
system, the low priority process may
starve and never get the CPU for
execution.

2. Non-Preemptive Priority Scheduling: In


Non-Preemptive Priority Scheduling, a
process that is currently running on the
CPU will not be removed from the CPU,
PREEMPTIVE EXAMPLE
irrespective of the priority of other
processes in the ready queue. Once a
process is scheduled, it runs to
completion.

In computer systems, processes are often assigned


priorities to determine their order of execution.
Priorities are usually represented by a range of
numbers, such as 0 to 7 or 0 to 4095. However,
there is no universally agreed-upon standard for
whether a lower or higher number represents a
higher priority.

Different operating systems or scheduling


algorithms may adopt different conventions for
PROBLEM WITH PRIORITY SCHEDULING By gradually increasing the priority of waiting
processes, the aging technique ensures that low-
When using priority scheduling algorithms, a priority processes are not indefinitely blocked. It
significant problem that can occur is indefinite provides a fair opportunity for all processes to
blocking or starvation. This happens when a low- execute, regardless of their initial priority.
priority process is continuously unable to access
In simpler terms, indefinite blocking occurs when
the CPU because higher-priority processes keep
low-priority processes keep waiting for the CPU
taking up the available resources. In other words,
because higher-priority processes are always given
low-priority processes can be neglected and wait
priority. The solution of aging tackles this
indefinitely for their turn to use the CPU.
problem by gradually increasing the priority of
To address this issue, a solution called aging is waiting processes over time. This ensures that
introduced. Aging is a technique where the even processes with initially low priority
priority of processes that have been waiting in eventually get a chance to execute.
the system for a long time gradually increases
over time.
3) ROUND ROBIN ALGORITHM
Here's how aging works in simpler terms:
Round Robin (RR) is a pre-emptive scheduling
1. Waiting Processes :- When a process is
algorithm that is designed for time-sharing
ready to run but is waiting for the CPU,
systems. The name "round robin" comes from the
it is considered blocked. Low-priority
principle known as round-robin scheduling, where
processes may experience indefinite
each task is given an equal and fixed amount of
blocking because higher-priority processes
time (also known as quantum) in turn.
keep getting the CPU.
In simpler terms, imagine you are at a dinner
2. Gradual Priority Increase :- With the
table with friends and a dish comes out. You
aging technique, the priority of a waiting
each take turns serving yourselves a small bit of
process is slowly increased over time.
the food at a time, and the dish goes around and
Let's say we have a range of priorities
around the table until everyone is satisfied or the
from 0 (low) to 127 (high). We can
dish is empty. That's the round robin principle -
increment the priority of a waiting
everyone gets a little bit at a time in turns, until
process by 1 every 15 minutes.
all is done.
3. Aging Effect :- As time passes, the
Here's how the Round Robin scheduling algorithm
priority of the waiting process gradually
works:
rises. Even a process with an initial
priority of 0, which is the lowest priority, 1. Each process in the system is placed into
would eventually have the highest priority the ready queue.
in the system. This means that after a 2. The first process is removed from the
certain period of waiting, the low-priority ready queue and is set to run for a
process will finally get its chance to be specific amount of time, defined by the
executed. time quantum.
3. If the process finishes before or when the
4. Timeframe: In this example, it would take
time quantum expires, it is removed from
no more than 32 hours for a process with
the CPU and the next process in the
an initial priority of 0 to age to a priority
ready queue is set to run.
127 process. So, even if a process starts
4. If the time quantum expires before the
with the lowest priority, it will eventually
process finishes, the process is moved to
be given the highest priority after a
certain waiting period.
the end of the ready queue, and the next not available at that moment, the process enters
process in the queue is set to run. a waiting state until the resource becomes
5. This cycle repeats until all processes have available.
finished.
Now, deadlock occurs when multiple processes
One of the major challenges in round robin are waiting for resources that are held by other
scheduling is determining the length of the time waiting processes, and as a result, none of the
quantum. If the time quantum is too large, the processes can progress. It's like a traffic jam
benefits of the round robin principle (fairness, where all the cars are waiting for each other to
responsiveness) might be lost. If the time move, but no one can move because they are all
quantum is too small, the system could spend too blocked.
much time switching between tasks, which
To better understand deadlock, let's imagine a
decreases overall system efficiency. It's important
scenario with three tape drives and three
to strike a balance based on the specific needs of
processes. Each process initially holds one tape
the system.
drive. Now, if each process requests an additional
One advantage of round robin scheduling is its tape drive and those drives are already held by
simplicity and fairness - every process gets an other processes, we have a deadlock situation.
equal share of the CPU. However, it doesn't Each process is waiting for a resource (an
always provide the best average waiting time or additional tape drive) that can only be released
turnaround time, especially if the processes vary by one of the other waiting processes. None of
significantly in their CPU requirements. the processes can move forward because they are
all stuck waiting for something that won't
EXAMPLE 1 :-
happen.

In general, deadlock occurs when a set of


processes are stuck in a state where each process
is waiting for an event that can only be caused
by another process in the set. The events we are
concerned about here are resource acquisition and
release.

To deal with deadlocks, operating systems use


techniques such as resource allocation algorithms
and deadlock detection algorithms. These
techniques aim to prevent or resolve deadlocks by
EXAMPLE 2 :-
carefully managing resource allocation and
ensuring that processes can make progress.

Understanding deadlock is important in operating


systems because it helps system designers and
DEADLOCK administrators ensure that processes can run
smoothly without getting stuck in situations
In a computer system, multiple processes may where they cannot proceed due to resource
compete for a limited number of resources. These dependencies.
resources can include things like memory space,
Under the normal model of operation , a process
CPU cycles, files, printers, or other input/output may utilize a resource in only the following
devices. When a process needs a resource, it sequence :-
requests it. If the resource is available, the
process can use it. However, if the resource is
• When a process needs to use a resource, waiting for resources that are held by
it requests it from the operating system. other waiting processes.
If the requested resource is available, the
3. No Preemption: Resources cannot be
process can start using it immediately.
forcibly taken away (preempted) from the
However, if the resource is currently
processes that are currently holding them.
being used by another process, the
A resource can only be released
requesting process has to wait until it
voluntarily by the process that is currently
becomes available.
using it after it has completed its task.
• Once the process acquires the resource, it This condition ensures that a process
can utilize it to perform its designated cannot be interrupted or have its
task. For example, if the resource is a resources forcibly taken away by another
printer, the process can start printing process, which could potentially lead to
documents. During this phase, the process inconsistencies or incorrect results.
actively interacts with the resource and
4. Circular Wait: There must exist a circular
performs the necessary operations.
chain or cycle of processes, each of which
• After completing its work with the is waiting for a resource held by the next
resource, the process releases it back to process in the chain. In simpler terms,
the operating system. By releasing the there is a set of processes, {P0, P1, ...,
resource, the process indicates that it no Pn}, where P0 is waiting for a resource
longer needs it and allows other processes held by P1, P1 is waiting for a resource
to potentially acquire and use it. held by P2, and so on, until Pn is waiting
for a resource held by P0. This circular
dependency among processes creates a
DEADLOCK CHARACTERIZATION
deadlock situation where no process can
Necessary conditions for a deadlock to occur: proceed because each process is waiting
for a resource held by another process in
1. Mutual Exclusion: At least one resource in
the cycle.
the system must be held in a non-
sharable mode. This means that only one It's important to note that all four conditions
process can use the resource at a time. If must hold simultaneously for a deadlock to occur.
another process requests that resource If any one of these conditions is not satisfied, a
while it is already being used, the deadlock cannot arise. Additionally, the circular-
requesting process has to wait until the wait condition implies the hold-and-wait
resource becomes available. For example, condition, so these conditions are not completely
if a printer is being used by one process, independent.
another process must wait until the
Identifying and understanding these necessary
printer is released.
conditions for deadlocks helps in developing
2. Hold and Wait: There must be a situation strategies and techniques to prevent or resolve
where a process is holding at least one deadlock situations in operating systems.
resource and is waiting to acquire
RESOURCE ALLOCATION GRAPH
additional resources that are currently
being held by other processes. In other A resource-allocation graph is a directed graph
words, a process that already holds some that helps to describe and analyze deadlock
resources can request additional resources situations in a system. It consists of two sets of
while still retaining the resources it vertices: one set represents the active processes in
currently has. This condition can lead to the system (P = {P1, P2, ..., Pn}), and the other
a deadlock when multiple processes are
set represents the resource types in the system (R
= {R1, R2, ..., Rm}).

In the graph, a directed edge from a process Pi


to a resource type Rj, denoted as Pi→Rj, indicates
that process Pi has requested an instance of
resource type Rj and is currently waiting for that
resource. Conversely, a directed edge from a
resource type Rj to a process Pi, denoted as
Rj→Pi, signifies that an instance of resource type
Multiple Resource Instances: Now imagine you
Rj has been allocated to process Pi. Request
have a toy that many other kids also have. It's
edges point to the resource type nodes, while
like a popular toy that many people can play
assignment edges also indicate the specific
with at the same time. In the context of
instance of the resource that has been assigned.
deadlock, multiple resource instances mean there
Pictorially, processes are represented as circles, are several copies of that resource available in
while resource types are represented as squares. the system. It's like having many pieces of that
Resource instances are depicted as dots within toy. If there is a cycle involving this type of
the squares. resource, it suggests the possibility of deadlock,

The resource-allocation graph helps in identifying but it's not definite. More investigation is needed

deadlock situations based on its structure. Here to confirm if deadlock has actually happened.

are some important observations:

1. No Cycles: If the resource-allocation graph


does not contain any cycles, then there is
no deadlock in the system. Each process
can proceed with its execution, and
resources are being utilized effectively.

If the graph contains a cycle, it indicates a


potential deadlock situation. However, the
presence of a cycle is not sufficient to confirm a
deadlock. The implications depend on the
availability of resource instances.

Single Resource Instance: Imagine you have a toy To summarize:


that only exists in one copy. You can think of it
• Single resource instance: Only one piece
as a unique and special toy that no one else has.
or copy of a resource exists. If there is a
In the context of deadlock, a single resource
cycle involving this resource type,
instance means there is only one of that
deadlock is certain.
particular resource available in the system. It's
• Multiple resource instances: Many pieces
like having only one piece of that toy. If there is
or copies of a resource exist. If there is a
a cycle involving this type of resource, it means
cycle involving this resource type, it
deadlock is certain and the processes involved are
indicates a potential deadlock, but further
stuck and cannot move forward.
analysis is needed to confirm if deadlock
has occurred.

The resource-allocation graph serves as a useful


tool to identify potential deadlock situations by
analyzing the presence of cycles and the
availability of resource instances. It helps in lead to system paralysis and a complete halt in
understanding the relationship between processes progress.
and resources in the system, aiding in deadlock
prevention and resolution strategies. Understanding the different methods for handling
deadlocks helps in designing operating systems
METHODS OF HANDLING A DEAD LOCK that strike a balance between preventing
deadlocks, recovering from them, or dealing with
There are three different methods for dealing them when they occur. Each method has its own
with the deadlock problem: advantages and trade-offs in terms of system
complexity, resource utilization, and overall
Deadlock Prevention: Deadlock prevention aims to
system performance.
ensure that the system never enters a deadlock
state. It requires the use of protocols and DEADLOCK PREVENTION
strategies to eliminate one or more of the
necessary conditions for deadlock to occur. By As we have seen earlier , for a deadlock to occur
addressing these conditions, the system can , each of the four necessary conditions must hold.
prevent deadlocks from happening altogether. By ensuring at least one of these conditions can
This approach typically involves careful resource not hold , we can prevent the occurance of a
dead lock , let us elaborate on this approach by
allocation, avoiding circular wait, and ensuring
examining each of the four necessary conditions
that resources are not held indefinitely. The goal separetely.
is to proactively design the system in a way that
deadlocks cannot arise. Mutual Exclusion: Mutually exclusive is a
satistical term describing two or more events that
Deadlock Recovery: Deadlock recovery involves
can not happen sumultaneously. This condition
allowing the system to enter a deadlock state and
means that some resources cannot be shared by
then taking action to recover from it. This
multiple processes simultaneously. For example, if
approach acknowledges the possibility of deadlock
there is only one printer, only one process can
occurrence but provides mechanisms to detect
use it at a time. However, sharable resources,
deadlocks and resolve them. Techniques such as
such as read-only files, can be used by multiple
resource preemption (taking resources from one
processes simultaneously without causing
process to give to another) or process termination
deadlocks.
(ending one or more processes involved in the
deadlock) can be used to break the deadlock and Hold and Wait: To prevent the hold-and-wait
restore system functionality. Deadlock recovery condition, we can use different protocols. One
typically requires additional overhead and may approach is to require processes to request and
involve sacrificing processes or resources to be allocated all the resources they need before
resolve the deadlock. they start executing. Another approach allows a
process to request resources only when it has
Deadlock Ignorance: Some operating systems,
none. It can request and use resources but must
including UNIX, adopt the approach of ignoring
release them before requesting additional
the problem of deadlocks altogether. They assume
resources. These protocols aim to avoid situations
that deadlocks are rare events and focus on
where a process is holding resources and waiting
maximizing system performance and resource
for others, reducing the chance of deadlocks.
utilization.
No-Preemption: Preemption means taking
If a system does not employ deadlock prevention,
resources away from processes that are currently
avoidance, or recovery mechanisms, deadlock
holding them. To prevent this condition, a
situations may occur. In such cases, the system
protocol can be used. If a process is waiting for a
may be in a deadlock state without any means of
resource that cannot be immediately allocated, all
recognizing or resolving the deadlock. This can
the resources currently held by that process are
preempted (taken back). The process is then By implementing these prevention techniques, we
restarted only when it can regain its old aim to ensure that at least one of the necessary
resources and acquire the new ones it is conditions for deadlock does not hold, thus
requesting. preventing deadlocks from occurring. It involves
managing resource allocation, timing of requests,
Circular Wait: This condition can be prevented by
and the order in which resources are requested.
imposing a total ordering of resource types and
requiring processes to request resources in an It's important to note that these prevention
increasing order. Each resource type is assigned a protocols also have some disadvantages. They can
unique number, allowing comparison and lead to low resource utilization, as some
determination of their order. For example, if we resources may remain allocated but unused for a
have tape drives, disk drives, and printers, we long time. Additionally, there is a possibility of
assign numbers to them (e.g., tape drive = 1, starvation, where a process with multiple
disk drive = 2, printer = 3). Processes must resource requirements may have to wait
request resources in increasing order of these indefinitely because some of the needed resources
numbers. This way, circular wait, where are always allocated to other processes.
processes are waiting for resources in a circular
dependency, can be avoided.
RECOVERY FROM DEAD LOCK
Imagine you have different types of toys, such as
toy cars, dolls, and building blocks. To prevent a Process Termination: When a deadlock is
situation where kids are waiting for each other's detected, there are two ways to break the
toys and no one can play, we can assign a deadlock. One option is to inform the system
number to each type of toy. Let's say toy cars are operator and let them manually resolve the
number 1, dolls are number 2, and building deadlock. The other option is to let the system
blocks are number 3. automatically recover from the deadlock. One
way to do this is by aborting (stopping) one or
Now, when kids want to play with toys, we ask
more processes involved in the deadlock.
them to request toys in order of their numbers.
This means they can only request toy cars first • Abort all deadlocked processes: In this
(number 1), then dolls (number 2), and finally method, all the processes involved in the
building blocks (number 3). They must follow this deadlock are forcefully stopped. This
order when asking for toys. breaks the deadlock cycle, but it comes
with a cost. The processes may have been
By doing this, we make sure that kids are not
running for a long time and may have
waiting for toys in a circular way. For example,
completed some work. Aborting them
if a kid is playing with a toy car (number 1) and
means discarding their partial results and
wants a doll (number 2), they have to wait until
potentially needing to redo their
the doll is available. Another kid can have the
computations.
doll, but they cannot ask for a toy car (number
1) because it comes before the doll in the order. • Abort one process at a time: Instead of
This prevents a situation where kids are waiting aborting all processes at once, this
for each other's toys and no one can play. method aborts one process at a time until
the deadlock is resolved. After each
By having this resource ordering and making sure
process is aborted, a deadlock detection
kids request toys in the correct order, we can
algorithm is invoked to check if the
avoid the problem of circular wait and ensure
deadlock is still present. This method
that everyone gets a chance to play with different
incurs additional overhead because each
toys.
process needs to be checked, but it avoids
discarding all the work done by the CHAPTER THREE
processes.

Resource Preemption: Another way to resolve MEMORY MANAGEMENT


deadlocks is by preempting (taking back)
resources from processes involved in the deadlock
and reallocating them to other processes until the
ADDRESS BINDING
deadlock is broken.
Address binding refers to the process of
• Selecting a victim: To decide which connecting or linking addresses used in a
resources and processes to preempt, a program to specific locations in memory. Address
binding is like giving a special home address to
strategy is needed. The system must
things in a computer. It helps the computer know
determine which processes can give up
where to find and store information.
their resources without causing further
problems. Imagine you have a toy box with different toys
inside. Each toy has a special place where it
• Rollback: When a resource is preempted
belongs. For example, the toy car goes in one
from a process, a decision must be made
corner, the doll goes in another corner, and the
about what to do with that process. It
building blocks go in a different corner.
may need to roll back (undo) some of its
work to a previous state, as it can no Address binding in a computer is similar. When a
longer continue with the preempted program runs, it needs to find and store
resource. information in the computer's memory. The
computer gives each piece of information a
• Starvation: To prevent starvation (a
special address, just like each toy has a special
process being constantly preempted and
place in the toy box.
unable to make progress), mechanisms
need to be in place to ensure that all Address binding is important because it helps the
processes eventually get their required computer keep track of where things are stored.
resources and are not left waiting When the program needs to find information, it
indefinitely. can go directly to the correct address to get it.
And when the program wants to save new
These recovery methods aim to break the
information, it knows where to store it. Think of
deadlock and restore system functionality. They
it as giving each thing in the computer its own
involve stopping processes or taking back
home address, so the computer can find and keep
resources to resolve the circular dependencies
track of everything easily.
causing the deadlock. Both methods have their
own considerations in terms of cost, overhead, In the context of address binding, the term
and potential impact on the progress of processes . "binding" refers to the process of connecting or
associating a specific address with a particular
entity in a computer system. It establishes a
relationship between a symbolic representation of
an address (such as a variable name in a
program) and the actual physical or virtual
memory location where the data or instructions
are stored.

Binding ensures that when a program needs to


access information or instructions, it knows
exactly where to find them in memory. It "binds"
or links the symbolic representation of an address
to the actual location in memory where the data instructions that can be adjusted later. We call
resides. these instructions "relocatable code."

The purpose of binding is to enable efficient and Imagine you're going on a trip, but you don't
reliable access to data and instructions during know the exact address of your destination yet.
program execution. It helps the computer system You can still pack your bag with things you
accurately map and manage the memory might need and get ready to go. Similarly, the
resources required by programs, ensuring that the program prepares its instructions and data
right data is retrieved from the right place in without knowing the exact memory addresses.
memory.
When it's time to start the program, the computer
In simpler terms, binding is like creating a finds a suitable place in memory to load it.
connection or a link between a name (symbolic During this loading process, the computer figures
representation) and the place (memory address) out the final memory addresses where the
where something is stored. It helps the computer program will be stored. It's like finding the exact
system know where to find and store information, address of your destination before you start your
making it easier to access and manage data and journey.
instructions.
Once the program's instructions and data are
When a program is being prepared to run on a loaded into memory and their final memory
computer, its instructions and data need to be addresses are determined, the program is ready
assigned specific memory addresses. The process to run. It can access its instructions and data at
of assigning these addresses is called binding. the correct memory locations and perform its
There are three common stages where binding tasks.
can take place:
In simple terms, load time binding is like packing
Compile Time: If the computer knows exactly your bag without knowing the exact address, but
where the program will be stored in memory when you're about to start your trip, you find
before it even runs, the compiler (a special out the exact address and can start moving.
program) can generate instructions that directly Similarly, the program prepares its instructions
reference those memory addresses. It's like without knowing the exact memory addresses, but
creating a road map with fixed destinations. when it's time to run, the computer figures out
where to put the program in memory.
Load Time: If the memory addresses are not
known at compile time, the compiler generates Execution Time: In some cases, a program might
instructions that can be adjusted later. These need to move around in memory while it's
instructions are called relocatable code because running. This could be because the program is
they can be moved around. During the loading too big to fit entirely in one memory segment or
process, the final binding of memory addresses because the computer needs to optimize the
takes place. It's like waiting until you know the memory usage. In these situations, binding is
exact location of your destination before getting delayed until runtime, when the program is
directions. actually being executed. It's like deciding where
to go while you're on the road.
Let's simplify it:
Let’s simplify it
When a program is being prepared to run on a
computer, it needs to know where to find its Imagine you're playing with building blocks, and
instructions and data in memory. Sometimes, the you have a big tower you're building. But as you
program doesn't know the exact memory keep adding more blocks, the tower gets too tall
addresses in advance, so it uses special to fit on just one table. So, you decide to move
parts of the tower to other tables to make room once. Instead, we keep parts of the program on
for everything. the disk until they are needed.

In a computer program, sometimes the program Here's how it works:


itself is like that big tower. It's too big to fit
1. The main part of the program is loaded
entirely in one part of memory. So, while the
into memory and starts running.
program is running, it might need to move
2. As the program runs, it might come
around in memory to find enough space. It's like
across a part, called a routine, that it
the program is deciding where to go while it's
needs to use. Before loading the routine,
already running.
the program checks if it's already in
When a program moves around in memory memory.
during execution, the binding of addresses is 3. If the routine is not in memory, a special
delayed until that moment. The computer figures program called a relocatable linking
out the memory addresses for the program while loader is called. This program loads the
it's running. It's like making decisions about needed routine into memory from the
where to put the blocks while you're already disk.
playing with them. 4. Once the routine is loaded, the main
program can use it as needed.
This can happen for different reasons. Maybe the
5. Unused routines stay on the disk and are
program is really large and needs to spread out
never loaded into memory, saving
across different parts of memory. Or maybe the
memory space.
computer wants to optimize how it uses memory
to make things faster. In these cases, the program The advantage of dynamic loading is that we
decides where to go in memory as it goes along. only load the parts of the program that we
actually need, when we need them. This makes
In simple terms, execution time binding is like
better use of memory because we don't waste
rearranging your building blocks as you play with
memory space on parts of the program that are
them because your tower is getting too tall.
not being used.
Similarly, a program might need to move around
in memory while it's running because it's too big It's important to note that dynamic loading
or the computer wants to be efficient. doesn't require special support from the operating
system. It's up to the programmers and users to
The purpose of these different binding stages is to
design their programs in a way that takes
make sure that the program knows where to find
advantage of dynamic loading. They need to
its instructions and data in memory. It allows the
organize their programs into separate routines
computer to efficiently execute the program and
and use the relocatable linking loader when
manage memory resources effectively.
necessary.
DYNAMIC LOADING
In simpler terms, dynamic loading is like only
When we want to use a computer program, we taking out the toys or tools you need from a big
usually load the whole program into memory box instead of taking everything out at once. It
before we start using it. But what if the program saves space and makes things more efficient
is really big and has many parts that we might because you only load what you need when you
not need right away? It would take up a lot of need it.
memory space unnecessarily.
DYNAMIC LINKING
To solve this problem, we can use a technique
When we write computer programs, we often use
called dynamic loading. With dynamic loading,
libraries that have pre-existing code for common
we don't load the entire program into memory at
tasks. These libraries are like collections of useful
functions that we can use in our programs, just each program has its own copy of the toy
like using tools to build something. (function) it needs.

In most operating systems, when we finish But with dynamic linking, it's different. Instead of
writing our program, we combine it with the taking the toy out of the box, you keep it in the
library code to make a complete program. This is box and tell your friends that you want to use a
called static linking. It's like putting all the tools specific toy. When you want to play with it, you
we need inside our project, even if other people borrow it from the box and use it, and then put
are using the same tools for their projects. it back when you're done.

But dynamic linking works differently. Instead of This way, you and your friends can share the
including the library code in our program, we toys in the box without needing to have multiple
wait until the program is actually being used to copies. It saves space and makes it easier to find
link it with the library code. It's like borrowing the toy you want. That's how dynamic linking
tools from a shared toolbox when we need them. works with programs and libraries. Programs can
borrow functions from a shared library when they
Here's how dynamic linking works :-
need them and return them when they're done.
1. When we finish writing our program, we
So, dynamic linking is like sharing a big box of
tell the computer that we want to use
toys with your friends. You borrow the toys
certain functions from the library, like
(functions) you need from the box when you
asking for specific tools.
want to play with them and put them back when
2. When the program is run, the computer
you're finished. It helps save space and makes it
loads it into memory, just like opening a
easier for everyone to find and use the toys they
book to read.
need.
3. Now, our program needs to use the
functions from the library. Instead of The cool thing about dynamic linking is that
having its own copy of the library code, many programs can share the same library code,
the program uses a reference to find the like sharing tools with friends. This saves
functions it needs. memory because we don't need to copy the entire
4. The computer then finds the actual library library for every program. Without dynamic
code and connects it with our program, linking, each program would have its own copy
like getting the right tools from the of the library code, and that would be wasteful.
toolbox and putting them in our hands.
Dynamic linking is often used with libraries that
5. Finally, our program can use the library
have common functions that many programs use.
functions as if they were part of the
Instead of making a copy of the library for each
program itself, just like using the
program, we share it among them. When a
borrowed tools to do our work.
program needs a specific function, it gets it from
Let’s simplify it the shared library, just like borrowing a tool
from a shared toolbox and returning it when
Imagine you have a big box full of toys, and
we're done.
your friends also have the same box. Each toy is
like a function that helps you do something OVERLAYS
special, like a magic wand or a super-fast race
Imagine you have a very big puzzle with many
car.
pieces, but your table is too small to fit all the
Usually, when you want to play with a toy, you pieces at once. However, you still want to solve
take it out of your box and keep it with you the puzzle and see the complete picture. To
while you play. That's like static linking, where tackle this challenge, you divide the puzzle into
smaller sections, and you place one section on
the table at a time. As you work on one section and swap them as the program progresses. It's a
and need a different section, you swap the way to manage memory limitations and work
current section with the new one. This way, you with programs that are larger than the available
can solve the entire puzzle using the limited memory capacity.
space on your table.
LOGICAL VERSUS PHYSICAL ADDRESS SPACE
Now, let's relate this to computer programs. In
older computer systems with limited memory, Logical Address: Imagine you have a virtual map
programs could be larger than what could fit in that helps you find your favorite toys in a large
the available memory at once. This is where toy store. The map has numbers and symbols that
overlays come into play. Overlays are a technique guide you to where each toy is located. This
used to divide a program into smaller logical virtual map is like a logical address.
sections or overlays. In computer systems, a logical address is a virtual
Here's how overlays work in simpler terms: address generated by the CPU while a program is
running. It doesn't physically exist in the
1. Dividing the Program: Just like dividing a computer's memory. It's like a reference or code
puzzle, a program is divided into smaller that helps the computer know where to find
logical sections based on its functionality. specific information. The set of all logical
Each section contains a specific part of addresses generated by a program is called the
the program's code. logical address space.
2. Swapping Sections: The computer can Physical Address: Now, imagine you're in a real
only load one section into memory at a toy store where the toys are physically placed on
time due to limited memory capacity. As different shelves. Each toy has its own specific
the program execution progresses and location on a shelf. These physical locations
reaches a point where a different section represent the physical addresses.
is needed, the current section is swapped
out of memory, and the new section is In computer systems, a physical address is the
loaded. actual location of data in the computer's memory.
It's like the specific shelf and spot where a toy is
3. Manual Management: Overlays require placed in the toy store. While the program
manual programming and management. generates logical addresses, the physical addresses
The programmer needs to determine are computed by a hardware device called the
which sections are needed at different Memory Management Unit (MMU). The physical
points in the program and handle the address space is the collection of all physical
swapping of sections in and out of addresses corresponding to the logical addresses.
memory.
To summarize :-
4. Limited Memory Utilization: Overlays are
used when the entire program cannot fit • Logical Address: It's like a virtual map
into the available memory all at once. By that helps you find toys in a toy store.
dividing the program into smaller sections It's a reference used by the computer to
and swapping them as needed, memory locate information while a program is
can be effectively utilized. running.

So, overlays are like dividing a large puzzle into • Physical Address: It's like the actual
smaller sections and swapping them on and off location of toys on shelves in a toy store.
the table as you solve it. Similarly, in computer It represents the physical memory
programs, overlays divide a program into smaller locations where data is stored. The MMU
sections, load one section at a time into memory, computes the physical address
corresponding to a logical address.
In simpler terms, a logical address is like a map Swapping requires a special place called a
guiding the computer to find information, and a backing store, usually a fast disk, to store the
physical address is the actual location where the programs temporarily. The computer keeps track
information is stored in the computer's memory. of which programs are in memory and which are
in the backing store. When the CPU scheduler
wants to run a program, it checks if it's in
SWAPPING
memory. If not, and there's no free memory
space, it swaps out a program from memory and
When a computer is running many programs at
swaps in the desired program.
the same time, it needs to manage the memory
efficiently. Swapping is a technique used to It's important to note that swapping takes some
temporarily move a program out of memory and time because programs need to be moved in and
store it on a special place called a "backing out of memory. This switching time is called the
store," like a fast disk. The program can then be context switch time and can affect the overall
brought back into memory later to continue performance of the system.
running.
Contiguous Allocation
Here's how swapping works:
In a computer's main memory, we need to
1. Imagine a computer that can run multiple
allocate space for both the operating system and
programs simultaneously, like a round-
user processes. One way to do this is by using
robin game where everyone gets a turn.
contiguous allocation, which means that each
2. Each program gets a limited amount of
process is placed in a single, uninterrupted
time called a "quantum" to run on the
section of memory.
CPU. When the quantum is up, the
program is temporarily moved out of To manage this allocation, the memory is divided
memory. into two partitions: one for the operating system
3. The memory manager takes the program and one for the user processes. In our discussion,
that just finished and swaps it out to the let's assume that the operating system resides in
backing store, freeing up memory space. the low memory area.
4. Meanwhile, the CPU scheduler gives a
In contiguous allocation, each process is assigned
turn to another program that is already in
a specific section of memory that is large enough
memory.
to accommodate its needs. To keep track of
5. The swapped-out program can stay in the
where each process is located, we use two special
backing store until it's time for its turn
registers: the base register and the limit register.
again. Then it's swapped back into
memory for continued execution. The base register points to the smallest memory
6. This swapping process continues, with address of a process, while the limit register
programs taking turns in memory while indicates the size of that process's memory
others are swapped out. section. Together, these registers define the
boundaries of the process in memory.
Swapping is also used in priority-based
scheduling, where higher-priority programs get For example, let's say we have three processes:
priority for CPU time. If a high-priority program Process A, Process B, and Process C. Each process
needs to run, the memory manager can swap out is assigned a contiguous section of memory. The
a lower-priority program to make room for it. base and limit registers for each process specify
When the high-priority program is done, the the starting address and the size of their memory
lower-priority program can be swapped back in. sections.
Using these registers, the computer can easily Single Partition Allocation
locate and access the instructions and data for
each process when needed. Imagine that the computer's memory is like a big
playground. In this playground, we have two
Contiguous allocation allows multiple processes to
areas: one area for the operating system and one
coexist in memory at the same time, making
area for the user programs.
efficient use of the available memory space.
However, it also means that processes need to be The operating system is like the supervisor of the
assigned continuous blocks of memory, which can playground, and it wants to make sure that
limit the flexibility and efficiency of memory everything is running smoothly. It stays in the
utilization. low part of the playground, like the ground floor
of a building. The user programs, on the other
Imagine you have a big room called the
hand, stay in the high part of the playground,
computer's memory. In this room, you need to
like the upper floors of a building.
keep the operating system and different programs.
To do this, you divide the room into two parts: To protect the operating system from any changes
one for the operating system and one for the or problems caused by the user programs, we use
programs. a special system. We have two special guards
called the relocation register and the limit
Now, let's focus on the part for the programs.
register. The relocation register tells us where the
Each program needs its own space to stay in the
operating system starts in the memory, and the
memory. In contiguous allocation, we give each
limit register tells us how much space it takes.
program a special area that is like its own little
house. This house is a single piece of space in Whenever a user program wants to access the
the memory, without any gaps in between. memory, we check if the address it wants to use
is within the allowed range set by the relocation
To remember where each program's house is, we
and limit registers. If it is, we allow the program
use two special signs. One sign tells us the
to access that part of the memory. This way, we
starting address of the house, and the other sign
make sure that the program can't go beyond its
tells us how big the house is. With these signs,
allowed area and cause any trouble for the
we can find each program's house easily.
operating system or other programs.
For example, let's say we have three programs:
When a new program is selected to run by the
Program A, Program B, and Program C. Each
computer, we have a person called the dispatcher
program has its own house in the memory. The
who prepares everything. The dispatcher sets the
signs tell us where each house starts and how
relocation and limit registers to the correct values
much space it takes.
for that program. This helps to protect the
This way, when we need to run a program or do program and keep it within its allocated memory
something with it, we know exactly where to space.
find it in the memory.
One important thing to note is that this system
Contiguous allocation allows many programs to allows the operating system to change its size
live in the memory together, making good use of dynamically. This means that the size of the
the available space. But it also means that operating system can grow or shrink as needed.
programs need to have one continuous block of For example, if a certain part of the operating
space, which can sometimes limit how efficiently system is not being used, it can be removed from
we can use the memory. the memory to make space for other things. This
flexibility helps to optimize the memory usage
and make the best out of it.
So, in simple terms, we use the relocation and
limit registers to make sure that the operating
system and user programs stay in their designated
areas in the memory. This protects them from
causing problems for each other. The system also
allows the operating system to change its size
dynamically to make the most efficient use of the
memory.

Let’s simplify it

Imagine you're playing in a big playground.


There are different areas for different things. One
area is for the grown-ups who make sure
everything runs smoothly, like the playground
supervisors. They stay on the ground floor.
Another area is for kids like you, who play on
the upper floors.

To keep things organized, we have special guards Multiple Partition Allocation


called relocation and limit registers. These guards
help make sure everyone stays in their own play In a computer system, we have a limited amount
area. When a kid wants to play, we check if they of memory available to store programs and data.
are going to the right part of the playground. To make the best use of this memory, we divide
The relocation register tells us where the grown- it into fixed-sized sections called partitions. Each
ups' area starts, and the limit register tells us partition can hold one program or process.
how much space they have. When a new process needs memory, the
When a new kid comes to play, we have operating system looks for a partition that is big
someone called the dispatcher who sets enough to accommodate it. If there is an
everything up. They make sure the relocation and available partition, the process is loaded into it.
limit registers are set correctly for each kid. This When a process finishes or terminates, the
helps keep everyone safe and prevents them from partition becomes available again for another
going where they shouldn't. process to use.

The cool thing is that the grown-ups' area can Initially, all the memory is considered as one
change its size if needed. If they have extra space large block called a hole. As processes come and
that's not being used, they can make it smaller go, the memory is divided into different-sized
and let the kids use it. This way, everyone gets holes. When a process arrives and needs memory,
to play and have fun! the operating system searches for a hole that is
large enough to hold it. If the hole is larger than
So, in simple words, the relocation and limit
needed, it is divided into two parts: one part is
registers are like guards in the playground. They
allocated to the arriving process, and the other
make sure the grown-ups and kids stay in their
part becomes a new hole. When a process
own areas. And the grown-ups can change the
finishes, its memory block is released and
size of their area to share it with the kids.
returned to the set of holes. If the newly released
memory is adjacent to other holes, they can be
merged to form a larger hole.

When we have different holes (spaces) in the


memory, we need to decide which hole to use
for a new program. There are three ways to sections, each with its own designated space for
choose a hole: toys. This means that each child (or process) can
have their own separate space to keep their toys.
First-fit: Imagine you have a set of blocks (holes)
They don't have to share the same space with
in different sizes. With first-fit, you pick the first
other children.
block that is big enough to fit the program. You
don't keep searching for a better fit once you find So, the main difference between single and
a suitable block. multiple partition allocation is the number of
dedicated spaces available for the toys (or
Best-fit: In this case, you want to find the
processes). In single partition allocation, there is
smallest block that can accommodate the
only one shared space, while in multiple partition
program. You may need to look through all the
allocation, there are multiple separate spaces.
blocks to find the smallest one, unless they are
arranged from smallest to largest. The goal is to FRAGMENTATION IN OPERATING SYSTEM
minimize the amount of leftover space (empty
space). Fragmentation refers to the phenomenon where
available memory in a computer system is
Worst-fit: Here, you choose the largest available
divided into small, non-contiguous pieces, making
block, regardless of whether it is bigger than
it difficult to allocate large enough memory
what the program needs. This strategy can create
blocks for processes or programs. There are two
bigger leftover space, which might be useful for
types of fragmentation: external fragmentation
future programs.
and internal fragmentation.
So, in summary:
External fragmentation occurs when the free
• First-fit: Choose the first suitable hole. memory space is broken into small, scattered
• Best-fit: Pick the smallest hole that fits. pieces, even though the total amount of free
• Worst-fit: Select the biggest hole memory may be sufficient to satisfy a request. It
available. is like having a jigsaw puzzle with many small
pieces that don't fit together neatly. External
fragmentation makes it challenging to find a large
DIFFERENCE BETWEEN SINGLE AND MULTIPLE
PARTITION ALLOCATION enough continuous block of memory for a
process, leading to inefficient memory utilization.
Let's imagine we have a room (which represents
Internal fragmentation, on the other hand,
the computer's memory) where we want to
happens within a partition allocated to a process.
accommodate different toys (which represent
It occurs when the allocated memory is slightly
different processes or programs running on the
larger than what the process actually needs,
computer).
leaving behind unused and wasted memory
In single partition allocation, the room is divided within that partition. It's like having a container
into two sections: one section for the toys that that is slightly bigger than what you need,
belong to the room itself (like the room's own resulting in some empty space inside.
toys) and another section for the toys that are
Fragmentation occurs as processes are loaded and
brought in by children (like the toys of the user
removed from memory, leading to small gaps or
processes). The important thing to note here is
wasted space.
that there is only one dedicated space for the
children's toys, and they need to share that To address external fragmentation, one solution is
space. compaction, which rearranges the memory
contents to place all the free memory together in
On the other hand, in multiple partition
one large block. This helps reduce the scattered
allocation, the room is divided into multiple
small holes of free memory, making it easier to To keep track of where each page is located in
find contiguous blocks for new processes. memory, there is a special table called the page
table. Think of it as a map that tells you which
For internal fragmentation, allocating slightly
pages are stored in which frames. When the
larger memory blocks than requested helps
program needs to access a specific part of its
minimize the overhead of managing very small
memory, the CPU uses the page number and an
holes of free memory. This means that there may
offset to look up the page's location in the page
be some unused memory within a partition, but
table. This tells the computer's memory unit
the difference between the allocated memory and
where to find the actual data in physical
the requested memory is kept relatively small.
memory.
In simpler terms, fragmentation is like having
So, in simpler terms, paging is like storing Lego
scattered puzzle pieces or empty spaces in a
blocks on a shelf and bringing them out as
container. It makes it difficult to find a big
needed. It allows programs to use memory that is
enough space for something or results in some
not necessarily located in one continuous block.
wasted space. Compaction helps put the scattered
Instead, the memory is divided into fixed-sized
memory pieces together, while allocating slightly
pages, and the computer keeps track of where
more memory than needed minimizes wasted
each page is stored using a page table.
space inside memory partitions.

Solution to Fragmentation
• Compaction (already seen it)
• Paging
• Segmentation

PAGING

Imagine you have a big box of Lego blocks. The


blocks are of the same size, and you also have a
special shelf to store them. Now, when you want
to build something with the blocks, you don't
need to have all the blocks out on the table at
Imagine you have a big library with lots of
once. Instead, you can keep some blocks on the
books. Each bookshelf in the library can hold a
shelf and bring them out as you need them. This
fixed number of books, let's say 8 books per
way, you can work with a limited number of
shelf. The library also has a catalog that tells you
blocks at a time without cluttering up your
which book is on which shelf.
workspace.
Now, when you want to read a book, you don't
Paging is a similar concept in a computer.
need to bring the entire library to your reading
Instead of having all the parts of a program in
table. Instead, you can look up the catalog to
memory at once, the program's memory is
find the shelf number where the book is located,
divided into fixed-sized blocks called pages, just
and then you can take that book from the shelf
like the Lego blocks. The physical memory of the
and read it.
computer is also divided into blocks called
frames, which are the same size as the pages. In computer systems, paging works in a similar
way. The computer's memory is like the library,
When a program is running, its pages are loaded
and the books are the pages of a program or
into any available frames in the computer's
process. Each page has a fixed size, let's say 4
memory. It's like taking out the necessary Lego
pages per book. The computer also has a catalog
blocks from the shelf and placing them on the
called the page table that tells you which page is
table when you need them.
stored in which part of memory.
When a program is running, it doesn't need to position within that segment. This combination of
have all its pages in memory at once. Just like segment number and offset is used to access a
you don't need all the books from the library on specific byte in memory.
your reading table. Instead, the program's pages
To keep track of where each segment is in the
are loaded into available slots in the computer's
physical memory, the computer uses a table
memory. Each slot can hold one page, just like a
called a segment table. This table contains pairs
bookshelf can hold one book.
of information: the base address and the limit of
To access a specific part of the program's each segment. The base address tells the
memory, the CPU uses a logical address. This computer where a segment starts in memory, and
logical address consists of a page number and an the limit specifies the size of the segment.
offset. The page number tells you which page the
When a program wants to access a specific byte
data is on, just like the catalog tells you which
in memory, it uses the segment number and
shelf the book is on. The offset tells you the
offset to find the corresponding segment in the
specific location within the page, just like the
segment table. From there, the computer can
page number on the book tells you the specific
calculate the physical address of the desired byte
chapter or page.
by adding the offset to the base address of the
So, in simpler terms, paging is like having a segment.
library with lots of books, where each book is
Segmentation allows programs to have different-
divided into fixed-sized pages. The computer's
sized segments and provides a more flexible way
memory is like the bookshelves, and the page
of managing memory. It aligns with how users
table is like the catalog that tells you where each
and programmers think about memory, as
page is stored. When a program needs to access
separate and distinct groups rather than a linear
its memory, the CPU uses a logical address,
sequence of bytes.
which consists of a page number and an offset,
to find the corresponding page in memory. Example :-

SEGMENTATION

Imagine you have a big toy box filled with


different toys. When you want to play with a
specific toy, you don't think about its position in
the box as a linear order of toys. Instead, you
think of the toys as separate groups or segments
based on their type, such as dolls, cars, or
building blocks.

In a computer, segmentation is a way of


organizing memory that matches this way of
thinking. Instead of considering memory as a long For example, let's say you want to find byte
sequence of bytes, we divide it into different number 53 in segment number 2. You look at the
segments or groups. Each segment can have a segment table, which tells you that segment 2
specific purpose, like storing instructions or data. starts at memory location 4300 (base address) and
is 400 bytes long (limit). To find byte 53, you
When a program runs, it is divided into
add 53 to the starting location (4300 + 53), and
segments, just like the toys are divided into
you get the memory location 4353.
groups. Each segment has a segment number that
identifies it and an offset that specifies the
Similarly, if you want to find byte number 852 in • Offset: Represents the position or location
segment number 3, you look at the segment table of a byte within the segment.
and find that segment 3 starts at memory • Base address: Marks the starting memory
location 3200. You add 852 to 3200, and you get location of a segment.
the memory location 4052.

But it's important to be careful! If you try to Difference between paging and
access a byte that is outside the limit of a segmentation
segment, like byte 1222 in segment 0, it would
cause a problem. The segment table tells you that Segmentation and paging are two different
segment 0 is only 1000 bytes long, so trying to memory management techniques used in
access byte 1222 would be an error, and the operating systems. Here's a comparison between
computer would notify the operating system. the two:
So, segmentation is like having different boxes for Segmentation:
storing information, and each box has a number.
• In segmentation, the logical address space
To find something inside a box, you need to
of a process is divided into variable-sized
know the box number and the position of what
segments, each representing a different
you're looking for.
part of the program (such as code
In the example provided, the segment number, segment, data segment, stack segment,
offset, and base address can be identified as etc.).
follows :- • Each segment has its own starting address
• Segment number: The segment number is and length.
the identifier for a specific segment or • Segments can vary in size and can be
box of memory. In the example, segment allocated and deallocated dynamically as
numbers are mentioned as "segment 2," needed.
"segment 3," and "segment 0." Each • Segmentation provides a flexible memory
segment represents a different area of allocation scheme that allows processes to
memory. grow or shrink based on their memory
requirements.
• Offset: The offset refers to the position or • It allows for logical separation of different
location of a specific byte within a parts of a program and provides
segment. It tells us how far into the protection between segments.
segment we need to go to find the • Segmentation may lead to external
desired byte. For instance, in the fragmentation, where free memory
example, byte numbers like "53," "852," becomes scattered in small chunks,
and "1222" represent the offset. making it harder to allocate contiguous
• Base address: The base address is the blocks of memory.
starting memory location of a segment. It Paging:
tells us where the segment begins in
physical memory. In the example, the • In paging, the logical address space of a
base addresses for segments are mentioned process is divided into fixed-sized blocks
as "location 4300" and "3200." called pages.
• Physical memory is divided into fixed-
To summarize: sized blocks called frames.
• Segment number: Identifies the specific • Pages and frames are of the same size,
segment or box of memory. and each page can be mapped to any
available frame in physical memory.
• Paging provides a uniform and fixed-size used. When the computer needs to work on
memory allocation scheme. something, it takes out the necessary parts from
• It eliminates external fragmentation the bookshelf and puts them in its memory
because each page can be placed (which is like your table). If it needs more space,
anywhere in physical memory as long as it can swap out some parts to make room for
a free frame is available. new ones.
• Paging introduces the concept of a page
By using virtual memory, the computer can run
table, which is used to map logical
big programs and work on large projects, even if
addresses to physical addresses.
it doesn't have a lot of memory. It can easily
• Paging requires hardware support,
switch between different tasks and use its
specifically a memory management unit
resources more efficiently.
(MMU) that handles the translation of
logical addresses to physical addresses. So, virtual memory is like a magical bookshelf
that helps the computer handle big tasks by
In summary, segmentation divides the logical
storing parts of them when they are not needed,
address space into variable-sized segments, while
and bringing them back when they are. It's a
paging divides it into fixed-sized pages.
clever way to make the most of limited space
Segmentation allows for flexible memory
and keep everything running smoothly.
allocation, while paging provides a more uniform
memory allocation scheme. Both techniques have
their advantages and trade-offs, and their choice
Here's a simplified explanation of virtual memory:
depends on factors such as the system
requirements and hardware capabilities. In a computer, programs and data are stored in
memory for the CPU to access and execute.
However, the physical memory of a computer is
VIRTUAL MEMORY limited, and sometimes programs are too big to
fit entirely in memory. Virtual memory solves this
Imagine you have a small table in your room
problem by using a portion of the hard disk as
where you can do your homework. But
an extension of the physical memory.
sometimes your homework is too big to fit on the
table. So what can you do? You can use a Virtual memory works by dividing programs into
special trick called virtual memory. smaller chunks called pages. These pages are
loaded into the physical memory only when they
Virtual memory is like having a magical
are needed. When a program tries to access a
bookshelf that can hold all your books. When
page that is not currently in the physical
you need to work on a big project, you take out
memory, a process called demand paging is used.
a few pages from the bookshelf and put them on
The operating system brings the required page
your table. These pages represent the parts of
from the hard disk into the physical memory and
your project that you are currently using.
then allows the program to access it.
If you need more pages, but your table is full,
This approach has several advantages:
you can put some of the pages back on the
bookshelf and take out new ones. This way, you 1. Larger Programs: With virtual memory,
always have enough pages on your table to do programs can be larger than the physical
your work, even if the project is really big. memory. They can be divided into pages,
and only the necessary pages are loaded
Virtual memory helps your computer do the same
into memory as needed.
thing. It has a special space on the hard disk
called the bookshelf, which can store parts of 2. Efficient Memory Usage: Virtual memory
programs and data that are not currently being allows multiple programs to share the
same physical memory. Each program is DEMAND PAGING
given a portion of the memory, and as
pages are swapped in and out, the Imagine you have a big box filled with your toys,
physical memory is used more efficiently. but you don't have enough space to take them all
out and play with them at once. So instead, you
3. Increased Performance: Virtual memory
decide to take out only the toys you want to play
reduces the need for constant swapping of
with right now, and leave the rest in the box.
entire programs in and out of memory.
This improves overall performance and Demand paging is similar to this idea. When a
allows more programs to run program is running on a computer, instead of
simultaneously, increasing the utilization loading the entire program into memory at once,
of the CPU. the computer only brings in the parts of the
program that are needed at the moment. It's like
4. Flexibility: Certain parts of a program,
taking out the specific pages of a book that you
like error handling routines or rarely used
want to read, rather than carrying the entire
features, don't need to be loaded into
book with you.
memory until they are actually required.
This saves memory space and improves By using demand paging, the computer can save
efficiency. a lot of memory space because it doesn't need to
load everything at once. It only brings in the
pages of the program that the user wants to use.
This helps the computer work more efficiently
and use its resources wisely.

Page Fault :- Imagine you have a special


bookshelf where you keep all your books. When
you want to read a book, you take it from the
bookshelf and start reading. But what if the book
you want is not on the bookshelf? That's when
you need to go to a storage room where you
keep the rest of your books.

In computer terms, a page fault is like not


finding a book you want to read on the
In simpler terms, virtual memory is like having a bookshelf. It means that the computer doesn't
big workspace where you can keep all your books have a particular part of the program you're
and papers. You can't fit all of them on your using in its memory. So, it needs to go to
desk at once, so you put some on a nearby shelf. another storage place (like a hard disk) to find
When you need a specific book, you take it from that missing part, called a page, and bring it into
the shelf and place it on your desk. If you need memory.
another book that is not on your desk, you put
While the computer is fetching the missing page,
one of the books back on the shelf to make
the program might have to wait for a short
room. This way, you can work with many books
while. It's like you waiting for someone to bring
without cluttering your desk. Virtual memory
you the book you wanted to read. Once the
works in a similar way, allowing computers to
missing page is brought into memory, the
handle large programs without running out of
program can continue running smoothly without
memory.
any interruptions.

So, a page fault is when the computer needs to


go to another place to find a missing part of a
program, and it may cause a brief delay in the All the information about files is organized and
program's execution. It's similar to you waiting stored in a directory structure, which is like a
for a book to be brought to you before you can digital filing cabinet. Each file has its own entry
continue reading. in the directory, containing its name and unique
identifier.

So, a file is a named collection of information


CHAPTER FOUR stored on a computer, and it has attributes that
describe its properties, such as its type, size,
FILE SYSTEMS location, and access permissions. The directory
structure keeps track of all the files, just like a
Imagine you have a collection of different things, filing cabinet helps you keep track of your
like books, toys, and drawings. To keep them belongings.
organized, you give each item a name, like "My FILE OPERATIONS
Favorite Book," "Teddy Bear," or "My Artwork."
These names help you and others easily identify Creating a file: When we want to create a file,
and find the items you're looking for. the computer needs to do two things. First, it

In computers, a file is similar to one of those looks for a suitable place in its memory to store

named items. It's a way to store information, the file. Then, it adds an entry in a special list

such as text documents, pictures, or music. Just called the directory, which keeps track of all the

like you have names for your items, a file also files. This way, the computer knows the file

has a name that helps humans recognize and exists and where it is stored.

access it. Writing a file: Imagine you want to write

A file has certain characteristics or attributes that something down in a notebook. To do that on a

describe it. These attributes include: computer, you use a special command that tells
the computer the name of the file you want to
• Name: The human-readable name that write to and the information you want to write.
helps identify the file. It's like giving instructions to the computer to
• Identifier: A unique number or tag save your words or data in the file.
assigned to the file by the computer
system. Reading a file: Just as you can read from a book

• Type: The type of file, such as a or a paper, you can also read from a file on a

document, image, or video. computer. When you want to read a file, you use

• Location: The specific place where the file a command that tells the computer the name of

is stored on the computer's storage the file you want to read from and where in the

devices, like a hard disk. computer's memory you want the information

• Size: The current size of the file, from the file to be stored. The computer then

measured in bytes, which determines how brings that information into the memory for you

much space it occupies. to access.

• Protection: Information about who can Repositioning within a file: Let's say you have a
access the file, read it, modify it, or big book with many pages. Sometimes you want
execute it. to go directly to a specific page without reading
• Time, date, and user identification: Details everything from the beginning. Similarly, when
about when the file was created, last working with files on a computer, you can use a
modified, and last accessed, which can command to tell the computer to find a particular
help with security and tracking usage. part of a file. The computer searches the
directory for the right file and moves a pointer
called the current-file-position pointer to the is used to open a file. It searches for the file in
specified location within the file. It's like telling the directory and copies its information into the
the computer to jump to a specific page in the open-file table. This way, we can access the file
book. directly without searching for it every time.

Deleting a file: To delete a file, we search the Once a file is open, several pieces of information
directory for the named file. Having found the are associated with it :-
associated directory entry, we release all file
• File pointer: This keeps track of the
space, so that it can be reused by other files, and
current position in the file, so we know
erase the directory entry.
where to read from or write to next.
Truncating a file: Truncating a file is like erasing • File-open count: This counts how many
all the content inside it, but keeping the file's times the file has been opened by
other information the same. It's a bit like having different processes. When this count
a notebook with things written in it and deciding reaches zero, meaning no process is using
to clear all the pages while keeping the cover the file, it can be removed from the
and other details the same. table.
• Disk location: This information tells us
When you truncate a file, you are essentially
where the file is stored on the disk. It
removing all the data or text inside the file,
helps the system quickly access the file's
making it empty. However, the file's name,
data without repeatedly reading it from
location, permissions, and other attributes remain
the disk.
unchanged. It's a way to start fresh with the file,
• Access rights: Each process opens a file
as if it were brand new, but without the need to
with specific access rights, determining
delete and recreate the file from scratch.
what operations it can perform on the
Think of it as taking a notebook and erasing file. This information is stored in a table
everything you wrote on the pages, but the for each process, allowing the operating
notebook itself still has the same cover, title, and system to allow or deny further I/O
other information. It's a way to keep the file's requests.
structure intact while removing its contents.
When we are done working with a file, we close
These are the basic operations performed on files: it. Closing a file removes its entry from the open-
creating a file, writing to a file, reading from a file table, indicating that we no longer need to
file, and repositioning within a file. Each access it actively.
operation has its own purpose and involves
Think of it like having a library card to borrow
different commands or instructions that the
books. When you want to read a book, you first
computer understands.
show your library card, which has all the
When we work with files, there are many necessary information about you and the book
operations we can perform, such as reading, you want to read. Once you're done reading the
writing, creating, and deleting files. To make book, you return it, and the library removes the
these operations faster, the operating system book's details from your library card.
keeps track of information about open files in a
FILE TYPES
small table called the open-file table. This table
helps the system find the necessary details about
When we work with files, we often come across
a file without constantly searching through the different types of files, such as documents,
directory. images, videos, and music. To help us identify
the type of a file easily, many systems use a
When we want to perform operations on a file,
naming convention that includes a file extension.
we first need to open it. The open() system call
The file extension is a part of the file name that specific order, one record after another. It is like
comes after a period (.) character. reading a book from start to finish, where you
read each page in order without skipping any
For example, let's say we have a file named
pages.
"mydocument.docx". Here, "mydocument" is the
name of the file, and "docx" is the file extension. In sequential access, a read operation reads the
The file extension tells us that this file is a next portion of the file in the order it was
document and it is associated with a specific written. Each time we read, a file pointer is
program or file format, in this case, Microsoft automatically moved to the next position, keeping
Word. track of the current location. Similarly, a write
operation appends data to the end of the file,
Different file types have different extensions. Here
extending the file's content.
are some common file types and their extensions:
Sequential access is commonly used by editors,
• Documents: Files containing text, such as
compilers, and other programs that process data
essays, reports, or letters. They often have
in a linear manner. However, it may not be
extensions like .docx (Microsoft
efficient for large files when we need to access
Word), .pdf (Portable Document Format),
specific records randomly.
or .txt (Plain Text).
• Images: Files that represent pictures or Direct Access: Direct access, also known as
graphics. They can have extensions relative access, allows for random access to data
like .jpg or .jpeg (JPEG image), .png within a file. In this method, the file is divided
(Portable Network Graphics), or .gif into fixed-length logical records or blocks, and
(Graphics Interchange Format). each block is assigned a unique number.
• Videos: Files that contain moving images. With direct access, we can read or write records
They can have extensions like .mp4 in any order, without having to go through the
(MPEG-4 video), .avi (Audio Video entire file sequentially. For example, we can read
Interleave), or .mov (QuickTime Movie). block 14, then jump to block 53, and then write
• Music: Files that store audio recordings or to block 7. This random access is possible
songs. They can have extensions like .mp3 because of the underlying disk-based storage,
(MPEG Audio Layer 3), .wav (Waveform which allows accessing any block directly.
Audio File Format), or .flac (Free Lossless
Audio Codec). To perform direct access, file operations are
modified to include the block number as a
By looking at the file extension, both the user parameter. Instead of reading or writing the next
and the operating system can quickly identify the record, we specify the block number we want to
type of the file and determine which program read or write.
should be used to open or work with it. This
makes it easier to organize and manage different It's important to note that not all operating
types of files. systems support both sequential and direct access
methods for files. Some systems only allow
ACCESS METHODS sequential access, while others only support direct
access. The choice of access method depends on
When we want to access information stored in a
the specific needs of the application and the
file, there are different methods we can use. Let's
capabilities of the operating system
look at two common access methods: sequential
access and direct access. DIRECTORY STRUCTURE

Sequential Access: Sequential access is the Storage Structure :- Imagine you have a big box
simplest method of accessing information in a where you can keep all your toys. This box is
file. With sequential access, data is processed in a like a computer's storage device, called a disk.
Normally, you put all your toys in the box and DIRECTORY OVERVIEW
that's it.
In simpler terms, when we talk about a directory
But sometimes, you might want to organize your structure, we need to think about the things we
toys in different ways. You could divide the box can do with it. These are the different operations:
into sections and put different types of toys in
1. Search for a file: It's like looking for a
each section. You could also use some parts of
specific toy in your toy box. We want to
the box for things other than toys, like storing
find the right place in the directory where
extra clothes or snacks.
a particular file is listed.
In a computer, the box or disk can be used
2. Create a file: Imagine you want to add a
entirely for storing files. However, sometimes it's
new toy to your toy box. Similarly, we
useful to divide the disk into smaller parts called
can create a new file and include it in
partitions or slices. Each partition can have its
the directory.
own set of files. This way, you can keep different
types of files separate from each other. 3. Delete a file: Sometimes, you may not
want a toy anymore and want to remove
Sometimes, you might have more than one disk,
it from your toy box. Similarly, we want
and you can combine them to make a bigger
to be able to remove a file from the
storage space called a volume. Each volume is
directory when we don't need it anymore.
like a virtual box where you can store even more
files. 4. List a directory: When we want to see all
the toys in our toy box, we can make a
Now, every box or volume needs a special list
list of them. Similarly, we want to be
called a directory. This list keeps track of all the
able to see a list of all the files in a
toys or files inside the box or volume. It has
directory and get information about each
information like the names of the files, where
file.
they are located, how big they are, and what
type they are. This helps you find and organize 5. Rename a file: Suppose you want to
your files easily. change the name of a toy because its
name doesn't match anymore. Similarly,
So, in simpler terms, a disk is like a big box
we can change the name of a file in the
where you keep files. You can divide the box
directory to reflect its new content or
into sections for different types of files. If you
purpose. This may also change its position
have more than one box, you can combine them
in the directory.
to make an even bigger storage space. And to
keep track of all the files, there is a special list 6. Traverse the file system: Imagine you
called a directory that tells you information about want to explore and look at every toy
each file. and box within your toy box collection.
Similarly, we might want to access and
go through every directory and file in a
directory structure, exploring the entire
system.

So, these operations involve finding files, adding


new files, removing files, listing files, changing
file names, and exploring the entire directory
structure.
SINGLE LEVEL DIRECTORY TWO LEVEL DIRECTORY

In a computer, a directory is like a folder that Imagine you have a big box where you keep all
helps organize files. It keeps track of all the files your toys, but now you want to share the box
on a computer and provides a way to access with your friend. To avoid confusion, you decide
them easily. Just like how you can have different to create a separate section in the box for each
folders to store your toys, a computer has of you. This way, your toys are kept separate
directories to store files. from your friend's toys. This is similar to a two-

Now, let's talk about the single-level directory level directory structure.

structure in simpler terms: In a two-level directory structure, each user has

Imagine you have a big box where you keep all their own directory, which is like their personal

your toys, and you don't have any other boxes or toy section in the box. Each user's directory lists

folders to organize them. All your toys are mixed only the files that belong to that user. When a

together in that one box. This is similar to a user starts using the computer or logs in, the

single-level directory structure. system checks the master directory to find their
specific directory.
In a single-level directory, all the files are stored
in one directory. It's like putting all your toys in Here are some key points about the two-level

one big box. This makes it easy to understand directory structure:

and manage because there is only one place to 1. User's own directory: Each user gets their
look for files. own separate space to store their files. It's

However, there are some limitations to this like having your own personal section in

approach: the toy box where you can keep your


toys.
1. Unique names: Since all the files are in
the same directory, they must have 2. Unique file names: Within each user's

different names. It's like each toy in your directory, file names only need to be

box needs to have a different name so unique within that user's directory. It's

that you can identify them easily. If two like each user can have their own toy

toys have the same name, it would be named "Teddy" because they have their

confusing. own section in the box.

2. Difficulty in remembering: Even if there is 3. Master file directory: There is a master

only one user and all the files are in the directory that keeps track of each user's

same directory, it can become difficult to directory. It helps the system find the

remember the names of all the files as the right user's directory when they log in.

number of files increases. It's like trying It's like having a list of names that tells

to remember the names of all the toys in you where each person's toy section is in

a big box. As the number of toys (or the box.

files) grows, it becomes harder to keep 4. System executable files: Apart from user
track of everything. directories, there is usually a separate

So, in summary, a single-level directory structure directory for system executable files.

is like having all your toys in one big box. It's These are special files that make the

simple to understand and manage, but it has computer work.

limitations like the need for unique file names 5. Access to directories: The system can
and difficulty in remembering the names of all decide whether users are allowed to
the files. access directories other than their own. If
allowed, there must be a way to specify 1. Root directory: The root directory is like
which directory they want to access. If the base of the tree. It's the top-level
access is denied, special arrangements directory from which all other directories
must be made for users to run programs and files stem. Think of it as the starting
located in system directories. point of the entire file organization.

6. Search path: A search path is like a list of 2. Paths and organization: Every file in the
directories where the system looks for system has a unique path name. It's like a
specific programs. Each user can have specific address that tells you where a file
their own unique search path to find is located within the tree structure. Users
programs. can create subdirectories (smaller
branches) and organize their files within
So, in simpler terms, a two-level directory
them to keep things organized.
structure is like having separate toy sections in a
shared toy box. Each user has their own section, 3. Current directory: Each user or process
and their files are listed only in their section. has a concept of a current directory. It's
There is a master list that helps the system find like a starting point for that user or
each user's section. Users may or may not be process to search for files. All searches for
able to access other sections, and they can have files happen relative to the current
their own search paths to find programs. directory.

4. Absolute and relative pathnames: Files can


TREE STRUCTURED DIRECTORIES be accessed using absolute pathnames,
which start from the root of the tree and
Imagine you have a big tree in your backyard. At provide the full path to a file.
the bottom of the tree, there is a main root from Alternatively, files can be accessed using
which all the branches grow. Each branch can relative pathnames, which are relative to
have smaller branches growing out of it, and the current directory.
those smaller branches can have even more
5. Special structure: Directories are stored in
branches. It's like a family tree, where each
the computer's memory just like any other
person has children, and those children have
file, but there is a special bit that
their own children, and so on.
identifies them as directories.
In a computer's directory structure, we can think Additionally, directories have a specific
of it as a tree like this. The tree has a main root structure that the operating system
directory, just like the base of the tree in your understands, helping it manage and
backyard. This root directory is the starting point navigate the tree structure efficiently.
for the entire file organization.
So, in simpler terms, a tree-structured directory is
In this tree structure, users can create their own like a big tree where files and subdirectories are
subdirectories (smaller branches) and organize organized. The root directory is the starting
their files within them. Each subdirectory can point, and users can create their own
contain files or even more subdirectories. subdirectories to keep their files organized. Each
file has a unique address within the tree. Users
Here are some key points about tree-structured
have a current directory from which they search
directories :-
for files. Directories are stored in a special way,
allowing the operating system to understand and
manage the tree structure effectively.
ACYCLIC GRAPH DIRECTORIES to be created for directories and files in
different file systems.
Imagine you have a big playground, and there
• Windows: Windows primarily supports
are multiple pathways connecting different areas
symbolic links, which are known as
of the playground. Some pathways lead to the
shortcuts. Shortcuts are similar to
same location. It's like having different routes to
symbolic links and can be used to link
reach the same place.
directories and files.
In a computer's directory structure, we can have
However, there are considerations when using
a similar arrangement using an acyclic graph.
links:
Unlike a tree structure where each file has a
unique location, in an acyclic graph, directories • For hard links, a reference count is
can have shared subdirectories and files. This maintained, and when all the references
means the same file can exist in multiple to a file are removed, the disk space can
directories, and any changes made to the file are be reclaimed.
immediately visible in all the locations. • For symbolic links, there is a question of
To implement this sharing, we use a concept what to do if the original file is moved or
called "links." A link is like a pointer or deleted. Options include adjusting the
reference to another file or subdirectory. It's a symbolic links to reflect the new location
way of saying that this file can be found in or leaving them "dangling," meaning they
multiple places. There are two types of links are no longer valid when used.
commonly used: So, in simpler terms, an acyclic-graph directory
1. Hard links: A hard link involves multiple structure allows files and subdirectories to be
directory entries that all refer to the same shared. It's like having different pathways in a
file. It's like having signposts pointing to playground that lead to the same location. Links,
the same location from different paths. such as hard links and symbolic links, are used
Hard links are only valid within the same to create these connections. Hard links refer to
file system. the same file in multiple locations, while
symbolic links provide information on where to
2. Symbolic links: A symbolic link is a
find the linked file. Different operating systems
special file that contains information
have different approaches to links.
about where to find the linked file. It's
like having a signpost that tells you FILE SYSTEM MOUNTING
where to go to find the real location.
Symbolic links can be used to link Imagine you have a big toy storage area where
directories and files in different file you keep all your toys. But sometimes, you have
systems or within the same file system. so many toys that you need more than one
storage area to fit them all. In that case, you can
In the context of different operating systems: combine the different storage areas into one big
• UNIX: UNIX supports both hard links and storage space. This way, you can access all your
symbolic links. Hard links keep track of toys from a single place.
how many directory entries refer to the In a computer, a file system is like a storage area
same file using a reference count. When a for files. Sometimes, the files are too big to fit in
reference is removed, the link count is just one storage area, so we need to combine
reduced, and when it reaches zero, the multiple file systems to create one large storage
disk space can be reclaimed. Symbolic area.
links provide flexibility by allowing links
Mounting a file system is the process of space. It allows the computer to access and
combining these multiple file systems into one organize files from multiple file systems as if they
big tree structure. It's like connecting the are part of a single structure. It's like connecting
different storage areas to create a unified space different storage areas so you can find and play
for all the files. with your toys from one place.

Here's how the mounting process works:

1. Naming and location: To mount a file FILE SHARING


system, we need to tell the computer the
name of the storage device (like a disk) File sharing is when multiple users want to work

and the location where we want to attach together or access the same files to achieve a

the file system. The location is typically common computing goal. It's like sharing toys

an empty folder, like an empty toy box with your friends so that everyone can play

waiting to be filled. together and have fun.

2. Verification: The computer checks if the


storage device contains a valid file MULTIPLE USERS
system. It reads the structure of the
device, making sure it's in the expected
When an operating system allows multiple users,
format. It's like checking if the toy
there are important considerations for file
storage area is suitable for storing toys.
sharing, file naming, and file protection:
3. Attaching and accessing: Once the
1. Access to other users' files: The operating
verification is successful, the computer
system can either allow users to access
notes in its system that the file system is
each other's files by default or require
mounted at the specified location. This
specific permission from the owner of the
allows the computer to access and
file. It's like deciding whether you can
navigate the file system as part of its
borrow a friend's toy without asking or if
overall file structure. It's like connecting
you need their permission first.
the storage area to the rest of the toy
organization, so you can easily find and 2. Owner and group concept: To implement
play with any toy from the unified space. sharing and protection, the system keeps
track of additional attributes for files and
For example, consider a situation where you have
directories. These include the owner (the
a main storage area on your computer's hard
user who has the most control over the
disk, and you also have a separate file system on
file) and the group (a subset of users who
a floppy disk. Before mounting, these file systems
can share access to the file).
are separate and you can't access the files on the
floppy disk easily. But when you mount the file 3. File and directory attributes: The owner
system on the floppy disk, it becomes part of the and group IDs of a file or directory are
main file structure, and you can access the files stored with other file attributes. When a
on the floppy disk from specific locations in the user wants to perform an operation on a
main structure. file, their user ID is compared with the
owner attribute to determine if they are
Similarly, if you have multiple hard disks, you
the owner of the file. Similarly, the group
can mount all of them into a single unified
IDs are compared to see which
structure.
permissions apply. Based on the
So, in simpler terms, file-system mounting is like comparison, the system allows or denies
combining different storage areas into one big the requested operation.
REMOTE FILE SYSTEMS When computers want to access files on another
computer, they can use a remote file system. It's
1. Networking and sharing: With networks, like asking a friend to fetch a toy from their toy
computers can communicate with each storage area and share it with you. In this case,
other even if they are far apart. This the computer with the files is called the server,
allows sharing of resources, including data and the computer accessing the files is called the
in the form of files. It's like being able to client.
share toys with your friends who are in
different houses. Client-Server Model :-

2. FTP (File Transfer Protocol): Originally, 1. Client and server: The client computer
file sharing across systems was done using wants to access files, and the server
FTP. It allowed individual files to be computer has those files. It's like you
transported between systems as needed. asking your friend (client) to get a toy
FTP can require an account or password from their toy storage (server) for you.
for access, or it can be anonymous, not 2. User IDs and group IDs: To work
requiring any user name or password. properly, the user IDs and group IDs must
3. Distributed file systems: There are various be consistent across both systems. This is
forms of distributed file systems that important when multiple computers
enable remote file systems to be managed by the same organization share
connected to a local directory structure. files with a common group of users. It's
This means you can access files on remote like making sure you and your friend
systems as if they are part of your own agree on who the toys belong to.
computer. The actual files are still 3. Security concerns: There are security
transported across the network as needed, concerns in this model. Servers often
possibly using FTP as the underlying restrict access to trusted systems only to
transport mechanism. prevent impersonation (a computer
4. World Wide Web (WWW): The WWW has pretending to be another). They may also
made it easy to access files on remote restrict remote access to read-only mode.
systems without connecting their entire Additionally, servers control which file
file systems. This is often done using FTP systems can be mounted remotely, and
as the underlying file transport they usually keep limited, relatively
mechanism, and it allows you to view public information within those file
and download files from remote systems. systems, protected by regular backups.
The NFS (Network File System) is an
So, in simpler terms, file sharing is when example of such a system.
multiple users work together or access the same
files. It's like sharing toys with your friends. The Failure Modes :-
operating system determines whether users can 1. Local file system failures: Local file
access each other's files. There are owners who systems can fail for various reasons, like
have more control over the files, and groups of disk failure, corruption of directory
users who can share access. With networking, structure or other disk management
files can be shared across computers using information, hardware or cable failures,
protocols like FTP. Remote file systems can be or human errors. These failures may cause
connected to a local structure, allowing access to the computer to crash, and human
files on remote systems. The World Wide Web intervention is needed to fix the problems.
also allows you to access files on remote systems
using FTP.
2. Remote file system failures: Remote file Types of Access :-
systems have additional failure modes due
1. Read: It means looking at the contents of
to the complexity of network systems.
a file, like reading a book.
Interruptions in the network between two
2. Write: It means making changes or adding
computers can occur due to hardware
new information to a file, like writing in
failure, poor configuration, or network
a notebook.
implementation issues.
3. Execute: It means running or using a file
3. Handling loss of remote file system: as a program, like playing a game on a
Imagine the client computer is using a computer.
remote file system, and suddenly, it 4. Append: It means adding new information
becomes unreachable. In this situation, at the end of a file, like adding a new
the client computer should not behave as page to a book.
it would if a local file system was lost. 5. Delete: It means removing a file
Instead, it can either terminate all completely, like throwing away a toy.
operations to the lost server or delay 6. List: It means seeing the name and details
operations until the server is reachable of a file, like looking at a list of toys and
again. These behaviors are defined in the their descriptions.
remote-file-system protocol. Terminating
operations can result in users losing data,
so most protocols allow delaying file Access Control: To control access to files, we
system operations with the hope that the usually associate each file with an access-control
remote server will become available list (ACL). It's like having a list of people who
again. can play with a specific toy and what they are
allowed to do with it.
So, in simpler terms, a client-server model is like
asking a friend (client) to fetch a toy from their When someone wants to access a file, the
toy storage (server) and share it with you. In computer checks the access-control list for that
remote file systems, computers access files on file. If the person is listed with the requested
other computers. Security measures are in place access (like read or write), they are allowed to
to protect access, and certain failures can occur perform that action. If they are not listed or don't
in both local and remote file systems. When the have the required access, they are denied access.
remote file system is lost, the client computer can
One way to simplify access control is by
either stop operations or wait for the server to be
categorizing users into three groups for each
reachable again.
file :-
PROTECTION 1. Owner: The person who created the file.
They have the most control and can do
When we want to protect files on a computer, we
all actions on the file.
need to control who can do what with those
2. Group: A set of people who share the file
files. This is like having different rules for
and need similar access. They have
different toys so that everyone plays with them
specific permissions, like read and write,
properly. Access control mechanisms help us
but can't delete the file.
control the types of actions that can be
3. Universe: All other users of the system
performed on files.
who don't fall into the owner or group
category. They have limited permissions,
usually just read access.
For example, let's say Sara is writing a book and pages of a book without needing to jump
hires three graduate students (Jim, Dawn, and around.
Jill) to help. Sara should have full access to the
3. Storage allocation issues: Similar to
book file, while the students should have read
allocating blocks of memory, allocating
and write access but not the ability to delete the
contiguous disk space involves
file. Other users in the system should only have
considerations like first fit, best fit, and
read access. To achieve this, a group called "text"
fragmentation problems. However, with
is created with the students as members, and the
disks, moving the disk heads to different
access rights are set accordingly.
locations takes time, so it may be more
To ensure proper control, permissions and access beneficial to keep files contiguous
lists need to be managed carefully. In some whenever possible.
systems, only authorized individuals, like
4. Compacting the disk: Even file systems
managers, can create or modify groups.
that don't store files contiguously by
So, in simpler terms, access control is about default can benefit from utilities that
setting rules for who can do what with files. We compact the disk. These utilities rearrange
have different types of access like read, write, the files and make them contiguous,
and execute. Access-control lists help us keep improving performance.
track of who can access files and what they can
5. Problems with file growth: There can be
do with them. By categorizing users into owner,
issues when files grow or when the exact
group, and universe, we simplify access control.
size of a file is unknown at creation time:
It's like having different rules for different people
to play with different toys. • Overestimating the file's final size
leads to external fragmentation
and wastes disk space.
ALLOCATION METHODS • Underestimating the size may
require moving the file or aborting
When we store files on a disk, we need to decide a process if the file outgrows its
how to allocate space for those files. It's like allocated space.
finding the best places to keep your toys in your • Slow growth over time with a
toy storage area so that they can be easily predetermined initial allocation
accessed. There are different methods for may result in a lot of unusable
allocating disk space, and we will discuss three space before the file fills it.
major methods: contiguous, linked, and indexed. 6. Extents: Another variation of contiguous
1) Contiguous Allocation :- allocation is allocating file space in large
chunks called extents. If a file outgrows
1. Contiguous allocation: This method its original extent, an additional extent is
requires that each file is stored in a allocated. An extent can be as large as a
continuous block of space on the disk. It's complete track or even a cylinder, aligned
like putting all the parts of a puzzle on appropriate boundaries.
together in one place.
So, in simpler terms, allocation methods are ways
2. Performance benefits: Contiguous to decide where to store files on a disk.
allocation provides fast performance Contiguous allocation means keeping the parts of
because reading consecutive blocks of the a file together. It helps with fast access, but
same file doesn't require moving the disk problems can arise when files grow or their sizes
heads much. It's like flipping through are unknown. Compacting the disk can improve
performance. Extents are large chunks of space
used for allocation. It's like finding the best spots puzzle pieces together to form larger
to store your toys together or in big chunks. clusters.

2) LINKED ALLOCATION 6. Reliability and File Allocation Table


(FAT): A problem with linked allocation is
When we store files on a disk, we need to decide the risk of losing or damaging a pointer.
how to allocate space for those files. We have Doubly linked lists provide some
discussed contiguous allocation, and now let's protection, but they require additional
simplify the concept of linked allocation. overhead and wasted space. In the DOS
operating system, a variation called the
Linked Allocation:
File Allocation Table (FAT) is used. It
1. Linked allocation: In linked allocation, stores all the links in a separate table at
each file is like a chain made up of the beginning of the disk. This allows the
blocks on the disk. The blocks can be FAT table to be cached in memory,
scattered anywhere on the disk, and the improving random access speeds.
directory contains pointers to the first and
So, in simpler terms, linked allocation is like
last blocks of the file.
connecting blocks of a file together using
2. Example: Imagine a file with five blocks. pointers. It doesn't suffer from external
The first block might be at block 9, then fragmentation and allows files to grow
it continues to block 16, then block 1, dynamically. Sequential access works well, but
then block 10, and finally block 25. It's random access can be slower. Clusters of blocks
like connecting different puzzle pieces can be used to reduce wasted space. The File
together to form a complete picture. Allocation Table (FAT) is a variation of linked
allocation used in DOS, which improves random
3. Benefits of linked allocation: Linked
access speed. It's like connecting puzzle pieces to
allocation solves the problems of external
create a picture or using a table to keep track of
fragmentation and pre-known file sizes.
links in a storybook.
Files can grow dynamically at any time
without issues. It's like being able to add
more puzzle pieces to a picture whenever INDEXED ALLOCATION
needed.
We have discussed contiguous and linked
4. Sequential access efficiency: Linked
allocation methods for storing files on a disk.
allocation is efficient for sequential access,
Now let's simplify the concept of indexed
where you read the blocks in order. It's
allocation.
like reading a storybook from the
beginning to the end. However, random Indexed Allocation:
access, where you jump to different parts 1. Indexed allocation: Indexed allocation
of the file, is less efficient as you need to combines all the indexes needed to access
start from the beginning of the list for each file into a common block specific to
each new location. that file. Instead of scattering the indexes
5. Clusters and internal fragmentation: To all over the disk or using a FAT table,
reduce the space wasted by pointers, each file has its own index block, which
clusters of blocks can be allocated is like an array of disk-block addresses.
together. This helps to manage internal 2. Wasted disk space: Indexed allocation
fragmentation, where some space within a requires allocating an entire index block
block is unused. It's like grouping several for each file, regardless of the number of
data blocks the file contains. This leads to
some disk space being wasted compared book or a table of contents with pointers to
to linked lists or FAT tables. It's like different sections. The combined scheme used in
having a separate sheet of paper for each UNIX inodes allows for quick access to some data
chapter in a book, even if some chapters blocks and uses indirect pointers for accessing
are shorter. more blocks. It's like having bookmarks to reach
specific pages and using references for other
3. Different approaches: There are different
pages.
approaches to implementing the index
block:

• Linked scheme: In the linked


scheme, an index block is one
disk block that can be read and
written in a single disk operation.
The first index block contains
some header information and the
addresses of the first N data
blocks. If needed, it may also
include a pointer to additional
linked index blocks.
• Multi-level index: In the multi-
level index scheme, the first index
block contains pointers to
secondary index blocks. These
secondary index blocks, in turn,
contain pointers to the actual data
blocks. It's like having a table of
contents pointing to different
sections, and each section has its
own page numbers.
• Combined scheme: The combined
scheme, used in UNIX inodes,
stores the first few data block
pointers directly in the inode
itself. Then, singly, doubly, and
triply indirect pointers are used to
access more data blocks as
needed. It's like having quick
access to a few specific pages in a
book and using bookmarks to
reach other pages.

In simpler terms, indexed allocation uses a


separate index block for each file, making it
easier to find specific data blocks. However, it
requires some disk space to be wasted. Different
approaches, like the linked scheme or multi-level
index, are used to organize the index blocks. It's
like having separate sheets for each chapter in a

You might also like