GargR - Vermag. 2017 OperatingSystemsAnInt
GargR - Vermag. 2017 OperatingSystemsAnInt
This is a very complex model and requires lots of wait() and signal() opera-
tions of semaphores.
Self-Quiz
SUMMARY
POINTS TO REMEMBER
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
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
INTRODUCTION
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.
DEADLOCK CHARACTERIZATION
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
Self-Quiz
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
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.
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
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?
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
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
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
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
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:
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.
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
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.
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.
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
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.
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?
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:
Resource Preemption
When preempting resources to relieve deadlock, there are three important
issues to be taken care of.
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
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
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
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.
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 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
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.
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.
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
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.
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
Self-Quiz
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
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
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.
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
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:
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.
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.
P2 517 KB 600 KB
P3 212 KB 300 KB
P4 526 KB 700 KB
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
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.
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
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
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
Self-Quiz
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.
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.
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
Advantages of 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.
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
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.
126 • Operating Systems
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
Self-Quiz
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.
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
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
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
INTRODUCTION
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
Self-Quiz
DEMAND 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.
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
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:
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
Self-Quiz
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
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
The total number of page faults and page replacements in the given
example are as follows:
Page faults = 9
Page replacements = 6
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
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.
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
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.
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.
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.
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?
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
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.
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
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.
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
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:
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
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.
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
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
Self-Quiz
SUMMARY
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
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
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
INTRODUCTION
CONCEPT OF 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.
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
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.
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
Self-Quiz
ACCESS METHODS
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.
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
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
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
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.
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.
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
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.
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
Self-Quiz
FILE-SYSTEM STRUCTURE
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
file-organization module
I/O control
devices
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.
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.
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
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
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.
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
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.
Self-Quiz
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.
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).
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.
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
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.