Operating System CIC305 - Dr. Ashish (Complete OS)
Operating System CIC305 - Dr. Ashish (Complete OS)
Ashish
0
Syllabus
Unit-1
UNi
UNIT I
UNIT-II
Dr. Ashish
1
Unit-1
UNi
1 INTRODUCTION
• An Operating System acts as a communication bridge (interface) between the user
and computer hardware.
• The purpose of an operating system is to provide a platform on which a user can execute
programs conveniently and efficiently.
• The main goal of the Operating System is to make the computer environment more
convenient to use and the Secondary goal is to use the resources most efficiently.
Dr. Ashish
2
Unit-1
UNi
• Convenience
• Efficiency
• Hardware Abstraction
• Security
1. Convenience
An Operating System's primary and first goal is to provide a friendly and convenient
environment to the user. It is optional to use Operating System. Still, things become harder
when the user has to perform all the process scheduling and convert user commands to machine
language so that system can perform tasks. So, we use an Operating System to act as a bridge
between us and the computer hardware. We only have to give commands to the system, and OS
will take the instructions and do the rest of the work. Because of this operating system should
be convenient to use and operate by the user.
2. Efficiency
The second and important goal of an Operating System is efficiency. An operating system
should utilize all the resources efficiently. The management of resources and programs should
be done so that no resource is kept idle and memory is used for no use.
The operating system can work/operate on different machines with different processors and
memory configurations. This makes the operating system more reliable.
Also, the operating system can protect itself and the user from accidental damage from the user
program.
4. Hardware Abstraction
Dr. Ashish
3
Unit-1
UNi
The operating system can conceal or can be said to control all functions and resources of the
computer. The user can give commands and access any function or resource of the computer
without facing any difficulties. In this way, the Operating system communicates between the
user and computer hardware.
5. Security
An operating system provides the safety and security of data between the user and the hardware.
OS enables multiple users to securely share a computer(system), including files, processes,
memory, and device separately.
• Resource Management in Operating System is the process to manage all the resources
efficiently like CPU, memory, input/output devices, and other hardware resources
among the various programs and processes running in the computer.
• Resource management is an important thing because resources of a computer are limited
and multiple processes or users may require access to the same resources like CPU,
memory etc. at the same time. The operating system has to manage and ensure that all
processes get the resources they need to execute, without any problems like deadlocks.
Resource Allocation: This terms defines the process of assigning the available resources to
processes in the operating system. This can be done dynamically or statically.
Resource: Resource can be anything that can be assigned dynamically or statically in the
operating system. Example may include CPU time, memory, disk space, and network
bandwidth etc.
Dr. Ashish
4
Unit-1
UNi
Process: Process refers to any program or application that is being executed in the operating
system and has its own memory space, execution state, and set of system resources.
Scheduling: It is the process of determining from multiple number of processes which process
should be allocated a particular resource at a given time.
Deadlock: When two or more processes are waiting for some resource but resources are busy
somewhere else and resources are also waiting for some process to complete their execution .
In such condition neither resources will be freed nor process would get it and this situation is
called deadlock.
Semaphore: It is the method or tool which is used to prevent race condition. Semaphore is an
integer variable which is used in mutual exclusive manner by various concurrent cooperative
process in order to achieve synchronization.
Mutual Exclusion: It is the technique to prevent multiple number of process to access the same
resources simultaneously.
1. Memory Management
2. Processor Management
3. Device Management
4. File Management
5. Network Management
Dr. Ashish
5
Unit-1
UNi
• Security
• Job accounting
• It keeps track of primary memory, i.e., which bytes of memory are used by which user
program. The memory addresses that have already been allocated and the memory
addresses of the memory that has not yet been used.
• In multiprogramming, the OS decides the order in which processes are granted memory
access, and for how long.
• It Allocates the memory to a process when the process requests it and deallocates the
memory when the process has terminated or is performing an I/O operation.
Dr. Ashish
6
Unit-1
UNi
Dr. Ashish
7
Unit-1
UNi
following file management activities. It keeps track of where information is stored, user access
settings, the status of every file, and more. These facilities are collectively known as the file
system. An OS keeps track of information regarding the creation, deletion, transfer, copy, and
storage of files in an organized way. It also maintains the integrity of the data stored in these files,
including the file directory structure, by protecting against unauthorized access.
• The operating system is in charge of storing and accessing files. The creation of files, the
creation of directories, the reading and writing of data from files and directories, as well
as the copying of the contents of files and directories from one location to another are all
included in storage management.
• Assists businesses in storing more data on existing hardware, speeding up the data
retrieval process, preventing data loss, meeting data retention regulations, and lowering
IT costs
2.6 Following are some of the important activities that an Operating System performs −
• Security – For security, modern operating systems employ a firewall. A firewall is a type
of security system that monitors all computer activity and blocks it if it detects a threat.
• Job Accounting – As the operating system keeps track of all the functions of a computer
system. Hence, it makes a record of all the activities taking place on the system. It has an
account of all the information about the memory, resources, errors, etc. Therefore, this
information can be used as and when required.
Dr. Ashish
8
Unit-1
UNi
• Control over system performance – The operating system will collect consumption
statistics for various resources and monitor performance indicators such as reaction time,
which is the time between requesting a service and receiving a response from the system.
• Error detecting aids – While a computer system is running, a variety of errors might occur.
Error detection guarantees that data is delivered reliably across susceptible networks. The
operating system continuously monitors the system to locate or recognize problems and
protects the system from them.
• Coordination between other software and users – The operating system (OS) allows
hardware components to be coordinated and directs and allocates assemblers, interpreters,
compilers, and other software to different users of the computer system.
• In the beginning, computers were extremely large machines that ran from a console. In
general, tape drivers or card readers were used for input, and tape drives, punch
cards, and line printers were used for output. Users had no direct interface with the
system, and job execution was done in a batch system. These systems are known as
batched operating systems, and users have to prepare a job separately to execute it.
Dr. Ashish
9
Unit-1
UNi
There are mainly two types of the batch operating system. These are as follows:
• The user did not directly interact with the computer system for job execution in a simple
batch operating system. However, the user was required to prepare a job that included
the program, control information, and data on the nature of the job on control cards. The
job was then submitted to the computer operator, who was usually in the form of a punch
card. The program's output included results and registers and memory dumps in the
event of a program error. The output appeared after some time that could take days,
hours, and minutes.
Dr. Ashish
10
Unit-1
UNi
• Its main role was to transfer control from one job to another. Jobs with similar
requirements were pooled together and processed through the processor to improve
processing speed. The operators were used in the program to create batches with similar
needs. The computer runs the batches one by one when they became available. This
system typically reads a sequence of jobs, each with its control cads and predefined job
tasks.
Dr. Ashish
11
Unit-1
UNi
• Spooling deals with many jobs that have already been read and are waiting to run on
disk. A disk containing a pool of jobs allows the operating system to choose which job
to run next to maximize CPU utilization.
• Jobs that come on magnetic tape or cards directly cannot be run in a different order.
Jobs run sequentially because they are executed in a first-come, first-served manner.
When various jobs are stored on a direct access device, job scheduling becomes possible
like a disk. Multi-programming is an important feature of job scheduling. For
overlapped I/O, spooling and offline operations have their limitations. Generally, a
single user could not maintain all of the input/output devices, and CPU buys at all times.
Dr. Ashish
12
Unit-1
UNi
Multiprogramming Operating Systems can be simply illustrated as more than one program is
present in the main memory and any one of them can be kept in execution. This is basically used
for better execution of resources.
Dr. Ashish
13
Unit-1
UNi
• There is not any facility for user interaction of system resources with the system.
Dr. Ashish
14
Unit-1
UNi
Dr. Ashish
15
Unit-1
UNi
Soft Real-Time Systems: These OSs are for applications where time-constraint is less strict.
3.4 Distributed OS
This system is based on autonomous but interconnected computers communicating with each
other via communication lines or a shared network. Each autonomous system has its own
processor that may differ in size and function. These operating systems are often used for tasks
such as telecommunication networks, airline reservation controls and peer-to-peer networks.
A distributed operating system serves multiple applications and multiple users in real time. The
data processing function is then distributed across the processors. Potential advantages and
disadvantages of distributed operating systems are:
Advantages Disadvantages
They allow a faster exchange of data among users. They're expensive to install.
Failure in one site may not cause much disruption to the They require a high level of expertise to
system. maintain.
Dr. Ashish
16
Unit-1
UNi
Advantages Disadvantages
Dr. Ashish
17
Unit-1
UNi
• It is an evolution of serial processing wherein tasks are broken down into manageable
tasks that can be executed concurrently.
• Further, each part is broken down into a series of instructions executed simultaneously on
different CPUs. The working of the parallel operating system is such that the CPU and its
components are divided into smaller parts, each having full speed and power.
• In a normal operating system, once an I/O device is identified, it will use the CPU to
transfer the information into the memory before performing any operations on it, like
processing and transmitting.
• By parallel operating systems, however, more data can be transferred and processed
simultaneously, resulting in quicker data transfer.
Dr. Ashish
18
Unit-1
UNi
Tasks are performed with a more speedy Tasks are performed with a less speedy
3. process. process.
These systems are multiprocessor In Distributed Systems, each processor has its
4. systems. own memory.
It is also known as a tightly coupled Distributed systems are also known as loosely
5. system. coupled systems.
These systems share a memory, clock, and These systems do not share memory or clock
7. peripheral devices in contrast to parallel systems.
4 Spooling
Spooling is the process of pushing data from various I/O jobs into a buffer, disc, or somewhere
in the memory so that a device can access the data when it is ready.
Dr. Ashish
19
Unit-1
UNi
In order to maintain the spooling buffer, the OS handles I/O device data spooling because the
devices have varying data access rates. Buffer acts as a waiting station for data to rest while
slower devices catch up. Print Spooling is a spooling application.
Dr. Ashish
20
Unit-1
UNi
CPU Scheduling
5 Process concept
5.1 Process
A program is a piece of code which may be a single line or millions of lines. A computer
program is usually written by a computer programmer in a programming language. For
example, here is a simple program written in C programming language −
#include <stdio.h>
int main() {
printf("Hello, World! \n");
return 0;
}
Dr. Ashish
21
Unit-1
UNi
A computer program is a collection of instructions that performs a specific task when executed
by a computer. When we compare a program with a process, we can conclude that a process is a
dynamic instance of a computer program.
A Process Control Block is a data structure maintained by the Operating System for every
process. The PCB is identified by an integer process ID (PID). A PCB keeps all the information
needed to keep track of a process as listed below in the table −
Dr. Ashish
22
Unit-1
UNi
The architecture of a PCB is completely dependent on Operating System and may contain
different information in different operating systems. Here is a simplified diagram of a PCB −
Dr. Ashish
23
Unit-1
UNi
Dr. Ashish
24
Unit-1
UNi
• The process advances to the ready state after the I/O operation is completed or the resource
becomes available.
Until the main memory becomes available, the process stays in the suspend-ready state. The
process is brought to its ready state when the main memory becomes accessible.
• The process gets moved to the suspend-ready state once the resource becomes accessible.
The process is shifted to the ready state once the main memory is available.
Note – 02:
Only one process can run at a time on a single CPU.
Dr. Ashish
25
Unit-1
UNi
Note – 04:
It is much more preferable to move a given process from its wait state to its suspend wait
state.
• Consider the situation where a high-priority process comes, and the main memory is full.
• Then there are two options for making space for it. They are:
1. Suspending the processes that have lesser priority than the ready state.
2. Transferring the lower-priority processes from wait to the suspend wait state.
Now, out of these: Moving a process from a wait state to a suspend wait state is the superior
option.
• It is because this process is waiting already for a resource that is currently unavailable.
Dr. Ashish
26
Unit-1
UNi
degree of multi-programming. However, the main goal of this type of scheduler is to offer a
balanced mix of jobs, like Processor, I/O jobs., that allows managing multiprogramming.
A running process can become suspended if it makes an I/O request. A suspended processes can’t
make any progress towards completion. In order to remove the process from memory and make
space for other processes, the suspended process should be moved to secondary storage.
Dr. Ashish
27
Unit-1
UNi
• Independent process.
• Co-operating process.
An independent process is not affected by the execution of other processes while a co-operating
process can be affected by other executing processes. Though one can think that those processes,
which are running independently, will execute very efficiently, in reality, there are many situations
when co-operative nature can be utilized for increasing computational speed, convenience, and
modularity. Inter-process communication (IPC) is a mechanism that allows processes to
communicate with each other and synchronize their actions. The communication between these
processes can be seen as a method of co-operation between them. Inter process communication
(IPC) is a process that allows different processes of a computer system to share information. IPC
lets different programs run in parallel, share data, and communicate with each other. It’s important
for two reasons: First, it speeds up the execution of tasks, and secondly, it ensures that the tasks
run correctly and in the order that they were executed.
1. Shared Memory
2. Message passing
• Communication between processes using shared memory requires processes to share some
variable, and it completely depends on how the programmer will implement it.
• Suppose process1 and process2 are executing simultaneously, and they share some resources or use
some information from another process. Process1 generates information about certain computations or
resources being used and keeps it as a record in shared memory.
• When process2 needs to use the shared information, it will check in the record stored in shared memory
and take note of the information generated by process1 and act accordingly. Processes can use shared
memory for extracting information as a record from another process as well as for delivering any
specific information to other processes.
Dr. Ashish
28
Unit-1
UNi
• Establish a communication link (if a link already exists, no need to establish it again.)
• Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destination) or send(message)
– receive(message, host) or receive(message)
The message size can be of fixed size or of variable size. If it is of fixed size, it is easy for an
OS designer but complicated for a programmer and if it is of variable size then it is easy for a
programmer but complicated for the OS designer. A standard message can have two
parts: header and body.
The header part is used for storing message type, destination id, source id, message length,
and control information. The control information contains information like what to do if runs
out of buffer space, sequence number, priority. Generally, message is sent using FIFO style.
Message Passing through Communication Link. Direct and Indirect Communication link
Direct Communication links are implemented when the processes use a specific process
identifier for the communication, but it is hard to identify the sender ahead of time.
For example the print server.
In-direct Communication is done via a shared mailbox (port), which consists of a queue of
messages. The sender keeps the message in mailbox and the receiver picks them up.
Read More
https://www.geeksforgeeks.org/inter-process-communication-
ipc/#:~:text=Inter%2Dprocess%20communication%20(IPC),Shared%20Memory
Dr. Ashish
29
Unit-1
UNi
Types of Interrupt
Interrupt signals may be issued in response to
hardware or software events. These are classified
as hardware interrupts or software interrupts,
respectively.
https://www.javatpoint.com/what-is-interrupt-
in-os
Dr. Ashish
30
Unit-1
UNi
8 Thread
A thread is a single sequence stream within a process. Threads
are also called lightweight processes as they possess some of the
properties of processes. Each thread belongs to exactly one
process. In an operating system that supports multithreading, the
process can consist of many threads. But threads can be effective
only if the CPU is more than 1 otherwise two threads have to
context switch for that single CPU.
In a process, a thread refers to a single sequential activity being executed. these activities are also
known as thread of execution or thread control. Now, any operating system process can execute
a thread. we can say, that a process can have multiple threads.
The process can be split down into so many threads. For example, in a browser, many tabs can be
viewed as threads. MS Word uses many threads - formatting text from one thread, processing input
from another thread, etc.
Types of Threads: In the operating system, there are two types of threads.
Important: Modern operating systems allow a process to be divided into multiple threads of
execution. The threads within a process share all process management information except for
information directly related to execution. This information is moved out of the PCB into thread
control block (TCBs). Threads in a process can execute different parts of the program code at
the same time. They can also execute the same parts of the code at the same time, but with
different execution state:
• They have independent current instructions; that is, they have (or appear to have)
independent program counters.
• They are working with different data; that is, they are (or appear to be) working with
independent registers.
This is accomplished by moving execution state out of the PCB into thread control blocks (TCBs).
Each thread has its own TCB.
Dr. Ashish
31
Unit-1
UNi
1. Thread Creation: A thread is started within a process using system calls (e.g.,
pthread_create() or CreateThread()).
2. Thread Execution: Threads execute concurrently, either via time-slicing on a single CPU
or in parallel on multiple cores.
3. Thread Synchronization: Mechanisms like mutexes, semaphores, and condition
variables prevent race conditions when accessing shared resources.
4. Thread Scheduling: The OS determines which thread runs using preemptive or
cooperative scheduling.
5. Thread Termination: A thread can end normally when its task is done, or it can be
forcefully terminated by the OS or other threads.
6. Context Switching: The OS switches between threads, saving and loading their execution
state as needed.
A thread is a light weight process which is similar to a process where every process can have
one or more threads. Each thread contains a Stack and a Thread Control Block. There are four
basic thread models :
1. User Level Single Thread Model :
Dr. Ashish
32
Unit-1
UNi
Dr. Ashish
33
Unit-1
UNi
9 CPU Scheduling
• Scheduling is the process of allotting the CPU to the processes present in the ready queue.
We also refer to this procedure as process scheduling.
• The operating system schedules the process so that the CPU always has one process to
execute. This reduces the CPU’s idle time and increases its utilization.
• The part of OS that allots the computer resources to the processes is termed as
a scheduler. It uses scheduling algorithms to decide which process it must allot to the
CPU.
Dr. Ashish
34
Unit-1
UNi
Dr. Ashish
35
Unit-1
UNi
Preemptive Scheduling:
Preemptive scheduling is used when a process switches from running state to ready
state or from waiting state to ready state.
The resources (mainly CPU cycles) are allocated to the process for the limited amount
of time and then is taken away, and the process is again placed back in the ready queue
if that process still has CPU burst time remaining.
That process stays in ready queue till it gets next chance to execute.
Non-Preemptive Scheduling:
Non-preemptive Scheduling is used when a process terminates, or a process switch
from running to waiting state.
In this scheduling, once the resources (CPU cycles) is allocated to a process, the process
holds the CPU till it gets terminated or it reaches a waiting state.
In case of non-preemptive scheduling does not interrupt a process running CPU in
middle of the execution.
Instead, it waits till the process complete its CPU burst time and then it can allocate the
CPU to another process.
Dr. Ashish
36
Unit-1
UNi
Basis for
Preemptive Scheduling Non Preemptive Scheduling
Comparison
Once resources are allocated to a
Basic The resources are allocated to a process, the process holds it till it
process for a limited time. completes its burst time or switches to
waiting state.
Process can be interrupted in Process can not be interrupted till it
Interrupt
between. terminates or switches to waiting state.
If a high priority process
If a process with long burst time is running
Starvation frequently arrives in the ready
CPU, then another process with less CPU
queue, low priority process may
burst time may starve.
starve.
Preemptive scheduling has
Non-preemptive scheduling does not have
Overhead overheads of scheduling the
overheads.
processes.
Flexibility Preemptive scheduling is flexible. Non-preemptive scheduling is rigid.
Preemptive scheduling is cost Non-preemptive scheduling is not cost
Cost
associated. associative.
9.2 Scheduling Criteria
There are several different criteria to consider when trying to select the "best" scheduling
algorithm for a particular situation and environment,
including:
o CPU utilization - Ideally the CPU would be busy
100% of the time, so as to waste 0 CPU cycles. On a
real system CPU usage should range from 40% (
lightly loaded ) to 90% ( heavily loaded. )
o Throughput - Number of processes completed per
unit time. May range from 10 / second to 1 / hour
depending on the specific processes.
o Turnaround time - Time required for a particular process to complete, from submission
time to completion.
o Waiting time - How much time processes spend in the ready queue waiting their turn to get
on the CPU.
Dr. Ashish
37
Unit-1
UNi
o Response time - The time taken in an interactive program from the issuance of a command
to the commence of a response to that command.
In brief:
1. Arrival Time: Time at which the process arrives in the ready queue.
2. Completion Time: Time at which process completes its execution.
3. Burst Time: Time required by a process for CPU execution.
4. Turn Around Time: Time Difference between completion time and arrival time. Turn
Around Time = Completion Time – Arrival Time
5. Waiting Time(W.T): Time Difference between turnaround time and burst time.
Waiting Time = Turn Around Time – Burst Time
6. Response Time
In an interactive system, turn-around time is not the best criterion. A process may
produce some output fairly early and continue computing new results while previous
results are being output to the user. Thus another criterion is the time taken from
submission of the process of the request until the first response is produced. This
measure is called response time.
Response Time = CPU Allocation Time(when the CPU was allocated for the first) -
Arrival Time
Advantages-
• It is simple and easy to understand.
Dr. Ashish
38
Unit-1
UNi
Example 1:
Dr. Ashish
39
Unit-1
UNi
Example 2:
Consider the processes P1, P2, P3 given in the below table, arrives for execution in the same
order, with Arrival Time 0, and given Burst Time,
PROCESS ARRIVAL TIME BURST TIME
P1 0 24
P2 0 3
P3 0 3
Gantt chart
P1 P2 P3
0 24 27 30
Average Waiting Time = (Total Wait Time) / (Total number of processes) = 51/3 = 17 ms
Total Turn Around Time: 24 + 27 + 30 = 81 ms
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
= 81 / 3 = 27 ms
Throughput = 3 jobs/30 sec = 0.1 jobs/sec
Example 3:
1. Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in
the same order, with given Arrival Time and Burst Time.
PROCESS ARRIVAL TIME BURST TIME
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Dr. Ashish
40
Unit-1
UNi
Gantt chart
P1 P2 P3 P4
0 8 12 21 26
Average Waiting Time = (Total Wait Time) / (Total number of processes)= 35/4 = 8.75 ms
Total Turn Around Time: 8 + 11 + 19 + 23 = 61 ms
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
61/4 = 15.25 ms
Dr. Ashish
41
Unit-1
UNi
Dr. Ashish
42
Unit-1
UNi
Advantages-
SRTF is optimal and guarantees the minimum average waiting time.
It provides a standard for other algorithms since no other algorithm performs better
than it.
Disadvantages-
It can not be implemented practically since burst time of the processes can not be
known in advance.
It leads to starvation for processes with larger burst time.
Priorities can not be set for the processes.
Processes with larger burst time have poor response time.
Example-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
Solution-
If the CPU scheduling policy is SJF non-preemptive, calculate the average waiting time and
Dr. Ashish
43
Unit-1
UNi
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Example-02:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
If the CPU scheduling policy is SJF pre-emptive, calculate the average waiting time and average
turnaround time.
Solution-
Dr. Ashish
44
Unit-1
UNi
Gantt Chart-
Now,
Dr. Ashish
45
Unit-1
UNi
10.3 SRTF
Dr. Ashish
46
Unit-1
UNi
Dr. Ashish
47
Unit-1
UNi
Example-03:
Consider the set of 6 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is shortest remaining time first, calculate the average
waiting time and average turnaround time.
Solution-
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Dr. Ashish
48
Unit-1
UNi
Example -04:
Consider the set of 3 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is SRTF, calculate the average waiting time and average turn
around time.
Solution- Gantt
Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Now,
Average Turn Around time = (13 + 4 + 20) / 3 = 37 / 3 = 12.33 unit
Average waiting time = (4 + 0 + 11) / 3 = 15 / 3 = 5 unit
Dr. Ashish
49
Unit-1
UNi
Example-05:
Consider the set of 4 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is SRTF, calculate the waiting time of process P2.
Solution-
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Thus,
Turn Around Time of process P2 = 55 – 15 = 40 unit
Waiting time of process P2 = 40 – 25 = 15 unit
Dr. Ashish
50
Unit-1
UNi
Advantages-
Disadvantages-
• It leads to starvation for processes with larger burst time as they have to repeat the cycle
many times.
• Its performance heavily depends on time quantum.
• Priorities can not be set for the processes.
With decreasing value of time quantum,
• Number of context switch increases
• Response time decreases
Dr. Ashish
51
Unit-1
UNi
Example-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the
average waiting time and average turnaround time.
Solution-
Ready Queue- P5, P1, P2, P5, P4, P1, P3, P2, P1
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Dr. Ashish
52
Unit-1
UNi
Problem-02:
Consider the set of 6 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
If the CPU scheduling policy is Round Robin with time quantum = 2, calculate the average
waiting time and average turnaround time.
Solution-
Ready Queue- P5, P6, P2, P5, P6, P2, P5, P4, P1, P3, P2, P1
Gantt chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Dr. Ashish
53
Unit-1
UNi
54
Dr. Ashish
Unit-1
UNi
Problem-03: Consider the set of 6 processes whose arrival time and burst time are given
below-
55
Dr. Ashish
Unit-1
UNi
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 32 32 – 5 = 27 27 – 5 = 22
P2 27 27 – 4 = 23 23 – 6 = 17
P3 33 33 – 3 = 30 30 – 7 = 23
P4 30 30 – 1 = 29 29 – 9 = 20
P5 6 6–2=4 4–2=2
P6 21 21 – 6 = 15 15 – 3 = 12
Now,
56
Dr. Ashish
Unit-1
UNi
highest priority.
• In case of a tie, it is broken by FCFS Scheduling.
• Priority Scheduling can be used in both preemptive and non-preemptive mode.
• The waiting time for the process having the highest priority will always be zero
in preemptive mode.
• The waiting time for the process having the highest priority may not be zero in
non- preemptive mode.
Priority scheduling in preemptive and non-preemptive mode behaves exactly same under
following conditions-
• The arrival time of all the processes is same
• All the processes become available
Advantages-
• It considers the priority of the processes and allows the important processes to run
first.
• Priority scheduling in pre-emptive mode is best suited for real time operating
system.
Disadvantages-
• Processes with lesser priority may starve for CPU.
• There is no idea of response time and waiting time.
57
Dr. Ashish
Unit-1
UNi
58
Dr. Ashish
Unit-1
UNi
59
Dr. Ashish
Unit-1
UNi
Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority)
Solution-
Gantt
Chart-
Now, we know-
• Turn Around time = Exit time – Arrival time
• Waiting time = Turn Around time – Burst time
Problem-02: Consider the set of 5 processes whose arrival time and burst time are given
below-
60
Dr. Ashish
Unit-1
UNi
Now, we know-
• Turn Around time = Exit time – Arrival time
• Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 15 15 – 0 = 15 15 – 4 = 11
P2 12 12 – 1 = 11 11 – 3 = 8
P3 3 3–2=1 1–1=0
P4 8 8–3=5 5–5=0
P5 10 10 – 4 = 6 6–2=4
Now,
• Average Turn Around time = (15 + 11 + 1 + 5 + 6) / 5 = 38 / 5 = 7.6 unit
• Average waiting time = (11 + 8 + 0 + 0 + 4) / 5 = 23 / 5 = 4.6 unit
61
Dr. Ashish
Unit-1
UNi
References
• https://byjus.com/gate/process-state-in-operating-system-notes/
• https://www.tutorialspoint.com/operating_system/os_process_scheduling.htm
• https://www.geeksforgeeks.org/context-switch-in-operating-system/
• https://www.scaler.com/topics/scheduling-criteria-in-os/
62
Dr. Ashish
Unit-1
UNi
UNIT-II
Process Synchronization
and
Memory Organization & Management
UNIT-II
63
Dr. Ashish
• Mutual Exclusion
Mutual Exclusion is a property of process synchronization that states that “no two processes
can exist in the critical section at any given point of time“. The term was first coined
by Dijkstra. Any process synchronization technique being used must satisfy the property of
mutual exclusion, without which it would not be possible to get rid of a race condition.
The need for mutual exclusion comes with concurrency. There are several kinds of concurrent
execution:
• Interrupt handlers
• Distributed systems
• When using shared resources, it is important to ensure mutual exclusion between various
processes. There cannot be two processes running simultaneously in either of their critical
sections.
• It is not advisable to make assumptions about the relative speeds of the unstable processes.
• For access to the critical section, a process that is outside of it must not obstruct another
process.
• Its critical section must be accessible by multiple processes in a finite amount of time;
multiple processes should never be kept waiting in an infinite loop.
• Hardware Method: Special-purpose machine instructions are used for accessing shared
resources. This method is faster but cannot provide a complete solution. Hardware
solutions cannot give guarantee the absence of deadlock and starvation.
64
Unit-2
UNi
o Mutual Exclusion
65
Dr. Ashish
Unit-2
UNi
o Progress
Progress means that if one process doesn't need to execute into critical section then it should not
stop other processes to get into the critical section. If no process is executing in its critical section
and some processes wish to enter their critical sections, then only those processes that are not
executing in their remainder sections can participate in deciding which will enter its critical
section next, and this selection cannot be postponed indefinitely.
o Bounded Waiting
There exists a bound, or limit, on the number of times that other processes are allowed to
enter their critical sections after a process has made a request to enter its critical section and
before that request is granted.
We should be able to predict the waiting time for every process to get into the critical section.
The process must not be endlessly waiting for getting into the critical section.
66
Dr. Ashish
Unit-2
UNi
Algorithm 1
• Satisfies mutual exclusion
The turn is equal to either i or j and hence one of Pi
and Pj can enter the critical section
• Does not satisfy progress
Example: Pi finishes the critical section and then gets
stuck indefinitely in its remainder section. Then Pj
enters the critical section, finishes, and then finishes
its remainder section. Pj then tries to enter the critical
section again, but it cannot since turn was set to i by
Pj in the previous iteration. Since Pi is stuck in the
remainder section, turn will be equal to i indefinitely
and Pj can’t enter although it wants to. Hence no
process is in the critical section and hence no
progress.
67
Dr. Ashish
Unit-2
UNi
68
Dr. Ashish
Unit-2
UNi
3. Peterson’s Solution
While(1)
{
flag[0] = true;
turn = 1;
while (flag[1] ==True && turn == 1);
P0
Critical Section
flag[0] = false;
}
While(1)
{
flag[1] = true;
turn = 0; /* Change to Both waiting, turn=0 */
P1
while (flag[0]==True && turn == 0);
Critical Section
flag[1] = false;
}
Explanation of Peterson's Algorithm
The algorithm utilizes two main variables, which are a turn and a flag. turn is an integer variable that indicates whose turn it
is to enter the critical section. flag is an array of Boolean variables. Each element in the array represents the intention of a
process to enter the critical section. Let us look at the explanation of how Peterson's algorithm in OS works:
• Firstly, both processes set their flag variables to indicate that they don't currently want to enter the critical section. By
default, the turn variable is set to the ID of one of the processes, it can be 0 or 1. This will indicate that it is initially the
turn of that process to enter the critical section.
• Both processes set their flag variable to indicate their intention to enter the critical section.
• Then the process, which is having the next turn, sets the turn variable to its own ID. This will indicate that it is its turn to
enter the critical section. For example, if the current value of turn is 1, and it is now the turn of Process 0 to enter,
Process 0 sets turn = 0.
• Both processes enter a loop where they will check the flag of the other process and wait if necessary. The loop continues
as long as the following two conditions are met:
i. The other process has expressed its intention to enter the critical section (flag[1 - processID] == true for Process
processID).
ii. It is currently the turn of the other process (turn == 1 - processID).
• If both conditions are satisfied, the process waits and yields the CPU to the other process. This ensures that the other
process has an opportunity to enter the critical section.
• Once a process successfully exits the waiting loop, then it can enter the critical section. It can also access the shared
resource without interference from the other process. It can perform any necessary operations or modifications within
this section.
• After completing its work in the critical section, the process resets its flag variable. Resetting is required to indicate that
this process no longer wants to enter the critical section (flag[processID] = false). This step ensures that the process can
enter the waiting loop again correctly if needed.
69
Dr. Ashish
Unit-2
UNi
70
Dr. Ashish
Unit-2
UNi
2. Swap:
Swap algorithm is a lot like the TestAndSet algorithm. Instead of directly setting lock to true in
the swap function, key is set to true and then swapped with lock. First process will be executed,
and in while(key), since key=true , swap will take place and hence lock=true and key=false. Again
next iteration takes place while(key) but key=false , so while loop breaks and first process will
enter in critical section. Now another process will try to enter in Critical section, so again key=true
and hence while(key) loop will run and swap takes place so, lock=true and key=true (since
lock=true in first process). Again on next iteration while(key) is true so this will keep on executing
and another process will not be able to enter in critical section. Therefore Mutual exclusion is
ensured. Again, out of the critical section, lock is changed to false, so any process finding it gets
t enter the critical section. Progress is ensured. However, again bounded waiting is not ensured
for the very same reason.
Swap Pseudocode –
// Shared variable lock initialized to false
// and individual key initialized to false;
boolean lock;
Individual key;
71
Dr. Ashish
Unit-2
UNi
while (1){
key = true;
while(key)
swap(lock,key);
critical section
lock = false;
remainder section
}
boolean lock;
72
Dr. Ashish
Unit-2
UNi
Individual key;
Individual waiting[i];
while(1){
waiting[i] = true;
key = true;
while(waiting[i] && key)
key = TestAndSet(lock);
waiting[i] = false;
critical section
j = (i+1) % n;
while(j != i && !waiting[j])
j = (j+1) % n;
if(j == i)
lock = false;
else
waiting[j] = false;
remainder section
}
• Semaphore
Semaphores are integer variables that are used to solve the critical section problem by using two
atomic operations, wait and signal that are used for process synchronization.
• Wait –P( )
The wait operation decrements the value of its argument S, if it is positive. If S is negative
or zero, then no operation is performed.
wait(S)
{
while (S<=0);
S--;
}
•
Signal----V( )
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}
73
Dr. Ashish
Unit-2
UNi
o Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary semaphores. Details
about these are given as follows −
• Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate the resource access, where the semaphore count is the
number of available resources. If the resources are added, semaphore count automatically
incremented and if the resources are removed, the count is decremented.
• Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to 0 and
1. The wait operation only works when the semaphore is 1 and the signal operation
succeeds when semaphore is 0. It is sometimes easier to implement binary semaphores
than counting semaphores.
1. P operation is also called wait, sleep, or down operation, and V operation is also called signal,
wake-up, or up operation.
2. Both operations are atomic and semaphore(s) is always initialized to one. Here atomic means
that variable on which read, modify and update happens at the same time/moment with no
pre-emption i.e. in-between read, modify and update no other operation is performed that may
change the variable.
A critical section is surrounded by both operations to implement process synchronization. See the
below image. The critical section of Process P is in between P and V operation
74
Dr. Ashish
Unit-2
UNi
75
Dr. Ashish
Unit-2
UNi
76
Dr. Ashish
Unit-2
UNi
Solution
Variables used –
Readers Problem
while(TRUE)
77
Dr. Ashish
Unit-2
UNi
{
//a reader wishes to enter critical section
//this mutex is used for mutual exclusion before readcount is
changed
wait(mutex);
//this will ensure that now, other readers can enter critical
section
signal(mutex);
if(readers_count == 0)
// if readers_count is 0 we must restore w value to 1 so
writers can write
signal(wrt);
78
Dr. Ashish
Unit-2
UNi
• A philosopher can only eat if both immediate left and right chopsticks of the philosopher is
available. In case if both immediate left and right chopsticks of the philosopher are not
available then the philosopher puts down their (either left or right) chopstick and starts
thinking again.
• The dining philosopher demonstrates a large class of concurrency control problems hence it's
a classic synchronization problem.
• When a philosopher thinks, he does not interact with others. When he gets hungry, he tries to
pick up the two chopsticks that are near to him.
• For example, philosopher 1 will try to pick chopsticks 1 and 2. But the philosopher can pickup
only one chopstick at a time. He cannot take a chopstick that is already in the hands of his
neighbour.
• The philosopher stars to eat when he has both his chopsticks in his hand. After eating the
philosopher puts down both the chopsticks and starts to think again.
o Approach to Solution
Here is the simple approach to solving it:
1. We convert each fork as a binary semaphore. That implies there exists an array of
semaphore data types of forks having size = 5. It initially contains 1 at all positions.
2. Now when a philosopher picks up a fork he calls wait() on fork[i] which means that i'th
philosopher has acquired the fork.
3. If a philosopher has done eating he calls release (). That implies fork[i] is released and
any other philosopher can pick up this fork and can start eating.
79
Dr. Ashish
Unit-2
UNi
do{
wait(fork[i]);
wait(fork[(i + 1) % 5]);
//eat noodles
signal(fork[i]);
signal(fork[(i + 1) % 5]);
//think
}while(1);
From the above solution of the dining philosopher problem, we have proved that no two
neighboring philosophers can eat at the same point in time. The drawback of the above solution
is that this solution can lead to a deadlock condition. This situation happens if all the philosophers
pick their left chopstick at the same time, which leads to the condition of deadlock and none of
the philosophers can eat.
o Maximum number of philosophers on the table should not be more than four, in this case,
chopstick C4 will be available for philosopher P3, so P3 will start eating and after the finish
of his eating procedure, he will put down his both the chopstick C3 and C4, i.e. semaphore
C3 and C4 will now be incremented to 1. Now philosopher P2 which was holding chopstick
C2 will also have chopstick C3 available, hence similarly, he will put down his chopstick
after eating and enable other philosophers to eat.
o A philosopher at an even position should pick the right chopstick and then the left chopstick
while a philosopher at an odd position should pick the left chopstick and then the right
chopstick.
o Only in case if both the chopsticks ( left and right ) are available at the same time, only then
a philosopher should be allowed to pick their chopsticks
o All the four starting philosophers ( P0, P1, P2, and P3) should pick the left chopstick and then
the right chopstick, whereas the last philosopher P4 should pick the right chopstick and then
the left chopstick. This will force P4 to hold his right chopstick first since the right chopstick
of P4 is C0, which is already held by philosopher P0 and its value is set to 0, i.e C0 is already
0, because of which P4 will get trapped into an infinite loop and chopstick C4 remains vacant.
Hence philosopher P3 has both left C3 and right C4 chopstick available, therefore it will start
eating and will put down its both chopsticks once finishes and let others eat which removes
the problem of deadlock.
80
Dr. Ashish
Unit-2
UNi
81
Dr. Ashish
Unit-2
UNi
• Independent process.
• Co-operating process.
An independent process is not affected by the execution of other processes while a co-operating
process can be affected by other executing processes. Though one can think that those processes,
which are running independently, will execute very efficiently, in reality, there are many situations
when co-operative nature can be utilized for increasing computational speed, convenience, and
modularity. Inter-process communication (IPC) is a mechanism that allows processes to
communicate with each other and synchronize their actions. The communication between these
processes can be seen as a method of co-operation between them. Inter process communication
(IPC) is a process that allows different processes of a computer system to share information. IPC
lets different programs run in parallel, share data, and communicate with each other. It’s important
for two reasons: First, it speeds up the execution of tasks, and secondly, it ensures that the tasks
run correctly and in the order that they were executed.
3. Shared Memory
4. Message passing
• Communication between processes using shared memory requires processes to share some
variable, and it completely depends on how the programmer will implement it.
• Suppose process1 and process2 are executing simultaneously, and they share some resources or use
some information from another process. Process1 generates information about certain computations or
resources being used and keeps it as a record in shared memory.
• When process2 needs to use the shared information, it will check in the record stored in shared memory
and take note of the information generated by process1 and act accordingly. Processes can use shared
memory for extracting information as a record from another process as well as for delivering any
specific information to other processes.
82
Dr. Ashish
Unit-2
UNi
• Establish a communication link (if a link already exists, no need to establish it again.)
• Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destination) or send(message)
– receive(message, host) or receive(message)
The message size can be of fixed size or of variable size. If it is of fixed size, it is easy for an
OS designer but complicated for a programmer and if it is of variable size then it is easy for a
programmer but complicated for the OS designer. A standard message can have two
parts: header and body.
The header part is used for storing message type, destination id, source id, message length,
and control information. The control information contains information like what to do if runs
out of buffer space, sequence number, priority. Generally, message is sent using FIFO style.
Message Passing through Communication Link. Direct and Indirect Communication link
Direct Communication links are implemented when the processes use a specific process
identifier for the communication, but it is hard to identify the sender ahead of time.
For example the print server.
In-direct Communication is done via a shared mailbox (port), which consists of a queue of
messages. The sender keeps the message in mailbox and the receiver picks them up.
83
Dr. Ashish
Unit-2
UNi
84
Dr. Ashish
Unit-2
UNi
Semaphore Customers = 0;
Semaphore Barber = 0;
Mutex Seats = 1;
int FreeSeats = N;
Barber { Customer {
while(true) { while(true) {
/* waits for a customer (sleeps). * /* protects seats so only 1 cu
/ stomer tries to sit
down(Customers); in a chair if that's the case.*/
Read More
https://www.geeksforgeeks.org/inter-process-communication-
ipc/#:~:text=Inter%2Dprocess%20communication%20(IPC),Shared%20Memory
85
Dr. Ashish
Unit-2
UNi
Memory
Management
Memory Organization & Management: Memory Organization, Memory
Hierarchy, Memory Management Strategies, Contiguous versus non- Contiguous
memory allocation, Partition Management Techniques, Logical versus Physical
Address space, swapping, Paging, Segmentation, Segmentation with Paging
Virtual Memory: Demand Paging, Page Replacement, Page-replacement
Algorithms, Performance of Demand Paging, Thrashing, Demand Segmentation,
and Overlay Concepts.
86
Dr. Ashish
Unit-2
UNi
11 INTRODUCTION
• The term memory can be defined as a collection of data in a specific format. It is used to
store instructions and process data. The memory comprises a large array or group of words
or bytes, each with its own location. The primary purpose of a computer system is to
execute programs.
• These programs, along with the information they access, should be in the main memory
during execution. The CPU fetches instructions from memory according to the value of
the program counter.
• To achieve a degree of multiprogramming and proper utilization of memory, memory
management is important. Many memory management methods exist, reflecting various
approaches, and the effectiveness of each algorithm depends on the situation.
A memory unit is the collection of storage units or devices together. The memory unit stores the
binary information in the form of bits. Generally, memory/storage is classified into 2 categories:
• Volatile Memory: This loses its data, when power is switched off.
• Non-Volatile Memory: This is a permanent storage and does not lose any data when
power is switched off.
87
Dr. Ashish
Unit-2
UNi
1. Registers
Registers are small, high-speed memory units located in the CPU. They are used to store the most
frequently used data and instructions. Registers have the fastest access time and the smallest
storage capacity, typically ranging from 16 to 64 bits.
2. Cache Memory
Cache memory is a small, fast memory unit located close to the CPU. It stores frequently used
data and instructions that have been recently accessed from the main memory. Cache memory is
designed to minimize the time it takes to access data by providing the CPU with quick access to
frequently used data.
3. Main Memory
Main memory, also known as RAM (Random Access Memory), is the primary memory of a
computer system. It has a larger storage capacity than cache memory, but it is slower. Main
memory is used to store data and instructions that are currently in use by the CPU.
4. Secondary Storage
Secondary storage, such as hard disk drives (HDD) and solid-state drives (SSD), is a non-volatile
memory unit that has a larger storage capacity than main memory. It is used to store data and
instructions that are not currently in use by the CPU. Secondary storage has the slowest access
time and is typically the least expensive type of memory in the memory hierarchy.
5. Magnetic Disk
Magnetic Disks are simply circular plates that are fabricated with either a metal or a plastic or a
magnetized material. The Magnetic disks work at a high speed inside the computer and these are
frequently used.
6. Magnetic Tape
Magnetic Tape is simply a magnetic recording device that is covered with a plastic film. It is generally
used for the backup of data. In the case of a magnetic tape, the access time for a computer is a little slower
and therefore, it requires some amount of time for accessing the strip.
88
Dr. Ashish
Unit-2
UNi
To understand memory management, the difference between a physical (or absolute) address
and a logical (or virtual) address must be identified.
Physical Address – An address loaded into the memory address register. That is, the memory
placed on the physical address bus to retrieve or store memory.
Logical Address – An address generated by the CPU (before being placed in the memory address
register during execution).
11.3 Concept of Logical Address Space and Physical Address Space
• Physical Address Space: An address seen by the memory unit (i.e the one loaded into
the memory address register of the memory) is commonly known as a “Physical Address”.
A Physical address is also known as a Real address. The set of all physical addresses
corresponding to these logical addresses is known as Physical address space. A physical
89
Dr. Ashish
Unit-2
UNi
address is computed by MMU. The run-time mapping from virtual to physical addresses
is done by a hardware device Memory Management Unit (MMU). The physical address
always remains constant.
The MMU (Memory Management Unit) is the hardware which changes logical addresses to
physical addresses.
90
Dr. Ashish
Unit-2
UNi
91
Dr. Ashish
Unit-2
UNi
The choice of address binding type depends on factors such as program complexity, memory
requirements, and the desired level of flexibility. While compile-time binding offers simplicity
and efficiency, load-time binding and runtime binding provide more dynamic memory
management capabilities. The appropriate type of address binding is determined based on the
specific needs of the program and the operating system environment.
92
Dr. Ashish
Unit-2
UNi
o Simple to implement.
93
Dr. Ashish
Unit-2
UNi
o Wastage of memory space due to unused memory as the process is unlikely to use all the
available memory space.
o The CPU remains idle, waiting for the disk to load the binary image into the main memory.
o It cannot be executed if the program is too large to fit the entire available main memory
space.
94
Dr. Ashish
Unit-2
UNi
1. FIXED PARTITIONING
In this technique, the main memory is divided into partitions of equal or different sizes. The operating
system always resides in the first partition while the other partitions can be used tostore user processes.
The memory is assigned to the processes in contiguous way.
In fixed partitioning,
1. Internal Fragmentation
If the size of the process is lesser then the total size of the partition then some size of the partitionget
wasted and remain unused. This is wastage of the memory and called internal fragmentation.
As shown in the image below, the 4 MB partition is used to load only 3 MB process and theremaining
1 MB got wasted.
2. External Fragmentation
The total unused space of various partitions cannot be used to load the processes even though
there is space available but not in the contiguous form.
As shown in the image below, the remaining 1 MB space of each partition cannot be used as a unit
to store a 4 MB process. Despite of the fact that the sufficient space is available to load the process,
process will not be loaded.
If the process size is larger than the size of maximum sized partition then that process cannot be
loaded into the memory. Therefore, a limitation can be imposed on the process size that is it cannot
be larger than the size of the largest partition.
By Degree of multi programming, we simply mean the maximum number of processes that canbe
loaded into the memory at the same time. In fixed partitioning, the degree of multiprogramming is
fixed and very less due to the fact that the size of the partition cannot be varied according to the size
of processes.
95
Dr. Ashish
Unit-2
UNi
Dynamic partitioning tries to overcome the problems caused by fixed partitioning. In this technique,
the partition size is not declared initially. It is declared at the time of process loading.
The first partition is reserved for the operating system. The remaining space is divided into parts. The
size of each partition will be equal to the size of the process. The partition size varies according to the
need of the process so that the internal fragmentation can be avoided.
96
Dr. Ashish
Unit-2
UNi
1. No Internal Fragmentation
Given the fact that the partitions in dynamic partitioning are created according to the need of the
process, It is clear that there will not be any internal fragmentation because there will not be any unused
remaining space in the partition.
In Fixed partitioning, the process with the size greater than the size of the largest partition could not
be executed due to the lack of sufficient contiguous memory. Here, In Dynamic partitioning,the
process size can't be restricted since the partition size is decided according to the process size.
Due to the absence of internal fragmentation, there will not be any unused space in the partitionhence
more processes can be loaded in the memory at the same time.
External Fragmentation
Absence of internal fragmentation doesn't mean that there will not be external fragmentation.
Let's consider three processes P1 (1 MB) and P2 (3 MB) and P3 (1 MB) are being loaded in the
respective partitions of the main memory.
After some time P1 and P3 got completed and their assigned space is freed. Now there are twounused
partitions (1 MB and 1 MB) available in the main memory but they cannot be used to load a 2 MB
process in the memory since they are not contiguously located.
The rule says that the process must be contiguously present in the main memory to get executed.We
need to change this rule to avoid external fragmentation.
97
Dr. Ashish
Unit-2
UNi
In Fixed partitioning, the list of partitions is made once and will never change but in dynamic
partitioning, the allocation and deallocation is very complex since the partition size will be varied
every time when it is assigned to a new process. OS has to keep track of all the partitions.
Due to the fact that the allocation and deallocation are done very frequently in dynamic memory
allocation and the partition size will be changed at each time, it is going to be very difficult for OS to
manage everything.
98
Dr. Ashish
Unit-2
UNi
12 Compaction
• Compaction is a technique to collect all the free memory present in the form of fragments into one large
chunk of free memory, which can be used to run other processes.
• It does that by moving all the processes towards one end of the memory and all the available free space
towards the other end of the memory so that it becomes contiguous.
• It is not always easy to do compaction. Compaction can be done only when the relocation is dynamic and
done at execution time. Compaction can not be done when relocation is static and is performed at load
time or assembly time.
12.1 Purpose of Compaction in Operating System
While allocating memory to process, the operating system often faces a problem when there’s a
sufficient amount of free space within the memory to satisfy the memory demand of a process.
however the process’s memory request can’t be fulfilled because the free memory available is in
a non-contiguous manner, this problem is referred to as external fragmentation. To solve such kinds
of problems compaction technique is used.
12.2 Issues with Compaction
Although the compaction technique is very useful in making memory utilization efficient and reduces
external fragmentation of memory, the problem with it is that a large amount of time is wasted in the
process and during that time the CPU sits idle hence reducing the efficiency of the system.
99
Dr. Ashish
Unit-2
UNi
2. Best Fit Allocate the process to the partition which is the first smallest sufficient partition among the free
available partition. It searches the entire list of holes to find the smallest hole whose size is greater than or equal
to the size of the process.
3. Worst Fit Allocate the process to the partition which is the largest sufficient among the freely available
partitions available in the main memory. It is opposite to the best-fit algorithm. It searches the entire list of holes
to find the largest hole and allocate it to process.
100
Dr. Ashish
Unit-2
UNi
4. Next Fit: Next fit is similar to the first fit but it will search for the first sufficient partition from the last
allocation point.
Is Best-Fit really best?
Although best fit minimizes the wastage space, it consumes a lot of processor time for searching the block which
is close to the required size. Also, Best-fit may perform poorer than other algorithms in some cases. For example,
see the below exercise.
Exercise: Consider the requests from processes in given order 300K, 25K, 125K, and 50K. Let there be two
blocks of memory available of size 150K followed by a block size 350K.
Which of the following partition allocation schemes can satisfy the above requests?
A) Best fit but not first fit.
B) First fit but not best fit.
C) Both First fit & Best fit.
D) neither first fit nor best fit.
Solution: Let us try all options.
Best Fit:
300K is allocated from a block of size 350K. 50 is left in the block.
25K is allocated from the remaining 50K block. 25K is left in the block.
125K is allocated from 150 K block. 25K is left in this block also.
50K can’t be allocated even if there is 25K + 25K space available.
First Fit:
300K request is allocated from 350K block, 50K is left out.
25K is allocated from the 150K block, 125K is left out.
Then 125K and 50K are allocated to the remaining left out partitions.
So, the first fit can handle requests.
So option B is the correct choice.
101
Dr. Ashish
Unit-2
UNi
5. 25 K , 50 K , 100 K , 75 K
In the question, there are five partitions in the memory. 3 partitions are having processes inside
them and two partitions are holes.
Our task is to check the algorithm which can satisfy the request optimally.
1. 25 K requirement
The algorithm scans the list until it gets first hole which should be big enough to satisfy the
request of 25 K. it gets the space in the second partition which is free hence it allocates 25 K out
of 75 K to the process and the remaining 50 K is produced as hole.
102
Dr. Ashish
Unit-2
UNi
2. 50 K requirement
The 50 K requirement can be fulfilled by allocating the third partition which is 50 K in size to
the process. No free space is produced as free space.
3. 100 K requirement
100 K requirement can be fulfilled by using the fifth partition of 175 K size. Out of 175 K, 100 K
will be allocated and remaining 75 K will be there as a hole.
4. 75 K requirement
Since we are having a 75 K free partition hence we can allocate that much space to the process
which is demanding just 75 K space.
103
Dr. Ashish
Unit-2
UNi
Using first fit algorithm, we have fulfilled the entire request optimally and no useless space is
remaining.
Let's see, How Best Fit algorithm performs for the problem.
6. 25 K requirement
To allocate 25 K space using best fit approach, need to scan the whole list and then we find that a
75 K partition is free and the smallest among all, which can accommodate the need of the
process.
Therefore 25 K out of those 75 K free partition is allocated to the process and the remaining 5o
K is produced as a hole.
7. 50 K requirement
To satisfy this need, we will again scan the whole list and then find the 50 K space is free which
the exact match of the need is. Therefore, it will be allocated for the process.
8. 100 K requirement
100 K need is close enough to the 175 K space. The algorithm scans the whole list and then
allocates 100 K out of 175 K from the 5th free partition.
104
Dr. Ashish
Unit-2
UNi
9. 75 K requirement
75 K requirement will get the space of 75 K from the 6th free partition but the algorithm will
scan the whole list in the process of taking this decision.
By following both of the algorithms, we have noticed that both the algorithms perform similar to
most of the extant in this case.
Both can satisfy the need of the processes but however, the best fit algorithm scans the list again
and again which takes lot of time.
Therefore, if you ask me that which algorithm performs in more optimal way then it will be First
Fit algorithm for sure.
105
Dr. Ashish
Unit-2
UNi
• In Operating Systems, Paging is a storage mechanism used to retrieve processes from the secondary
storage into the main memory in the form of pages.
• The main idea behind the paging is to divide each process in the form of pages. The main memory will
also be divided in the form of frames.
• One page of the process is to be stored in one of the frames of the memory. The pages can be stored at the
different locations of the memory but the priority is always to find the contiguous frames or holes.
• Pages of the process are brought into the main memory only when they are required otherwise they reside
in the secondary storage.
• Different operating system defines different frame sizes. The sizes of each frame must be equal.
Considering the fact that the pages are mapped to the frames in Paging, page size needs to be as same as
frame size.
106
Dr. Ashish
Unit-2
UNi
• Let us consider the main memory size 16 Kb and Frame size is 1 KB therefore the main
memory will be divided into the collection of 16 frames of 1 KB each.
• There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB each. Each process is
divided into pages of 1 KB each so that one page can be stored in one frame.
• Initially, all the frames are empty therefore pages of the processes will get stored in the
contiguous way.
• Frames, pages and the mapping between the two is shown in the image below.
107
Dr. Ashish
Unit-2
UNi
Physical address space in a system can be defined as the size of the main memory. It is really important to
compare the process size with the physical address space. The process size must be less than the physical address
space.
Therefore,
Physical Address = 13 bits
In General,
If, Physical Address Space = N Words
then, Physical Address = log2 N Logical Address Space
Logical address space can be defined as the size of the process. The size of the process should be less enough so
that it can reside in the main memory.
108
Dr. Ashish
Unit-2
UNi
Let's say,
Logical Address Space = 128 MB = (2 ^ 7 X 2 ^ 20) Bytes = 2 ^ 27 Bytes Word size = 4 Bytes = 2 ^ 2 Bytes
Logical Address Space (in words) = (2 ^ 27) / (2 ^ 2) = 2 ^ 25 Words Logical Address = 25 Bits
In general,
If, logical address space = L words Then, Logical Address = Log2L bits
• Page Table is a data structure used by the virtual memory system to store the mapping between logical
addresses and physical addresses.
• Logical addresses are generated by the CPU for the pages of the processes therefore they are generally
used by the processes.
• Physical addresses are the actual frame address of the memory. They are generally used by the hardware
or more specifically by RAM subsystems.
109
Dr. Ashish
Unit-2
UNi
• The CPU always accesses the processes through their logical addresses. However, the main memory
recognizes physical address only.
• In this situation, a unit named as Memory Management Unit comes into the picture. It converts the
page number of the logical address to the frame number of the physical address. The offset remains
same in both the addresses.
• To perform this task, Memory Management unit needs a special kind of mapping which is done by
page table. The page table stores all the Frame numbers corresponding to the page numbers of the page
table.
• In other words, the page table maps the page number to its actual location (frame number) in the
memory.
• In the image given below shows, how the required word of the frame is accessed with the help of offset.
In operating systems, there is always a requirement of mapping from logical address to the physical address.
However, this process involves various steps which are defined as follows.
14.4.1 Generation of logical address
CPU generates logical address for each page of the process. This contains two parts: page number and offset.
14.4.2 Scaling
To determine the actual page number of the process, CPU stores the page table base in a special register. Each
time the address is generated, the value of the page table base is added to the page number to get the actual
location of the page entry in the table. This process is called scaling.
14.4.3 Generation of physical Address
The frame number of the desired page is determined by its entry in the page table. A physical address is generated
which also contains two parts : frame number and offset. The Offset will be similar to the offset of the logical
address therefore it will be copied from the logical address.
14.4.4 Getting Actual Frame Number
The frame number and the offset from the physical address is mapped to the main memory in order to get the
actual word address.
110
Dr. Ashish
Unit-2
UNi
o Performance of Paging :
Evaluating of paging performance is one of the important tasks. Consider the main memory access time is M and
the page table is stored in the main memory then the evaluating expression for effective memory access time is as
follows.
Along with page frame number, the page table also contains some of the bits representing the extra information
regarding the page.
Let's see what the each bit represents about the page.
1. Caching Disabled
Sometimes, there are differences between the information closest to the CPU and the information closest to the
user. Operating system always wants CPU to access user's data as soon as possible. CPU accesses cache which
can be inaccurate in some of the cases, therefore, OS can disable the cache for the required pages. This bit is set to
1 if the cache is disabled.
2. Referenced
There are variouspage replacement algorithms which will be covered later in this tutorial. This bit is set to 1 if the
page is referred in the last clock cycle otherwise it remains 0.
3. Modified
111
Dr. Ashish
Unit-2
UNi
This bit will be set if the page has been modified otherwise it remains 0.
4. Protection
The protection field represents the protection level which is applied on the page. It can be read only or read &
write or execute. We need to remember that it is not a bit rather it is a field which contains many bits.
5. Present/Absent
In the concept of demand paging, all the pages doesn't need to be present in the main memory Therefore, for all
the pages that are present in the main memory, this bit will be set to 1 and the bit will be 0 for all the pages which
are absent.
If some page is not present in the main memory then it is called page fault.
However, the part of the process which is being executed by the CPU must be present in the main memory during
that time period. The page table must also be present in the main memory all the time because it has the entry for
all the pages.
1. Logical Address = 24 bits
2. Logical Address space = 2 ^ 24 bytes
3. Let's say, Page size = 4 KB = 2 ^ 12 Bytes
4. Page offset = 12
5. Number of bits in a page = Logical Address - Page Offset = 24 - 12 = 12 bits
6. Number of pages = 2 ^ 12 = 2 X 2 X 10 ^ 10 = 4 KB
7. Let's say, Page table entry = 1 Byte
8. Therefore, the size of the page table = 4 KB X 1 Byte = 4 KB
112
Dr. Ashish
Unit-2
UNi
In translation look aside buffers, there are tags and keys with the help of which, the mapping is done.
TLB hit is a condition where the desired entry is found in translation look aside buffer. If this happens then the CPU simply
access the actual location in the main memory.
However, if the entry is not found in TLB (TLB miss) then CPU has to access page table in the main memory and then access
the actual frame in the main memory.
113
Dr. Ashish
Unit-2
UNi
• Let us assume 2 processes, P1 and P2, contains 4 pages each. Each page size is 1 KB. The main
memory contains 8 frame of 1 KB each. The OS resides in the first two partitions. In the third
partition, 1st page of P1 is stored and the other frames are also shown as filled with the different pages
of processes in the main memory.
• The page tables of both the pages are 1 KB size each and therefore they can be fit in one frame each.
The page tables of both the processes contain various information that is also shown in the image.
• The CPU contains a register which contains the base address of page table that is 5 in the case of P1
and 7 in the case of P2. This page table base address will be added to the page number of the Logical
address when it comes to accessing the actual corresponding entry.
114
Dr. Ashish
Unit-2
UNi
115
Dr. Ashish
Unit-2
UNi
16 Demand Paging
How does Demand Paging Work?
The demand paging system depends on the page table implementation because the page table helps map logical
memory to physical memory. Bitwise operators are implemented in the page table to indicate that pages are ok or
not (valid or invalid). All valid pages exist in primary memory, and invalid pages exist in secondary memory.
Now all processes go-ahead to access all pages, and then the following things will be happened, such as:
1. Attempt to currently access page.
2. If the page is ok (Valid), then all processing instructions work as normal.
3. If anyone's page is found invalid, then a page fault issue arises.
4. Now memory reference is determined whether a valid reference exists on the auxiliary memory or not. If
not exist, then the process is terminated, otherwise needed pages are paged in.
5. Now disk operations are implemented to fetch the required page into primary memory.
116
Dr. Ashish
Unit-2
UNi
1. If the CPU wants to access page P2 of a process P, it will first search the page in the page table.
2. As the page table does not contain this page so it will be a trapor page fault. The control goes to the
operating system as soon as the trap is generated and context switching happens.
3. The OS system will put the process in a waiting/ blocked state. The OS system will now search that page
in the backing store or secondary memory.
4. The OS will then read the page from the backing store and load it to the main memory.
5. Next, the OS system will update the page table entry accordingly.
6. Finally, the control is taken back from the OS, and the execution of the process is resumed.
Hence, the operating system follows these steps whenever a page fault occurs and the required page is brought
into memory.
So whenever a page fault occurs, as shown in all the above steps 2 to 6. This time taken to service the page fault is
called the Page fault service time.
Effective Memory Access Time: When the page fault rate is 'p' while executing any process, then the effective
memory access time is calculated as follows:
117
Dr. Ashish
Unit-2
UNi
There are two main aspects of virtual memory, Frame allocation and Page Replacement. It is very important to
have the optimal frame allocation and page replacement algorithm. Frame allocation is all about how many
frames are to be allocated to the process while the page replacement is all about determining the page number
which needs to be replaced in order to make space for the requested page.
Types of Page Replacement Algorithms
There are various page replacement algorithms. Each algorithm has a different method by which the pages can be
replaced.
1. Optimal Page Replacement algorithm → this algorithms replaces the page which will not be
referred for so long in future. Although it can not be practically implementable but it can be used as a
benchmark. Other algorithms are compared to this in terms of optimality.
2. Least recent used (LRU) page replacement algorithm → this algorithm replaces the page which
has not been referred for a long time. This algorithm is just opposite to the optimal page replacement
algorithm. In this, we look at the past instead of staring at future.
3. FIFO → in this algorithm, a queue is maintained. The page which is assigned the frame first will be
replaced first. In other words, the page which resides at the rare end of the queue will be replaced on
the every page fault.
118
Dr. Ashish
Unit-2
UNi
119
Dr. Ashish
Number of Page Faults in FIFO = 6
17.1 BELADY'SANOMALY
In the case of LRU and optimal page replacement algorithms, it is seen that the
number of page
faults will be reduced if we increase the number of frames. However, Balady
found that, In FIFO page replacement algorithm, the number of page faults will
get increased with the increment in number of frames.
This is the strange behavior shown by FIFO algorithm in some of the cases. This
is an Anomaly called as Belady'sAnomaly.
Let's examine such example :
The reference String is given as 0 1 5 3 0 1 4 0 1 5 3 4. Let's analyze the behavior of
FIFO algorithm in two cases.
Request 0 1 5 3 0 1 4 0 1 5 3 4
Frame 3 5 5 5 1 1 1 1 1 3 3
Frame 2 1 1 1 0 0 0 0 0 5 5 5
Frame 1 0 0 0 3 3 3 4 4 4 4 4 4
Miss/Hit Miss Miss Miss Miss Miss Miss Miss Hit Hit Miss Miss Hit
[email protected]
Case 2: Number of frames = 4
Req uest
0 1 5 3 0 1 4 0 1 5 3 4
Frame 4 3 3 3 3 3 3 5 5 5
Fra me 3 5 5 5 5 5 5 1 1 1 1
Fra me 2 1 1 1 1 1 1 0 0 0 0 4
Fra me 1
0 0 0 0 0 0 4 4 4 4 3 3
[email protected]
• SEGMENTATION
In Operating Systems, Segmentation is a memory management technique in which,
the memory is divided into the variable size parts. Each part is known as segment
which can be allocated to a process.
The details about each segment are stored in a table called as segment table. Segment
table is stored in one (or many) of the segments.
[email protected]
[email protected]
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compare to the page table in paging.
Disadvantages
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.
Paging VS Segmentation
Sr No Paging Segmentation
2 Paging divides program into fixed size Segmentation divides program into
pages. variable size segments.
3 OS is responsible Compiler is responsible.
9 Page table is used to maintain the page Segment Table maintains the segment
information. information
10 Page table entry has the frame number Segment table entry has the base address of
and some flag bits to represent details the segment and some protection bits for the
about pages. segments.
[email protected]
18 Paging with segmentation
INTRODUCTION:
• Paged Segmentation and Segmented Paging are two different memory management
techniques that combine the benefits of paging and segmentation.
1. Paged Segmentation is a memory management technique that divides a process’s
address space into segments and then divides each segment into pages. This allows
for a flexible allocation of memory, where each segment can have a different size,
and each page can have a different size within a segment.
2. Segmented Paging, on the other hand, is a memory management technique that
divides the physical memory into pages, and then maps each logical address used
by a process to a physical page. In this approach, segments are used to map virtual
memory addresses to physical memory addresses, rather than dividing the virtual
memory into pages.
3. Both Paged Segmentation and Segmented Paging provide the benefits of paging,
such as improved memory utilization, reduced fragmentation, and increased
performance. They also provide the benefits of segmentation, such as increased
flexibility in memory allocation, improved protection and security, and reduced
overhead in memory management.
o Segmented Paging
The memory management unit (MMU) will use the segment table
which will contain the address of page table(base) and limit. The page
table will point to the page frames of the segments in main memory.
[email protected]
Advantages of Segmented Paging
1. The page table size is reduced as pages are present only for data of segments,
hence reducing the memory requirements.
2. Gives a programmers view along with the advantages of paging.
3. Reduces external fragmentation in comparison with segmentation.
4. Since the entire segment need not be swapped out, the swapping out into
virtual memory becomes easier .
Disadvantages of Segmented Paging
1. Internal fragmentation still exists in pages.
2. Extra hardware is required
3. Translation becomes more sequential increasing the memory access time.
[email protected]
4. External fragmentation occurs because of varying sizes of page tables and
varying sizes of segment tables in today’s systems.
o Paged Segmentation
1. In segmented paging, not every process has the same number of segments and the
segment tables can be large in size which will cause external fragmentation due to
the varying segment table sizes. To solve this problem, we use paged
segmentation which requires the segment table to be paged. The logical address
generated by the CPU will now consist of page no #1, segment no, page no #2 and
offset.
2. The page table even with segmented paging can have a lot of invalid pages. Instead
of using multi level paging along with segmented paging, the problem of larger page
table can be solved by directly applying multi level paging instead of segmented
paging.
[email protected]
19 Overlays in Memory Management
One of the main limitations imposed on programmers in the early days of computers
was the size of the computer's memory. If the program was larger than the available
memory, it could not be loaded, severely restricting program size. The main
problem with fixed partitioning is that the size of a process has to be limited by the
maximum size of the partition, which means a process can never span over another.
The obvious solution would be to increase the amount of memory available, but this
would significantly increase the cost of the computer system. To solve this problem,
earlier people have used some solution called Overlays.
The concept of overlays is that it will not use the complete program simultaneously
whenever a process is running. It will use only some part of it. Then overlays
concept says that whatever part you require, you load it, and once the part is
done, you just unload it, which means pull it back and get the new part you
required and run it.
Formally, "The process of transferring a block of program code or other data into
internal memory, replacing what is already stored".
Sometimes it happens that compared to the size of the biggest partition, the size
of the program will be even more. Then, in that case, you should go with
overlays.
[email protected]
So overlay is a technique to run a program that is bigger than the size of the physical
memory by keeping only those instructions and data that are needed at any given time.
Divide the program into modules so that not all modules need to be in the memory
simultaneously. In memory management, overlays work in the following steps, such as:
The best example of overlays is assembler. Consider the assembler has 2 passes, 2 pass
means at any time it will be doing only one thing, either the 1st pass or the 2nd pass.
This means it will finish 1st pass first and then 2nd pass. Let's assume that the available
main memory size is 150KB and the total code size is 200KB.
1. Pass 1.......................70KB
2. Pass 2.......................80KB
3. Symbol table............30KB
4. Common routine......20KB
As the total code size is 200KB and the main memory size is 150KB, it is impossible
to use 2 passes together. So, in this case, we should go with the overlays technique.
20 Thrashing
In case, if the page fault and swapping happens very frequently at a higher rate, then
the operating system has to spend more time swapping these pages. This state in the
operating system is termed thrashing. Because of thrashing the CPU utilization is
going to be reduced.
Let's understand by an example, if any
process does not have the number of
frames that it needs to support pages in
active use then it will quickly page fault.
And at this point, the process must replace
some pages. As all the pages of the
process are actively in use, it must replace
[email protected]
a page that will be needed again right away. Consequently, the process will quickly
fault again, and again, and again, replacing pages that it must bring back in
immediately. This high paging activity by a process is called thrashing.
During thrashing, the CPU spends less time on some actual productive work spend
more time swapping.
Causes of Thrashing
[email protected]
thrashing. As it is also proved that there are many disadvantages of Local Page
replacement. Thus local page replacement is simply an alternative to Global Page
replacement.
Techniques used to handle the thrashing
As we have already told you the Local Page replacement is better than the Global
Page replacement but local page replacement has many disadvantages too, so it is not
suggestible. Thus given below are some other techniques that are used:
Page Fault Frequency
[email protected]
Question on TLB
GATE | GATE-CS-2014-(Set-3)
Consider a paging hardware with a TLB. Assume that the entire page
table and all the pages are in the physical memory. It takes 10
milliseconds to search the TLB and 80 milliseconds to accessthe
physical memory. If the TLB hit ratio is 0.6, the effective memory
access time (in milliseconds) is .
A. 120
B. 122
C. 124
D. 118
Given,
Q. Consider a main memory with five page frames and the following
sequence of page references: 3, 8, 2, 3, 9, 1, 6, 3, 8, 9, 3, 6, 2, 1, 3.
which one of the following is true with respectto page replacement
policies First-In-First-out (FIFO) and Least Recently Used (LRU)?
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Syllabus
UNIT-III
UNIT – IV
File System: Introduction, File Organization, Logical File System, Physical File
System, File Allocation strategy, Free Space Management, File Access Control, Data
Access Techniques, Data Integrity Protection, Case study on file system viz FAT32,
[email protected]
21 Deadlock
Every process needs some resources to complete its execution. However, the resource is
granted in a sequential order.
Let us assume that there are three processes P1, P2 and P3. There are three different
resources R1, R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned to
P3.
After some time, P1 demands for R1 which is being used by P2. P1 halts its execution since
it can't complete without R2. P2 also demands for R3 which is being used by P3. P2 also
stops its execution because it can't continue without R3. P3 also demands for R1 which is
being used by P1 therefore P3 also stops its execution.
[email protected]
A resource can only be shared in mutually exclusive manner. It implies, if two process
cannot use the same resource at the same time.
• A process waits for some resources while holding another resource at the same time.
• Hold and wait is when a process is holding a resource and waiting to acquire another
resource that it needs but cannot proceed because another process is keeping the first
resource. Each of these processes must have a hold on at least one of the resources it’s
requesting.
3. No preemption
• The process which once scheduled will be executed till the completion. No other
process can be scheduled by the scheduler meanwhile.
• Preemption means temporarily interrupting a task or process to execute another task or
process. Preemption can occur due to an external event or internally within the system.
If we take away the resource from the process that is causing deadlock, we can avoid
deadlock. But is it a good approach? The answer is NO because that will lead to an
inconsistent state.
• For example, if we take away memory from any process(whose data was in the process
of getting stored) and assign it to some other process. Then will lead to an inconsistent
state.
4. Circular Wait
All the processes must be waiting for the resources in a cyclic manner so that the last
process is waiting for the resource which is being held by the first process.
[email protected]
21.2 Methods For Handling Deadlocks
Methods that are used in order to handle the problem of deadlocks are as follows:
Basically, with the help of this method, the operating system keeps an eye on each
allocation, and make sure that allocation does not cause any deadlock in the system.
[email protected]
22 Deadlock Prevention
If we simulate deadlock with a table which is standing on its four legs then we can also
simulate four legs with the four conditions which when occurs simultaneously, cause the
deadlock.
However, if we break one of the legs of the table then the table will fall definitely. The same
happens with deadlock, if we can be able to violate one of the four necessary conditions and
don't let them occur together then we can prevent the deadlock.
22.1.1 Spooling
For a device like printer, spooling can work. There is a memory associated with the printer
which stores jobs from each of the process into it. Later, Printer
collects all the jobs and print each one of them according
to FCFS. By using this mechanism, the process doesn't
have to wait for the printer and it can continue whatever it
was doing. Later, it collects the output when it is produced.
We cannot force a resource to be used by more than one process at the same time since it will
not be fair enough and some serious problems may arise in the performance. Therefore, we
cannot violate mutual exclusion for a process practically.
Hold and wait condition lies when a process holds a resource and waiting for some other
resource to complete its task. Deadlock occurs because there can be more than one process
which are holding one resource and waiting for other in the cyclic order.
[email protected]
• However, we have to find out some mechanism by which a process either doesn't hold any
resource or doesn't wait. That means, a process must be assigned all the necessary resources
before the execution starts. A process must not wait for any resource once the execution
has been started.
• This can be implemented practically if a process declares all the resources initially.
However, this sounds very practical but can't be done in the computer system because a
process can't determine necessary resources initially.
• Process is the set of instructions which are executed by the CPU. Each of the instruction
may demand multiple resources at the multiple times. The need cannot be fixed by the OS.
22.3 No Preemption
• Deadlock arises due to the fact that a process can't be stopped once it starts. However, if
we take the resource away from the process which is causing deadlock then we can prevent
deadlock.
• This is not a good approach at all since if we take a resource away which is being used by
the process then all the work which it has done till now can become inconsistent.
• Consider a printer is being used by any process. If we take the printer away from that
process and assign it to some other process then all the data which has been printed can
become inconsistent and ineffective and also the fact that the process can't start printing
again from where it has left which causes performance inefficiency.
[email protected]
23 Deadlock Ignorance or Ostrich Algorithm
• The simplest approach is the ostrich algorithm: stick your head in the sand and pretend
there is no problem at all.
• Different people react to this strategy in different ways. Mathematicians find it totally
unacceptable and say that deadlocks must be prevented at all costs.
• Engineers ask how often the problem is expected, how often the system crashes for other
reasons, and how serious a deadlock is.
• If deadlocks occur on average once every five years, but system crashes due to hardware
failures, compiler errors, and operating system bugs occur once a week, most engineers
would not be willing to pay a large penalty in performance or convenience to eliminate
deadlocks.
• Most operating systems, including UNIX and Windows, just ignore the problem on the
assumption that most users would prefer an occasional deadlock to a rule restricting all
users to one process, one open file, and one of everything.
• If deadlocks could be eliminated for free, there would not be much discussion. The problem
is that the price is high, mostly in terms of putting inconvenient restrictions on processes,
as we will see shortly. Thus we are faced with an unpleasant trade-off between convenience
and correctness, and a great deal of discussion about which is more important, and to whom.
Under these conditions, general solutions are hard to find.
24 Deadlock Avoidance in OS
• The operating system avoids Deadlock by knowing the maximum resource requirements
of the processes initially, and also, the Operating System knows the free resources available
at that time.
• The operating system tries to allocate the resources according to the process requirements
and checks if the allocation can lead to a safe state or an unsafe state. If the resource
allocation leads to an unsafe state, then the Operating System does not proceed further with
the allocation sequence.
Safe State and Unsafe State
Safe State – If Operating System is able to satisfy the needs of all
processes with their resource requirements. So all the processes are able
to complete their execution in a certain order. So, If the Operating System
is able to allocate or satisfy the maximum resource requirements of all
the processes in any order then the system is said to be in Safe State. So
safe state does not lead to Deadlock.
Unsafe State - If the Operating System is not able to prevent Processes
from requesting resources which can also lead to a Deadlock, then the
System is said to be in an Unsafe State.
Unsafe State does not necessarily cause deadlock it may or may not cause deadlock.
[email protected]
24.1 Banker’s Algorithm
Example:
Considering a system with five processes P0 through P4 and three resources of type A, B, C. Resource
type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time t0 following
snapshot of the system has been taken:
[email protected]
[email protected]
25 Deadlock Detection and Recovery
25.1 Deadlock Detection
• When a deadlock is detected, the system can initiate recovery procedures to break the
deadlock and restore the system to a functional state.
• It is essential to monitor and detect deadlocks as early as possible to prevent any negative
impact on the system's performance and stability. A delay in detecting deadlocks can result
in significant wait times for processes and unresponsive systems, leading to user frustration
and potential business losses. There are two primary methods for detecting deadlocks:
resource allocation graph (RAG) and wait-for graph (WFG).
[email protected]
25.1.2 Wait-For Graph (WFG)
The wait-for graph (WFG) is a common method used in
deadlock detection in computer systems. The WFG is a
graphical representation of the dependencies between the
processes and the resources that they are waiting for. In the
WFG, the nodes represent processes, and the resources are
represented as edges. Each edge points from the process
that is waiting for a resource to the process that currently
holds that resource. The WFG method can efficiently
detect deadlocks by analysing the graph for cycles. If a
cycle is found, it indicates that a set of processes is waiting
for resources that are being held by other processes in the same set, resulting in a deadlock.
The system can then take appropriate actions to break the deadlock, such as rolling back the
transactions or aborting some of the processes.
A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is
a time and space-consuming process. Real-time operating systems use Deadlock recovery.
1. Killing the process –Killing all the processes involved in the deadlock. Killing process one by one.
After killing each process check for deadlock again and keep repeating the process till the system
recovers from deadlock. Killing all the processes one by one helps a system to break circular wait
conditions.
2. Resource Preemption –Resources are preempted from the processes involved in the deadlock, and
preempted resources are allocated to other processes so that there is a possibility of recovering the
system from the deadlock. In this case, the system goes into starvation.
3. Concurrency Control – Concurrency control mechanisms are used to prevent data inconsistencies
in systems with multiple concurrent processes. These mechanisms ensure that concurrent processes
do not access the same data at the same time, which can lead to inconsistencies and errors.
Deadlocks can occur in concurrent systems when two or more processes are blocked, waiting for
[email protected]
each other to release the resources they need. This can result in a system-wide stall, where no
process can make progress. Concurrency control mechanisms can help prevent deadlocks by
managing access to shared resources and ensuring that concurrent processes do not interfere with
each other.
Then,
Available
=[00]+[01]
=[01]
Step-02:
Then-Available
=[01]+[10]
=[11]
Step-03:
Available
=[11]+[01]
=[12]
Thus,
[email protected]
References
1. https://www.geeksforgeeks.org/deadlock-
prevention/#:~:text=Deadlock%20avoidance%20is%20another%20technique,that%20cou
ld%20lead%20to%20deadlocks.
2. https://www.javatpoint.com/os-deadlock-avoidance
3. https://www.scaler.com/topics/operating-system/deadlock-avoidance-in-os/
4. https://www.tutorialspoint.com/deadlock-avoidance
5. https://www.studytonight.com/operating-system/deadlock-avoidance-in-operating-system
[email protected]
What is Disk Scheduling Algorithm?
Seek Time - It is the time taken by the disk arm to locate the desired track.
Rotational Latency - The time taken by a desired sector of the disk to rotate itself to the
position where it can access the Read/Write heads is called Rotational Latency.
Transfer Time - It is the time taken to transfer the data requested by the processes.
Disk Access Time - Disk Access time is the sum of the Seek Time, Rotational Latency, and
Transfer Time.
[email protected]
[email protected]
[email protected]
[email protected]
(199-0); Calculate Two
Answers, Ans 1. Add
199 in Total 2. Without
adding 199
[email protected]
[email protected]
2.4 Operating System Considerations
• The operating system performs several functions that can have a significant impact on database application
performance.
• The operating system schedules threads of control (called processes in some operating systems), the
fundamental units of execution in a computer system. Issues here include the amount of time needed to
schedule a new thread (the less the better); the time slice of a database thread (should be long); and whether
different threads should have different priorities (database threads should all run at the same priority for most
applications). Some database systems such as Sybase implement threads within operating system processes
so the overhead of thread switching is potentially lower.
• The operating system manages virtual and physical memory mappings. The issue here is how big to make the
portion of virtual memory that is shared by all database threads, the database buffer, and how much random
access memory to devote to that buffer.
• The operating system controls the number of user threads that can access the database concurrently. The goal
is to have enough to accommodate the available users while avoiding thrashing.
• The operating system manages files. Issues here include whether the files can span devices (necessary for
large databases); whether the files can be built from contiguous portions of the disk (helpful for scan
performance); whether file reads can perform lookahead (also helpful for scan performance)
[email protected]
File-System
1 File Concepts, Attributes, Operations
1.1 File
• A file is a named collection of related information that is recorded on secondary storage such as magnetic
disks, magnetic tapes and optical disks.
• In general, a file is a sequence of bits, bytes, lines or records whose meaning is defined by the files creator
and user.
• Image file
An image file is a sequence of visual information, for example, vector art.
[email protected]
o Identifier ( e.g. inode number )
o Type - Text, executable, other binary, etc.
o Location - on the hard drive.
o Size
o Protection
o Time & Date
o User ID
1.4 File Operations
• The file ADT (Abstract Data Type) supports many common operations:
o Creating a file
o Writing a file
o Reading a file
o Repositioning within a file
o Deleting a file
o Truncating a file.
• Most OSes require that files be opened before access and closed after all access is complete. Normally
the programmer must open and close files explicitly, but some rare systems open the file automatically
at first access. Information about currently open files is stored in an open file table, containing for
example:
o File pointer - records the current position in the file, for the next read or write access.
o File-open count - How many times has the current file been opened ( simultaneously by
different processes ) and not yet closed? When this counter reaches zero the file can be removed from
the table.
o Disk location of the file.
o Access rights
• Some systems provide support for file locking.
o A shared lock is for reading only.
o A exclusive lock is for writing as well as reading.
o An advisory lock is informational only, and not enforced. ( A "Keep Out" sign, which may be ignored.
)
o A mandatory lock is enforced. ( A truly locked door. )
o UNIX used advisory locks, and Windows uses mandatory locks.
[email protected]
1.5 File Types
Windows ( and some other systems ) use special file extensions to indicate the type of each file:
• Some files contain an internal structure, which may or may not be known to the OS.
• For the OS to support particular file formats increases the size and complexity of the OS.
• UNIX treats all files as sequences of bytes, with no further consideration of the internal structure. (With
the exception of executable binary programs, which it must know how to load and find the first
executable statement, etc )
• Macintosh files have two forks - a resource fork, and a data fork. The resource fork contains information
relating to the UI, such as icons and button images, and can be modified independently of the data fork,
which contains the code or data as appropriate.
[email protected]
Internal File Structure
• Disk files are accessed in units of physical blocks, typically 512 bytes or some power-of-two multiple
thereof. (Larger physical disks use larger block sizes, to keep the range of block numbers within the
range of a 32-bit integer. )
• Internally files are organized in units of logical units, which may be as small as a single byte, or may be
a larger size corresponding to some data record or structure size.
• The number of logical units which fit into one physical block determines its packing, and has
an impact on the amount of internal fragmentation (wasted space) that occurs.
• As a general rule, half a physical block is wasted for each file, and the larger the block sizes the more
space is lost to internal fragmentation.
[email protected]
2 File System Structure
• Hard disks have two important properties that make them suitable for secondary storage of files in file
systems: (1) Blocks of data can be rewritten in place, and (2) they are direct access, allowing any block
of data to be accessed with only ( relatively ) minor movements of the disk heads and rotational latency.
• Disks are usually accessed in physical blocks, rather than a byte at a time. Block sizes may range from
512 bytes to 4K or larger.
• File systems organize storage on disk drives, and can be viewed as a layered design:
o At the lowest layer are the physical devices, consisting of the magnetic media, motors & controls, and
the electronics connected to them and controlling them. Modern disk put more and more of the electronic
controls directly on the disk drive itself, leaving relatively little work for the disk controller card to
perform.
o I/O Control consists of device drivers, special software programs ( often written in assembly ) which
communicate with the devices by reading and writing special codes directly to and from memory
addresses corresponding to the controller card's registers. Each controller card ( device ) on a system
has a different set of addresses ( registers, a.k.a. ports ) that it listens to, and a unique set of command
codes and results codes that it understands.
o The basic file system level works directly with the device drivers in terms of retrieving and storing raw
blocks of data, without any consideration for what is in each block. Depending on the system, blocks
may be referred to with a single block number, ( e.g. block # 234234 ), or with head-sector-cylinder
combinations.
o The file organization module knows about files and their logical blocks, and how they map to physical
blocks on the disk. In addition to translating from logical to
physical blocks, the file organization module also maintains the list of free blocks, and allocates free
blocks to files as needed.
o The logical file system deals with all of the meta data associated with a file ( UID, GID, mode, dates, etc
), i.e. everything about the file except the data itself. This level manages the directory structure and the
mapping of file names to file control blocks, FCBs, which contain all of the meta data as well as block
number information for finding the data on the disk.
• The layered approach to file systems means that much of the code can be used uniformly for a wide
variety of different file systems, and only certain layers need to be filesystem specific. Common file
systems in use include the UNIX file system, UFS, the Berkeley Fast File System, FFS, Windows
systems FAT, FAT32, NTFS, CD-ROM systems ISO 9660, and for Linux the extended file systems ext2
and ext3 ( among 40 others supported)
[email protected]
• Layered file system.
An ILE C/C++ program can use either externally described or program-described files. If it uses an externally
described file, the ILE C/C++ compiler can extract information from the externally described file, and
automatically include field information in your program. Your program does not need to define the field
information. For further information see “Using Externally Described Files in Your Programs”. A physical file
can have a keyed sequence access path. This means that data is presented to an ILE C/C++ program in a sequence
that is based on one or more key fields in the file.
2. Logical files: Logical files do not contain data. They contain a description of records that are found in one or
more physical files. A logical file is a view or representation of one or more physical files. Logical files that
contain more than one format are referred to as multi-format logical files. If your program processes a logical
file that contains more than one record format, you can use the _Rformat() function to set the format you wish
to use. Some operations cannot be performed on logical files. If you open a logical file for stream file processing
[email protected]
with open modes W, W+, WB, or WB+, the file is opened but not cleared. If you open a logical file for record
file processing with open modes WR or WR+, the file is opened but not cleared. Records in iSeries database
files can be described using either a field-level description or a record-level description. The field-level
description of the record includes a description of all fields and their arrangement in this record. Since the
description of the fields and their arrangement is kept within a database file and not in your ILE C/C++ program,
database files created with a field-level description are referred to as externally described files.
It takes up space in memory, and it includes the original It doesn't take up any memory space. It does not include
data. data.
It exists without a Logical file. A logical file cannot exist without a physical file.
The CRTPF command is utilized to make such an The CRTLF command is utilized to make such an
object in a physical file. object in a logical file system.
If there is a logical file for a physical file, the physical If a physical file has a logical file, the logical file can be
file may not be erased until the logical file is deleted. removed without deleting the actual file.
[email protected]
4 File/Disk Space Allocation Methods
The allocation methods define how the files are stored in the disk blocks. There
are three main disk space or file allocation methods.
4.1 Contiguous Allocation
• Contiguous Allocation requires that all blocks of a file be kept together contiguously.
• Performance is very fast, because reading successive blocks of the same file generally requires no
movement of the disk heads, or at most one small step to the next adjacent cylinder.
• Storage allocation involves the same issues discussed earlier for the allocation of contiguous blocks of
memory ( first fit, best fit, fragmentation problems, etc. ) The distinction is that the high time penalty
required for moving the disk heads from spot to spot may now justify the benefits of keeping files
contiguously when possible.
• ( Even file systems that do not by default store files contiguously can benefit from certain utilities that
compact the disk and make all files contiguous in the process. )
• Problems can arise when files grow, or if the exact size of a file is unknown at creation time:
o Over-estimation of the file's final size increases external fragmentation and wastes disk space.
o Under-estimation may require that a file be moved or a process aborted if the file grows beyond its
originally allocated space.
o If a file grows slowly over a long time period and the total final space must be allocated initially, then a
lot of space becomes unusable before the file fills the space.
• A variation is to allocate file space in large contiguous chunks, called extents.
When a file outgrows its original extent, then an additional one is allocated. ( For example an extent may
be the size of a complete track or even cylinder, aligned on an appropriate track or cylinder boundary. )
The high- performance files system Veritas uses extents to optimize performance.
[email protected]
Contiguous allocation of disk space.
4.2 Linked Allocation
• Disk files can be stored as linked lists, with the expense of the storage space consumed by each link. (
E.g. a block may be 508 bytes instead of 512. )
• Linked allocation involves no external fragmentation, does not require pre-known file sizes, and allows
files to grow dynamically at any time.
• Unfortunately linked allocation is only efficient for sequential access files, as random access requires
starting at the beginning of the list for each new location access.
• Allocating clusters of blocks reduces the space wasted by pointers, at the cost of internal fragmentation.
• Another big problem with linked allocation is reliability if a pointer is lost or damaged. Doubly linked
lists provide some protection, at the cost of additional overhead and wasted space.
[email protected]
Linked allocation of disk space.
• The File Allocation Table, FAT, used by DOS is a variation of linked allocation, where all the links are
stored in a separate table at the beginning of the disk. The benefit of this approach is that the FAT table
can be cached in memory, greatly improving random access speeds.
• File-allocation table.
4.3 Indexed Allocation
• Indexed Allocation combines all of the indexes for accessing each file into a common
block ( for that file ), as opposed to spreading them all over the disk or storing them in a
FAT table.
[email protected]
• Indexed allocation of disk space.
• Some disk space is wasted ( relative to linked lists or FAT tables ) because an entire index block must
be allocated for each file, regardless of how many data blocks the file contains. This leads to questions
of how big the index block should be, and how it should be implemented. There are several approaches:
o Linked Scheme - An index block is one disk block, which can be read and written in a single disk
operation. The first index block contains some header information, the first N block addresses, and if
necessary a pointer to additional linked index blocks.
o Multi-Level Index - The first index block contains a set of pointers to secondary index blocks, which in
turn contain pointers to the actual data blocks.
o Combined Scheme - This is the scheme used in UNIX inodes, in which the first 12 or so data block
pointers are stored directly in the inode, and then
singly, doubly, and triply indirect pointers provide access to more data blocks as needed.
( See below. ) The advantage of this scheme is that for small files ( which many are ), the
data blocks are readily accessible ( up to 48K with 4K block sizes ); files up to about
4144K ( using 4K blocks ) are accessible with only a single indirect block ( which can be
[email protected]
cached ), and huge files are still accessible using a relatively small number of disk
accesses ( larger in theory than can be addressed by a 32-bit address, which is why some
systems have moved to 64-bit file pointers. )
• The optimal allocation method is different for sequential access files than for random
access files, and is also different for small files than for large files.
• Some systems support more than one allocation method, which may require specifying
how the file is to be used ( sequential or random access ) at the time it is allocated. Such
systems also provide conversion utilities.
• Some systems have been known to use contiguous access for small files, and
automatically switch to an indexed scheme when file sizes surpass a certain threshold.
• And of course some systems adjust their allocation schemes ( e.g. block sizes ) to best
match the characteristics of the hardware for optimum performance.
5 Free-Space Management
Free space management is a critical aspect of operating systems as it involves managing the
available storage space on the hard disk or other secondary storage devices. The operating system
uses various techniques to manage free space and optimize the use of storage devices. Here are
some of the commonly used free space management techniques. The operating system allocates
memory space to the files in the hard disk. After allocating memory to the files in the hard disk,
there is free space or void space left for other files. Moreover, different files get stored and deleted
in the memory simultaneously.
We can find the free block number from the bit vector using the following method-
Block number = (Number of bits per word )* (number of 0-value words) + (offset of first bit)
We will now find the first free block number in the above example.
The first group of 8 bits (00111100) constitutes a non-zero word since all bits are not 0. After
finding the non-zero word, we will look for the first 1 bit. This is the third character of the non-
zero word. Hence, offset = 3.
Therefore, the first free block number = 8 * 0 + 3 = 3.
• Advantages
• It is simple to understand.
• It is an efficient method.
• It occupies less memory.
• Disadvantages
• For finding a free block, the operating system may need to search the entire bit vector.
• To detect the first 1 in a word that is not 0 using this method, special hardware support
is needed.
• Keeping the bit vector in the main memory is possible for smaller disks but not for
larger ones. For example, a 1.3 GB disk with 512-byte blocks would need a bit vector
of over 332 KB to track its free blocks. Giving away 332 KB just to maintain its free
block space is not so efficient in the long run.
5.2 Linked List
• Disadvantages
• This method is inefficient since we need to read each block to traverse the list, which
takes more I/O time.
5.4 Counting
• When there are multiple contiguous blocks of free space then the system can keep track of
the starting address of the group and the number of contiguous free blocks. As long as the
average length of a contiguous group of free blocks is greater than two this offers a savings
in space needed for the free list. ( Similar to compression techniques used for graphics
images when a group of pixels all the same color is encountered.)
• This method takes advantage of the fact that several contiguous blocks may be allocated or
freed simultaneously. In this method, a linked list is maintained but in addition to the
pointer to the next free block, a count of free contiguous
blocks that follow the first block is also maintained. Thus
each free block in the disk will contain two things-
1. A pointer to the next free block.
For example, consider a disk having 16 blocks where block numbers 3, 4, 5, 6, 9, 10, 11, 12, 13,
and 14 are free, and the rest of the blocks, i.e., block numbers 1, 2, 7, 8, 15 and 16 are allocated to
some files.
If we apply the counting method, Block 3 will point to Block 4 and store the count 4 (since Block
3, 4, 5, and 6 are contiguous). Similarly, Block 9 will point to Block 10 and keep the count of 6
(since Block 9, 10, 11, 12, 13, and 14 are contiguous). This is also represented in the following
figure-
This method also overcomes the disadvantages of the linked list method since there is no need to
traverse the whole list.
• The resulting data structures could be VERY inefficient if not implemented carefully. For
example, freeing up a 1 GB file on a 1 TB file system could involve updating thousands of
blocks of free list bit maps if the file was spread across the disk.
• ZFS uses a combination of techniques, starting with dividing the disk up into ( hundreds
of ) metaslabs of a manageable size, each having their own space map.
• Free blocks are managed using the counting technique, but rather than write the
information to a table, it is recorded in a log-structured transaction record. Adjacent free
blocks are also coalesced into a larger single free block.
• An in-memory space map is constructed using a balanced tree data structure, constructed
from the log data.
• The combination of the in-memory tree and the on-disk log provide for very fast and
efficient management of these very large files and free blocks.
• Sequential-access file.
6.2 Direct Access
• Jump to any record and read that record. Operations supported include:
o read n - read record number n. ( Note an argument is now required. )
o write n - write record number n. ( Note an argument is now required. )
o jump to record n - could be 0 or the end of file.
o Query current record - used to return back to this record later.
o Sequential access can be easily emulated using direct access. The inverse is complicated
and inefficient.
• Simulation of sequential access on a direct-access file.
For the Better Classification of the files, the Files will be stored in Directory
Directory == Folder
• An indexed access scheme can be easily built on top of a direct access system. Very large
files may require a multi-tiered indexing scheme, i.e. indexes of indexes.
• Alternatively a physical disk can be broken up into multiple partitions, slices, or mini-disks, each of
which becomes a virtual disk and can have its own filesystem. ( or be used for raw storage, swap space,
etc. )
• Or, multiple physical disks can be combined into one volume, i.e. a larger virtual disk, with its own
filesystem spanning the physical disks.
• A typical file-system organization.
Directory Overview
• Single-level directory.
6.3.3 Two-Level Directory
• An obvious extension to the two-tiered directory structure, and the one with which we
are all most familiar.
• Each user / process has the concept of a current directory from which all (
relative ) searches take place.
• Files may be accessed using either absolute pathnames ( relative to the root of the tree )
or relative pathnames ( relative to the current directory. )
• Directories are stored the same as any other file in the system, except there is a bit that
identifies them as directories, and they have some special structure that the OS
understands.
• One question for consideration is whether or not to allow the removal of directories that
are not empty - Windows requires that directories be emptied first, and UNIX provides
an option for deleting entire sub-trees.
• Tree-structured directory structure.
6.3.5 Acyclic-Graph Directories
• When the same files need to be accessed in more than one place in the directory structure ( e.g. because
they are being shared by more than one user / process ), it can be useful to provide an acyclic-graph
structure. ( Note the directed arcs from parent to child. )
• UNIX provides two types of links for implementing the acyclic-graph structure. ( See "man ln" for more
details. )
o A hard link ( usually just called a link ) involves multiple directory entries that both refer to the same
file. Hard links are only valid for ordinary files in the same filesystem.
o A symbolic link, that involves a special file, containing information about where to find the linked file.
Symbolic links may be used to link directories and/or files in other filesystems, as well as ordinary files
in the current filesystem.
• If cycles are allowed in the graphs, then several problems can arise:
o Search algorithms can go into infinite loops. One solution is to not follow links in search algorithms. (
Or not to follow symbolic links, and to only allow symbolic links to refer to directories. )
o Sub-trees can become disconnected from the rest of the tree and still not have their reference counts
reduced to zero. Periodic garbage collection is
required to detect and resolve this problem. ( chkdsk in DOS and fsck in UNIX search
for these problems, among others, even though cycles are not supposed to be allowed
in either system. Disconnected disk blocks that are not marked as free are added back
to the file systems with made-up file names, and can usually be safely deleted. )
A. Ext2-
The ext2 or second extended file system is a file system for the Linux kernel. It was initially
designed by Remy Card as a replacement for the extended file system (ext). It was the first
commercial-grade file system for Linux. Maximum file size can be from 16 GB to 2 TB.
Overall ext2 file system size can be from 2 TB to 32 TB.
B. Ext3
Ext3 the third extended file system is the commonly used filesystem on Linux. It is extension
of ext2 . Ext3 is introduced in 2001 which is Developed by Stephen Tweedie. Starting from
Linux Kernel 2.4.15 ext3 was available. Maximum file size can be from 16GB to 2TB. Over
all ext3 file system size can be from 2TB to 32TB. There are three types of journaling available
in ext3 file system.