OS Module - 4
OS Module - 4
CPU
RV Institute of Technology & Management®
Module IV
4.1Memory Management
Memory management is concerned with managing the primary memory. Memory consists of
array of bytes or words each with their own address. The instructions are fetched from the
memory by the CPU based on the value program counter.
Program must be
→ brought (from disk) into memory and
→ placed within a process for it to be run.
Main-memory and registers are only storage CPU can access directly.
Register access in one CPU clock.
Main-memory can take many cycles.
Cache sits between main-memory and CPU registers.
Protection of memory required to ensure correct operation.
A pair of base- and limit-registers define the logical (virtual) address space
(Fig. 3.9 & 3.1
process terminates it returns back the memory space. During execution the process will
go through different steps and in each step the addresses represented in different ways. In source
program the address is symbolic. The compiler converts the symbolic address to re-locatable
address. The loader will convert this re-locatable address to absolute address (Fig. 3.11).
Binding of instructions and data can be done at any step along the way:
Compile time: -If we know whether the process resides in memory then absolute code
can be generated. If the static address changes, then it is necessary to re-compile the code from
the beginning.
Load time: -If the compiler doesn‘t know whether the process resides in memory then it
generates the re-locatable code. In this the binding is delayed until the load time.
Execution time: -If the process is moved during its execution from one memory segment
to another then the binding is delayed until run time. Special hardware is used for this. Most of
the general-purpose operating system uses this method.
The address generated by the CPU is called logical address or virtual address. The
address seen by the memory unit i.e., the one loaded in to the memory register is called the
physical address. Compile time and load time address binding methods generate some logical
and physical address. The execution time addressing binding generate different logical and
physical address. Set of logical address space generated by the programs is the logical address
space.
The base register is also called re-location register. Value of the re-location register is
added to every address generated by the user process at the time it is sent to memory.
The above Fig. 3.12 shows that dynamic re-location which implies mapping from virtual
addresses space to physical address space and is performed by the hardware at run time. Re-
location is performed by the hardware and is invisible to the user dynamic relocation makes it
possible to move a partially executed process from one area of memory to another without
affecting.
For a process to be executed it should be loaded in to the physical memory. The size of
the process is limited to the size of the physical memory. Dynamic loading is used to obtain
better memory utilization. In dynamic loading the routine or procedure will not be loaded until it
is called. Whenever a routine is called, the calling routine first checks whether the called routine
is already loaded or not. If it is not loaded it cause the loader to load the desired program in to
thememory and updates the programs address table to indicate the change and control is passed
to newly called routine.
Advantage: Gives better memory utilization. Unused routine is never loaded. Do not need
special operating system support. This method is useful when large amount of codes are needed
to handle infrequently occurring cases.
Some operating system supports only the static linking. In dynamic linking only, the
main program is loaded in to the memory. If the main program requests a procedure, the
procedure is loaded and the link is established at the time of references. This linking is postponed
until the execution time. With dynamic linking a ―stub‖ is used in the image of each library
referenced routine.
A ―stub‖ is a piece of code which is used to indicate how to locate the appropriate
memory resident library routine or how to load library if the routine is not already present.
When―stub‖ is executed, it checks whether the routine is present is memory or not. If not, it
loads the routine in to the memory. This feature can be used to update libraries i.e., library is
replaced by anew version and all the programs can make use of this library.
More than one version of the library can be loaded in memory at a time and each program
uses its version of the library. Only the programs that are compiled with the newversion are
affected by the changes incorporated in it. Other programs linked before new versionis installed
will continue using older libraries this type of system is called ―shared library‖.
4.2Swapping
Eg:- In a multi-programming environment with a round robin CPU scheduling whenever the time
quantum expires then the process that has just finished is swapped out and a new process swaps
in to the memory for execution.
Normally the process which is swapped out will be swapped back to the same memory
space that is occupied previously. This depends upon address binding.
If the binding is done at load time, then the process is moved to same memory location. If
the binding is done at run time, then the process is moved to different memory location. This is
because the physical address is computed during run time.
Swapping requires backing store and it should be large enough to accommodate the
copies of all memory images (Fig. 3.13). The system maintains a ready queue consisting of all
the processes whose memory images are on the backing store or in memory that are ready to run.
Swapping is constant by other factors: To swap a process, it should be completely idle. A
process may be waiting for an i/o operation. If the i/o is asynchronously accessing the user
memory for i/o buffers, then the process cannot be swapped.
One of the simplest methods for memory allocation is to divide memory in to several
fixed partition. Each partition contains exactly one process. The degree of multi-programming
depends on the number of partitions. In multiple partition method, when a partition is free,
process is selected from the input queue and is loaded in to free partition of memory. When
process terminates, the memory partition becomes available for another process. Batch OS uses
the fixed size partition scheme.
The OS keeps a table indicating which part of the memory is free and is occupied. When
the process enters the system, it will be loaded in to the input queue. The OS keeps track of the
memory requirement of each process and the amount of memory available and determines which
process to allocate the memory.
When a process requests, the OS searches for large hole for this process, hole is a large
block of free memory available. If the hole is too large it is split in to two. One part is allocated
to the requesting process and other is returned to the set of holes. The set of holes are searched to
determine which hole is best to allocate. There are three strategies to select a free hole:
First bit:-Allocates first hole that is big enough. This algorithm scans memory from the
beginning and selects the first available block that is large enough to hold the process.
Best bit:-It chooses the hole i.e., closest in size to the request. It allocates the smallest
hole i.e., big enough to hold the process.
Worst fit:-It allocates the largest hole to the process request. It searches for the largest
hole in the entire list.
First fit and best fit are the most popular algorithms for dynamic memory allocation. First
fit is generally faster. Best fit searches for the entire list to find the smallest hole i.e., large
enough. Worst fit reduces the rate of production of smallest holes. All these algorithms
suffer
from fragmentation.
Memory protection means protecting the OS from user process and protecting
process from one another. Memory protection is provided by using a re-location register, with
a limit register. Re-location register contains the values of smallest physical address and limit
register contains range of logical addresses (Fig. 3.14).
(Re-location = 100040 and limit = 74600). The logical address must be less than the
limit register; the MMU maps the logical address dynamically by adding the value in re-
location register. When the CPU scheduler selects a process for execution, the dispatcher
loads the re-location and limit register with correct values as a part of context switch. Since
every address generated by the CPU is checked against these register we can protect the OS
and other users‘ programs and data from being modified.
4.2.3 Fragmentation
External Fragmentation exists when there is enough memory space exists to satisfy
the request, but it not contiguous i.e., storage is fragmented in to large number of small holes.
4.3 Paging
Paging is a memory management scheme that permits the physical address space of a
process to be non-contiguous. Support for paging is handled by hardware. It is used to avoid
external fragmentation. Paging avoids the considerable problem of fitting the varying sized
memory chunks on to the backing store. When some code or date residing in main memory
Physical memory is broken in to fixed sized blocks called frames (f). Logical
memory is broken in to blocks of same size called pages (p).
When a process is to be executed its pages are loaded in to available frames from
backing store. The blocking store is also divided in to fixed-sized blocks of same size as
memory frames. The following Fig. 3.16 shows paging hardware:
Logical address generated by the CPU is divided in to two parts: page number (p) and
page offset(d). The page number (p) is used as index to the page table. The page table contains
base address of each page in physical memory.
This base address is combined with the page offset to define the physical memory i.e.,
sent to the memory unit. The page size is defined by the hardware.
The size of a power of 2, varying between 512 bytes and 10Mb per page. If the size of
logical address space is 2^m address unit and page size is 2^n, then high order m-n designates the
page number and n low order bits represents page offset.
• The page-table contains the base-address of each page in physical-memory (Fig. 3.17):
• Address generated by CPU is divided into 2 parts:
1) Page-number(p) is used as an index to the page-table and
2) Offset(d) is combined with the base-address to define the physical-
address. This physical-address is sent to the memory-unit.
Fig. 4.9 Free frames (a) before allocation and (b) after allocation
Eg:- To show how to map logical memory in to physical memory (Fig. 3.18) consider a
page size of 4 bytes and physical memory of 32 bytes (8 pages). Logical address 0 is page 0 and
offset 0. Page 0 is in frame 5. The logical address 0 maps to physical address 20. [(5*4) +
0].logical address 3 is page 0 and offset 3 maps to physical address 23 [(5*4) + 3]. Logical
address4 is page 1 and offset 0 and page 1 is mapped to frame 6. So logical address 4 maps to
physical address 24 [(6*4) + 0]. Logical address 13 is page 3 and offset 1 and page 3 is mapped
to frame
2. So logical address 13 maps to physical address 9 [(2*4) + 1].
The hardware implementation of the page table can be done in several ways:
The simplest method is that the page table is implemented as a set of dedicated registers.
These registers must be built with very high-speed logic for making paging address translation.
Every accessed memory must go through paging map. The use of registers for page table is
satisfactory if the page table is small.
If the page table is large then the use of registers is not visible. So, the page table is kept
in the main memory and a page table base register [PTBR] points to the page table. Changing the
page table requires only one register which reduces the context switching type. The problem with
this approach is the time required to access memory location. To access a location [i] first we
have to index the page table using PTBR offset. It gives the frame number which is combined
with the page offset to produce the actual address. Thus, we need two memory accesses for a
byte.
The only solution is to use special, fast, lookup hardware cache called translation look
aside buffer [TLB] or associative register. LB is built with associative register with high speed
memory. Each register contains two paths a key and a value.
values, if found the corresponding value field is return and searching is fast. TLB is used with
the page table as follows: TLB contains only few page table entries. When a logical address is
generated by the CPU, its page number along with the frame number is added to TLB.
If the page number is found its frame memory is used to access the actual memory. If
the page number is not in the TLB (TLB miss) the memory reference to the page table is made.
When the frame number is obtained use can use it to access the memory. If the TLB is full of
entries the OS must select anyone for replacement.
Each time a new page table is selected the TLB must be flushed [erased] to ensure that
next executing process do not use wrong information. The percentage of time that a page
number is found in the TLB is called HIT ratio.
4.3.3 Protection
Memory protection in paged environment is done by protection bits that are associated
with eachframe these bits are kept in page table. x One bit can define a page to be read-write or
read-only. To find the correct frame number every reference to the memory should go through
page table. At the same timephysical address is computed. The protection bits can be checked to
verify that no writers are made to read-only page. Any attempt to write in to read-only page
causes a hardware trap to the OS. This approach can be used to provide protection to read-only,
read- write or execute-only pages. One more bit is generally added to each entry in the page
table: a valid-invalid bit (Fig. 3.20).
A valid bit indicates that associated page is in the processes logical address space and thus it is a
legal or valid page. If the bit is invalid, it indicates the page is not in the processes logical
addressed space and illegal. Illegal addresses are trapped by using the valid-invalid bit. The OS
sets this bit for each page to allow or disallow accesses to that page.
Advantage of
paging:
Disadvantage:
Systems that use inverted page-tables have difficulty implementing shared-memory.
a. Hierarchical paging:
Recent computer system supports a large logical address apace from 2^32 to 2^64. In
thissystem the page table becomes large.
So, it is very difficult to allocate contiguous main memory for page table. One
simplesolution to this problem is to divide page table in to smaller pieces.
One way is to use two-level paging algorithm in which the page table itself is also paged.
Eg:-In a 32 bit machine with page size of 4kb.
A logical address is divided in to a page number consisting of 20 bits anda page offset
of 12 bit.
The page table is further divided since the page table is paged, the page number
isfurther divided in to 10-bit page number and a 10-bit offset.
Consider the system with a 32-bit logical-address space and a page-size of 4 KB.
A logical-address is divided into
→ 12-bit page-offset.
→ 10-bit page-offset.
Each element in the hash table contains the following three fields: Virtual page number x
Mapped page frame value x Pointer to the next element in the linked list.
Working:
Virtual page number is taken from virtual address. Virtual page number is hashed in to
hash table (Fig. 3.24).
Virtual page number is compared with the first element of linked list. Both the values are
matched, that value is (page frame) used for calculating the physical address. If not match then
entire linked list is searched for matching virtual page number. Clustered pages are similar to
hash table but one difference is that each entity in the hash table refer to several pages.
Since the address spaces have grown to 64 bits, the traditional page tables become a
problem. Even with two level page tables. The table can be too large to handle. An inverted page
table has only entry for each page in memory. Each entry consisted of virtual address of the page
stored in that read-only location with information about the process that owns that page.
Each virtual address in the Inverted page table consists of triple <process-id , page number
, offset >. The inverted page table entry is a pair <process-id , page number>. When a memory
reference is made, the part of virtual address i.e., <process-id , page number> is presented in to
memory sub-system.
The inverted page table is searched for a match. If a match is found at entry I then the
physical address <i, offset> is generated. If no match is found then an illegal address access has
been attempted. This scheme is as shown in Fig. 3.25.
4.5 Segmentation
[Link] Normally, the user-program is compiled, and the compiler automatically constructs segments
[Link] reflecting the input program. For ex:
Hardware Support
Segment-table maps 2-dimensional user-defined addresses into one-dimensional physical-addresses.
In the segment-table, each entry has following 2 fields:
Consider we have five segments numbered from 0 through 4. The segments are stored in physical
memory as shown in Fig.. The segment table has a separate entry for each segment, giving start address
in physical memory (or base) and the length of that segment (or limit). For example, segment 2 is
400 bytes long and begins at location 4300. Thus, a reference to byte 53 of segment 2 is mapped
onto location 4300 + 53 = 4353.
Virtual-Memory Management
4.9 Background
Virtual memory is a technique that allows the execution of processes that are not completely in
memory. One major advantage of this scheme is that programs can be larger than physical
memory.
In practice, most real processes do not need all their pages, or at least not all at once, for
several reasons:
1. Error handling code is not needed unless that specific error occurs, some of
which are quite rare.
2. Certain features of certain programs are rarely used.
The ability to load only the portions of processes that are actually needed has several
benefits:
o Programs could be written for a much larger address space (virtual memory space
) than physically exists on the computer.
o Because each process is only using a fraction of their total address space, there is
more memory left for other programs, improving CPU utilization and system
throughput.
o Less I/O is needed for swapping processes in and out of RAM, speeding things
up.
The Fig. 4.21 below shows the general layout of virtual memory, which can be much larger
than physical memory
Fig. 4.21 Diagram showing virtual memory that is larger than physical
memory.
The Fig. below shows virtual address space, which is the programmer‘s logical view
of process memory storage. The actual physical layout is controlled by the process's
page table.
Note that the address space shown in Fig. 4.2 is sparse - A great hole inthe middle
of the address space is never used, unless the stack and/or the heap grow to fill the
hole.
Virtual memory also allows the sharing of files and memory by multiple processes,
with several benefits:
System libraries can be shared by mapping them into the virtual address space of
more than one process.
Processes can also share virtual memory by mapping the same block of memory to
more than one process as shown in Fig. 4.3.
Process pages can be shared during a fork( ) system call, eliminating the need to copy
all of the pages of the original ( parent ) process.
The basic idea behind demand paging is that when a process is swapped in, the
pager only loads into memory those pages that is needed presently.
Pages that are not loaded into memory are marked as invalid in the page table,using
the invalid bit. Pages loaded in memory are marked as valid.
If the process only ever accesses pages that are loaded in memory ( memory resident
pages ), then the process runs exactly as if all the pages were loaded in to memory as
shown in Fig. 4.5.
Fig. 4.25 Page table when some pages are not in main memory.
On the other hand, if a page is needed that was not originally loaded up, then a
page fault trap is generated, which must be handled in a series of steps:
1. The memory address requested is first checked, to make sure it was a
valid memory request.
2. If the reference is to an invalid page, the process is terminated. Otherwise,
if the page is not present in memory, it must be paged in.
3. A free frame is located, possibly from a free-frame list.
In an extreme case, the program starts execution with zero pages in memory. Here
NO pages are swapped in for a process until they are requested by page faults. This
is known as pure demand paging.
The hardware necessary to support demand paging is the same as for paging and
swapping: A page table and secondary memory.
Even if only one access in 1000 causes a page fault, the effective access time drops from 200
nanoseconds to 8.2 microseconds, a slowdown of a factor of 40 times. In order to keep the
slowdown less than 10%, the page fault rate must be less than 0.0000025, or one in 399,990
accesses.
4.3 Copy-on-Write
The idea behind a copy-on-write is that the pages of a parent process is shared by the child
process, until one or the other of the processes changes the page. Only when a process
changes any page content, that page is copied for the child as shown in Fig. 4.7 and 4.8.
Only pages that can be modified need to be labeled as copy-on-write. Code segments can
simply be shared.
Some systems provide an alternative to the fork() system call called a virtual memory fork,
vfork( ). In this case the parent is suspended, and the child uses the parent's memory pages.
This is very fast for process creation, but requires that the child not modifying the address
space of parent.
Fig. 4.27 Before process 1 modifies page C. Fig. 4.28 After process 1 modifies page C.
In order to make the most use of virtual memory, we load several processes into
memory at the same time. Since we only load the pages that are actually needed by each
process at any given time, there are frames to load many more processes in memory.
If some process suddenly decides to use more pages and there aren't anyfree
framesavailable. Then there are several possible solutions to consider:
1. Adjust the memory used by I/O buffering, etc., to free up some frames for
user processes.
2. Put the process requesting more pages into a wait queue until some free
frames become available.
3. Swap some process out of memory completely, freeing up its page frames.
4. Find some page in memory that isn't being used right now, and swap that
page only out to disk, freeing up a frame that can be allocated to the process
requesting it. This is known as page replacement, and is the most common
solution as shown in Fig. 4.9. There are many different algorithms for page
replacement.
The previously discussed page-fault processing assumed that there would be free frames
available on the free-frame list. Now the page-fault handling must be modified to free up a
frame if necessary, as follows:
Note that step 2c adds an extra disk write to the page-fault handling, thus doubling the time
required to process a page fault. This can be reduced by assigning a modify bit, or dirty bit to
each page in memory, indicating whether or not it has been changed since it was last loaded in
from disk. If the page is not modified the bit is not set.
If the dirty bit has not been set, then the page is unchanged, and does not need to be written out
to disk. Many page replacement strategies specifically look for pages that do not have their
dirty bit set.
There are two major requirements to implement a successful demand paging system.
The overall goal in selecting and tuning these algorithms is to generate the
fewest number of overall page faults. Because disk access is so slow relative to memory
access, even slight improvements to these algorithms can yield large improvements in
overall system performance.
Algorithms are evaluated using a given string of page accesses known as a
reference string.
To determine the number of page faults for a particular reference string and page-replacement
algorithm, we also need to know the number of page frames available. Obviously, as the
number of frames available increases, the number of page faults decreases as shown in Fig.
4.11.
Or a FIFO queue can be created to hold all pages in memory. As new pages are
brought in, they are added to the tail of a queue, and the page at the head of the
queue is the next victim as shown in Fig. 4.12.
In the following example, a reference string is given and there are 3 free frames.
There are 20-page requests, which results in 15-page faults.
The same reference string used for the FIFO example is used in the example
below, here the minimum number of possible page faults is 9 as shown in Fig.
4.14.
Unfortunately OPT cannot be implemented in practice, because it requires the
knowledge of future string, but it makes a nice benchmark for the comparison
and evaluation of real proposed new algorithms.
The LRU (Least Recently Used) algorithm, predicts that the page that has not
been used in the longest time is the one that will not be used again in the near
future.
Some view LRU as analogous to OPT, but here we look backwards in
time instead of forwards.
Fig. 4.15 illustrates LRU for our sample string, yielding 12 page faults, ( as
compared to 15 for FIFO and 9 for OPT. )
LRU is considered a good replacement policy, and is often used. There are two
simple approaches commonly used to implement this:
1. Counters. With each page-table entry a time-of-use field is associated. Whenever a
reference to a page is made, the contents of the clock register are copied to the time-of-
use field in the page-table entry for that page. In this way, we always have the "time" of
the last reference to each page. This scheme requires a search of the page table to find
the LRU page and a write to memory for each memory access.
2. Stack. Another approach is to use a stack, and whenever a page is accessed, pull
that page from the middle of the stack and place it on the top. The LRU page will
always be at the bottom of the stack as shown in Fig. 4.16. Because this requires
removing objects from the middle of the stack, a doubly linked list is the
recommended data structure.
Neither LRU or OPT exhibit Belady's anomaly. Both belong to a class of page-
replacement algorithms called stack algorithms, which can never exhibit Belady's
anomaly.
Fig. 4.36 Use of a stack to record the most recent page references.
Many systems offer some degree of hardware support, enough to approximate LRU.
In particular, many systems provide a reference bit for every entry in a page table,
which is set anytime that page is accessed. Initially all bits are set to zero, and they
can also all be cleared at any time. One bit distinguishes pages that have been
accessed since the last clear from those that have not been accessed.
If a page is found with its reference bit as ‗0‘, then that page is selected as
thenext victim.
If the reference bit value is ‗1‘, then the page is given a second chance and its reference
Thus, a page that is given a second chance will not be replaced until all other pages
have been replaced (or given second chances). In addition, if a page is used often, then
it sets its reference bit again.
This algorithm is also known as the clock algorithm. One way to implement the second-
chance algorithm is as a circular queue. A pointer indicates which page is to be replaced next.
When a frame is needed, the pointer advances until it finds a page with a 0-reference bit. As it
advances, it clears the reference bits. Once a victim page is found, the page is replaced, and the
new page is inserted in the circular queue in that position.
This algorithm searches the page table in a circular fashion, looking for the first page it
can find in the lowest numbered category. i.e. it first makes a pass looking for a (0, 0),
and then if it can't find one, it makes another pass looking for a ( 0, 1 ), etc.
The main difference between this algorithm and the previous one is the preference for
replacing clean pages if possible.
e) Counting-Based Page Replacement
There are several algorithms based on counting the number of references that have
been made to a given page, such as:
o Least Frequently Used, LFU: Replace the page with the lowest reference count.
A problem can occur if a page is used frequently initially and then not used any
more, as the reference count remains high. A solution to this problem is to right-
shift the counters periodically, yielding a time-decaying average reference count.
o Most Frequently Used, MFU: Replace the page with the highest reference
count. The logic behind this idea is that pages that have already been referenced
a lot have been in the system a long time, and we are probably done with them,
whereas pages referenced only a few times have only recently been loaded, and
we still need them.
f) Page-Buffering Algorithms
Maintain a certain minimum number of free frames at all times. When a page-
fault occurs, go ahead and allocate one of the free frames from the free list first,
so that the requesting process is in memory as early as possible, and then select a
victim page to write to disk and free up a frame.
Keep a list of modified pages, and when the I/O system is idle, these pages are
written to disk, and then clear the modify bits, thereby increasing the chance of
finding a "clean" page for the next potential victim and page replacement can be
done much faster.
Thus allocates 4 frames and 57 frames to student process and database respectively.
4.6 Thrashing
Thrashing is the state of a process where there is high paging activity. A process that is
spending more time paging than executing is said to be thrashing.
In the graph given below, CPU utilization is plotted against the degree of multiprogramming.
As the degree of multiprogramming increases, CPU utilization also increases, although more
slowly, until a maximum is reached.
If the degree of multiprogramming is increased even further, thrashing sets in, and CPU
utilization drops sharply as shown in Fig. 4.18. At this point, to increase CPU utilization and
stop thrashing, we must decrease the degree of multiprogramming.
The locality model states that, as a process executes, it moves from locality to locality. A locality is a
set of pages that are actively used together (Fig. 4.19). A program is generally composed of several
different localities, which may overlap. For example, when a function is called, it defines a new
locality.
In this locality, memory references are made to the instructions of the function call, its local variables,
and a subset of the global variables. When we exit the function, the process leaves this locality, since
the local variables and instructions of the function are no longer in active use.
The working set model is based on the concept of locality, and defines a working set window,
of length delta. Whatever pages are included in the most recent delta page references are said
to be in the processes working set window, and comprise its current working set, as illustrated
in Fig. 4.20:
The selection of delta is critical to the success of the working set model - If it is too small then
it does not encompass all of the pages of the current locality, and if it is too large, then it
encompasses pages that are no longer being frequently accessed.
The total demand of frames, D, is the sum of the sizes of the working sets for all processes
(D=WSSi). If D exceeds the total number of available frames, then at least one process is
thrashing, because there are not enough frames available to satisfy its minimum working set. If
D is significantly less than the currently available frames, then additional processes can be
launched.
The hard part of the working-set model is keeping track of what pages are in the current
working set, since every reference adds one to the set and removes one older page.
When page- fault rate is too high, the process needs more frames and when it is too low, the
process may have too many frames.
The upper and lower bounds can be established on the page-fault rate. If the actual page- fault
rate exceeds the upper limit, allocate the process another frame or s spend the process. If the
page-fault rate falls below the lower limit, remove a frame from t e process. Thus, we
directly measure and control the page-fault rate to prevent [Link]
The file system consists of two distinct parts: a collection of files, each storing related data, and a
directory structure, which organizes and provides information about all the files in the system.
b) File Operations
The operating system provides system calls to create, write, read, reposition, delete,
and truncate files.
o Creating a file - Two steps are necessary to create a file
Find space in the file system for the file.
Make an entry for the new file in the directory.
o Writing a file - To write a file, the system call consists of both the name of the file
and the information to be written to the file. Given the name of the file, the
system searches the directory to find the file's location. The system must
keep a write pointer to the location in the file where the next write is to
take place. The write pointer must be updated whenever a write occurs.
o Reading a file - To read from a file, the system call that specifies the name of the
file and where the next block of the file should be put. The directory is
searched for the file, and the system needs to keep a read pointer to the
location in the file where the next read is to take place. Once the read has
taken place, the read pointer is updated.
o Repositioning within a file - The directory is searched for the file, and the file
pointer is repositioned to a given value. This file operation is also known
as a file seek.
o Deleting a file – To delete a file, search the directory for the file. Release all file
space, so that it can be reused by other files, and erase the directory entry.
Truncating a file - The user may want to erase the contents of a file but keep its attributes.
Rather than forcing the user to delete the file and then recreate it, this function allows
all attributes to remain unchanged –except for file length. The file size is reset to zero.
Information about currently open files is stored in an open file table. It contains
information‘s like:
o File pointer - records the current position in the file, for the next read or write
access.
o File-open count - How many times has the current file been opened by different
processes, at the same time and not yet closed? When this counter reaches zero
the file can be removed from the table.
o Disk location of the file – The information needed to locate the file on disk is
kept in memory so that the system does not have to read it from disk for each
operation.
o Access rights – The file access permissions are stored on the per-process table so
that the operating system can allow or deny subsequent I/O requests.
Some systems provide support for file locking.
o A shared lock is for reading only.
o An exclusive lock is for writing as well as reading.
o An advisory lock, it is up to the software developers to ensure that locks are acquired
or released.
o A mandatory lock, prevents any other process from accessing the locked file. (A
truly locked door.)
For instance, only a file with a ―. corn", ".exe", or ".bat‖, can be executed.
c) File Structure
The study of different ways of storing files in secondary memory such that they can be
easily accessed.
File types can be used to indicate the internal structure of the file. Certain files must be in
a particular structure that is understood by the operating system.
For example, the operating system requires that an executable file have a specific
structure so that it can determine where in memory to load the file and the location of the
first instruction.
UNIX treats all files as sequences of bytes, with no further consideration of the internal
structure.
Macintosh files have two forks - a resource fork, and a data fork. The resource fork
contains information relating to the UI, such as icons and button images. The data fork
contains the traditional file contents-program code or data.
The file information is accessed and read into computer memory. The information in the file
can be accessed in several ways.
a) Sequential Access
Here information in the file is processed in order, one record after the other.
This mode of access is a common method; for example, editors and compilers
usually access files in this fashion.
A sequential access file emulates magnetic tape operation, and generally supports a
few operations:
o read next - read a record and advance the file pointer to the next position.
o write next - write a record to the end of file and advance the file pointer to
the next position as shown in Fig. 4.23.
o skip n records - May or may not be supported. ‗n‘ may be limited to
positive numbers, or may be limited to +/- 1.
b) Direct Access
A file is made up of fixed-length logical records that allow programs to read and write
records randomly. The records can be rapidly accessed in any order.
Direct access is of great use for immediate access to large amount of information.
Eg: Database file. When a query occurs, the query is computed and only the selected rows are
access directly to provide the desired information.
Operations supported include:
read n - read record number n. (position the cursor to n and then read the record)
write n - write record number n. (position the cursor to n and then write the record)
jump to record n – move to nth record (n- could be 0 or the end of file)
If the record length is L, there is a request for record ‗N‘. Then the direct access
to the starting byte of record ‗N‘ is at L*(N-1)
Eg: if 3rd record is required and length of each record(L) is 50, then the starting
position of 3rd record is L*(N-1)
Address = 50*(3-1) = 100.
These methods generally involve the construction of an index for the file called index file.
The index file is like an index page of a book, which contains key and address. To find a
record in the file, we first search the index and then use the pointer to access the record
directly and find the desired record.
An indexed access scheme can be easily built on top of a direct access system.
For very large files, the index file itself is very large. The solution to this is to create an
index for index file. i.e. multi-level indexing as shown in Fig.
4.24.
a) Single-Level Directory
It is the simplest directory structure.
All files are contained in the same directory, which is easy to support and understand.
b) Two-Level Directory
Each user gets their own directory space - user file directory (UFD)
File names only need to be unique within a given user's directory.
A master file directory (MFD) is used to keep track of each user‘s directory,
and must be maintained when users are added to or removed from the system.
When a user refers to a particular file, only his own UFD is searched.
All the files within each UFD are unique.
To create a file for a user, the operating system searches only that user's UFD to
ascertain whether another file of that name exists.
To delete a file, the operating system confines its search to the local UFD; thus, it
cannot accidentally delete another user's file that has the same name. The user
directories themselves must be created and deleted as necessary.
This structure isolates one user from another. Isolation is an advantage when the
users are completely independent but is a disadvantage when the users want to
cooperate on some task and to access one another's files as shown in Fig. 4.27.
c) Tree-Structured Directories
A tree structure is the most common directory structure.
The tree has a root directory, and every file in the system has a unique path name.
A directory (or subdirectory) contains a set of files or subdirectories.
One bit in each directory entry defines the entry as a file (0) or as a subdirectory (1).
Special system calls are used to create and delete directories.
Path names can be of two types: absolute and relative. An absolute path begins at the
root and follows a down to the specified file, giving the directory names on the path.
A relative path defines a path from the current directory (Fig. 4.28).
For example, in the tree-structured file system of Fig. below if the current directory is
root/spell/mail, then the relative path name is prt/first and the files absolute path name
root/spell/mail/prt/jirst.
Directories are stored the same as any other file in the system, except there is a bit
that identifies them as directories, and they have some special structure that the OS
understands.
One question for consideration is whether or not to allow the removal of directories
that are not empty - Windows requires that directories be emptied first, and UNIX
provides an option for deleting entire sub-trees.
d) Acyclic-Graph Directories
When the same files need to be accessed in more than one place in the directory
structure (e.g. because they are being shared by more than one user), it can be useful
to provide an acyclic-graph structure. (Note the directed arcs from parent to child. )
o UNIX provides two types of links (pointer to another file) for implementing the
acyclic-graph structure.
o A hard link (usually just called a link) involves multiple directory entries that
both refer to the same file. Hard links are only valid for ordinary files in the same
filesystem.
o A symbolic link, that involves a special file, containing information about where
to find the linked file. Symbolic links may be used to link directories and/or files
in other filesystems, as well as ordinary files in the current filesystem as shown in
Fig. 4.29.
Hard links require a reference count, or link count for each file, keeping track of
how many directory entries are currently referring to this file. Whenever one of the
references is removed the link count is reduced, and when it reaches zero, the disk
space can be reclaimed.
For symbolic links there is some question as to what to do with the symbolic
links when the original file is moved or deleted:
o One option is to find all the symbolic links and adjust them also.
o Another is to leave the symbolic links dangling, and discover that they are no
longer valid the next time they are used.
o What if the original file is removed, and replaced with another file having the
same name before the symbolic link is next used?
Another approach to deletion is to preserve the file until all references to it are deleted. To
implement this approach, we must have some mechanism for determining that the last
reference to the file has been deleted.
When a link or a copy of the directory entry is established, a new entry is added to the file-
reference list. When a link or directory entry is deleted, we remove its entry on the list. The
file is deleted when its file-reference list is empty.
If cycles are allowed in the graphs, then several problems can arise:
o Search algorithms can go into infinite loops. One solution is to not follow links in
search algorithms. (Or not to follow symbolic links, and to only allow symbolic links
to refer to directories)
o Sub-trees can become disconnected from the rest of the tree and still not havetheir
reference counts reduced to zero. Periodic garbage collection is required to detect and
resolve this problem. (chkdsk in DOS and fsck in UNIX search for these problems,
among others, even though cycles are not supposed to be allowed in either system.
Disconnected disk blocks that are not marked as free are added back to the file
systems
The basic idea behind mounting file systems is to combine multiple file systems into one large
tree structure.
The mount command is given a filesystem to mount and a mount point (directory) on which to
attach it.
Once a file system is mounted onto a mount point, any further references to that directory
actually refer to the root of the mounted file system.
Any files (or sub-directories) that had been stored in the mount point directory prior to
mounting the new filesystem are now hidden by the mounted filesystem, and are no longer
available. For this reason, some systems only allow mounting onto empty directories (Fig.
4.31 a).
Filesystems can only be mounted by root, unless root has previously conFig.d certain
filesystems to be mountable onto certain pre-determined mount points. (E.g. root may allow
users to mount floppy filesystems to /mnt or something like it) Anyone can run the mount
command to see what filesystems are currently mounted (Fig. 4.31 b).
The traditional Windows OS runs an extended two-tier directory structure, where the first tier
of the structure separates volumes by drive letters, and a tree structure is implemented below
that level.
Macintosh runs a similar system, where each new volume that is found is automatically mounted
and added to the desktop when it is found. More recent Windows systems allow filesystems to be
mounted to any directory in the filesystem, much like UNIX.
Fig. 4.32 shows the effects of mounting the volume residing on /device/dsk over /users. If the volume is
unmounted, the file system is restored to the situation depicted in Fig. 4.31.
Fig. 4.51 File system, (a) Existing system, (b) Unmounted volume. Fig. 4.52 Mount point
The advent of the Internet introduces issues for accessing files stored on remote
computers The original method was ftp, allowing individual files to be transported across
systems as needed. Ftp can be either account and password controlled, or anonymous,
not requiring any user name or password.
Various forms of distributed file systems allow remote file systems to be mounted onto
a local directory structure, and accessed using normal file access commands. (The actual
files are still transported across the network as needed, possibly using ftp as the
underlying transport mechanism.)
The WWW has made it easy once again to access files on remote systems without
mounting their filesystems, generally using (anonymous) ftp as the underlying file
transport
mechanism.
When one computer system remotely mounts a filesystem that is physically located
on another system, the system which physically owns the files acts as a server, and
the system which mounts them is the client.
User IDs and group IDs must be consistent across both systems for the system to
work properly. (I.e. this is most applicable across multiple computers managed by
the same organization, shared by a common group of users. )
The same computer can be both a client and a server. (E.g. cross-linked file systems.
)
There are a number of security concerns involved in this model:
o Servers commonly restrict mount permission to certain trusted systems only.
Spoofing (a computer pretending to be a different computer) is a potential
security risk.
o Servers may restrict remote access to read-only.
o Servers restrict which filesystems may be remotely mounted. Generally, the
information within those subsystems is limited, relatively public, and
protected by frequent backups.
The Domain Name System, DNS, provides for a unique naming system across
all of the Internet.
Domain names are maintained by the Network Information System, NIS, which
unfortunately has several security issues. NIS+ is a more secure version, but has
not yet gained the same widespread acceptance as NIS.
Microsoft's Common Internet File System, CIFS, establishes a network login
for each user on a networked system with shared file access. Older
Windowssystems used domains, and newer systems (XP, 2000), use active
directories. User names must match across the network for this system to be
valid.
A newer approach is the Lightweight Directory-Access Protocol, LDAP, which
provides a secure single sign-on for all users to access all resources on a
network. This is a secure system which is gaining in popularity, and which has
the maintenance advantage of combining authorization information in one central
location.
c) Failure Modes
When a local disk file is unavailable, the result is generally known immediately,
and is generally non-recoverable. The only reasonable response is for the response
to fail.
However, when a remote file is unavailable, there are many possible reasons, and
whether or not it is unrecoverable is not readily apparent. Hence most remote
access
IV SEMESTER OPERATING SYSTEM (18CS43) 26
RV Institute of Technology & Management®
systems allow for blocking or delayed response, in the hopes that the remote
system (or the network) will come back up eventually.
Consistency Semantics deals with the consistency between the views of shared files
on a networked system. When one user changes the file, when do other users see the
changes?
The series of accesses between the open () and close () operations of a file is called the
file session.
a) UNIX Semantics
b) Session Semantics
c) Immutable-Shared-Files Semantics
Under this system, when a file is declared as shared by its creator, then the name
cannot be re-used by any other process and it cannot be modified.
4.11 Protection
The information in a computer system must be stored safely without any physical damage (the
issue of reliability) and improper access (the issue of protection).
Reliabilityis generally provided by duplicate copies of files. Many computers have systems
programs that automatically copy disk files to tape at regular intervals (once per day or week or
month). The damage could be due to hardware problems (such as errors in reading or writing),
power surges or failures, head crashes, dirt, temperature extremes, and vandalism.
Systems that do not permit access to the files of other users do not need protection. Thus, we
could provide complete protection by prohibiting access. Alternatively, we could provide free
access with no protection. Several different types of operations may be controlled:
Read- Read from the file.
Write- Write or rewrite the file.
Execute - Load the file into memory and execute it.
Append- Write new information at the end of the file.
Delete - Delete the file and free its space for possible
reuse. List - List the name and attributes of the file.
To make access to files depending on the identity of the user. Different users may need different
types of access to a file or directory. The most general scheme to implement dependent access is
to associate with each file and directory an access-control list (ACL) specifying user names and
the types of access allowed for each user. When a user requests access to a particular file, the
operating system checks the access list associated with that file. If that user is listed for the
requested access, the access is allowed. Otherwise, a protection violation occurs, and the user job
is denied access to the file.
If we want to allow everyone to read a file, we must list all users with read access. This
technique has two undesirable consequences:
Constructing such a list may be a tedious, if we do not know in advance the list of users in the
system.
The directory entry, must be of variable size, as the list grows, resulting in more complicated
space management.
These problems can be resolved by use of a condensed version of the access list. To condense the
length of the access-control list, many systems recognize three classifications of users in
connection with each file:
Owner- The user who created the file is the owner.
Group - A set of users who are sharing the file and need similar access is a group, or
work group.
Universe- All other users in the system constitute the universe.
The most common recent approach is to combine access-control lists with owner, group, and
universe access control scheme.
Some system allows users to associate a password to a subdirectory, rather than only to file.
To improve I/O efficiency, I/O transfers between memory and disk are performed in units
of blocks. Block sizes may range from 512 bytes to 4K or larger. (Rather than transferring
a byte at a time,)
A file system poses two quite different design problems.
The first problem is defining how the file system should look to the user. This
task involves defining a file and its attributes, the operations allowed on a file, and
the directory structure for organizing files.
The second problem is creating algorithms and data structures to map the logical
file system onto the physical secondary-storage devices.
File systems organize storage on disk drives, and can be viewed as a layered design:
At the lowest layer are the physical devices, consisting of the magnetic media, motors &
controls, and the electronics connected to them and controlling them. Modern disk put more
and more of the electronic controls directly on the disk drive itself, leaving relatively little
work for the disk controller card to perform.
Lowest level, I/O Control consists of device drivers, which communicate with the devices by
reading and writing special codes directly to and from memory addresses corresponding to the
controller card's registers. Each controller card (device ) on a system has a different set of
addresses ( registers, ports ) that it listens to, and a unique set of command codes and results
codes that it understands.
The basic file system level works directly with the device drivers in terms of retrieving and
storing raw blocks of data, without any consideration for what is in each block.
The file organization module knows about files and their logical blocks, and how they map to
physical blocks on the disk. In addition to translating from logical to physical blocks, the file
organization module also maintains the list of free blocks, and allocates free blocks to files as
needed.
The logical file system deals with all of the meta data associated with a file (UID, GID, mode,
dates, etc), i.e. everything about the file except the data itself. This level manages the
directory
structure and the mapping of file names to file control blocks, FCBs, which contain all of the
meta data as well as block number information for finding the data on the disk.
The layered approach to file systems means that much of the code can be used
uniformly for a wide variety of different file systems, and only certain layers need to be
filesystem specific.
On disk, the file system may contain information about how to boot an operating system stored
there, the total number of blocks, the number and location of free blocks, the directory structure,
and individual files.
A boot-control block, (per volume) can contain information needed by the system to boot an
operating system from that volume. If the disk does not contain an operating system, thisblock
can be empty. It is typically the first block of a volume In UFS, it is called the boot block; in
NTFS, it is the partition boot sector.
A volume control block, (per volume) contains volume (or partition) details, such as the
number of blocks in the partition, size of the blocks, free block count and free-block pointers,
and free FCB count and FCB pointers. In UFS, this is called a superblock; in NTFS, it is
stored in. the master file table.
A directory structure (per file system), containing file names and pointers to
corresponding FCBs. UNIX uses inode numbers, and NTFS uses a master file table.
The File Control Block, FCB, (per file) containing details about ownership, size,
permissions, dates, etc. UNIX stores this information in inodes, and NTFS in the
master file table as a relational database structure.
Fig. 4.33 illustrates some of the interactions of file system components when files are
created and/or used:
When a new file is created, a new FCB is allocated and filled out with important
information regarding the new file.
When a file is accessed during a program, the open( ) system call reads in the FCB
information from disk, and stores it in the system-wide open file table. An entry is added
to the per-process open file table referencing the system-wide table, and an index into
theper- process table is returned by the open( ) system call. UNIX refers to this index as a
file descriptor, and Windows refers to it as a file handle.
If another process already has a file open when a new request comes in for the same file,
and it is sharable, then a counter in the system-wide table is incremented and the per-
process table is adjusted to point to the existing entry in the system-wide table.
When a file is closed, the per-process table entry is freed, and the counter in the system-
wide table is decremented. If that counter reaches zero, then the system wide table is also
freed. Any data currently stored in memory cache for this file is written out to disk if
necessary.
Fig. 4.54 In-memory file-system structures, (a) File open, (b) File read.
4.13.1 Partitions and Mounting
Physical disks are commonly divided into smaller units called partitions. They can
also be combined into larger units, but that is most commonly done for RAID
installations and is left for later chapters.
Partitions can either be used as raw devices ( with no structure imposed upon them
), or "cooked;' containing a file system. they can be formatted to hold a filesystem
( i.e. populated with FCBs and initial directory structures as appropriate. ) Raw
partitions are generally used for swap space, and may also be used for certain
programs such as databases that choose to manage their own disk storage system.
Partitions containing filesystems can generally only be accessed using the file
system structure by ordinary users, but can often be accessed as a raw device also
by root.
Boot information can be stored in a separate partition. Again, it has its ownformat,
because at boot time the system does not have file-system device drivers loaded
and therefore cannot interpret the file-system format.
The boot block is accessed as part of a raw partition, by the boot program prior to
any operating system being loaded. Modern boot programs understand multiple
OSes and filesystem formats, and can give the user a choice of which of several
available systems to boot.
The root partition contains the OS kernel and at least the key portions of the OS
needed to complete the boot process. At boot time the root partition is mounted,
and control is transferred from the boot program to the kernel found there. ( Older
systems required that the root partition lie completely within the first 1024
cylinders of the disk, because that was as far as the boot program could
reach.
Once the kernel had control, then it could access partitions beyond the 1024
cylinder boundary. )
Continuing with the boot process, additional filesystems get mounted, adding their
information into the appropriate mount table structure. As a part of the mounting
process the file systems may be checked for errors or inconsistencies, either
because they are flagged as not having been closed properly the last time they
were used, or just for general principals. Filesystems may be mounted either
automatically or manually. In UNIX a mount point is indicated by setting a flag in
the in-memory copy of the inode, so all future references to that inode get re-
directed to the root directory of the mounted filesystem.
pointed to
Thus, the file-system implementation consists of three major layers, as depicted schematically in
Fig. 4.34. The first layer is the file-system interface, based on the open (), read (), write (), and
close () calls and on file descriptors. The second layer is called the virtual file system (VFS)
layer; it serves two important functions:
2. The VFS provides a mechanism for uniquely representing a file throughout a network.
The VFS is based on a file-representation structure, called a vnode, that contains a
numerical designator for a network-wide unique file. (UNIX inodes are unique within
only a single file system.) This network-wide uniqueness is required for support of
network file systems. The kernel maintains one vnode structure for each active node (file
or directory).
3. The layer implementing the file system type or the remote-file-system protocol is the third
layer of the architecture.
some drawbacks.
The disadvantage of a linear list of directory entries is that finding a file requires
a linear search.
To overcome this, a software cache is implemented to store the recently
accessed directory structure.
Deletions can be done by moving all entries, flagging an entry as deleted, or
by moving the last entry into the newly vacant position.
A sorted list allows a binary search and decreases the average search time.
However, the requirement that the list be kept sorted may complicate creating and
deletingfiles,
A linked list makes insertions and deletions into a sorted list easier, with overhead
for the links.
An advantage of the sorted list is that a sorted directory listing can
beproduced without a separate sort step.
b) Hash Table
With this method, a linear list stores the directory entries, but a hash data structure is
also used.
The hash table takes a value computed from the file name and returns a pointer to the
file name in the linear list. Therefore, it can greatly decrease the directory search time.
Here collisions may occur. Collision is the situation where two file names hash to the
same location.
Alternatively, a chained-overflow hash table can be used. Each hash entry can be a linked
list instead of an individual value, and we can resolve collisions by adding the new entry
to the linked list.
The major disadvantage with a hash table are its generally fixed size and the dependence
of the hash function on that size. For example, assume that we make a linear-probing hash
table that holds 64 entries. The hash function converts file names into integers from 0 to
63, for instance, by using the remainder of a division by 64. If we later try to create a 65th
file, we must enlarge the directory hash table—say, to 128 entries. As a result, we need a
new hash function that must map file names to the range 0 to 127, and we must
reorganize the existing directory entries to reflect their new hash-function values.
a) Contiguous Allocation
Storage allocation is done by using one of the algorithms (first fit, best fit, worst
fit).
The allocation of blocks contiguous leads to external fragmentation.
Problems can arise when files grow, or if the exact size of a file is unknown at
creation time:
o Over-estimation of the file's final size increases external fragmentation and
wastes disk space.
o Under-estimation may require that a file be moved or a process aborted if
the file grows beyond its originally allocated space.
o If a file grows slowly over a long time period and the total final space
must be allocated initially, then a lot of space becomes unusable before the
file fills the space.
A variation is to allocate file space in large contiguous chunks (Fig. 4.35),
called extents. When a file outgrows its original extent, then an additional
one block is allocated. A pointer points from last block of contiguous
memory allocation to the extended chunk.
b) Linked Allocation
Disk files can be stored as linked lists, with the expense of the storage space
consumed by each link. (E.g. a block may be 508 bytes instead of 512.)
Linked allocation involves no external fragmentation, does not require pre-known
file sizes, and allows files to grow dynamically at any time as shown in Fig. 4.36.
Unfortunately, linked allocation is only efficient for sequential access files, as
random access requires starting at the beginning of the list for each new location
access.
Allocating clusters of blocks reduces the space wasted by pointers, at the cost of
internal fragmentation.
Another big problem with linked allocation is reliability if a pointer is lost or
damaged. Doubly linked lists provide some protection, at the cost of additional
overhead and wasted space.
The File Allocation Table, FAT, used by DOS is a variation of linked allocation,
where all the links are stored in a separate table at the beginning of the disk. The
benefit of this approach is that the FAT table can be cached in memory, greatly
improving random access speeds (Fig. 4.37).
c) Indexed Allocation
Indexed Allocation combines all of the indexes (block numbers) for accessing
each file into a common block (for that file).
Each file will have a common block called the index block as shown in Fig. 4.38.
Some disk space is wasted (relative to linked lists or FAT tables) because an
entire index block must be allocated for each file, regardless of how many data
blocks the file contains. This leads to questions of how big the index block should
be, and how it should be implemented. There are several approaches:
Linked Scheme - An index block is one disk block, which can be read
and written in a single disk operation. The first index block contains
some header information, the first N block addresses, and if necessary a
pointer to additional linked index blocks.
Multi-Level Index - The first index block contains a set of pointers to
secondary index blocks, which in turn contain pointers to the actual data
blocks.
Combined Scheme - This is the scheme used in UNIX inodes, in which
the first 12 entries data block pointers are stored directly in the inode,
and then singly, doubly, and triply indirect pointers provide access to
more data blocks as needed. The advantage of this scheme is that for
small files ( files stored in less than 12 blocks ), the data blocks are
readily accessible ( up to 48K with 4K block sizes ); files up to about
4144K ( using 4K blocks ) are accessible with only a single indirect
block ( which can be cached ), and huge files are still accessible using a
relatively small number of disk accesses ( larger in theory than can be
addressed by a 32- bit address, which is why some systems have moved
to 64-bit file pointers. ) A UNIX inode is shown in Fig. 4.39.
4.16 Performance
Optimal allocation method is different for sequential access files than for random access
files, and is also different for small files than for large files.
Some systems support more than one allocation method, which may require specifying how
the file is to be used (sequential or random access) at the time it is allocated. Such systems
also provide conversion utilities.
Some systems have been known to use contiguous access for small files, and automatically
switch to an indexed scheme when file sizes surpass a certain threshold.
And of course some systems adjust their allocation schemes (e.g. block sizes ) to best match the
characteristics of the hardware for optimum performance.
a) Bit Vector
Fast algorithms exist for quickly finding contiguous blocks of a given size
One simple approach is to use a bit vector, in which each bit represents a disk
block, set to 1 if free or 0 if allocated.
For example, consider a disk where blocks 2,3,4,5,8,9, 10,11, 12, 13, 17and 18 are free, and
the rest of the blocks are allocated. The free-space bit map would be
0011110011111100011
Easy to implement and also very efficient in finding the first free block or
‗n‘ consecutive free blocks on the disk.
The down side is that a 40GB disk requires over 5MB just to store the bitmap.
b) Linked List
A linked list can also be used to keep track of all free blocks.
Traversing the list and/or finding a contiguous block of a given size are not easy,
but fortunately are not frequently needed operations (Fig. 4.40). Generally, the
system just adds and removes single blocks from the beginning of the list.
The FAT table keeps track of the free list as just one more linked list on the table.
A variation on linked list free lists. It stores the addresses of n free blocks in the
first free block. The first n-1 blocks are actually free. The last block contains the
addresses of another n free blocks, and so on.
The address of a large number of free blocks can be found quickly.
d) Counting
When there are multiple contiguous blocks of free space then the system can
keep track of the starting address of the group and the number of contiguous free
blocks.
Rather than keeping al list of n free disk addresses, we can keep the address of
first free block and the number of free contiguous blocks that follow the first
block.
Thus, the overall space is shortened. It is similar to the extent method of
allocating blocks.
e) Space Maps (New)
Sun's ZFS file system was designed for huge numbers and sizes of files,
directories, and even file systems.
The resulting data structures could be inefficient if not implemented carefully. For
example, freeing up a 1 GB file on a 1 TB file system could involve updating
thousands of blocks of free list bit maps if the file was spread across the disk.
ZFS uses a combination of techniques, starting with dividing the disk up into
(hundreds of) metaslabs of a manageable size, each having their own space map.
Free blocks are managed using the counting technique, but rather than write the
information to a table, it is recorded in a log-structured transaction record.
Adjacent free blocks are also coalesced into a larger single free block.
An in-memory space map is constructed using a balanced tree data structure,
constructed from the log data.
The combination of the in-memory tree and the on-disk log provide for very fast
and efficient management of these very large files and free blocks.
III SEMESTER OPERATING SYSTEM (BCS303) 40