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

GargR - Vermag. 2017 OperatingSystemsAnInt

Uploaded by

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

GargR - Vermag. 2017 OperatingSystemsAnInt

Uploaded by

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

90 • Operating Systems

When a customer arrives:


QQ If no one else is inside the shop, they wake up the barber and waits to sit in
the barber chair and get their hair cut.
QQ If the barber is busy, the customer waits their turn in a waiting chair. Once
a haircut of the customer is finished:
QQ Barber opens the exit door and waits for customer to leave (close it behind them).
QQ Checks that is there any customers waiting? If there is none, he goes to
sleep in the barber chair.
QQ Only one customer is waiting (and the customer is sleeping), then the barber will:
•• Wake up the customer.
•• Wait for the customer to sit down in the barber chair.
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

This is a very complex model and requires lots of wait() and signal() opera-
tions of semaphores.

Self-Quiz

Q18. Explain the bounded buffer problem of synchronization.


Q19. Explain the dining philosopher problem of synchronization.
Q20. Explain the sleeping barber problem of synchronization.

SUMMARY

In operating system, the executing processes may be either independent


processes or cooperating processes. Cooperating processes require an inter-process
communication mechanism to communicate with each other. The communication
is achieved by two schemes, i.e., shared memory and message passing.
Mutual exclusion is required for a given collection of cooperating sequential
processes that are sharing data. This can be solved by ensuring that at a time
only one process or thread undergoes its critical section. Various ­algorithms
are available to solve the critical-section problem. The major disadvantage of
these solutions is that they all require busy waiting. To solve this busy waiting
problem semaphores is used. Semaphores can be used to solve various synchro-
nization problems and can be implemented efficiently, especially if hardware
support for atomic operations is available. The synchronization problems (such
as the bounded-buffer problem, the readers-writers problem, and the dining-
philosophers problem) problems are used to test almost every newly proposed
synchronization scheme.
2017. Mercury Learning & Information.

POINTS TO REMEMBER

QQ A mechanism that allows cooperating processes to exchange data and infor-


mation is known as inter-process communication (IPC).

EBSCO Publishing: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA 1809145; Garg, R., Verma, G.;
Operating Systems : An Introduction Account:ehost.
Process Synchronization • 91

QQ The two fundamental models of inter-process communication are: shared


memory model and message passing model.
QQ Message passing may be either blocking or non-blocking.
QQ The situations, where several processes are allowed to access and manipu-
late the same data concurrently and the outcome of the execution depends
on the order in which the access takes place is called a race condition.
QQ Critical-section is a segment of code where the process may be exchanging
common variables, updating a table, writing a file, etc.
QQ To solve the critical section problem, the three main requirements are: mu-
tual exclusion, progress and bounded wait.
QQ In OS, to handle critical-section, the two approaches are: preemptive and
non-preemptive kernels.
QQ Semaphore is a synchronization tool.
QQ Two types of semaphores are available, i.e., counting and binary semaphore.

REVIEW QUESTIONS
Q1. Provide the solution of critical section problem.
Q2. Explain the binary semaphore with an example.
Q3. Write a brief note on inter-process communication.
Q4. How is the concurrency problem solved with producer-consumer
problem?
Q5. Explain how readers-writers problem can be solved by using
semaphore.
Q6. What is the critical section? How can we obtain a solution to the criti-
cal section problem?
Q7. Write an algorithm to explain producer/consumer using semaphores.
Q8. Discuss one classical problem related to the process synchronization.
Q9. What do you understand by critical section? What are the requirements
of a solution to the critical section problem? Discuss mutual exclusion
implementation with the help of Test-and-Set machine instructions.
Q10. State the finite buffer Producer-Consumer problem. Give solution of
the problem using semaphore.
Q11. What do you mean by concurrent process? Discuss the inter-process
communication in detail.
Q12. Write and explain the Peterson solution to the critical-section problem.
Q13. Discuss the message passing system. Explain how message passing can
be used to solve Producer-Consumer problem with infinite buffer.
Q14. Define semaphore with suitable example.
Q15. Define message-passing and shared-memory inter-process communi-
cation.
Q16. List the essential requirements of critical-section problem.
Q17. State and describe the producer-consumer problem with its suitable
solution.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
CHAPTER
7
DEADLOCKS

Purpose of the Chapter


The purpose of this chapter is to define deadlock and describe how and
why deadlock occurs. Deadlock is an unfavorable state in the system which
needs to be taken care of. This chapter further explains how a deadlock can be
handled in a system. Three different ways to handle and remove deadlock from
the system are explained in detail.

INTRODUCTION

On a one-way road, suppose two cars come from opposite directions.


Neither of them are ready to move back. If the road is so narrow that they
cannot pass on the sides, then both cars will be stuck on the road until one of
them moves back. This situation is called deadlock. Both cars will not be able
to move further in their desired direction, and hence they are deadlocked. This
situation of deadlock of the two cars is shown in Figure 7.1.

FIGURE 7.1. Two cars in deadlock state

Such a situation may also arise in the computer system. Suppose there is
a set of processes which are holding a resource each and are also waiting for

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
94 • Operating Systems

a resource which is held in another process. Then such a situation will create
deadlock for the processes. Processes are said to deadlock in this situation.
A situation when two or more processes get blocked and cannot proceed
further with their processing because of interdependency of resources is called
deadlock. This situation is shown in Figure 7.2.

FIGURE 7.2. Two processes in deadlock state

In other words, a deadlock occurs when two or more processes attempt


to access a resource that the other process has locked and hence cannot be
shared. As a result, each process waits for a resource that the other has locked,
and neither process can be finished.
For example, two resources, a printer and a compact disk (CD) writer
drive, are attached to a computer. Two processes, P and Q, exist in a ready
queue. Both processes P and Q want to read a document from the CD pres-
ently placed in the CD writer drive, make necessary changes to the document,
write it back to the CD, and get a modified document printed. Both processes
require both resources, the CD drive and printer, to finish their task.
The process P acquires the CD writer drive and locks it in non-sharable
mode. The process Q acquires the printer and locks it in non-sharable mode.
The process P waits to obtain the printer, and process Q waits to obtain the CD
drive. Neither of the processes will be able to continue processing. This will
lead to a hang situation, which is called deadlock.

DEADLOCK CHARACTERIZATION

In a computer system, deadlock arises when four conditions occur simulta-


neously. When all these conditions occur at the same time, then only a dead-
lock may be created among processes. The conditions are as follows:
QQ Mutual Exclusion: Mutual Exclusion means only one at a time. This
means at a particular time stamp only one process can use a resource. Mul-
tiple processes are not allowed to use the same resource at the same time.
If the need occurs to access the resource simultaneously, then a queue is
formed and the processes get access to the resource according to their turn.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Deadlocks • 95

For example, a process P locks the resource R in exclusive mode. This


means no other process can acquire the resource R. In other words, the
concept of mutual exclusion means that a resource can be used by only one
process at a time.
QQ Hold and Wait: When a process is holding some resources and waiting
for other resources to finish execution, then such a situation is called Hold
and Wait. This means a process is allowed to request new resources even
if it is holding some. In other words the processes involved in a deadlock
acquire a minimum of one resource in exclusive mode and wait for another
resource which has been acquired by another process in exclusive mode
(non-sharable mode).
QQ No preemption: If a process is holding some resource, it cannot be taken
from it until the process is finished with it. The process will release the
acquired resource after completing its task. This situation is called no
preemption. This means resources cannot be taken from a process forci-
bly. In other words processes do not release the resources allotted to them
voluntarily without utilizing the resources. For example, process P1 holds
the resource R1 exclusively and waits for the resource R2, which is locked
by process P2 in exclusive mode. The concept of no preemption means
that the operating system cannot revoke the resource R1 from process
P1 forcefully.
QQ Circular wait: Each process is waiting to obtain a resource which is held
by another process in such a way so that they form a cycle. A closed chain
or a circular relationship exists among the processes involved in a deadlock.
For example, there are three processes, P1, P2, and P3. The process P1
is waiting for a resource that has been exclusively acquired by the process
P2, process P2 is waiting for a resource which is held by P3, and in turn
P3 is waiting for a resource being held by P1. This situation will result in
circular wait.

Self-Quiz

Q1. Define deadlock.


Q2. What are the necessary conditions which should be present for dead-
lock to occur?
Q3. Why is the occurrence of deadlocks unfavorable in the system?

RESOURCE ALLOCATION GRAPH

A graph is a collection of points and lines connecting them. The points of


a graph are known as nodes. The lines connecting the nodes are called edges.
A resource allocation graph is a directed graph that consists of processes and
resources as the two types of nodes, and indicates (a) the processes requesting
resources and (b) the resources held by particular processes.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
96 • Operating Systems

A resource allocation graph is also known as a RAG. It consists of a set of


nodes P={P1, P2, . . . Pn} and R={R1, R2, . . . Rm} where P represents a set of
processes and R represents a set of various resources present in the system. It
also consists of two sets of directed edges:

QQ Request Edge: This edge extends from a process to the resource being
requested by the process. It is represented as Pi → Rj.
QQ Assignment Edge: This edge extends from resource to process. It in-
dicates which resource is allocated to which process. It is represented as
Rj → Pi.

Purpose of a Resource Allocation Graph


The presence of deadlock can be indicated with the help of a RAG. In other
words, a resource allocation graph is an analytical tool which is used to detect
the presence of deadlock in a system. How a RAG determines the presence of
deadlock in a system is described as follows:

QQ If the graph contains no cycles, then there is no deadlock in the


system.
QQ If the graph contains a cycle, then there may or may not be a deadlock
depending on the following conditions.
QQ If there is only one instance per resource type, then deadlock is present.
QQ If several instances per resource type are present, then there is the possi­
bility of deadlock.

Examples of Some Resource Allocation Graphs


Figure 7.3 illustrates a RAG showing a deadlock situation, and Figure 7.4
illustrates a cycle in a RAG with no deadlock.

FIGURE 7.3. A Resource Allocation Graph showing a deadlock situation

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Deadlocks • 97

FIGURE 7.4. Cycle in a Resource Allocation Graph with no deadlock

Self-Quiz
Q4. What do you mean by a resource allocation graph?
Q5. Name some components of a RAG.
Q6. Name the edges which are present in a RAG.
Q7. What is the purpose of a resource allocation graph?

METHODS FOR HANDLING DEADLOCKS

Occurrence of deadlock in a system is not a favorable situation. There are


various ways to handle deadlock. Different operating systems follow different
methods to deal with the problem of deadlock, but the main methods are as
follows:
QQ Deadlock prevention
QQ Deadlock avoidance
QQ Deadlock detection
Let us discuss each one in brief.

DEADLOCK PREVENTION

As explained in Section 7.1, there are four conditions which should hold
simultaneously for deadlock to occur in the system. If any of these conditions
can be made false, then deadlock can be prevented. These conditions can be
prevented in the following ways:

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
98 • Operating Systems

Mutual Exclusion: When resources are sharable it is not necessary for this
condition to be present. For example, read-only files can be used by processes
at the same time, so there is no need for mutual exclusion.
But in the case of non-sharable resources, it is necessary for the mutual
exclusion conditions to be present. For example, this includes resources like
printers or disk drives. Such resources can handle requests from one process
at a time. Hence mutual exclusion is required.
Hold and Wait: The system should make sure that whenever a process
requests a new resource, there should not be any resources in its hold. This
means that a process should never hold a resource and request new ones at
the same time. This can be achieved by providing all the resources in advance.
All the resources required by the process during its lifetime are allocated to it
before starting its execution. In this way, the condition of Hold and Wait can
be prevented and hence deadlock can be prevented.
But this is not favorable, as it will lead to very low resource utilization. All
the resources are allocated in advance, but all may not be required by the
process at the same time. Hence some resources will be idle, which will result
in low utilization of resources. It may also lead to starvation of processes, as a
process may need some very popular resources for its execution. It may not get
those resources because of their constant usage by other processes.
No Preemption: This condition can be prevented from occurring in many
ways. If a process is requesting a resource that is busy, then all the resources
held by the requesting process should be preempted. Another way is to
preempt some of its resources, but not all. Preempt only those resources that
are required by other processes. In this way, the no preemption condition can
be prevented.
But preventing this condition is also not good, because it will result in a
wastage of time, and the process already waited in grabbing the resources. In
addition, the process needs to be restarted, which results in overhead.
Circular Wait: This condition can be prevented by imposing a complete
ordering in which a process should request resources. The process should
be allowed to request resources in such a way so that a cycle is not formed. All the
resources are numbered, and the process can only request resources in increasing
number order. The main aim of doing this is to prevent the formation of a cycle.
But it is not always possible to follow this rule. Another problem is deter-
mining how to do the relative ordering of the resources.

Self-Quiz

Q7. Name three methods that can be used to handle deadlock.


Q8. How can deadlock prevention be done?
Q9. What is mutual exclusion?
Q10. Give one situation where there is no need of mutual exclusion in a
computer system.
Q11. What is the drawback of preempting resources from the processes?

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Deadlocks • 99

DEADLOCK AVOIDANCE

In the deadlock prevention method as explained in Section 7.4, one of the


four conditions needs to be prevented, which is not always possible. Hence
another method called deadlock avoidance is used. In this method, deadlock
is avoided. Whenever any process requests a resource, the system state is
checked. If granting the request leaves the system in a safe state, then the
resource is allocated; otherwise, it is not. A system is considered to be in a
safe state if all the requests of processes are granted to acquire the various
resources without leading the system into a deadlock state. A system is consid-
ered safe if there is a safe sequence of processes.
QQ A safe sequence of processes is {P0, P1, P2, . . . Pn} such that all of the
resource requests for Pi can be granted using the resources currently al-
located to Pi and all processes Pj where j < i.
QQ If all the processes prior to Pi finish and free up their resources, then Pi will
be able to finish also, using the resources that have been freed up.
QQ If a safe sequence does not exist, then the system is in an unsafe state,
which may result in deadlock.
QQ All safe states are free from deadlock, but unsafe states may result in deadlocks.
This means whenever a request comes from a process for a resource, the
whole system is checked for a safe or unsafe state. Two different situations
are possible in a system, which are as follows:
QQ Systems having a single instance of resources.
QQ Systems having multiple instances of resources.
Separate methods are used in each situation for deadlock avoidance.

Systems Having a Single Instance of Resources


A system having a single instance means that there is just one copy of the
particular resource available. In such a situation, a resource allocation graph
algorithm is used as a solution in deadlock avoidance.

Resource Allocation Graph Algorithm


If there are only single instances of the resources, then deadlock can be
detected by the presence of cycles in the resource allocation graphs. Little
modifications are done in the RAG. Now along with the request edges and
assignment edges, one more edge called the claim edge is used. The claim
edge is a directed edge. It is represented by a dashed line. This is shown in
Figure 7.5.
A claim edge points from a process to a resource that it may request in
the future. In this algorithm, all claim edges are added to the graph for any
particular process before that process is allowed to request any resources.
Processes may only make requests for resources for which they have already
established claim edges. When a process makes a request, the claim edge
Pi → Rj is converted to a request edge. This is shown is Figure 7.6.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
100 • Operating Systems

FIGURE 7.5. Resource Allocation Graph (RAG) for deadlock avoidance

FIGURE 7.6. An unsafe state in a Resource Allocation Graph

When a resource is released, the assignment edge converts back to a claim


edge. If resource allocation results in a cycle, then the request will not be
granted even if the resource is available. This approach works by denying
requests that would produce cycles in the resource allocation graph, taking
claim edges into effect.

Systems Having Multiple Instances of Resources


When more than one instance of a resource is present, the resource alloca-
tion graph method is not successful. Hence another method called the Bank-
er’s algorithm is used.

Banker’s Algorithm
The Banker’s algorithm gets its name from the basic concept of the banking
system. A banker will never loan out money to its clients until it is assured
that the loan will be enough to satisfy the needs of all customers. Whenever a
process starts it will declare its maximum requirement of resources in advance.
Whenever any request is made from any process, before granting the request
the scheduler will check whether it will leave the system in a safe state. If the
system goes into an unsafe state, then the process will have to wait even if
resources are available. The Banker’s algorithm uses four main data structures,
where P is the number of processes and R is the number of resource types:

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Deadlocks • 101

QQ Available[R] represents how many resources are presently available


of each type.
QQ Max [P] [R] represents the maximum demand of each process of each
resource.
QQ Allocation [P] [R] represents the number of each resource type
allocated to each process.
QQ Need [P] [R] represents the remaining resources needed of each type
for each process.
QQ Need[ i ][ j ] = Max[ i ][ j ] - Allocation[ i ][ j ]
for all i, j

Safety Algorithm
Before applying the Banker’s algorithm, some method is required to
determine whether the system is in a safe state or not. Hence an algorithm
is suggested to find the state of the system, and it is called the safety algo-
rithm. This algorithm is used to determine if the present state of a system
is safe or not.
The following steps are used in the safety algorithm:

1. Let Work and Finish be vectors of length m and n respectively.

QQ Work is a working copy of the available resources, which will be modified


during the analysis.
QQ Finish is a Boolean vector indicating whether a particular process can finish
or has finished so far in the analysis.
QQ Initialize Work to available, and Finish to false for all elements.

2. Find an i such that both (A) Finish[ i ] == false, and (B) Need[ i ]
< Work. This process has not finished, but could with the given available working
set. If no such i exists, go to Step 4.
3. Set Work = Work + Allocation[ i ], and set Finish[ i ] to true.
This corresponds to process i finishing up and releasing its resources back into
the work pool. Then loop back to Step 2.
4. If finish[ i ] == true for all i, then the state is a safe state, because a
safe sequence has been found.

Resource-Request Algorithm (The Banker’s Algorithm)


This algorithm is used to determine if a new request is safe. The request
will be granted if it leaves the system in a safe state. The request should always
be less than or equal to presently available resources. If the above written
statement is true, then pretend that it has been granted. After that, check if the
resulting state is safe or not.
If the system is safe, grant the request, and if not, deny the request, as
follows:

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
102 • Operating Systems

1. Let Request[ P ][ R ] indicate the number of resources of each type


currently requested by processes. If Request[ i ] > Need[ i ] for any
process i, raise an error condition.
2. If Request[ i ] > Available for any process i, then that process must
wait for resources to become available. Otherwise the process can continue to
Step 3.
3. Check to see if the request can be granted safely by pretending it has been
granted and then seeing if the resulting state is safe. If so, grant the request,
and if not, then the process must wait until its request can be granted safely.
The procedure for granting a request (or pretending to for testing purposes) is:

QQ Available = Available - Request


QQ Allocation = Allocation + Request
QQ Need = Need – Request

Example 7.1: Consider a system consisting of five resources of the same


type which are to be shared by four processes. Each process needs at most two
resources. Show that the system is deadlock free.
Solution: If the system is deadlocked, it implies that each process is holding
one resource and is waiting for one more. Since there are four processes
and five resources, one process must be able to obtain two resources. This
process requires no more resources and therefore it will return its resources
when done.
Example 7.2: Consider a system having five processes from P0 to P5.
There are three types of resources having various instances. Type A has eleven
instances, type B has six, and type C has seven instances. A snapshot of the
system is shown in Table 7.1.

TABLE 7.1. Processes and Resources Using Various Instances

Process Allocation Max Available


A B C A B C A B C
P0 1 2 1 8 6 4 4 4 3
P1 3 1 1 4 3 3
P2 4 1 3 10 1 3
P3 3 2 2 3 3 3
P4 1 1 3 5 4 4

Answer the following questions using the banker’s algorithm:


a. What is the content of the matrix need?
b. Is the system in a safe state?
c. If a request from process P1 arrives for (1, 0, 2), can the request be
granted immediately?

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Deadlocks • 103

Solution:
a. Need = Max – allocation
Applying this formula to Table 7.1, the matrix Need will be as shown in
Table 7.2.

TABLE 7.2. Need Matrix


Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

b. After applying the banker’s algorithm for finding the safe state, the
sequence <P1, P3, P4, P2, P0> is found. As the algorithm results
in a safe sequence, the given system is in a safe state. This means that
there is no deadlock right now.
c. Now a request from process P1 arrives for (1,0,2). It is to be found
out whether this request may be granted immediately or not. For
this the request should be less than or equal to availability, meaning
request <= availability. For process P1, request (1,0,2)
<=availability (3,3,2). Hence the request can be granted on a
temporary basis. On granting the request on a temporary basis, a new
matrix will be formed, which is shown in Table 7.3.

TABLE 7.3. Snapshot of System after Granting Request Temporarily

Process Allocation Need Available


A B C A B C A B C
P0 1 2 1 7 4 3 2 3 0
P1 4 1 3 0 2 0
P2 4 1 3 6 0 0
P3 3 2 2 0 1 1
P4 1 1 3 4 3 1

Now again the banker’s algorithm is applied to find a safe sequence.


After applying the banker’s algorithm, a sequence <P1, P3, P4, P0, P2>
is found, which indicates that the system is in a safe state. When the
system is in a safe state, the request of process C(1,0,2) can be granted
immediately.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
104 • Operating Systems

Self-Quiz

Q12. What is meant by the term deadlock avoidance?


Q13. Which method is used in deadlock avoidance when a single instance of
resources is present?
Q14. Which method is used in deadlock avoidance when multiple instances
of resources are present?
Q15. How did the banker’s algorithm get its name?

DEADLOCK DETECTION
Deadlock will never occur in the deadlock avoidance method, but the draw-
back is it needs a lot of calculations before fulfilling any request of the process.
These extensive calculations are a burden on the CPU, because during that
time no process can occur. In the deadlock avoidance method, before any
request may be granted, a safe sequence must to be found. Because of this
drawback, some other and better method is required. The suggested method
is deadlock detection. The following is true with this method:
QQ Deadlocks are allowed to occur in the system.
QQ They are detected from time to time.
QQ Recovery methods are used to recover from deadlock.
Two different cases are possible in the deadlock detection method, as
discussed in the following section.

Systems Having a Single Instance of Resources


If the system has only a single instance of each resource type, a wait-for
graph is used. A wait-for graph is a variation of the resource allocation graph.
A wait-for graph can be made from a resource allocation graph by removing
the resources and edges. An edge from Pi to Pj in a wait-for graph indicates
that process Pi is waiting for a resource that process Pj is currently holding. If
cycles are present then it indicates the presence of deadlocks. Wait-for graphs
are maintained in the system. From time to time, the presence of cycles is
searched for, which helps in the detection of deadlock.

Systems Having Multiple Instances of Resources


The detection algorithm is used to detect the presence of deadlocks.
It is similar to the banker’s algorithm explained in Section 7.5.2.1. But
there are two main differences between the detection algorithm and the
banker’s algorithm:

QQ In Step 1, the banker’s algorithm sets Finish[ i ] to false for all i. The detec-
tion algorithm sets Finish[ i ] to false only if Allocation[ i ] is not zero. If
the currently allocated resources for this process are zero, the algorithm
sets Finish[ i ] to true. This is essentially assuming that IF all of the other

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Deadlocks • 105

processes can finish, then this process can finish also. Furthermore, this
algorithm is specifically looking for which processes are involved in a dead-
lock situation, and a process that does not have any resources allocated
cannot be involved in a deadlock, and so can be removed from any further
consideration.
QQ Steps 2 and 3 are unchanged.
QQ In Step 4, the basic banker’s algorithm says that if Finish[ i ] == true for all
i, there is no deadlock. The detection algorithm is more specific, by stating
that if Finish[ i ] == false for any process Pi, then that process is specifically
involved in the deadlock that has been detected.

Detection-Algorithm Usage
In the deadlock detection method, the main question is when deadlock
detection should be done. The answer may depend on how frequently dead-
locks are expected to occur. There are two basic approaches. In the first
approach deadlock detection is done after every resource allocation. But this
approach has a lot of calculation overhead on the CPU. In the second approach
deadlock detection is done when CPU utilization reduces below the desired
value.

Self-Quiz

Q16. What is the main drawback of the deadlock avoidance method for
handling deadlock?
Q17. Deadlocks are allowed to occur in the deadlock detection method. Is
this statement true or false?
Q18. Name the type of graph that is maintained in the deadlock detection
method.
Q19. How is a wait-for graph different from a resource allocation graph?

RECOVERY FROM DEADLOCK


There are three basic ways to recover from deadlock:
1. Inform the system operator, and deadlock is removed manually.
2. Terminate one or more processes involved in the deadlock.
3. By preemption of resources.

Process Termination
Terminate all processes which are involved in the deadlock. This approach
will resolve the deadlock, but in this case more processes are terminated than
required. Terminate processes one by one until the deadlock is broken. In
this way a lower number of processes will be terminated. However, deadlock
detection is required after each step. When processes are to be terminated one

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
106 • Operating Systems

by one, then the victim process is to be chosen. The victim process is chosen
based on the following points:

1. Priority of the process.


2. Total processing done of the particular process.
3. How close a process is to its finish.
4. How many resources a process is holding.
5. Which types of resources a process is holding.
6. How many more resources the process needs to complete.
7. Whether the process is interactive or batch.

Resource Preemption
When preempting resources to relieve deadlock, there are three important
issues to be taken care of.

1. Selecting a victim: To choose out of many processes a victim process to be


terminated first.
2. Rollback: It is ideal to roll back a preempted process to a safe state in place
of terminating it fully.
3. Starvation: How do you guarantee that a process won’t starve because its
resources are constantly being preempted? One option would be to use a pri-
ority system and increase the priority of a process every time its resources get
preempted. Eventually it should get a high enough priority that it won’t get
preempted anymore.

SUMMARY

Various methods and ways are used in an operating system to deal with
the problem of deadlock. Approaches such as deadlock prevention, dead-
lock avoidance, deadlock detection, and recovery have been explained in the
chapter.

POINTS TO REMEMBER

QQ A situation when two or more processes are blocked and cannot proceed
further with their processing because of interdependency of resources is
called deadlock.
QQ In a computer system, deadlock arises when four conditions occur simul-
taneously.
QQ The four conditions are mutual exclusion, hold and wait, no preemption,
and circular wait.
QQ A directed graph which consists of two types of nodes as processes and
resources is called a resource allocation graph.
QQ Resource allocation graph in short is also known as RAG.
QQ The presence of deadlock can be indicated with the help of RAG.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Deadlocks • 107

QQ The main methods to deal with deadlock are deadlock prevention, dead-
lock avoidance, and deadlock detection.
QQ In the deadlock detection method, deadlocks are allowed to occur in the
system. They are detected from time to time.
QQ Recovery methods are used to recover from deadlock.

REVIEW QUESTIONS

Q1. Explain different conditions for deadlock.


Q2. Write down the methods for deadlock prevention.
Q3. How is recovery from deadlock done by using a combined approach?
Q4. (i) State four necessary conditions for deadlock to occur. Give a brief
argument for the reason each individual condition is necessary.
(ii) 
In the deadlock prevention method, the system grants resources
on an “all or none” basis to processes. Discuss its pros and cons.
Q5. Differentiate between deadlock and starvation.
Q6. Write and explain the banker’s algorithm for avoidance of deadlock.
Q7. Write an algorithm for the detection of deadlock in a system having
several instances of multiple resource type.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
CHAPTER
8
MAIN MEMORY MANAGEMENT

Purpose of this Chapter


The purpose of this chapter is to explain the need of main memory manage-
ment. Various types of memory management schemes are explained. The main
purpose of all the techniques is to allocate each program and its required
memory space. Concepts like paging, segmentation, and segmented paging
are also explained in detail.

INTRODUCTION

Whenever the programs are to be executed, they should be present in the main
memory. In multiprogramming, many programs are present in the main memory,
but the capacity of memory is limited. All of the programs cannot be present in
memory at the same time. This memory management is to be performed so all
of the programs get space in memory, and they get executed from time to time.
Various memory management schemes are there. The selection of a particular
scheme depends on many factors, especially the hardware design of the system.

MAIN MEMORY

Main memory is the temporary read/write memory of a computer. It is a


set of continuous locations. Each location has a specific address. Each address
is a binary address. For the convenience of the programmer and user, they are
represented in hexadecimal numbers.
The programs, which are a set of instructions, are stored in secondary
memory. When they need to be executed, they are loaded to main memory
from the secondary memory. The main memory instructions are stored in
various locations according to space availability.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
110 • Operating Systems

The groups of memory locations are called the address space. Two types
of address space are explained below, i.e., physical address space and logical
address space.
Physical Address Space
The address of any location in physical memory is called the physical address.
Physical memory means the main memory or Random Access Memory (RAM). In
main memory every location has an address. Whenever any data or information is
read from it, its address is given. That address is called the physical address.
A group of many physical addresses is called the physical address space.
The Features of the physical address space follow:
QQ The physical address is provided by the hardware.
QQ It is a binary number made with a combination of 0 and 1.
QQ It refers to a particular cell or location of primary memory.
QQ Such addresses have some end limits.
QQ The limits normally start from zero to some end limit (shown in Figure 8.1).
QQ All addresses do not only belong to the system’s main memory.

FIGURE 8.1. Main memory having locations from 0000 to 6400

Logical Address Space


The address of any location of virtual memory is called the logical address.
It is also known as the virtual address.
The group of many logical addresses is called the logical address space or
virtual address space. The features of the logical address space follow:
QQ Logical address is provided by the operating system kernel.
QQ Logical address space may not be continuous in memory.
QQ It might also be present in the form of segments.
QQ Sometimes logical addresses might be of the same value as a physical address.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 111

QQ The logical address is mapped to get the physical address.


QQ This mapping is done using address translation.
QQ The physical address space and logical address space are independent of
each other.

Mapping from the Logical Address to the Physical Address


The memory management unit is used to map the logical address to the
physical address. The memory management unit is also known as MMU. It
consists of a relocation register.
The logical address can be converted into a physical address using the relo-
cation register as shown in Figure 8.1. This register consists of a value.

FIGURE 8.2. Main Memory Management Unit (MMU)

QQ The CPU generates a logical address when a process wants to address a location.
QQ The value of the relocation register is added to the logical address.
QQ The new value which is produced as output is the value of a physical address.
QQ This physical address will point to a location in main memory.
QQ This location will be used as a pointer to the main memory, where the op-
eration is to be performed.
QQ Mapping of the virtual address into a physical address is also known as ad-
dress translation or address binding.

Self-Quiz
Q1. Define physical address and logical address.
Q2. What is the difference between the physical address space and the
logical address space?

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
112 • Operating Systems

OVERLAYS

The process is brought into the main memory to be executed. Some-


times the process size is large when compared to the available free space
in the main memory. A technique is used to help the process that is to be
executed, even if it does not have enough space in memory. This technique
is called overlays.

QQ With this technique, the complete process is not kept in the main
memory.
QQ Only some instructions and data required by the process are kept.
QQ Only those instructions that are to be executed first are placed in the main
memory.
QQ The rest of the process will be in disk.
QQ When other instructions are executed, they are placed in main memory.
QQ At that time, the previous instructions are removed, and space is created for
the next set of instructions.
QQ In this way a process whose size is much larger than the available memory
can be executed.
QQ This technique of overlays is implemented by the user.

An example of a two pass assembler is considered. Its different parts and


their size requirements are given in Table 8.1. Its total memory requirement is
200 KB, whereas the available memory is only 150 KB.

TABLE 8.1. Parts of two pass assembler and their memory requirements

Content Size
Pass 1 60 KB
Pass 2 90 KB
Symbol Table 20 KB
Routines 30 KB

Both Pass 1 and Pass 2 are not required to be present at the same time in
memory for the execution of the two pass assembler.

QQ First Pass 1 is loaded into memory.


QQ Its function is to construct a symbol table.
QQ When its work finishes, then it is removed from the main memory.
QQ When completed, Pass 2 is loaded into main memory as shown in
Figure 8.3.
QQ Its work is to generate the machine language code.
QQ This removal and bringing of required code into the memory is done by
overlay drivers.
QQ The overlay driver is loaded into memory.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 113

FIGURE 8.3. Overlays for a Two Pass Assembler

SWAPPING

When many processes are going to be executed and there is not sufficient
space in the main memory, then a method called swapping is used. In this
method, a lower priority process is removed from the main memory on a
temporary basis to create space for a higher priority process.

QQ A process is swapped out of main memory on a temporary basis.


QQ The swapped process is placed on the disk. This is also known as a
backing store.

FIGURE 8.4. Swapping of processes

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
114 • Operating Systems

QQ The higher priority process is placed in the place of the swapped


process.
QQ Lower priority processes are swapped out of memory.
QQ Higher priority processes are swapped into memory.
QQ This method is also known as Roll In and Roll Out and shown in
Figure 8.4.
QQ The overhead in this method is the swap time.
QQ This technique is used in many operating systems such as UNIX, Windows,
and Linux, etc.

Self-Quiz

Q3. Define overlays.


Q4. Why overlays are used?
Q5. What do you mean by swapping?

FIXED PARTITION MEMORY MANAGEMENT

In this memory management technique, the primary memory is divided


into fixed partitions. Dividing the main memory into sets of regions which
are non-overlapping is called partitioning. These partitions can be of equal or
unequal sizes as shown in Figure 8.5. The size of the partition is fixed at the
time of OS initialization and cannot be changed at runtime of programs. This is
the reason it is called fixed partitioning memory management.

FIGURE 8.5. Main memory with equal and unequal sized partitions

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 115

Any program or process that is smaller than or equal to a partition can be


loaded into the free partition. If there is no free partition, the operating system
swaps a process out of a partition to make space for it. Sometimes a program
may be too large to fit into a partition. In this case, the programmer would use
the technique overlays as explained in Section 8.2.

Process Allocation in Partitions


QQ A single queue is maintained.
QQ The process from that queue is allotted in a free partition in First In First
Out (FIFO) manner.

Limitations of Fixed Partitions


QQ The program size is limited to the largest partition which is available in the
main memory.
QQ This scheme suffers from internal fragmentation. Internal fragmentation means
the unused space within partitions after the allocation of process in a partition.

VARIABLE PARTITION MEMORY MANAGEMENT

In this memory management scheme, main memory is divided into parti-


tions of variable sizes. The method explained in Section 8.4 suffers from internal
fragmentation. The variable partition memory management scheme is free from
internal fragmentation because the process is allocated to a partition of its size.
Memory is not partitioned in advance. When a process is introduced, then only
according to its size and available free memory, is it allocated memory space.
Memory can be considered as a sequence of variable size free partitions.

Fragmentation in Detail
When a process is terminated, it must be removed from the main memory.
This creates free memory space. Some parts of memory are occupied by processes
and some locations are free. Such a snapshot of memory is said to be fragmented.
There are mainly two types of fragmentation, i.e., external and internal.
QQ External Fragmentation: External fragmentation means memory is
available as per the size of the process, but it is not continuous. Total space
available in the memory is large enough to accommodate the process, but
free space is not continuous.
This type of fragmentation is explained with the help of an example. Suppose
the main memory is allocated to three processes A, B, and C, respectively.
There are three non-contiguous holes of free memory spaces of size 2 MB,
6 MB, and 4 MB. This means a total of 12 MB of memory is free, but it is
present in fragmented order. In this situation if a new process is loaded into
the memory that requires 10 MB of memory space, it cannot be allocated
memory because there is no contiguous 10 MB of free memory space avail-
able. This snapshot is further shown in the Figure 8.6.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
116 • Operating Systems

FIGURE 8.6. External Fragmentation

QQ Internal Fragmentation: Sometimes more memory space is allocated to


the process that is actually required by it. This results in a waste of space,
as it cannot be allocated to other process. Such fragmentation is called in-
ternal fragmentation.
Suppose the size of main memory is 64 MB. It is divided into eight equal
blocks of 8 MB each. A process will be allocated one or more blocks
according to its size. A block cannot be shared between two processes.
The processes A and C require less space than 8 MB. One block has to be
allocated to each of them. This causes unused memory space within the
allotted memory of the processes. This fragmentation is internal fragmen-
tation, which is shown in Figure 8.7.

Compaction
Compaction is bringing free space together into one place in order that free
memory is available in a contiguous manner. When two adjacent holes appear
then they can be merged together to form a single big hole. This larger hole can
be used by the process that needs large memory requirements. Another method
is to relocate the processes, thus creating free space in a contiguous manner.
Compaction is used randomly, as free space will be created as soon as
processes are terminated and leave the memory. The process of compaction
is shown in Figure 8.8.

Process Allocation in Partitions


When a process arrives, then free memory space large enough to accom-
modate the incoming process is sought. If enough memory is available in a

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 117

continuous manner, then the incoming process is accommodated in it. Free


space is scattered throughout the memory. Three different techniques are
used to satisfy the request which are First fit, Best fit, and Worst fit. The free
spaces are called holes. A list of free holes as well as filled memory space is
maintained by the operating system.

FIGURE 8.7. Internal Fragmentation

FIGURE 8.8. Compaction of Free Memory Spaces

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
118 • Operating Systems

QQ First fit: Allocates the first hole which is large enough to satisfy the re-
quest. A list of free holes is maintained. Whenever a process arrives, the
list of free holes is searched in order to find the first hole large enough to
accommodate the incoming process.
QQ Best fit: Allocates the smallest hole that is large enough to accommodate
the incoming process.
QQ Worst fit: Allocates the largest hole that can accommodate the incoming
process. The list of free holes is maintained according to size. The largest
hole, in which the incoming process can accept, will be allocated.
Example 8.1: Suppose there are memory partitions of 200 KB, 600 KB,
300 KB, 400 KB and 700 KB. These partitions are in order. Use first fit, best fit,
and worst fit algorithms to allocate processes of parameters 312 KB, 517 KB,
212 KB, and 526 KB, and solve the following:

a. Represent the allocations with the help of a figure or table.


b. Which algorithms of the three make the most efficient use of memory?

Solution:
a. Table 8.2 represents the allocations of the processes according to first
fit. In this example the process P4 of size 526 KB will not find any free
partition to be accommodated in the memory.
TABLE 8.2. First Fit
First Fit
Process Process Size Block
P1 312 KB 600 KB

P2 517 KB 700 KB
P3 212 KB 200 KB
P4 526 KB —

Table 8.3 represents the allocations of the processes according to best fit.
In this example all processes will get free partitions to be accommodated into
the memory.

TABLE 8.3. Best Fit


Best Fit
Process Process Size Block
P1 312 KB 400 KB

P2 517 KB 600 KB
P3 212 KB 300 KB
P4 526 KB 700 KB

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 119

Table 8.4 represents the allocations of the processes according to worst fit.
In this example, the process P4 of size 526 kb will not find any free partition to
be accommodated in the memory.
a. The best fit algorithm makes the most efficient use of memory as all of
the four processes receive space in memory.

TABLE 8.4. Worst Fit


Worst Fit
Process Process Size Block
P1 312 KB 400 KB

P2 517 KB 600 KB
P3 212 KB 300 KB
P4 526 KB 700 KB

Limitations of Variable Partitions


This scheme suffers from external fragmentation. External fragmentation
means memory is available according to the size of the process, but it is not
continuous. When processes are allocated and de-allocated from memory, they
result in scattered partitions.
These free partitions are scattered throughout the memory.

Self-Quiz

Q6. How are processes allocated in memory using the fixed partition method?
Q7. What is the difference between fixed and variable partition memory
management schemes?

PAGING
In the paging technique of memory management, the physical memory is
divided into fixed-sized blocks. Paging is a method of non-contiguous memory
allocation. In paging, the logical address space is divided into fixed-sized
blocks known as pages. The physical address space is also divided into fixed-
sized blocks known as frames. Every page is must be mapped to a frame. The
concept of paging is shown in Figure 8.9. Characteristics include:
QQ Divides logical memory into blocks of the same size called pages.
QQ The size of the frames is usually in the power of 2.
QQ The size of frame usually ranges between 512 bytes and 8192 bytes.
QQ Management technique has to keep track of all free frames.
QQ If a program needs n pages of memory, then n free frames are found in the
memory and the program is loaded there.
QQ Pages are scattered throughout the memory.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
120 • Operating Systems

FIGURE 8.9. Paging

QQ A page table is maintained to translate logical addresses into physical addresses.


QQ Paging suffers from internal fragmentation.
QQ Paging does not suffer from external fragmentation.

The Linux operating system uses a page size of


Did you know?
4 KB.

The address generated by the CPU is divided into two parts: page number
and page offset.

QQ Page number (p) – used as an index into a page table that contains the base
address of each page in physical memory.
QQ Page offset (d) – combined with the base address to define the physical
memory address that is sent to the memory unit.

Mapping of Pages to Frames


For example, suppose the logical address space has 213 addresses. This
means that a logical address has 13 bits. The logical address space is 8 KB. There
are 8 pages in logical address space. The size of each page will be 210 bits which
means 1 KB. The size of physical address space is 212 bits which means 4 KB.
This means that a physical address is 12 bits long. In a logical address of 13 bits
the Most Significant Bit (MSB) (13-10) = 3 bits represents page number and
the rest of the bits represent the offset. If the logical address is 011 1010101010,
then it denotes the page number 011 (page 3) and offset 1010101010.
The operating system maintains a table to convert logical address into physical
address. This table is called a page table. The method of converting the logical
address into a physical address is called address translation. The process of address
translation for address 011 101010101 is explained with the help of Figure 8.10.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 121

FIGURE 8.10. Address translation in a paging system

Page Table
The page table is maintained in main memory. The address of the page
table in main memory is stored in a register which is called the Page Table
Base Register (PTBR). The size of page table is stored in a register called the
Page Table Length Register (PTLR). In this method of paging, the page table
two memory accesses are used whenever any data is to be retrieved from main
memory. One memory access is for age memory and a second memory access
is from main memory. Two memory accesses for any data retrieval use great
amounts of time. This problem is removed by using the Translation Look Aside
Buffer Register (TLBR) as shown in Figure 8.11.

Advantages of Paging

QQ Paging does not suffer from external fragmentation.


QQ Memory management algorithm is easy to use.
QQ As pages and frames are of equal size, swapping is easy.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
122 • Operating Systems

FIGURE 8.11. Concept of paging with Translation Look Aside Buffer (TLB)

Limitations of Paging

QQ Paging suffers from internal fragmentation.


QQ Additional memory space is required to store the page table, as the page
table is maintained in main memory.
QQ Multilevel paging increases number of memory references.

The Linux operating system uses a three level


Did you know?
page table.

Self-Quiz

Q8. Define paging.


Q9. Why is page size normally in the power of 2?
Q10. Paging suffers from which type of fragmentation?
Q11. Where is page table maintained?
Q12. What is the purpose of a page table?

SEGMENTATION

Segmentation is a technique of non-contiguous memory allocation. In this


method memory space is divided into unequal sized chunks which are known
as segments. In segmentation, an application is considered as a collection of

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 123

modules where each module can be loaded into a different segment. For
example an application may consist of many modules like the main program,
procedures, functions, symbol tables, data values, global variables, local vari-
ables, etc., and each module can be placed in a different segment. This can be
represented with the help of Figure 8.12, where 1-6 are different modules of
any application.

FIGURE 8.12. User view of memory

The features of segmentation follow:

QQ This method supports user’s view of memory.


QQ Logical address is divided in two parts: segment number and offset.
QQ Segment table is maintained.
QQ Each entry of the table has two values: base and limit.
QQ Base is the starting physical address where the segments reside in memory.
QQ Segment Table Base Register (STBR) points to the segment table’s location
in memory.
QQ Segment Table Length Register (STLR) indicates number of segments used
by a program.
QQ Segment number s is legal if s < STLR.

Implementation of Segmentation Hardware


Implementation of the segmentation technique is shown in Figure 8.13.

Accessing Addresses in Segmentation


For example the logical address 310000 indicates offset 10000 and segment
number 3. A segment table is used for validation of an address. The starting
address and size of the segments are stored in the segment table. It is repre-
sented with the help of an example shown in Figure 8.14.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
124 • Operating Systems

FIGURE 8.13. Segmentation

FIGURE 8.14. Segments and segment table

Advantages of Segmentation

QQ Segmentation does not suffer from internal fragmentation.


QQ The segment table consumes less memory space than page tables.
QQ As segment tables are small, memory reference is easy.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 125

Limitations of Segmentation
QQ Memory management algorithm used is complex.
QQ It suffers from external fragmentation.
QQ Swapping is not easy as segments are of unequal sizes.

Self-Quiz

Q13. Define segmentation.


Q14. Which type of fragmentation is present in segmentation?
Q15. What are the different parts of the logical address in segmentation?

SEGMENTED PAGING

The techniques of memory management explained in Sections 8.6 and 8.7,


i.e., paging and segmentation, have their advantages and disadvantages. Thus,
the advantages of both are taken and a new approach of memory management
is suggested which is called segmented paging. In this technique the user’s
memory space is broken into segments. Each segment is further broken into
fixed size pages as shown in Figure 8.15. Segmentation is visible to the user,
whereas paging is transparent to the user.

FIGURE 8.15. Segmented Paging

Address Translation in Segmented Paging


In this method two tables are used, i.e., a segment table and a page table. The
segment table has two fields: the base address and the size of segment. The base
address of a segment is the address of the first page in each segment. The size of
the segment is the number of the pages present in each segment. The page table
stores the address of the frame where a page is loaded into memory. The logical
address has three parts: segment number, page number, and an offset.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
126 • Operating Systems

FIGURE 8.16. Address Translation in Segmented Paging

Example of Segmented Paging


Below is an example of segmented paging:

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 127

Advantages of Segmented Paging

QQ Memory space used is less as compared to the pure paging technique.


QQ Individual pages are shared by copying page table entries.
QQ External fragmentation is not present in this method.
QQ Memory allocation is simple.
QQ Intel Pentium supports the memory management techniques of segmented
paging.

Self-Quiz

Q16. Define segmented paging?


Q17. What are the advantages of segmented paging compared to pure seg-
mentation and pure paging techniques?

PROTECTION SCHEMES

In almost all modern operating systems, for example Linux, Windows, etc,
it is normal to have many programs running concurrently in memory. Although
there are advantages, there are many problems with this. One problem is to
protect programs from interfering with one another. This is called protection.
Protection in the OS is necessary so processes do not refer to memory locations
of another process without permission. Address references must be checked
at runtime by the hardware. Protection in paging is suggested and explained
in the next section.

Protection in Paging
In a paged environment, memory protection is done by protection bits.
These bits are associated with each frame. Usually, these bits are stored in
a page table. One bit is used to define a page which is read-write or read-
only. Whenever data is retrieved from memory, the request will go to the page
table to find the correct frame number. Simultaneously, the physical address is
calculated. The protection bits are checked to verify that no writes are being
made to a read-only page. An attempt to write to a read-only page causes a
hardware trap to the operating system, and it results in memory-protection
violation. One additional bit named as a valid-invalid bit is attached with each
entry in the page table.

QQ When the valid-invalid bit is set to 1, then the page is considered as valid.
QQ When the bit is set to 0, then the page is invalid. This indicates that the
requested page is not in the logical address space of the process.

The OS sets this bit for each page which helps in deciding to grant or
discard access to the page. Illegal addresses are trapped with the use of the
valid-invalid bit. The use of a valid-invalid bit is explained further with the

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
128 • Operating Systems

help of an example. Let’s suppose there are memory locations ranging from
0 to 16383. For these many locations a total of 14 bits are required. The page
size is 2 KB; thus, a total of 6 pages will be present. Pages 0, 1, 2, 3, 4, and 5
(total 6 pages) are mapped through the page table. If any attempt is made to
generate an address in pages 6 or 7, then the valid-invalid bit will be designated
as invalid, and the computer will trap to the OS with an invalid page reference.
See Figure 8.17.

FIGURE 8.17. Valid (v) or invalid (i) bit in a page table.

COMPARISON BETWEEN PAGING AND SEGMENTATION

QQ In segmentation, the programmer is involved in creating and maintaining


the segments, whereas in paging there is no involvement of programmer.
QQ Segmentation is not transparent as the paging memory management
scheme. The programmer is not aware of the presence of paging in the
system, whereas this is not the case with segmentation.
QQ In segmentation, a mechanism provides protection to individual segments,
whereas no separate protection mechanism exists in paging.
QQ Only one linear address space is present in paging, whereas many linear
address spaces are present in segmentation.
QQ Shared code is present in segmentation but it is not in paging. This means
that sharing of pages is not allowed in the paging method, whereas sharing
of code is allowed in segmentation.
QQ The concept of paging was designed to get a large linear address space with-
out buying more physical memory, whereas segmentation was designed to
allow programs and data to be broken into logically independent address
spaces and to provide security and protection to individual segments.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Main Memory Management • 129

SUMMARY

The main memory stores the instructions which are to be executed by the
CPU. The organization and management of memory is the important task of
the OS. The OS allocates memory to various processes. Overlays and swap-
ping are explained as memory management schemes. Memory management
schemes for fixed partitions and variable partitions are explained. Memory
suffers from internal and external fragmentation. Memory allocation schemes,
i.e., paging and segmentation are also explained.

POINTS TO REMEMBER

QQ Physical memory means main memory or RAM.


QQ The address of any location in physical memory is called the physical address.
QQ A group of many physical addresses is called the physical address space.
QQ The address of any location of virtual memory is called the logical address.
It is also known as the virtual address.
QQ A group of many logical addresses is called the logical address space or
virtual address space.
QQ The logical address is to be mapped into the physical address.
QQ A technique is used with the help of which a process can be executed even if it
does not get the complete space in memory. This technique is called overlays.
QQ Swapping is a method in which a lower priority process is removed from
the main memory on a temporary basis in order to create space for a high
priority process.
QQ Paging is a method of non-contiguous memory allocation.
QQ In paging, the logical address space is divided into fixed sized blocks known
as pages.
QQ The size of the pages is usually in the power of 2.
QQ Paging suffers from internal fragmentation.
QQ Paging does not suffer from external fragmentation.
QQ Segmentation is a technique of non-contiguous memory allocation.
QQ In segmentation, an application is considered as a collection of modules
where each module can be loaded into different segments.
QQ This method supports the user’s view of memory.
QQ Protection in the OS is necessary so that processes do not refer memory
locations of other processes without permission.
QQ In a paged environment, memory protection is done by protection bits.

REVIEW QUESTIONS

Q1. Discuss the paging system for memory management in detail. Also dis-
cuss its advantages and disadvantages.
Q2. Discuss the similarities and differences between paging and segmen­
tation.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
130 • Operating Systems

Q3. What do you understand by fragmentation? Explain each type in


detail.
Q4. What are the different techniques to remove fragmentation in case of
multiprogramming with variable and fixed partitions? Discuss.
Q5. What is paging? Describe how a logical address is converted into a
physical address in a paged system. Further give two reasons as why
page sizes are kept in powers of 2.
Q6. Discuss the paged segmentation scheme of memory management,
and explain how a logical address is converted into physical address
in such a scheme.
Q7. What is paging and segmentation?
Q8. Explain the differences between internal and external fragmentation.
Q9. What is the difference between paging and segmentation?
Q10. Consider a logical address space of eight pages of 1024 words, each
mapped onto a memory of 32 frames then:
a. How many bits are there in logical address?
b. How many bits are there in physical address?

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
CHAPTER
9
VIRTUAL MEMORY

Purpose of the Chapter


Sometimes the size of a program is larger than the memory space available
to the programmer. To allow the programmer to manage programs larger than
the main memory size, the concept of virtual memory is used. This chapter
explains various elements of virtual memory, such as page fault, demand paging,
page replacement, and its algorithms. It further explains various issues related
to performance of virtual memory like allocation of frames and thrashing.

INTRODUCTION

Whenever programs are to be executed, they are placed in main memory.


If the size of the program is larger than the available memory, then this situa-
tion should not limit the execution of programs. To solve this issue many tech-
niques like overlays and swapping are explained in Chapter 8. Another concept
known as virtual memory is also used to solve this issue of main memory space
limitation. The basic concept of virtual memory is to store the instructions and
data of the program in the secondary memory and bring them into the main
memory when they are required for execution. In other words, the part of the
program that is required at any moment is loaded in main memory while rest
of the program is present in secondary memory. Virtual memory is useful in a
multitasking operating system, where memory available in the main memory
for user applications is divided among several user programs and every program
has to compete for its memory requirements.
A technique that allows the execution of processes that may not be
completely in main memory is called virtual memory.
Hence in virtual memory:

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
132 • Operating Systems

QQ There is a separation of user logical memory from physical memory.


QQ Only the part of the program required for execution is kept in memory.
QQ Logical address space is much larger than physical address space.
QQ Address spaces are allowed to be shared by several processes.

METHODS TO IMPLEMENT VIRTUAL MEMORY

Virtual memory is implemented in ways such as demand paging and demand


segmentation.
QQ In demand paging, the available memory space is divided into equal-size
pages, whereas in demand segmentation, memory is divided into various-
sized segments.
QQ The demand paging method is mainly used to implement virtual memory
because segment replacement algorithms are more complex than page re-
placement algorithms.
QQ Segment replacement algorithms are more complex than page replacement
algorithms (which will be explained in Section 9.4) because segments are of
different (variable) sizes.
QQ Despite its complexity demand segmentation is used in some operating sys-
tems. The IBM OS/2 operating system uses demand segmentation.
In this book, we use the technique of demand paging to implement virtual
memory.

Self-Quiz

Q1. Define virtual memory.


Q2. What is the purpose of virtual memory?
Q3. Name the ways used to implement virtual memory.
Q4. Why is the demand paging method more popular than demand seg-
mentation?

DEMAND PAGING

A program is initially stored in the secondary memory. When a page in the


program is required, it is swapped into the main memory. This is called demand
paging, because a page is not loaded into the main memory until it is required.
The technique of demand paging is similar to the paging explained in the previous
chapter. The only difference between paging and demand paging is that in demand
paging, pages are loaded into the main memory according to need (demand).
The paging technique in which pages are loaded into the main memory
according to demand or need is called demand paging.
Page Fault
When a page is required, the operating system scans the page table to
find out the physical address in the main memory in which the page is

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 133

loaded. If the page is found, then the operating system continues its task;
otherwise, a page fault occurs.
When the desired page is not present in the main memory, this condition
is known as a page fault.
A page fault indicates that the required page is not currently present
in the main memory. In the concept of virtual memory, it is not necessary
that all pages be present in the main memory all the time. In Figure 9.1
the snapshot of memory is shown in which all pages are not present in the
main memory.
In Figure 9.1, there are eight pages ranging from 0–7 present in the logical
memory. Out of these eight pages, only four pages are present in the physical
memory. For the pages that are present in the main memory, the valid-invalid
bit is “v.”

FIGURE 9.1. Snapshot of memory in which all pages are not present in the main memory

Steps to Handle Page Faults


If a process tries to access a page and that page is not present in the main
memory, then a page fault occurs. While accessing the page, its page table
entry is found to be invalid “I,” which indicates that the desired page is not
present in the main memory. The situation of a page fault is not desirable, and

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
134 • Operating Systems

it results in a page fault trap. There are six main steps to follow for the oper-
ating system to handle a page fault trap.
The steps for handling a page fault are as follows, as shown in Figure 9.2:

FIGURE 9.2. Steps for handling a page fault

1. The page containing the instruction to be executed is searched in the page


table. Then its valid-invalid bit is checked. In a page fault, its entry in the
page table will be invalid.
2. When the valid-invalid bit of the desired frame is “I,” then a page fault trap
occurs and a signal will go to the operating system.
3. The operating system will locate the desired page in secondary memory
(backing store).
4. A free frame is located in the main memory. In that free frame the located
frame from secondary memory is stored.
5. After storing the desired page in main memory, its page table entry is cor-
rected. Hence the valid-invalid bit is now set to valid “v,” as the page is now
present in the main memory.
6. The instruction that was interrupted by the illegal page address trap will
now be restarted. The process will now get the page into the main memory
and the desired instruction will be executed smoothly.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 135

Performance of Demand Paging


Page Fault Rate 0 d” p d” 1.0
If p = 0, there are no page faults
If p = 1, every reference is a fault
Effective Access Time (EAT) = (1 – p) x memory access + p (page fault
overhead + [swap page out] + swap page in + restart overhead)
For better performance of demand paging, the effective access time should
be as small as possible. The smaller the value of EAT, the better the perfor-
mance of demand paging.

Example of Demand Paging


Suppose the memory access time is equal to 1 microsecond. Fifty percent
of the time, the page that is being replaced has been modified, and hence it
needs to be swapped out.
Swap Page Time = 10 msec = 10,000 msec
Effective Access Time (EAT) = (1 – p) x 1 + p (15000)
1 + 15000P (in msec)

Advantages of Demand Paging


QQ It brings a page into memory only when it is needed.
QQ Less Input/Output (I/O) is needed if demand paging is used.
QQ Less memory space is required.
QQ There is a faster response from processes.
QQ More users are allowed to execute their programs.

Self-Quiz

Q5. Define demand paging.


Q6. What is a page fault?
Q7. When does a page fault occur?
Q8. “The value of effective access time in demand paging should be at a
minimum for better performance.” Justify the statement.

PAGE REPLACEMENT

In handling a page fault, what will happen if there is no free frame? The
technique known as page replacement is used. In page replacement, find some
page in memory which may not be in use and swap it out to create a free
frame in memory as shown in Figure 9.3. Various page replacement algorithms
exist for selecting the victim page which needs to be swapped out of the main
memory. Some pages may be brought into memory several times according to
their requirement. Although there are many page replacement algorithms to
select the victim page, the best algorithm is the one which gives the minimum
number of page faults.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
136 • Operating Systems

Selecting a page in memory which may not be in use and swapping it out
create a free frame in memory and placing the desired page in that newly
created free frame is called page replacement.

FIGURE 9.3. Page replacement between physical memory and secondary memory

PAGE REPLACEMENT ALGORITHMS

A page replacement algorithm is the algorithm used to select a page which


is to be swept out of the main memory to create space for the requested page
which has caused the page fault. There are many page algorithms, such as first
in first out, optimal page replacement, least recently used, and so on.

First In First Out Algorithm (FIFO)


This is the simplest of all page replacement algorithms. When the page
fault occurs, then the oldest page in the memory is swept out of the main
memory to create space for the requested page that needs to be executed.
A reference string of pages is maintained in memory which is consulted to
select a victim. Reference string means that pages are referred from the main
memory in this sequence.
In the FIFO algorithm, swap out the page which entered into memory first
to create a free frame.
The implementation of FIFO has been explained with the help of an
example.
Example 9.1: Consider a reference string 2,1,3,4,1,5,3,2,1,4,5. Suppose
there are three free frames in memory initially. All frames are empty at the
beginning. Calculate the total number of page faults and page replacements
using FIFO.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 137

Solution: The working of the algorithm is shown in Figure 9.4.


2, 1, 3, 4, 1, 5, 3, 2, 1, 4, 5

FIGURE 9.4. First In First Out algorithm

The total number of page faults and page replacements in the given
example are as follows:
Page faults = 9
Page replacements = 6

Advantages of the First In First Out (FIFO) Algorithm


QQ It is a very simple algorithm.
QQ It is easy to implement.
QQ This algorithm can be coded easily.

Drawbacks of FIFO
QQ If the oldest page is used frequently, then performance of this algorithm
decreases because the page swapped out will be required and it will result
in the page fault.
QQ The FIFO algorithm suffers from Belady’s Anomaly.

Belady’s Anomaly
According to Belady’s Anomaly, the total number of page faults may increase
with an increase in the number of free frames. This can be proven with the help
of an example.
Example 9.2: Consider Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.
Calculate the number of page faults using a FIFO algorithm having
a. 3 frames
b. 4 frames
Initially all frames are empty.
Solution:
a. The given reference string is implemented using three frames as shown
in Figure 9.5.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
138 • Operating Systems

FIGURE 9.5. FIFO using 3 free frames

The total number of page faults and page replacements in the given
example are as follows.
Page faults = 9
Page replacements = 6
b. The given reference string is implemented using four frames as shown
in Figure 9.6.

FIGURE 9.6. FIFO using 4 free frames

The total number of page faults and page replacements in the given
example are as follows:
Page faults = 10
Page replacements = 7
As shown in the above example, in the FIFO algorithm, the number of
page faults increases as the number of free frames are increased from 3 frames
to 4 frames. This anomaly of result is called Belady’s Anomaly.

Self-Quiz

Q9. Why is page replacement required?


Q10. What is Belady’s Anomaly?
Q11. What is the working principle of the First In First Out algorithm?

Optimal Page Replacement


In this algorithm the page in the main memory which will be not be used for the
longest time is swept out from the main memory to create space for the requested

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 139

page. In this method to select the victim page, the future reference screen must
be seen. The page which is not to be used in the near future will be removed from
the main memory to create a free frame. In optimal page replacement, remove the
page that will not be used for the longest period of time. The implementation of
optimal page replacement has been explained with the help of an example.
Example 9.3: Consider a reference string 2, 1, 3, 4, 1, 5, 3, 2, 1, 4, 5. A
reference string means that pages are referred from the main memory in this
sequence. Suppose there are three free frames in memory initially.
Solution: The working of the algorithm is shown in Figure 9.7.

FIGURE 9.7. Optimal Page Replacement algorithm

The total number of page faults and page replacements in the given
example are as follows:
Page faults = 7
Page replacements = 4
Advantages of the optimal page replacement algorithm are as follows:
QQ This algorithm produces a minimum number of page faults.
QQ It improves the system performance by reducing overhead for the handling
of page faults.
Disadvantages of the optimal page replacement algorithm are as follows:
QQ This algorithm is difficult to implement.
QQ To implement this method the future reference should be known, which is
not practical.

Least Recently Used Algorithm


In this algorithm the page that has not been used for the longest time is
swapped out of the main memory to create space for the requested page.
Replace the page which has not been used for a very long time.
Implementation of the LRU is done in many ways. One way is to implement
it using an array. The array stores the information about the pages present in the
main memory. The front end of the array stores the page which has been accessed
recently. The rear end of the array stores the page that has not been accessed
for the longest period of time. However, the page which is present in the main
memory is accessed the information about the page in the array is shifted to the

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
140 • Operating Systems

front end of the array. If the page fault occurs, the page indicated by the rear end of
the array is swapped out of the main memory and the requested page is swapped
in. The implementation of LRU has been explained with the help of an example.
Example 9.4: Consider a reference string 2, 1, 3, 4, 1, 5, 3, 2, 1, 4, 5.
Reference string means that pages are referred from the main memory in this
sequence. Suppose there are three free frames in memory initially.
Solution: The working of the algorithm is shown in Figure 9.8.

FIGURE 9.8. Least Recently Used algorithm

The total number of page faults and page replacements in the given
example are as follows:
Page faults = 9
Page replacements = 6
Advantages of the Least Recently Used Algorithm are as follows:
QQ It is a very feasible method to implement.
QQ It is not as simple as the FIFO algorithm, but it is not as complicated as
optimal page replacement.
Disadvantages of the Least Recently Used Algorithm are as follows:
QQ It requires additional data structure to maintain the array.
QQ Extra hardware support is required.

Self-Quiz

Q12. What is the main drawback of the optimal page replacement algo-
rithm?
Q13. What is the working principle of the LRU algorithm?

Second Chance Algorithm

QQ This algorithm is a variation of the FIFO algorithm. In this method the


page which is present for the longest time in the memory is given a second
chance to remain present in the main memory. When that page is encoun-
tered a second time, it is taken out to create room for the page that has
caused the page fault. This algorithm is also known as the clock algorithm.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 141

QQ This algorithm is implemented using the concept of a circular queue. Each


cell in the circular queue contains two values, a page number and its cor-
responding reference bit. The value of the reference bit can be either 0 or
1. If the value of the bit of the page is 1, it means that page is encountered
as the oldest page and allowed the second chance.
QQ If the value of the reference bit is zero, this indicates that the oldest page
has not been encountered yet. When the oldest page in the memory is
found for the first time, the reference bit is set from 0 to 1. The next time
that page is found, it is swept out of the main memory.

FIGURE 9.10. Implementation of the second chance algorithm

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
142 • Operating Systems

As shown in Figure 9.10, the pointer is directly at page 61. The page fault
occurs. Page 61 has not been given the chance to remain in the memory,
because its reference bit is 0. The reference bit of page 61 is made to be 1, and
the pointer is moved to the next page which is 39.
The reference bit of page 39 is 1. This indicates that page 39 has previously
been given the chance to remain loaded in the memory. This page is swept out
of the memory to create space for the requested page 43.

Enhanced Second Chance Algorithm


This algorithm is a variation of the second chance algorithm. In this method,
instead of using a reference bit, two bits are used. One bit stores the informa-
tion about whether the page was recently used or not. If this bit is 0 it means
the page has not been used recently. If the bit is 1 it indicates that the page has
been used recently. This is known as the usage bit.
The second bit stores information about whether or not a page has been
modified. If its value is zero it indicates that it has not been modified in the
recent past. If the value is 1 then it indicates that the page has been modified.
This bit is known as a modification bit.
One bit for each page is set by hardware whenever the page is modified.
If a dirty page is replaced, it must be written to a disk before its page frame is
reused. The bit used to indicate a modified page is called a dirty bit.
The usage bit and modification bit are represented as a pair.
QQ The combination of [0, 0] indicates that the page has not been used or
modified. This type of page is the best option of page replacement.
QQ The combination of [0, 1] indicates that the page has not been used but
has been modified. This type of page is the second best option of page re­
placement.
QQ The combination of [1, 0] indicates that the page has been used but not
modified. This type of page is the third best option of page replacement.
QQ The combination of [1, 1] indicates that the page has been used as well as
modified. This type of page is not a good option of page replacement.

Counting-Based Algorithms
Some page replacement utilizes the method of counting how many times
a page has been used. Such an algorithm is known as a counting-based algo-
rithm.
Examples of counting-based algorithms are the least frequently used algo-
rithm and the most frequently used algorithm.
Least Frequently Used Algorithm (LFU): In the LFU algorithm the page
that was referred to the lowest number of times from the time when the page
is loaded in the main memory is replaced when the page fault occurs. The
logic behind this algorithm is that some pages are accessed more frequently
than others.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 143

Most Frequently Used Algorithm (MFU): The most frequently used algo-
rithm has reversed the idea of the LFU method. In the MFU the page that has
been referred to the most is replaced. The idea behind the algorithm is that
the page that has been referred to the lowest number of times may have been
loaded recently and would be required soon.

ALLOCATION OF FRAMES

In multiprogramming environments, many processes are to be present


in the main memory. Each process should get a reasonable amount of free
memory space so as to have a minimum number of page faults. The number
of frames in memory is limited. In this case with a limited number of frames
and many processes, how would the operating system allocate the free frames
to process pages? There should be some techniques in which the operating
system allocates a minimum number of free frames to each process in the
main memory, because each process needs a minimum number of pages.
This is known as the allocation of frames. There are two major allocation
schemes used by an operating system for the allocation of frames. They are
as follows:

QQ Fixed allocation
QQ Priority allocation
Let us briefly discuss each one.

Fixed Allocation
There are two types of fixed allocation techniques.

QQ Equal Allocation– In the equal allocation technique, each process will get
an equal number of frames. The process size will not affect the number of
frames allotted to it. For example, if there are 10 processes in the system
and 200 free frames in the main memory, according to equal allocation
each process will get 20 frames each.
The drawback of the equal allocation technique is it does not consider the
size of the process in allocating the frames. Whether the process is small or
large, it will get an equal number of frames. Hence the larger processes will
face frequent page faults.
QQ Proportional Allocation– The drawback of the equal allocation technique
is removed in this method. The idea behind this method is the number of
frames allotted to a process should be according to the size of the process.
If total number of available frames is “m,” then ai frames are allotted to
process pi, where ai is
ai = si / S * m
Where S = “ si

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
144 • Operating Systems

Example 9.5: Suppose there are two processes, P1 and P2, with sizes of 20
KB and 137 KB each. The number of free frames available is 72. Calculate the
total number of frames allotted to each process using
a. Equal allocation
b. Proportional allocation

Solution:
a. In equal allocation method process P1 will get 36 frames and process
P2 will also get 36 frames. The requirement of P1 is only of 20 frames;
hence, it is useless to allot 36 frames to P1. That is why the equal alloca-
tion method is not used.
b. Applying the formula a = s / S * m i i

a1 = (20/157) * 72
= 9.1
= 9 frames approximately
Hence P1 will get 9 frames.
a2 = (137/157) * 72
= 62.8
= 63 frames approximately
Hence P2 will get 63 frames.

Priority Allocation
QQ This method is a variation of the proportional method.
QQ In this method, the proportional allocation scheme is used, but by the pri-
oritizing of the processes rather than by size.
QQ If process Pi generates a page fault, a frame from a process is selected for
replacement with a lower priority number.

Global vs. Local Replacement


Global Replacement
QQ A frame from any process can be selected for replacement.
QQ A process selects a replacement frame from the set of all frames; one pro-
cess can take a frame from another.
QQ Higher priority processes may get frames from lower priority processes.
QQ The total number of frames allocated to each process will change in this
method as a process can get frames from any other process.

Local Replacement
QQ Each process selects from only its own set of allocated frames.
QQ A process cannot take frames from other processes.
QQ The total number of frames allocated to each process will not change in this
method as a process cannot get frames from any other process.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 145

Self-Quiz

Q14. Why is the proportional allocation method a better approach as com-


pared to equal allocation?
Q15. What is the priority frame allocation method?
Q16. What is the main advantage of the priority frame allocation method?

THRASHING

If a page fault occurs every time page is referred, then the system is said to
be thrashing. Thrashing is directly related to the rate of page fault occurrence.
The higher the page fault rate, the more the process will be in a thrashing state.
Thrashing degrades the performance of the system. When thrashing occurs,
the system performance downgrades. This happens because the processor
spends more time in handling page faults rather than actual processing of
instructions present in the pages. Thrashing has a negative effect on the system
performance.
When the page fault rate increases, it needs to be handled by page fault
handling strategies. The steps followed in handling page faults will need
processing from the paging device. Hence, the queue at the paging device
increases. This will result in increased service time for a page fault. In other
words, when the page fault rate is high, then time taken for handling page
faults also increases. Hence the steps/transactions in the system will wait for
the paging device. This will decrease CPU utilization and system throughput
and increase system response time, resulting in below optimal performance of
a system.
As the degree of multiprogramming of the system increases beyond a
certain level, thrashing occurs. Hence thrashing is a threat when the degree
of multiprogramming of the system increases. This situation is shown with the
help of Figure 9.11.
The graph shown in Figure 9.11 indicates that:

FIGURE 9.11. Thrashing with increase in degree of multiprogramming

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
146 • Operating Systems

QQ Initially when there is an increase in the degree of multiprogramming,


there is also improved throughput.
QQ Increased throughput is desired for optimal system performance.
QQ CPU utilization reaches a maximum point as the degree of multiprogram-
ming increases.
QQ After reaching that point there is a decline in throughput.
QQ From that point a sharp decline in throughput occurs and thrashing sets in.
QQ Hence thrashing occurs in the overextended system (a large number of
processes in the main memory).

METHODS TO PREVENT THRASHING

As explained in Section 9.6, thrashing has a negative effect on the system


performance. Hence it needs to be prevented.
Various strategies can be used to eliminate the occurrence of thrashing.
The various methods are:
QQ By controlling the load on the system
QQ Proper selection of a replacement policy to implement virtual memory
QQ Working set strategy
QQ Concept of pre-paging
QQ Page fault frequency

By Controlling the Load on the System


As shown in Figure 9.11 the load increase in the main memory results in
thrashing. Hence if a limited number of processes is kept in the main memory,
then this may help in preventing thrashing. That is why multiprogramming should
only be increased up to a certain level beyond which it will not be desirable. The
aim of increasing a degree of multiprogramming is to get improved throughput.
If an increase in the degree of multiprogramming results in thrashing and decline
in throughput, then it is preferable to have a lower degree of multiprogramming.

Proper Selection of a Page Replacement Policy


There are various page replacement policies in virtual memory. The proper
selection of a replacement policy to implement virtual memory can play an
important role in the prevention of thrashing. Local page replacement is
considered to be a better approach than global page replacement. It limits the
effect of thrashing. In Local replacement each process selects from only its own
set of allocated frames. A process cannot take frames from other processes.
The total number of frames allocated to each process will not change in this
method, as processes cannot get frames from any other process. Whereas in
Global replacement, a frame from any process can be selected for replace-
ment. A process selects a replacement frame from the set of all frames; one
process can take a frame from another. A process of higher priority may get
frames from low priority processes. The total number of frames allocated to

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 147

each process will change in this method as the process can get frames from
any other process. The replacement policy which uses the global replacement
method is more likely to cause thrashing.

Working Set Strategy


The set of pages that a process is currently using is called the working set
of the process. If the working set of a process is known, then pre-paging can be
done, which will help in reducing page faults. This strategy will directly help in
preventing thrashing in a system. Working sets is a conceptual model proposed
by Peter Denning to prevent thrashing.
QQ If the sum of all working sets of all processes exceeds the size of memory,
then stop running some of the processes for a while.
QQ Divide processes into two groups: active and inactive:
•• When a process is active its entire working set must always be in
memory: never execute a thread whose working set is not resident.
•• When a process becomes inactive, its working set can migrate to sec-
ondary memory.
•• Threads from inactive processes are never scheduled for execution.
•• The collection of active processes is called the balance set.
•• The system must have a mechanism for gradually moving processes
into and out of the balance set.
•• As working sets change, the balance set must be adjusted.

How to Compute Working Sets


QQ Denning proposed a working set parameter T. In the proposed model all
pages which are referenced in the last T seconds will be present in the
working set.
QQ A clock algorithm can be used and extended to keep an idle time for
each page.
QQ Pages with idle times less than T are in the working set.

The Concept of Pre-Paging


The set of pages that a process is currently using is called a working set of
the process. If the working set of a process is known, then pre-paging can be
done, which will help in reducing page faults. The pages which will be required
in the near future can be fetched in advance and kept in the main memory.
This is known as pre-paging. This strategy will directly help in preventing
thrashing in a system.

Page Fault Frequency


In this method the frequency of page faults can be observed. If a particular
process is not facing page faults, then it indicates that the process has a sufficient

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
148 • Operating Systems

number of pages allotted to it. On the other side if another process is facing a
lot of page faults, then it indicates that the process has an insufficient number
of pages allotted to it. In such situations, pages from processes with less faults
can be taken and allotted to the process which is facing frequent page faults.
Hence by observing the frequency of page faults, the occurrence of thrashing
can be controlled.
Although there are many strategies to limit thrashing, it cannot be prevented
fully. If a single process is too large for memory, then the OS cannot do much
in preventing thrashing. That process will simply thrash.
In practice, today’s operating systems do not worry much about thrashing.
In personal computers, users can notice thrashing and handle it themselves
by just buying more memory. They can manage balance sets manually. In
fact thrashing is a bigger issue for timesharing machines with many users. In
timesharing systems, the users will be reluctant to stop their programs so that
others can progress. Also such a system had to handle thrashing automatically.
In fact technology advancements have made changes to operate machines in
a range where memory is even slightly overcommitted because it is better to
just buy more memory.

DEMAND SEGMENTATION

Demand segmentation is similar to the concept of demand paging, but with


a few changes. In demand segmentation memory is allocated to the processes
in the form of segments. Segment descriptors are maintained in memory (like
page tables) to keep track of segments. Segment descriptors have details of the
segments in which processes are stored. They contain a field called the valid/
invalid bit. Its value can be 0/1. If the value of this entry is 1, it indicates that
the segment is present in the main memory; otherwise, valid segments are not
present in the main memory and a segment fault arises. As segments are of
different sizes (unlike pages), base and limit are stored in the segment table.
There are some differences between demand paging and demand segmen-
tation, which are as follows:
QQ In demand paging, the available memory space is divided into equal size
pages, whereas in demand segmentation, memory is divided in various-
sized segments.
QQ The demand paging method is mainly used to implement virtual memory,
because segment replacement algorithms are more complex than page re-
placement algorithms.
QQ Segment replacement algorithms are more complex than page replace-
ment algorithms (will be explained in section 9.4), because segments are of
different (variable) sizes.
QQ The programmer is aware of demand paging, but they are not aware of the
presence of demand segmentation.
QQ There is only one address space in demand paging, yet there may be many
address spaces in demand segmentation.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 149

QQ Internal fragmentation is present in demand paging, but it is removed in


demand segmentation.
QQ External fragmentation is absent in demand paging, but it is present in
demand segmentation.
QQ Despite its complexity, demand segmentation is used in some operating
systems. The IBM OS/2 operating system uses demand segmentation.

Self-Quiz

Q17. Define thrashing.?


Q18. What is the main cause of thrashing?
Q19. How can thrashing be detected?
Q20. How can thrashing be removed?

SUMMARY

Virtual memory is considered as an illusion that a computer system possesses


more memory than it actually has. This concept makes a process independent
of the size of the real main memory. It allows many processes to be present
in the main memory for executing without being restricted by its size. Various
concepts related to virtual memory have been explained in the chapter.

POINTS TO REMEMBER

QQ A technique that allows the execution of processes that may not be com-
pletely in the main memory is called virtual memory.
QQ To allow the programmer to make programs larger than the main memory
size, the concept of virtual memory has been used.
QQ Virtual memory is implemented by demand paging and demand seg-
mentation.
QQ In demand paging, the available memory space is divided into equal-size
pages.
QQ In demand segmentation memory is divided in various-sized segments.
QQ The paging technique in which pages are loaded in the main memory ac-
cording to demand or need is called demand paging.
QQ When a desired page is not present in the main memory, then such a condi-
tion is known as a page fault.
QQ For better performance of demand paging, the effective access time should
be as small as possible.
QQ The smaller the value of EAT, the better the performance of demand paging.
QQ Selecting some page in memory that may not be in use and swapping it out
creates a free frame in memory, and placing the desired page in that newly
created free frame is called page replacement.
QQ There are many page algorithms, for example, first in first out, optimal page
replacement, and least recently used.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
150 • Operating Systems

QQ In the FIFO algorithm, swap out the page which was entered into memory
first to create a free frame.
QQ The FIFO algorithm suffers from Belady’s Anomaly.
QQ According to Belady’s Anomaly, the total number of page faults may in-
crease with an increase in number of free frames.
QQ In optimal page replacement, remove the page that will not be used for the
longest period of time.
QQ In LRU replace the page which has not been used for a very long time.
QQ In the second chance algorithm, the page which is present for the longest
time in memory is the given the second chance to remain present in the
main memory. When that page is encountered for a second time it is taken
out to create room for the page that has caused the page fault.
QQ The second chance algorithm is also known as the clock algorithm.
QQ To allocate a minimum number of free frames to each process in the main
memory because each process needs a minimum number of pages is known
as the allocation of frames. When the processor spends more time in han-
dling page faults rather than the actual processing of instructions present in
the pages, then this situation is known as thrashing.

REVIEW QUESTIONS

Q1. Discuss the following in detail.


a. Demand paging
b. Thrashing
Q2. What do you understand about page replacement? Name the algo-
rithms available for page replacement.
Q3. How many page faults occur in the optimal page replacement algo-
rithm with the following reference string for four page frames?
1,2,3,4,5,3,4,1,6,7,8,7,8,9,7,8,9,5,4,5,4,2
Q4. Consider the following page reference string 4,3,2,1,4,3,5,4,3,2,1,5.
How many page faults would occur for the following page replacement
algorithms assuming three frames? Initially all frames are empty.
a. FIFO
b. LRU
c. Optimal
Q5. What is thrashing? How is it overcome?
Q6. Define virtual memory concepts and also discuss page replacement
algorithms in brief.
Q7. When does page fault occur? Describe in detail the actions taken by
the operating system when a page fault occurs.
Q8. What is the cause of thrashing? How does a system detect thrashing?
Once it detects thrashing, what can the system do to eliminate this
problem?
Q9. Consider a demand paged system. Page tables are held in registers. It
takes 8 milliseconds to handle a page fault if an empty page is available

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
Virtual Memory • 151

or if the replaced page is not modified. The memory access time is


100 nanoseconds. Assume that the page to be replaced is modified 70
percent of time. What is the minimum acceptable page fault for an
effective access time of no more than 200 nanoseconds?
Q10. Explain advantages and disadvantages of thrashing.
Q11. Consider the following pages in a reference string.
1,2,0,3,5,1,5,7,2,0,3,5,4,1,2,5,3,7
Implement FIFO, LRU, and optimal page replacement algorithms
and calculate the total number of page faults.
Q12. Consider the following pages in a reference string.
1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6
Implement FIFO, LRU, and optimal page replacement algorithms
and calculate the total number of page faults using three and four
frames.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
CHAPTER
10
FILE SYSTEMS

Purpose of This Chapter


The purpose of this chapter is to explain the function of file systems, to
describe the interfaces to file systems, to discuss file-system design tradeoffs,
including access methods and directory structures, to describe the allocation
methods, and to explore file-system protection.

INTRODUCTION

In an operating system the file system is the most noticeable aspect.


The mechanism provided by the file system is to store online both the
data and the programs of the operating system and to access those data
and programs. The file system contains two different parts, a collection of
files (each file storing related data) and a directory structure (that organizes and
provides information about all the files in the system). This chapter emphasizes
the various aspects of files and the main directory structures, and describes the
methods to allocate disk space, recover freed space, track the locations of data,
and interface other parts of the operating system to secondary storage. Last,
it will discuss the ways of handling file protection. File protection is required
when there are multiple users and controlling is required to determine who
may access files and how the files may be accessed.

CONCEPT OF FILE

A disk can be considered as a linear sequence of fixed-size blocks. To


explain the file (a logical storage unit), the physical properties are abstracted
by the operating system from its storage devices. A file is a named collection of
related information. All this information is recorded on secondary storage as a

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
154 • Operating Systems

sequence of bytes. The file can be viewed as logical and physical. The logical or
programmer’s view depicts how the files can be used and what properties they
have (i.e., as the users can see it) whereas the physical or operating system’s
view represents how these files actually exist in secondary storage.
A file represents the programs (programs include source and object forms)
and the data (a data file can be numeric, alphanumeric, alphabetic, or binary).
A file is a sequence of bits, bytes, lines, or records. All the information that
is stored in a file is defined by the creator of that file. A file can store many
different types of information, such as source programs, object programs,
executable programs, numeric data, text, payroll records, graphic images,
sound recordings, and so on.
We know that a file can store different information. Therefore the struc-
ture of a file may also vary depending on its type. The file is said to be a
text file when the sequence of characters is organized into lines or pages.
When there is a sequence of subroutines and functions in a file, and these
subroutines and functions are further organized as declarations followed
by executable statements, then the file is said to be a source file. A file
that contains a sequence of bytes organized into blocks explicable by the
system’s linker is called an object file. A file that holds a series of code
sections that the loader can bring into memory and execute is known as an
executable file.
Attributes of a file: Although the attributes of a file may vary depending
on the operating system, usually the file consists of the following attributes:
QQ Name: The file is named so that it will be convenient for users. The name
of the file is the only information that is in the human readable format.
QQ Identifier: For any file identifier, the name cannot be read by the users, as
it in the non-human readable format. The identifier is a unique tag (usually
a number) that identifies the file within the file system.
QQ Type: This information is required by those systems that support different
types of files.
QQ Location: This information tells the location of a file stored on the de-
vice. Therefore it is a pointer to a device and to the location of the file on
that device.
QQ Size: This field gives the current size of the file or the maximum allowed
size for any file in bytes, words, or blocks.
QQ Protection: This field provides the access-control information, that is, it
determines that who can read, write, or execute a file.
QQ Time, date, and user identification: This information keeps track of
when and who created, last modified, or last accessed the file. All this infor-
mation is useful for protection, security, and usage monitoring.
The information about all the files is kept in the directory structure. This
directory structure also resides on the secondary storage. A directory contains
the name of the file and its unique identifier. Other attributes of the file will be
located by the identifier.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 155

Operations on file: A file is an abstract data type. The proper definition of a


file can be provided on the basis of the operations that can be performed on a file.
The six basic file operations are create, read, write, reposition, delete, and truncate.
To perform all these operations, the operating system can provide system calls.
QQ Create: For creating a file, two steps are required: (a) Space for the file
must be found in the file system, and (b) An entry regarding the new file
must be made in the directory.
QQ Write: For writing a file, a system call is made that specifies both the name
of the file and the information to be written to the file. A write pointer must
be kept by the system at the location where the next write is to take place.
Whenever a write takes place, the write pointer must be updated.
QQ Read: To perform the read operation on a file, a system call must be used,
which specifies the name of the file and the location in memory (i.e., where
the next block of the file should be put). Also, a read pointer is required that
is kept at that location in the file where the next read is to take place. The
read pointer is updated after the read has taken place. Typically, a process
can be either reading from or writing to a file, and therefore the current op-
eration location can be kept as a per-process current file-position pointer.
This same pointer can be used by both the read and write operations, which
saves space and reduces system complexity.
QQ Reposition: The directory is searched for the appropriate entry, and the
current-file-position pointer is repositioned to a given value. No Input/
Output is involved in repositioning within a file. This file operation is also
known as a file seek.
QQ Delete: For deleting a file, the directory is searched for the named file.
After finding the associated directory entry, all the file space is released
(so that this space can be reused by other files) and the directory entry is
erased.
QQ Truncate: There may be a case when the user may want to erase the con-
tents of a file but keep its attributes. Truncating a file allows all attributes to
remain unchanged except the length of file (the length is reset to zero and
its file space is released).
These six operations are the basic required file operations. Other than these
basic operations, some common operations are appending new information
at the end of an existing file and renaming an existing file. All these operations
can then be combined to perform other file operations such as to create a copy
of a file or to copy the file to another I/O. Some operations are also required
that allow a user to get and set the various attributes of a file.
The OS maintains a small table, known as the open-file table, which contains
the information about all open files. Whenever a file operation is requested,
the file is specified in this table through an index number. Therefore, overhead
of searching is not required. When the file is not being used, it gets closed by
the process, and its entry from the open-file table is removed by the operating
system.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
156 • Operating Systems

Most of the systems require that the programmer open a file explicitly
using the open() system call before that file can be used. The open() operation
takes a file name and starts searching the directory, by copying the directory
entry to the open-file table. Access-mode information (such as create, read-
only, read-write, append-only, and so on) can also be accepted by this system
call. The file will be opened for the process, if the request mode is allowed.
Usually the open() system call returns a pointer to the entry in the open-file
table. This pointer is used in all I/O operations, which avoids further searching
and simplifies the system-call interface.
To implement the open() and close() operations in an environment where
several processes may open the file at the same time is complicated. This
condition arises in those systems where at the same time several different
applications open the same file. Usually two levels of internal tables are used
by an operating system, that is, a per-process table and a system-wide table.
The per-process table keeps track of all the files that a process has open. For
example, the current file-position pointer for each file is found here. It can also
include the access rights to the file and the accounting information. Each entry
in the per-process table points to a system-wide table in sequence. The system-
wide table contains information that is process independent, such as the file
location on disk, access dates, and file size. Once a process opens the file, the
system-wide table includes an entry for that file.
Usually, the open-file table also has an open count which is associated with
each file to specify how many processes have the file open. Each close() system
call decreases this open count, and when the open count becomes zero, it
means that the file is no longer in use, and the entry is removed from the open-
file table. The following is some information associated with an open file: file
pointer, file-open count, disk location of the file, and access rights.

TABLE 10.1. Common File Types


file type usual extension function
executable exe, com, bin or none ready-to-run machine-language
program
object obj, o compiled, machine language, not linked
source code c, cc, java, pas, asm, a source code in various languages
batch bat, sh commands to the command interpreter
text txt, doc textual data, documents
word processor wp, tex, rtf, doc various word-processor formats
library lib, a, so, dll libraries of routines for programmers
print or view ps, pdf, jpg ASCII or binary file in a format for
printing or viewing
Archive arc, zip, tar related files grouped into one file, some-
times com­pressed, for archiving or storage
multimedia mpeg, mov, rm, mp3, binary file containing audio or A/V
avi information

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 157

Some of the operating systems provide facilities for locking an open file or
sections of a file. File locks allow one process to lock the file and prevent its
access from other processes. File locks are useful for the files that are shared
by several processes.
Types of File: To implement a file type, the most common technique is
to include the type as a part of the file name. The name of a file is split into
two parts, a name and an extension, both separated by a period character as
shown in Figure 10.1. Therefore, the user and the operating system can tell
the type of the file from its name only. For example, schedule.doc, Server.java,
and ReadThread.c. The extension of the file is used by the system to indicate
the file type and the type of operations that can take place on that file. For
example, only the files with a .com, .exe, or .bat extension can be executed. The
.com and .exe files are the two forms of binary executable files, and a .bat file is
a batch file that contains (in ASCII or American Standard Code for Implemen-
tation Interchange format) commands to the operating system. Microsoft Disk
Operating System or MS-DOS recognizes only a few extensions.
File extensions are also used by the application programs to specify the
type of the file in which they are interested. For example, the assemblers
expect source files to have an .asm extension, and the Microsoft Office Word
processor expects files to end with a .doc extension. As the operating system
does not support these extensions, they can be thought of as “hints” to the
applications that operate on them.
Many of the file systems support names as long as 255 characters. Some file
systems differentiate between upper and lower case letters (i.e., case sensitive
and case insensitive). Both Windows 95 and Windows 98 use the Microsoft
Disk Operating System or MS-DOS file system and therefore inherit many of
its properties, such as how the file names are constructed. Also, Windows NT
and Windows 2000 support the MS-DOS file system, so these two also inherit
its properties. However, these two systems also have a Native File System or
NTFS that has different properties (such as file names in Unicode).
Consider the Mac OS X OS, where each file has a type, such as TEXT (for
text file) or APPL (for application). Here each file also has the attribute of a
creator, which contains the name of the program that created it. The operating
system sets this attribute during the create() system call, and therefore its use
is enforced and supported by the system.
The UNIX system uses a basic magic number that is stored at the begin-
ning of some files to indicate roughly the type of file, such as executable
program, batch file, PostScript file, and so on. As all the files do not contain
the magic number, the features of a system cannot be based only upon this
information. Also, UNIX does not keep track of the name of the creating
program. File-name-extension hints are not allowed in UNIX, but these
extensions are neither enforced nor depended on by the OS; typically, they
are destined to help the users to determine the type of contents of the file.
Depending on the application the extensions can be used or ignored, but
this is totally up to the application’s programmer.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
158 • Operating Systems

Internal File Structure: To locate an offset within a file can be compli-


cated for the operating system. Usually disk systems have a well-defined
block size, which is determined by the size of a sector. The entire disk I/O
is performed in units of one block (physical record), and all blocks are of the
same size. It is improbable that the size of the physical record matches exactly
the length of the desired logical record. One common solution to this problem
is to pack a number of logical records into physical blocks.
For example, the UNIX operating systems defines all files to be the streams
of bytes, where each byte is individually addressable by its offset from the
beginning or end of the file. In this case, the size of the logical record is 1 byte.
The file system packs and unpacks bytes automatically into physical disk blocks
(512 bytes per block) as essential.
The file may be considered as a sequence of blocks, and all the basic I/O
functions operate in terms of the blocks.
Disk space is always allocated in blocks; therefore, some portion of the
last block of each file is wasted. For example, if each block were of 512 bytes,
then a file of 1,949 bytes would be allocated four blocks (i.e., 2,048 bytes) and
the last 99 bytes would be wasted. To allocate the complete file into blocks
rather than bytes is called the internal fragmentation. All the system files suffer
from internal fragmentation, that is, the larger the block size, the greater the
internal fragmentation.

Self-Quiz

Q1. Explain file attributes.


Q2. What are the operations that can be performed on a file? Explain.
Q3. Write a short note on the types of files.

ACCESS METHODS

The information that is stored in a file can be accessed in various ways.


Some systems can access the files using only one access method, whereas other
systems (such as IBM or International Business Machines), support various
access methods for accessing the file. Choosing the correct access method for
any particular application is a major designing problem.

Sequential Access
This is the simplest access method. In this method, processing of the infor-
mation that is stored in a file must be done in order, that is, one record after the
other. This is the most common method for accessing the file. For example, the
editors and the compilers of a computer usually access the files in the following
manner:
On a file, the reads and writes operations are used in bulk. A read opera-
tion, that is, “read next,” reads the next portion of the file and updates the file
pointer automatically, to which the I/O location would be tracked. In the same

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 159

way, the write operation, that is, “write next,” appends to the end of the file and
advances to the end of the newly written material. Such a file can be reset to
the beginning, and on some systems, a program may be able to skip forward or
backward n records for some integer n. This method is called sequential access
to the file and is based on a tape model of a file.

Direct Access
Another method for accessing a file is direct access, also known as the rela-
tive access method. A file is a collection of fixed length logical records, which
allow programs to read and write records quickly in a random order. The direct
access method is based on a disk model of a file, because disks allow random
access to any block of a file. For direct access, the file can be viewed as a
numbered sequence of blocks or records. For example, after reading block 17,
the program could read block 58 next, and then write block 9. In a direct access
method, there is no restriction on the order of reading or writing.
Direct access files can be used when large amounts of information require
immediate access. An example is in databases, where when a query arrives, the
block that contains the answer is computed first and then the read operation
takes place for that block directly to provide the desired information.
In the case of the direct access method, the file operations must be modi-
fied for the purpose of including the block number as a parameter. Therefore,
we have “read n” rather than “read next,” and “write n” rather than “write next,”
where “n” is the block number. An alternative approach is to keep the “read
next” and “write next” (as it is done in sequential access) and add an operation
to “position file to n,” where n is the block number. Then, to affect a “read n,”
first the “position to n” and then the “read next” commands are issued.
All the operating systems do not support both the sequential and the direct
access for files. Some systems allow only sequential file access, whereas others
allow only direct access. Some of the systems require that when a file is created
it must be defined as sequential or direct. This type file can be accessed only
by that method that was declared at file creation time.

Other Access Methods


On the top of the direct access method, other access methods can be built.
In these additional methods an index for a file is created, which contains
pointers to various blocks. Therefore, to find an entry in the file, first the
index is searched and then the pointer is used to access the file directly for the
purpose of finding the desired entry.
In the case of large file, the index for that file also becomes too large to
store in memory. This problem can be solved by creating an index for the index
file. In this case the primary index file would contain pointers that point to the
secondary index files, and the secondary index files would contain pointers that
point to the actual data items.
For example, a small master index is used by IBM’s Indexed Sequential
Access Method (ISAM) that points to disk blocks of a secondary index, and the

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
160 • Operating Systems

secondary index blocks point to the actual file blocks. The file is kept sorted on
a defined key. To search for a particular item, first a binary search should be
applied on the master index, through which the block number of the secondary
index is found. This block is read in, and again a binary search is applied to find
the block containing the desired record. Finally, this block is searched for in
a sequential manner. Though this method, any record can be located from its
key by most direct access reads.

Self-Quiz

Q4. What do you mean by access?


Q5. Explain the sequential access method.
Q6. What are the advantages of the direct access method over the sequen-
tial access method? Explain.

DIRECTORY STRUCTURE

A system may have zero or more file systems, and the file systems can be
of various types. Therefore, the file systems of computers can be widespread.
Some of the systems may store millions of files on Terabytes (TB) of disk. To
manage all these data, it is necessary to organize them. Using a directory is
required for organizing those data.

Storage Structure
For a file system, a disk may be used completely. Therefore, for storing
multiple file systems a whole disk or a part of a disk (where the other
part can be used for other things, such as swap space or unformatted disk
space) is required. On each part of the disk, a file system can be created.
The parts of a disk are known as partitions, slices, or minidisks (according
to IBM). The part of the disk that holds the file system is referred to as a
volume, and a volume can be thought of as a virtual disk. Multiple operating
systems can also be stored on a volume that allows a system to boot and run
more than one.
If a volume contains a file system, then it must also contain informa-
tion about the files in the system. All this information is kept in entries in
a directory or volume table of contents. The directory records informa-
tion such as name, type, address, current length, maximum length, date
last accessed, date last updated, owner ID, and protection information
for all files on that volume. A typical file-system organization is shown in
Figure 10.1.

Directory Overview
The directory can be viewed as a symbol table that translates the file names
into their directory entries. The following are the operations that have to be
performed on a directory when considering a particular directory structure:

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 161

FIGURE 10.1. A typical file system organization

QQ Search for a file: Need to find a particular file or find all the files whose
names match a particular pattern.
QQ Create a file: Need to create a new file and to add its entry to the
directory.
QQ Delete a file: Need to remove a file from the directory, when that file is
no longer required.
QQ List a directory: Need to list both the files in the directory and the direc-
tory contents for each file.
QQ Rename a file: Need to rename a file when the contents or use of the file
changes. Renaming a file may involve changing the position of the file entry
in the directory structure.
QQ Traverse the file system: Need to access every directory and every file
within a directory structure. Therefore, for reliability, the directory needs
a logical structure.

The most common schemes for defining the logical structure of a directory
would be single-level directory, two-level directory, tree-structured directo-
ries, acyclic-graph directory, and general-level directory.

Single-Level Directory
The single-level directory is the simplest method for defining the logical
structure of a directory. In the single-level directory structure all the files are
contained in the same directory as shown in Figure 10.2. The single-level
directory is easy to understand and to support.
This type of structure was commonly used in early computers (such
as super computers), because there was only one user. A single-level
directory does not work properly when the number of files on the system
increases or when the system has more than one user. Because in this
directory structure all files are in the same directory, their names must
be unique. If two users name their data file create, then the unique-name
rule is violated.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
162 • Operating Systems

FIGURE 10.2. Single-level directory

Two-Level Directory
The problems of a single level directory can be resolved by creating a sepa-
rate directory for each user. This standard solution is termed as a two-level
directory structure, where each user has a separate User File Directory (UFD).
The structure of all the UFDs is similar, but each UFD contains the files of
single user only.
When a user starts a job on the system, the Master File Directory (MFD) of
that system is searched. The index for this MFD will be either the user name
or the account number, and each entry of the MFD points to the UFD for
that user.
Whenever a user wants to refer to a particular file (for the purpose of crea-
tion, deletion, alteration, etc.) then only that user’s own UFD is searched to find
that file. The two-level directory structure solves the name-collision problem,
but it still has a drawback in that it isolates one user from the other. Isolating
the users could be an advantage when the users are not dependent on each
other, but is considered as a disadvantage when the users want to share files.
A two-level directory structure can be thought of as a tree, or an inverted
tree, or a tree of height 2. The root of the tree is the MFD, the direct descend-
ants of the root (MFD) are the UFDs, and the files are the direct descendants
of these UFDs.

FIGURE 10.3. Two-level directory Structure

These files are the leaves of the tree. The path from the root (MFD) to the
leaf (specified file) in the tree can be defined by specifying the name of the
user and the name of the file. To specify the volume of a file, additional syntax
is required. For example, in MS-DOS the volume of a file is specified by a letter
followed by a colon. Hence, the specification of a file might be: C:\user\create.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 163

Tree-Structured Directories
We have seen in a previous section how a two-level directory can be viewed
as a two-level tree. To extend a directory structure to a tree structure of arbi-
trary height as shown in Figure 10.4 is a natural generalization. According to
this generalization users are allowed to create their own subdirectories and
organize their files consequently. The tree-structured directory is the most
common directory structure.

FIGURE 10.4. Tree-Structured directory

In this type of directory structure, the tree has a root directory, and all the
files in the system have a unique path name. A directory is just another file, but is
treated in a special way. The internal formats of all the directories are similar. One
bit in each directory entry defines the entry as a file (0) or as a subdirectory (1).
The path name can be of two types, that is, absolute and relative. An abso-
lute path name begins from the root and follows a path down to the specified
file, giving the directory names on the path. The relative path name defines a
path from the current directory.
With a tree-structured directory system, users can also be allowed to access
the files of other users. For example, user Y can access a file of user X by speci-
fying its path names. User Y can specify either an absolute or a relative path
name. On the other hand, user Y can change the current directory to be user
X’s directory and access the file by its file names.

Acyclic-Graph Directories
The natural generalization of the tree-structured directory is the acyclic
graph. In the acyclic graph, the common subdirectory should be shared. At
certain times, a shared directory or file will exist in the file system in two (or
more) places.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
164 • Operating Systems

Sharing of files or directories is not allowed in a tree structure. An acyclic


graph (i.e., a graph that does not have any cycle), allows the directories to share
subdirectories and files as shown in Figure 10.5. The same file or subdirectory
may be present in two different directories.

FIGURE 10.5. Acyclic-graph directory Structure

The shared file (or directory) and the two copies of the file are not
the same thing. The two copies of the file imply that each programmer
can view the copy rather than the original file, but if any one programmer
changes the file, then those changes will not appear in the other’s copy.
Whereas in a shared file there exists only one actual file, so if any changes
are made by one person on the file, then the changes will be immediately
visible to the other.
There are various ways available to implement the shared file and subdirec-
tories. As demonstrated by many of the UNIX systems, the most common way to
implement the shared files and subdirectories is to create a new directory entry
called a link. A link is a pointer to another file or subdirectory. The directory is
searched when a reference to a file is made. If the entry of the directory is marked
as a link, then the name of the real file is included in the link information. The
link can be resolved by using that path name to locate the real file. Links are
easily identified by their format in the directory entry and are effectively named
indirect pointers.
Another common method to implement the shared files is to simply dupli-
cate all the information about them in both the sharing directories. There-
fore, both the entries are identical and equal. It is clear that a link is different
from the original directory entry; hence, the two are not equal. When a file
is modified, maintaining consistency is the major problem with duplicate
directory entries.
There are several problems with the acyclic-graph directory structure. The
first problem is that in this directory structure a file has multiple absolute path

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 165

names, so distinct file names may refer to the same file. Another problem is
regarding deletion, that is, when to deallocate or to reuse the space that is
allocated to the shared file. One possible solution is to remove the file when-
ever anyone deletes it, but the removal of the file will leave dangling pointers,
which will be pointing to the now non-existent file. The worst case occurs if
the dangling pointers point to the middle of other files, when the remaining
file pointers contain the addresses of actual disk and accordingly the space is
reused for other files.
The use of symbolic links to implement sharing in the system is easy to
handle. By deleting the link, only the link will be removed and the original
file will not be affected at all. But if the entry of the file is deleted, then the
allocated space for the file will be deallocated, and this will leave the dangling
links. These links cannot be considered until an attempt is made to use them.
And when these links are used, they point to the name of the file that does not
exist and fail to resolve the link. This type of access is treated in a similar way as
with any other illegal file name. In UNIX, symbolic links are left when a file is
deleted, and it depends on the user to realize that the original file is deleted or
has been replaced. Microsoft Windows also uses the same approach.
The other approach for deletion is to save the file until all the references to
it are deleted. For implementing this approach, a mechanism is required for
determining that the last reference to the file has been deleted. This approach
is not very feasible because of the variability and the large size of the file-
reference list.
However, it is not required to keep the entire list. The only requirement
is to keep count of the number of references. The reference count is incre-
mented when a new link or directory entry is added and is decremented when
a link or directory entry is deleted. When the reference count is 0, it means
that there is no remaining reference to it and the file can be deleted.
This approach is used by the UNIX operating system for non-symbolic links
(or hard links), where a reference count is kept in the file information block (or
inode). By effectively eliminating multiple references to directories, an acyclic-
graph structure is maintained.
To avoid the problems discussed above, some systems do not support the
concept of shared directories or links. For example, in MS-DOS, the directory
structure is a tree structure rather than an acyclic-graph directory structure.

General Graph Directory


An acyclic-graph structure ensures that there are no cycles, which
results in a major problem. Consider a two-level directory and allow the
users to create subdirectories; this will result in a tree-structured direc-
tory. It is very simple to preserve the tree structure if the new files and
directories are added to an existing tree structure. However, when the
new links are added to an existing tree-structured directory, then the tree
structure is destroyed, and it results in a simple graph structure as shown
in Figure 10.6.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
166 • Operating Systems

FIGURE 10.6. General Graph Directory

Self-Quiz

Q7. What do you understand by directory structure?


Q8. Explain the single-level directory structure.
Q9. What are the advantages and disadvantages of two-level directory
structure?
Q10. Write short notes on:
a. Tree-structured directory
b. Acyclic-graph directory

FILE-SYSTEM STRUCTURE

Secondary storage is provided by disks, where a file system is maintained.


Disks have two main characteristics that make them a suitable medium for
storing multiple files:

1. A disk can be rewritten in place; it is possible to read a block from the disk,
modify the block, and write it back into the same place.
2. A disk can access directly any given block of information it contains.
Therefore, it is easy to access any file either sequentially or randomly, and
switching from one file to another requires only moving the read-write heads
and waiting for the disk to rotate.
I/O transfer between memory and disk are performed in units of
blocks, rather than transferring a byte at a time, to improve the I/O effi-
ciency. Each block contains one or more sectors. Sectors vary from 32
bytes to 4,096 bytes (usually they are 512 bytes), depending on the disk
drive.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 167

For efficiently and conveniently accessing the disk, the operating system
imposes one or more file systems to allow the data to be stored, located, and
retrieved easily. For a file system the two design problems are:
1. To define how the file system should look to the user. This task involves defin-
ing a file and its attributes, the operations allowed on a file, and the directory
structure for organizing the files.
2. Creating algorithms and data structures to map the logical file system onto the
physical secondary-storage devices.
Generally, the file system is composed of many different levels. The struc-
ture shown in Figure 10.7 is an example of a layered design, where each level
uses the features of lower levels to create new features for use by higher levels.
The I/O control consists of device drivers and interrupts handlers to transfer
information between the main memory and the disk system. A device driver
can be thought of as a translator, which takes high-level commands as input and
gives output as low level, hardware-specific instructions. These instructions are
used by the hardware controller, which is an interface between the I/O devices
to the rest of the system. The device driver usually writes specific bit patterns
to special locations in the I/O controller’s memory to tell the controller which
device location to act on and what actions to take.
The only requirement of the basic file system is to issue generic commands
to the appropriate device driver for the purpose of reading and writing physical
blocks on the disk. Each of the physical blocks is identified by its numeric disk
address (for example, drive 2, cylinder 75, track 3, sector 13, etc.).

application programs

logical file system

file-organization module

basic file system

I/O control

devices

FIGURE 10.7. Layered file system

The file-organization module keeps information about the files and their
logical and physical blocks. If the type of file allocation used and the location of
the file is known, then the file-organization module can translate logical block
addresses to physical block addresses for the basic file system to transfer.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
168 • Operating Systems

The logical blocks of each file are numbered from 0 (or 1) through N.
Because the physical blocks that contain the data usually do not match the
logical numbers, a translation is required to locate each block. The free-space
manager is also included in the file-organization module, whose task is to keep
track of unallocated blocks and provide these blocks to the file-organization
module when requested.
The metadata information is managed by the logical file system. All of
the file-system structures (except the contents of the files) are included in
the metadata. A file-control block (FCB) contains information about the file,
which includes ownership, permissions, and the location of the file contents.
The logical file system is also responsible for protection and security.
For a file-system implementation, when a layered structure is used, then
the duplication of code is minimized. Sometimes both the I/O control and the
basic file-system code can be used by multiple file systems. In such cases each
file system can have their own logical file system and file system modules.
Most of the operating systems support more than one file system. For example,
most CD-ROMs are written in the ISO 9660 format, a standard format agreed
upon by CD-ROM manufacturers. In addition to removable-media file systems,
each operating system has one disk-based file system (or more). UNIX uses the
UNIX file system (UFS), which is based on the Berkeley Fast File System (FFS).
Windows NT, 2000, and XP support disk file-system formats of FAT, FAT32, and
KTFS (or Windows NT File System), as well as the CD-ROM, DVD, and floppy-
disk file-system formats. Although Linux supports over forty different file systems,
the standard Linux file system is known as the extended file system, with the most
common version being ext2 and ext3. There are also distributed file systems, in
which a file system on a server is mounted by one or more clients.

ALLOCATION METHODS

With the direct access nature of disks, the implementation of files becomes
more flexible (in most cases many files are stored on the same disk). The major
problem in file management is how the space for files will be allocated to get
effective utilization of disk space and quick access of files. The three widely
used disk space allocating methods are: contiguous, linked, and indexed. Each
of these methods has its own advantages and disadvantages. Some systems (for
example, Data General’s RDOS) use all three allocation methods. Generally, a
system will use only one particular method for all files.

Contiguous Allocation
In the contiguous allocation of files, each file occupies a set of contiguous
blocks (addresses) on the disk. The addresses of a disk define a linear ordering
on the disk. According to this linear ordering (assume that only one job is
accessing the disk), accessing block n + 1 after block n does not require any
head movement. But whenever head movement is required (it is required
when needing to access the first sector of one cylinder after accessing the last

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 169

sector of the previous cylinder), it will only move from one track to the next.
Therefore, the required number of disk seeks for accessing contiguously allo-
cated files is minimal, as is seek time when a seek is finally needed. Contiguous
allocation of a file is defined by the disk address and length of the first block.
If the file is h blocks long and starts at location n, then it occupies blocks n, n +
1, n + 2, . . ., n + h -1. The directory entry for each file indicates the address of
the starting block and the length of the area allocated for this file. Contiguous
allocation of files is shown in Figure 10.8.

FIGURE 10.8. Contiguous Allocation of Disk Space

To find the space for a new file is difficult in the contiguous alloca-
tion of files. If the file to be created is h blocks long, then the OS must
search for h free contiguous blocks. First-fit, best-fit, and worst-fit are the
most common strategies (as we have already studied) that are used to select
a free hole from the set of available holes. According to the simulation
results, both the first-fit and the best-fit strategies are better than worst-fit
in terms of time and storage utilization. But in terms of storage utilization
neither the first-fit nor the best-fit is best, but the first-fit strategy is gener-
ally faster.
All these algorithms also suffer from external fragmentation. As the
files are allocated and deleted from the disk, then the free disk space is
broken into small pieces. External fragmentation exists when enough disk
space is available to satisfy a request, but this space is not available in a
contiguous order; hence the storage is fragmented into a large number of
small holes.
To determine how much disk space is required for a file is another problem
that is associated with contiguous allocation. Whenever a new file is created,
then the total amount of space it will require must be known and allocated. But
how does the creator (program or person) come to know about the size of the
file that has to be created? In some cases, it is easy to determine the size of a
file (for example, copying an existing file), but generally the size of an output
file may be difficult to estimate.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
170 • Operating Systems

Linked Allocation
All the problems of contiguous allocation are solved by linked allocation.
In linked allocation, each file is a linked list of disk blocks, where all the disk
blocks may be scattered anywhere on the disk. In the linked allocation method,
the directory contains a pointer to the first and the last block of the file. For
example, as shown in Figure 10.9, a file that has five blocks starts with block 9
and then continues to block 16, then block 1, then block 10, and finally to block
25. Here each block contains a pointer to the next block.

FIGURE 10.9. Linked allocation of disk space

To create a new file, a new entry is to be created in the directory. In the


linked allocation method, each directory entry contains a pointer to the first
disk block of the file. To denote an empty file, this pointer is initialized to nil
(the end-of-list pointer value). The size field is also set to 0. A write operation
takes place by finding the first free block, then this new block is written, and at
last this new block is linked to the end of the file. To read a file, the blocks are
read by following the pointers from block to block.
The linked allocation does not suffer from external fragmentation, and any
of the free blocks can be used to satisfy a request. Therefore, the size of a file
should not be declared at the time of file creation. The size of the file can grow
as long as there are free blocks available.
Although linked allocation solves the external fragmentation and size decla-
ration problems of contiguous allocation, it also has some disadvantages:
1. The major problem of linked allocation is that it supports the sequential access
of files and is inefficient for direct access. For example, to find the jth block of a
file, it will start at the beginning of that file and follow the pointers until the jth
block is reached (each access to a pointer requires a disk read).
2. Another disadvantage of linked allocation is the space occupied by the point-
ers. For example, if a pointer requires 4 bytes out of a 512-byte block, then
around 0.78% of the disk is being used for pointers, instead of for information.
3. The most severe problem of linked allocation is reliability. A bug in the oper-
ating system or a disk hardware failure may result in picking up the wrong
pointer, and this error could in turn result in linking to another file.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 171

A variation on the linked allocation is the use of a file-allocation table (FAT),


shown in Figure 10.10. MS-DOS and OS/2 operating systems use this method
of disk-space allocation. In FAT, a section of the disk at the beginning of each
volume is set to one side to contain the table. This table contains one entry for
each disk block and is indexed by the block number. The FAT can be used simi-
larly as a linked list. In this the directory entry contains the block number of
the first block of the file and the table entry which is indexed by block number
contains the block number of the next block in the file. This chain continues
until the last block of the table (i.e., end-of-file) is reached. Here all the unused
blocks are indicated by a 0 table value. To allocate a new block to a file, the first
0-valued table entry has to be found and then the previous end-of-file value is
replaced with the address of the new block. Hence, the 0 is then replaced with
the end-of-file value.

FIGURE 10.10. File Allocation Table

Indexed Allocation
We know that linked allocation is not efficient for direct access because
the pointers to the blocks are scattered with the blocks all over the disk and
they must be retrieved in order. Indexed allocation method is used to solve
this problem by bringing all the pointers together into one location called the
index block.
Each file contains its own index block, which is an array of disk-block
addresses. The jth entry in the index block points to the jth block of the file. As
shown in Figure 10.11, the directory contains the address of the index block.
To find and read the jth block, the pointer is used in the jth index-block entry.
At the time of file creation, all pointers in the index block are set to nil.
When the jth block is first written, a block is obtained from the free-space
manager, and its address is put in the jth index-block entry.
Indexed allocation supports direct access of files without suffering from
external fragmentation, because any free block on the disk may satisfy a request
for more space.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
172 • Operating Systems

FIGURE 10.11. Indexed allocation of disk space

Self-Quiz
Q11. Explain contiguous allocation of files.
Q12. Why is linked allocation better than contiguous allocation? Explain.
Q13. How is indexed allocation used for allocating files on disk?

FREE-SPACE MANAGEMENT
As we know that disk space is limited, it is necessary to reuse the space from
deleted files for new files. A free-space list is maintained to keep track of free
disk space. This list records all free disk blocks, that is, those blocks that are not
allocated to some file or directory. When creating a file, the free-space list is
searched for the required amount of space and allocates that space to the new
file. This allocated space is then removed from the free-space list and when a
file is deleted, its disk space is added to the free-space list.
Bit Vector
The free-space list is commonly implemented as a bit map or bit vector. In
this method each block is represented by 1 bit. If the block is free, the bit is 1,
and if the block is allocated, the bit is 0. For example, when considering a disk
where blocks 1, 3, 4, 5, 7, 9, 10, 11, 12, 13, 16, 18, 25, 26, and 28 are free and
the rest of the blocks are allocated, the free-space bit map would be
010111010111110010100000011010000 . . .
This method is simple and efficient in finding the first free block or n
consecutive free blocks on the disk. In this technique, the first non-0 word,
that is, 1 bit, is scanned, which represents the location of the first free block.
The block number can be calculated by:
(Number of bits per word) x (number of 0-value words)+offset of first 1 bit.
The bit vectors are inefficient unless the entire vector is kept in main memory.
For small disks it is possible to keep the entire vector in the main memory,
but the same is not necessarily possible for large disks.
Linked List
Free-space management can also be done through a linked list, where all the
free disk blocks are linked together, by keeping a pointer to the first free block in

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 173

a special location on the disk and caching it in memory. This first block contains a
pointer to the next free disk block, and so on. For example (considering the previous
section example), a pointer is kept to block 1 as the first free block. Block 2 would
contain a pointer to block 3, which would point to block 4, which would point
to block 5, which would point to block 7, and so on. This scheme is not efficient
because to traverse the list, each block must be read, which requires considerable
I/O time. Although traversing a free list is not a frequent action. Generally, the
first free block is required by the operating system so that it can allocate that block
to a file. The FAT (File Allocation Table) method includes free-block accounting
in the allocation data structure. No other separate method is required.

Grouping
In this approach of free-space management, the addresses of n free blocks
are stored in the first free block. Among these blocks, the first n-1 blocks are
actually free. The last block contains the addresses of other n free blocks, and
so on. As compared to the liked list approach, in this method the addresses of
a large number of free blocks can be found quickly.

Counting
It is known that several contiguous blocks may be allocated or freed simulta-
neously, when the space is allocated with the contiguous-allocation algorithm or
through clustering. Therefore, rather than keeping a list of n free blocks, one should
keep the address of the first free block and the number n of free contiguous blocks
that follow the first block. Now each entry in the free-space list consists of a disk
address and a count. Even though each entry requires more space than a simple disk
address, the overall list will be shorter, as long as the count is generally greater than 1.

FIGURE 10.12. Linked free-space list on disk

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
174 • Operating Systems

PROTECTION

It is necessary to keep the information (that is stored in a computer system)


safe from physical damage (called reliability) and improper access (called
protection). Reliability can be generally provided by making duplicate copies
of the files. Protection can be provided in many ways. For a single user system,
protection can be provided by physically removing the floppy disks. However,
in a multiuser system, other mechanisms are needed.

Types of Access
Because the files are being accessed by other users, protection is required.
The systems that do not access the files of other users do not require protec-
tion. Therefore, the complete protection of a system can be provided by
prohibiting access. An alternative method is to provide free access with no
protection. Since both the approaches are not feasible, controlled access is
required.
Controlled access can be provided to the protection mechanism by limiting
the types of file access. Access can be granted or denied depending on some
factors. The type of access requested is one of them. Different types of opera-
tions that may be controlled are:
QQ Read: Read from the file.
QQ Write: Write or rewrite the file.
QQ Execute: Load the file into memory and execute it.
QQ Append: Write new information at the end of the existing file.
QQ Delete: Delete the file and free its space. This space can be reused.
QQ List: List the name and attributes of the file.
Other operations, like renaming, copying, and editing the file, may
also be controlled. For most of the systems, these higher-level functions
may be implemented by a system program that makes lower-level system
calls, and the protection is provided at the lower level only. For example,
to copy a file may simply be implemented by a sequence of read requests.
Here, the user with read access can also cause the file to be copied,
printed, and so on.
Many of the protection mechanisms have been proposed, and they all are
appropriate for their intended application and each of them has their own
advantages and disadvantages.

Access Control
For protection the most common approach is to make access totally
dependent on the identity of the user. Different types of files or directories can
be accessed by different users. To implement the identity-dependent access,
each file and directory is associated with an Access Control List (ACL). ACL
specifies the user names and the types of the access allowed for each user.
When a user requests access to a particular file, the operating system then

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 175

checks the access control list associated with that file. If that user’s identity is
available in the list for the requested access, the access is allowed. Otherwise,
a protection violation occurs.
The advantage of this approach is that it enables complex access method-
ologies. The length of the access list is a big problem. If everyone is allowed to
read a file, then all the users must be listed with read access. This technique
has two adverse effects:
QQ To construct such a list is a tedious and difficult task, especially when the
list of users in the system is not known in advance.
QQ The fixed-size directory entry now needs to be of variable size. This results
in more complicated space management.
These problems can be solved by using the condensed version of the access
list. To condense the length of the ACL, most of the systems provide three
classifications of users (in connection with each file):
QQ Owner: Owner is the user who created the file.
QQ Group: A group is a set of users who are sharing the file and need similar
access.
QQ Universe: All the other users in the system constitute the universe.
The most common and easy-to-implement approach is to combine access control
lists with the more general owner, group, and universe access control scheme.

Other Protection Approaches


Another approach for protection is to associate a password with each file.
As the password is required to access the computer system, similarly, access
to each file can also be controlled by the password. If the random passwords
are chosen and are changed regularly, then this scheme may be effective in
limiting the access to a file. The use of passwords also has some disadvan-
tages:
1. The number of passwords that a user desires to remember may become large,
making this scheme impractical.
2. If only one password is used for all the files, then once it is exposed, all files
are accessible.
To deal with this problem some of the systems (for example, TOPS-20)
allow a user to associate a password with a subdirectory, rather than with an
individual file. The IBM’s VM/CMS (International Business Machines Virtual
Machine/Cambridge Monitor System) operating system allows three pass-
words for a minidisk, one each for read, write, and multi-write access.

Self-Quiz

Q14. What is file protection? Explain.


Q15. How can the file be protected?

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
176 • Operating Systems

CASE STUDIES
FAT32 File System
In this section we will discuss the FAT32 file system that is included with
Microsoft Windows XP. An updated version of the FAT file system, that is, FAT
32, is included in Windows XP. The FAT32 file system allows a default cluster
size of 4 KB, and includes support for EIDE (Enhanced Integrated Drive
Electronics) hard disk sizes larger than 2 gigabytes (GB).

FAT32 Features
FAT32 has the following features:
QQ FAT32 supports drives of up to 2 terabytes in size.
QQ Space utilization of FAT32 is more efficient. FAT32 uses smaller clusters
(i.e., 4 KB clusters for drives of up to 8 GB in size), so 10–15% more disk
space can be used efficiently.
QQ FAT32 is robust. In this the root folder can be relocated and the backup
copy of the file allocation table is used instead of the default copy. The
boot record on FAT32 drives is expanded to include a backup copy of criti-
cal data structures. For that reason, FAT32 drives are less vulnerable to a
single point of failure.
QQ FAT32 is flexible. The root folder on a FAT32 drive is an ordinary cluster
chain; therefore, it can be located anywhere on the drive.

FAT32 Compatibility Considerations


To maintain the compatibility with existing programs, networks, and device
drivers, FAT32 was implemented with a slight change to the existing Windows
architecture, internal data structures, Application Programming Interfaces
(APIs), on-disk format, and so on. To store the cluster values, only 4 bytes are
now used. Hence, many internal and on-disk data structures and published
APIs have been revised or expanded. In some of the cases, the existing
APIs will not work on FAT32 drives. Most programs will remain unaffected
by these changes.
MS-DOS blocks device drivers (for example, Aspidisk.sys), and disk tools
will need to be revised to support FAT32 drives.
All of Microsoft’s disk tools have been revised to work with FAT32. Also,
Microsoft is working with leading device driver and disk tool manufacturers to
support them in revising their products to support FAT32.
Note: A FAT32 volume cannot be compressed by using Microsoft DriveS-
pace or DriveSpace 3.

Dual-Boot Computers
FAT 32 volumes can be accessed by Windows XP, Windows 2000, Windows
Me, Windows 98, and Windows 95 OSR2. The original version of Windows 95,

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 177

i.e., MS DOS, and Windows NT 4.0 do not recognize FAT32 partitions, and
hence are unable to boot from a FAT32 volume.
Note: FAT32 volumes cannot be accessed properly if the computer is
started by using another operating system (for example, a Windows 95 or
MS-DOS boot disk).

Creating FAT32 Volumes


In Windows XP the Disk Management snap-in is a tool for managing hard
disks and the volumes or partitions that they contain. Disk management can
be used to create new FAT32 volumes or format an existing volume to use
the FAT32 filesystem. FAT32 can also be used by formatting the basic and
dynamic volumes.
Creating a FAT32 Partition or Logical Drive: To create a new
FAT32 partition or logical drive in Windows XP, the following steps are to
be taken:

QQ Log on as an Administrator or as a member of the Administrators group.


QQ Now click Start, then right-click My Computer, and then click Manage.
QQ In the console tree, click Disk Management.
QQ In the Disk Management window, perform one of the following:
•• For creating a new partition, right-click unallocated space on the
basic disk where you want to create the partition, and then click New
Partition. OR
•• For creating a new logical drive in an extended partition, right-click
free space on an extended partition where you want to create the logi-
cal drive, and then click New Logical Drive.
QQ Now click Next in the New Partition Wizard.
QQ Click on the type of partition that you want to create (i.e., Primary parti-
tion, Extended partition, or Logical drive), and then click Next.
QQ The size of the partition is to be specified in the Partition size in MB box,
and then click Next.
QQ Now assign a drive letter or drive path to the new partition or logical drive,
and then click Next.
QQ Click Format this partition with the following settings, and then per-
form the following actions:
•• In the File system box, click FAT32.
•• In the Volume label box, type a name for the volume.
The disk allocation unit size can also be changed if required, or a
quick format can be performed if specified.
QQ Click Next.
QQ Make it confirm that the selected options are correct, and then click
Finish.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
178 • Operating Systems

In the appropriate basic disk in the Disk Management window, the new
partition or logical drive is created and appears.
Formatting an Existing Volume to Use FAT32: The following steps are
taken to format a volume:
QQ Click Start, then right-click My Computer, and then click Manage.
QQ Click Disk Management in the console tree.
QQ Right-click the volume in the Disk Management window that is to be for-
matted (or reformatted), and then click Format.
QQ Do the following in the Format dialog box:
•• In the File system box, click FAT32.
•• In the Volume label box, type a name for the volume.
The disk allocation unit size can also be changed if required, or a
quick format can be performed if specified.
QQ Click OK.
QQ When you are prompted to format the volume, click OK.
QQ Now the format process starts.

New Technology File System (NTFS)


NTFS was designed by Microsoft as a successor to FAT32. It was intro-
duced with the Windows NT operating system, and therefore it is named
NTFS. This file system supports 64-bit volume sizes. Disk space is allocated in
extents on top of clusters. The cluster size is a multiple of the disk’s block size,
usually n × 512 bytes. For today’s disk the common cluster size in NTFS is
4KB. NTFS provides journaling for reliability and file-based data compression
to reduce the space that a file uses on the disk.
The Master File Table (MFT) used in NTFS is similar to an inode. For all
files in the system, the MFT contains file records (Microsoft uses this term
for an inode). These records are organized in a B-Tree structure in place of
a linear array, which is used by most Unix-derived file systems (UFS, FFS,
ext2, ext3, etc.). In Unix-derived file systems, the inode area is a bunch of
pre-allocated clusters that are separate from file data, whereas the MFT
(used in Windows NT) itself is a file and can grow like any other file. It is
a special file known as a metafile. It can be allocated and managed like
any other file.
Other metafiles (special files) that can be handled just like other files are:
QQ Log file
QQ Volume file
QQ Attribute definition file
QQ Bitmap file (free blocks)
QQ Boot file (located in low-numbered consecutive blocks so the boot loader
can read them)
QQ Bad cluster file
QQ Root directory

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
File Systems • 179

The volume bitmap is a file which identifies free space within the volume.
This file can grow dynamically; hence, it allows the size of a volume to be
expanded dynamically.
Each entry of a file record in MFT contains a set of typed attributes, which
include the file name, creation date, and permissions. Some attributes may
have multiple instances. For example, a file name may also have an alternate
MS-DOS-compatible 8.3-character name.
Usually a file requires only one record. If more records are needed, then
the first record for the file will store the location of other file records. Hence
an “attribute list” attribute is added, which identifies the location of all other
attribute records for the file.
Unlike the other file system, small files and directories (1,500 bytes or
smaller) are completely contained within the file’s MFT record. As a result, no
additional data blocks are required to allocate these small files and directories.
Also, no additional data blocks are required to access the contents of the files.
If a file is very large, then no data will be stored within the file record. In
such cases, the file record data attribute contains pointers to the file data. This
collection of pointers is a list of extents. Here each pointer contains:
1. Starting cluster number in the file (cluster offset in the file).
2. Cluster number on the disk where this extent is located.
3. Cluster count: number of clusters in this extent.
As a file grows, additional extents are allocated if the last extent cannot be
expanded. If the file record (MFT entry) runs out of space, another MFT entry
is allocated.
Under NTFS a file may have multiple data streams associated with it. A
stream gets selected when the file is opened with a specific stream name (e.g.,
“filename:streamname”). Generally, this is not specified and the default, that
is, the main stream, is used. Additional streams are usually lost when the file is
copied to other file system types, such as FAT32 on a USB flash drive.
An NTFS directory is a file that contains file names and a reference to the
file. The reference identifies the file’s location in the MFT table. If the direc-
tory is small, it is stored in the MFT entry. But if it is large, then it is stored in
other clusters, because the files are too large to fit into an MFT entry.
To make searching possible through large directories, the directory file is
organized as a sorted B+ tree. In this case the MFT entry contains the top
node of the B+ tree and identifies the extents and offsets where the directory
entries live. Directories also contain data such as file size, last access time, and
last modification time. These data are redundant since they are present in the
MFT entry. This is done to optimize some directory listings. Presenting the
user with these common attributes does not require reading the file record for
each file in the directory.
NTFS allows the files to be compressed. Compression of either individual
files, files within a directory, or all files within the volume may be selected.
Compression is handled within the file system. It allows higher levels of the

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.
180 • Operating Systems

operating system to be unaware that it is happening. When a file is being


read, NTFS reads that portion into memory and decompresses it. Files are
compressed in 64 KB chunks. If the compression results in a size of 60 KB or
less, then the file system has saved at least one 4 KB cluster.

EXT2/EXT3/EXT4 File Systems of Linux


Ext2, Ext3, and Ext4 are the file systems that are created for Linux. In this
section we will discuss the following:
QQ High level differences between these file systems.
QQ How to create these file systems.
QQ How to convert one file system type to another type.
Ext2
Ext2 or Second Extended File System was introduced in 1993 and was
developed by Remy Card. Ext2 does not include a journaling feature. Ext2 is
recommended on flash drives and USB drives, as it does not need to do the
overhead of journaling. The maximum individual file size can be from 16 GB
to 2 TB. The overall ext2 file system size can be from 2 TB to 32 TB.
Ext3
Ext3 or Third Extended File System was introduced in 2001 and was
developed by Stephen Tweedie. Starting from Linux Kernel 2.4.15, ext3 was
available. The advantage of ext3 is that it allows journaling. In the file system,
journaling has a dedicated area where all the changes are tracked. Journaling
lowers the possibility of file system corruption when the system crashes. The
maximum individual file size can be from 16 GB to 2 TB, and the overall ext3
file system size can be from 2 TB to 32 TB. The three types of journaling avail-
able in the ext3 file system are:
QQ Journal: Content and metadata are saved in the journal.
QQ Ordered: Only the metadata is saved in the journal. By default, metadata
are journaled only after writing the content to disk.
QQ Writeback: Only the metadata is saved in the journal. Metadata might be
journaled either before or after writing the content to the disk.
The ext2 file system can be directly converted to the ext3 file system
(without backup/restore).
Ext4
Ext4 or Fourth Extended File System was introduced in 2008. Ext4 was
available starting from Linux Kernel 2.6.19. It supports a huge individual
file size and overall file system size. The maximum individual file size can be
from 16 GB to 16 TB and the overall maximum Ext4 file system size is 1 EB
(Exabyte). Where 1 EB = 1024 PB (petabyte) and 1 PB = 1024 TB (terabyte).
The Ext4 directory can contain a maximum of 64,000 subdirectories,
whereas the Ext3 directory can contain maximum of 32,000. An existing Ext3
file system can be mounted as an Ext4 file system, without upgrading it.

EBSCOhost: eBook Collection (EBSCOhost) printed on 11/21/2024 8:29:26 AM UTC via OPEN UNIVERSITY OF TANZANIA. All use subject to
https://www.ebsco.com/terms-of-use.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy