Module IV - OS
Module IV - OS
MODULE 4
Virtual memory is a technique that allows the execution of processes that are not completely in
memory. This technique frees programmers from the concerns of memory-storage limitations.
Virtual memory also allows processes to share files easily and to implement shared memory.
BACKGROUND
The memory-management algorithms are necessary because of one basic requirement: The
instructions being executed must be in physical memory. The first approach to meeting this
requirement is to place the entire logical address space in physical memory.
In many cases, the entire program is not needed. For instance, consider the following:
1. Programs often have code to handle unusual error conditions. Since these errors
seldom, if ever, occur in practice, this code is almost never executed.
2. Arrays, lists, and tables are often allocated more memory than they actually need.
3. Certain options and features of a program may be used rarely.
The ability to execute a program that is only partially in memory would confer many benefits:
1. A program would no longer be constrained by the amount of physical memory that is
available
2. Less I/O would be needed to load or swap user programs into memory, so each user
program would run faster.
3. 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.
The figure below shows the general layout of virtual memory, which can be much larger than
physical memory:
1
Operating Systems
The address space of a process refers to the logical (or virtual) view of how a process is stored in
memory. It is up to the memory management unit (MMU) to map logical pages to physical page
frames in memory.
As we allow for the heap to grow upward in memory as it is used for dynamic memory
allocation. Similarly, we allow for the stack to grow downward in memory through successive
function calls. The large blank space (or hole) between the heap and the stack is part of the
virtual address
Virtual address spaces that include holes are known as sparse address spaces. Using a sparse
address space is beneficial because the holes can be filled as the stack or heap segments grow.
In addition to separating logical memory from physical memory, virtual memory allows files and
memory to be shared by two or more processes through page sharing. This leads to the following
benefits:
1. System libraries can be shared by several processes through mapping of the shared
object into a virtual address space
2. virtual memory enables processes to share memory
3. Virtual memory can allow pages to be shared during process creation with the fork()
system call thus speeding up process creation
2
Operating Systems
DEMAND PAGING
Loading the entire program into memory results in loading the executable code for all
options, regardless of whether an option is ultimately selected by the user or not.
An alternative strategy is to load pages only as they are needed. This technique is known
as demand paging and is commonly used in virtual memory systems
A demand-paging system is similar to a paging system with swapping (Figure) where
processes reside in secondary memory (usually a disk). When we want to execute a
process, we swap it into memory.
The basic idea behind demand paging is that when a process is swapped in, its pages are
not swapped in all at once. Rather they are swapped in only when the process needs them.
( on demand. ) This is termed as lazy swapper, although a pager is a more accurate term.
Basic Concepts
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.
The hardware necessary to support demand paging is the same as for paging and
swapping: A page table and secondary memory.
3
Operating Systems
When this bit is set to "valid” the associated page is both legal and in memory. If the bit
is set to "invalid” the page either is not valid (that is, not in the logical address space of
the process) or is valid but is currently on the disk.
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:
The procedure for handling this page fault is straightforward
1. We check an internal table (usually kept with the process control block) for this
process to determine whether the reference was a valid or an invalid memory access.
2. If the reference was invalid, we terminate the process.
3. If it was valid, but we have not yet brought in that page, we now page it in.
4. We find a free frame (by taking one from the free-frame list, for example).
5. We schedule a disk operation to read the desired page into the newly allocated frame.
6. When the disk read is complete, we modify the internal table kept with the process
and the page table to indicate that the page is now in memory.
7. We restart the instruction that was interrupted by the trap. The process can now
access the page as though it had always been in memory.
4
Operating Systems
.
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.
5
Operating Systems
To compute the effective access time, we must know how much time is needed to
service a page fault. A page fault causes the following sequence to occur:
1. Trap to the OS.
2. Save the user registers and process state
3. Determine that the interrupt was a page fault. Check that the page reference was legal
and determine the location of the page on the disk
4. Issue a read from the disk to a free frame:
a. Wait in a queue for this device until the read request is serviced.
b. Wait for the device seek and/ or latency time.
c. Begin the transfer of the page to a free frame.
5. While waiting, allocate the CPU to some other user (CPU scheduling, optional).
6. Receive an interrupt from the disk I/0 subsystem (I/0 completed).
7. Save the registers and process state for the other user (if step 6 is executed).
Determine that the interrupt was from the disk
8. Correct the page table and other tables to show that the desired page is now in
memory.
9. Wait for the CPU to be allocated to this process again.
10. Restore the user registers, process state, and new page table, and then resume the
interrupted instruction.
There are three major components of the page-fault service time:
1. Service the page-fault interrupt.
2. Read in the page.
3. Restart the process.
With an average page-fault service time of 8 milliseconds and a memory access time of
200 nanoseconds, the effective access time in nanoseconds is
effective access time = (1 - p) x (200) + p (8 milliseconds)
= (1- p) X 200 + p X 8,000,000
= 200 + 7,999,800 X p.
6
Operating Systems
We see, then, that the effective access time is directly proportional to the page fault rate.
If one access out of 1,000 causes a page fault, the effective access time is 8.2
microseconds
COPY ON WRITE
Traditionally, fork() worked by creating a copy of the parent's address space for the child,
duplicating the pages belonging to the parent.
However, considering that many child processes invoke the exec() system call
immediately after creation, the copying of the parent's address space may be unnecessary.
Instead, we can use a technique known as copy on write which works by allowing the
parent and child processes initially to share the same pages. These shared pages are
marked as copy-on-write pages, meaning that if either process writes to a shared page, a
copy of the shared page is created.
Copy-on-write is illustrated in Figures below, which show the contents of the physical
memory before and after process 1 modifies page c.
For example, assume that the child process attempts to modify a page containing portions
of the stack, with the pages set to be copy-on-write. The OS will create a copy of this
page, mapping it to the address space of the child process. The child process will then
modify its copied page and not the page belonging to the parent process.
7
Operating Systems
Obviously, when the copy-on-write technique is used, only the pages that are modified by
either process are copied; all unmodified pages can be shared by the parent and child
processes only pages that can be modified need be marked as copy-on-write. Pages that
cannot be modified (pages containing executable code) can be shared by the parent and
child.
PAGE REPLACEMENT
Demand paging saves the I/0 necessary to load the five pages that are never used and
hence increases degree of multiprogramming
If some process suddenly decides to use more pages and there aren't any free frames
available. 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. There are many
different algorithms for page replacement.
8
Operating Systems
Notice that, if no frames are free, two page transfers (one out and one in) are required.
This situation effectively doubles the page-fault service time and increases the effective
access time accordingly.
We can reduce this overhead by using a modified bit or dirty bit. I.e.each page or frame
has a modify bit associated with it in the hardware. When we select a page for
replacement, we examine its modify bit.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.
1. A frame-allocation algorithm- centers around how many frames are allocated to
each process
2. A page-replacement algorithm-deals with how to select a page for replacement
when there are no free frames available.
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.
9
Operating Systems
Although FIFO is simple and easy to understand, it is not always optimal, or even
efficient.
Belady's anomaly tells that for some page-replacement algorithms, the page- fault rate
may increase as the number of allocated frames increases.
2. Optimal Page Replacement
The discovery of Belady's anomaly lead to the search for an optimal page- replacement
algorithm, which is simply that which yields the lowest of all possible page-faults, and
which does not suffer from Belady's anomaly.
Such an algorithm does exist, and is called OPT or MIN. This algorithm is "Replace the
page that will not be used for the longest time in the future."
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.
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.
10
Operating Systems
LRU is considered a good replacement policy, and is often used. There are two simple
approaches commonly used to implement this:
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.
11
Operating Systems
12
Operating Systems
o 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.
o This algorithm is also known as the clock algorithm.
4.3 Enhanced Second-Chance Algorithm
o The enhanced second chance algorithm looks at the reference bit and the modify bit
( dirty bit ) as an ordered page, and classifies pages into one of four classes:
( 0, 0 ) - Neither recently used nor modified.
( 0, 1 ) - Not recently used, but modified.
( 1, 0 ) - Recently used, but clean.
( 1, 1 ) - Recently used and modified.
o 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.
o The main difference between this algorithm and the previous one is the preference
for replacing clean pages if possible.
5. Counting-Based Page Replacement
There are many other algorithms that can be used for page replacement. For example,
we can keep a counter of the number of references that have been made to each page
and develop the following two schemes.
a) The 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.
b) The 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.
13
Operating Systems
6. 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.
Allocation of frames
The absolute minimum number of frames that a process must be allocated is dependent
on system architecture.
The maximum number is defined by the amount of available physical memory.
Allocation Algorithms
After loading of OS, there are two ways in which the allocation of frames can be done to the
processes.
1. Equal Allocation - If there are m frames available and n processes to share them, each
process gets m / n frames, and the leftovers are kept in a free-frame buffer pool.
2. Proportional Allocation - Allocate the frames proportionally depending on the size of
the process. If the size of process i is Si, and S is the sum of size of all processes in the
system, then the allocation for process Pi is ai = m * Si / S. where m is the free frames
available in the system.
Consider a system with a 1KB frame size. If a small student process of 10 KB and an interactive
database of 127 KB are the only two processes running in a system with 62 free frames.
With proportional allocation, we would split 62 frames between two processes, as follows-
m=62, S = (10+127)=137
Allocation for process 1 = 62 X 10/137 ~ 16
Allocation for process 2 = 62 X 127/137 ~57
Thus allocates 16 frames and 57 frames to student process and database respectively.
Variations on proportional allocation could consider priority of process rather than just their size.
Global versus Local Allocation
Page replacement can occur both at local or global level.
With local replacement, the number of pages allocated to a process is fixed, and page
replacement occurs only amongst the pages allocated to this process.
With global replacement, any page may be a potential victim, whether it currently
belongs to the process seeking a free frame or not.
Local page replacement allows processes to better control their own page fault rates, and
leads to more consistent performance of a given process over different system load levels.
Global page replacement is overall more efficient, and is the more commonly used
approach.
14
Operating Systems
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.
Cause of Thrashing
When memory is filled up and processes starts spending lots of time waiting for their
pages to page in, then CPU utilization decreases(Processes are not executed as they are
waiting for some pages), causing the scheduler to add in even more processes and
increase the degree of multiprogramming even more. Thrashing has occurred, and system
throughput plunges. No work is getting done, because the processes are spending all their
time paging.
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. At this point, to increase CPU utilization and stop thrashing, we must decrease
the degree of multiprogramming.
15
Operating Systems
Local page replacement policies can prevent thrashing process from taking pages away from
other processes, but it still tends to clog up the I/O queue
Working-Set Model
It is based on the assumption of locality. It starts by looking at how many frames a
process is actually using. This approach defines the locality of process execution.
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 (Figure). 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. We
may return to this locality later.
If we do not allocate enough frames to accommodate the size of the current locality, the
process will thrash, since it cannot keep in memory all the pages that it is actively using.
16
Operating Systems
Page-Fault Frequency
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.
We can establish upper and lower bounds on the desired page-fault rate (Figure 9.21). If
the actual page-fault rate exceeds the upper limit, we allocate the process another frame;
if the page-fault rate falls below the lower limit, we remove a frame from the process.
Thus, we can directly measure and control the page-fault rate to prevent thrashing.
17
Operating Systems
FILE SYSTEM
File Concept
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.
A file is a collection of related information that is recorded on secondary storage. A file is a
sequence of bits, bytes, lines, or records, the meaning of which is defined by the file's creator and
user
File Attributes
A file is named, for the convenience of its human users, and is referred to by its name. A file's
attributes vary from one OS to another but typically consist of these:
1. Name. The symbolic file name is the only information kept in human readable form.
2. Identifier. This unique tag, usually a number, identifies the file within the file system;
it is the non-human-readable name for the file.
3. Type. This information is needed for systems that support different types of files.
4. Location. This information is a pointer to a device and to the location of the file on
that device.
5. Size. The current size of the file (in bytes, words, or blocks) and possibly the
maximum allowed size are included in this attribute.
6. Protection. Access-control information determines who can do reading, writing,
executing, and so on.
7. Time, date, and user identification. This information may be kept for creation, last
modification, and last use. These data can be useful for protection, security, and usage
monitoring.
File Operations
The operating system provides system calls to create, write, read, reposition, delete, and truncate.
1. 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.
2. 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.
3. 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.
4. 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.
5. Deleting a file – To delete a file, search the directory for the file. Release all file
18
Operating Systems
space, so that it can be reused by other files, and erase the directory entry.
6. 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.
7. Information about currently open files is stored in an open file table. It contains
informations like:
a. File pointer - records the current position in the file, for the next read or
write access.
b. 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.
c. 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 opearation.
d. 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.
8. Some systems provide support for file locking.
a. A shared lock is for reading only.
b. A exclusive lock is for writing as well as reading.
c. An advisory lock , it is up to the software developers to ensure that locks
are acquired or released.
d. A mandatory lock , prevents any other process from accessing the
locked file.. ( A truly locked door. )
o UNIX uses advisory locks, and Windows uses mandatory locks.
File Types
File name consists of two parts: name and
extension
The user and the operating system can
identify the type of a file using the name.
Most operating systems allow users to
specify a file name as a sequence of
characters followed by a period and
terminated by an extension. Example :
resume.doc, threads.c
The system uses the extension to indicate
the type of the file and the type of operations
that can be done on that file.
For instance, only a file with a ".corn",
".exe", or ".bat”, can be executed
19
Operating Systems
Access methods
Files store information. When it is used, this information must be accessed and read into
computer memory.
1. 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:
read next - read a record and advance the file pointer to the next position.
write next - write a record to the end of file and advance the file
pointer to the next position.
skip n records - May or may not be supported. ‘n’ may be limited to
positive numbers, or may be limited to +/- 1.
2. 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 are 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.
3. Other access methods(Indexed methods)
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.
20
Operating Systems
Storage Structure
types of file systems in the Solaris:
1. tmpfs-a "temporary" file system. that is created in volatile main memory and has its
contents erased if the system reboots or crashes
2. objfs-a "virtual" file system (essentially an interface to the kernel that looks like a file
system) that gives debuggers access to kernel symbols
21
Operating Systems
3. dfs-a virtual file system that maintains "contract" information to manage which
processes start when the system boots and must continue to run during operation
4. lofs-a "loop back" file system that allows one file system to be accessed in place of
another one
5. prods-a virtual file system that presents information on all processes as a file system .
6. ufs, zfs-general-purpose file systems
Directory Overview
The directory can be viewed as a symbol table that translates file names into their directory
entries.
Directory operations to be supported include:
1. Search for a file - search a directory structure to find the entry for a particular file.
2. Create a file – create new files and add to the directory
3. Delete a file - When a file is no longer needed, erase it from the directory
4. List a directory - list the files in a directory and the contents of the directory entry.
5. Rename a file – Change the name of the file. Renaming a file may also allow its
position within the directory structure to be changed.
6. Traverse the file system - Access every directory and every file within a directory
structure.
Directory Structures -
1. Single-level Directory
The simplest directory structure is the single-level directory. All files are contained in the same
directory, which is easy to support and understand (Figure).
The limitations of this structure is that -
All files are in the same directory must have unique names.
Even a single user on a single-level directory may find it difficult to remember the
names of all the files as the number of files increases.
2. 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 users 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.
22
Operating Systems
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.
3. 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.
For example, in the tree-structured file system of figure 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.
23
Operating Systems
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.
4. 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. )
UNIX provides two types of links (pointer to another file)for implementing the
acyclic-graph structure.
i. 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.
ii. 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.
Windows only supports symbolic links, termed shortcuts.
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.
24
Operating Systems
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.
25
Operating Systems
Figure below 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
Figure
26
Operating Systems
File Sharing
1. Multiple Users
On a multi-user system, more information needs to be stored for each file:
The owner ( user ) who owns the file, and who can control its access.
The group of other user IDs that may have some special access to the file.
What access rights are afforded to the owner ( User ), the Group, and to the rest of the
world ( the universe, a.k.a. Others. )
Some systems have more complicated access control, allowing or denying specific
accesses to specifically named users or groups.
27
Operating Systems
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 systems allow for blocking or delayed
response, in the hopes that the remote system ( or the network ) will
come back up eventually.
Consistency Semantics
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.
1. UNIX Semantics
The UNIX file system (Chapter 17) uses the following consistency semantics:
Writes to an open file by a user are visible immediately to other users who have this
file open. One mode of sharing allows users to share the pointer of current location
into the file. Thus, the advancing of the pointer by one user affects all sharing users.
Here, a file has a single image that interleaves all accesses, regardless of their origin.
2. Session Semantics
The Andrew file system (AFS) (Chapter 17) uses the following consistency
semantics:
Writes to an open file by a user are not visible immediately to other users that have
the same file open. Once a file is closed, the changes made to it are visible only in
sessions starting later. Already open instances of the file do not reflect these changes.
3. Immutable-Shared-Files Semantics
28
Operating Systems
A unique approach is that of Once a file is declared as shared by its creator, it cam1ot
be modified. An immutable £ile has two key properties: its name may not be reused,
and its contents may not be altered.
Thus, the name of an immutable file signifies that the contents of the file are fixed.
The implementation of these semantics in a distributed system is simple, because the
sharing is disciplined (read-only).
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).
Reliability is 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.
Types of Access
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:
1. Read. Read from the file.
2. Write. Write or rewrite the file.
3. Execute. Load the file into memory and execute it.
4. Append. Write new information at the end of the file.
5. Delete. Delete the file and free its space for possible reuse.
6. List. List the name and attributes of the file
Access Control
The most common approach to the protection problem is to make access dependent 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 (ACJU 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.
This approach has the advantage of enabling complex access methodologies. The main
problem with access lists is their length. If we want to allow everyone to read a file, we
must list all users with read access. This technique has two undesirable consequences:
1. Constructing such a list may be a tedious and unrewarding task, especially if we do
not know in advance the list of users in the system.
2. The directory entry, previously of fixed size, now must be of variable size, resulting
in more complicated space management.
29
Operating Systems
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:
1. Owner. The user who created the file is the owner.
2. Group. A set of users who are sharing the file and need similar access is a group, or
work group.
3. Universe. All other users in the system constitute the universe.
30
Operating Systems
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.
o The basic file system level works directly with the device drivers in terms of
retrieving and storing raw blocks of data, without any consideration for what is in
each block.
o The file organization module knows about files and their logical blocks, and how they
map to physical blocks on the disk. In addition to translating from logical to physical
blocks, the file organization module also maintains the list of free blocks, and
allocates free blocks to files as needed.
o The logical file system deals with all of the meta data associated with a file (UID,
GID, mode, dates, etc), i.e. everything about the file except the data itself. This level
manages the directory structure and the mapping of file names to file control blocks,
FCBs, which contain all of the meta data as well as block number information for
finding the data on the disk.
The layered approach to file systems means that much of the code can be used uniformly
for a wide variety of different file systems, and only certain layers need to be filesystem
specific.
When a layered structure is used for file-system implementation, duplication of code is
minimized. The I/O control and sometimes the basic file-system code can be used by
multiple file systems.
Common file systems in use include the UNIX file system, UFS, the Berkeley Fast File
System, FFS, Windows systems FAT, FAT32, NTFS, CD-ROM systems ISO 9660, and
for Linux the extended file systems ext2 and ext3 .
31
Operating Systems
Figure 11.3 illustrates some of the interactions of file system components when files are
created and/or used:
o When a new file is created, a new FCB is allocated and filled out with important
information regarding the new file.
32
Operating Systems
o 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 the per-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.
o 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.
o 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.
33
Operating Systems
Boot information can be stored in a separate partition. Again, it has its own format,
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.
The root partition contains the OS kernel and at least the key portions of the OS needed
to complete the boot process.
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.
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 VFS layer. The VFS layer serves two important functions:
1. It separates file-system-generic operations from their implementation by defining a
clean VFS interface. Several implementations for the VFS interface may coexist on
the same machine, allowing transparent access to different types of file systems
mounted locally.
34
Operating Systems
Directory Implementation
The selection of directory-allocation and directory-management algorithms significantly affects
the efficiency, performance, and reliability of the file system. Directories need to be fast to
search, insert, and delete, with a minimum of wasted disk space
1. Linear List
A linear list is the simplest and easiest directory structure to set up, but it does have 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 deleting files,
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 be produced without a
separate sort step
2. 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.
35
Operating Systems
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.
Allocation Methods
1. Contiguous Allocation
Contiguous Allocation requires that all blocks of a file be kept together
contiguously.
Performance is very fast, because reading successive blocks of the same file
generally requires no movement of the disk heads, or at most one small step to the
next adjacent cylinder.
Storage allocation 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,
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.
36
Operating Systems
2. Linked Allocation
Disk files can be stored as linked lists, with the expense of the storage space
consumed by each link. ( E.g. a block may be 508 bytes instead of 512. )
Linked allocation involves no external fragmentation, does not require pre-known
file sizes, and allows files to grow dynamically at any time.
Unfortunately linked allocation is only efficient for sequential access files, as
random access requires starting at the beginning of the list for each new location
access.
Allocating clusters of blocks reduces the space wasted by pointers, at the cost of
internal fragmentation.
Another big problem with linked allocation is reliability if a pointer is lost or
damaged. Doubly linked lists provide some protection, at the cost of additional
overhead and wasted space.
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.
37
Operating Systems
3. 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.
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:
1. Linked scheme. An index block is normally one disk block. Thus, it can be read
and written directly by itself. To allow for large files, we can link together several
index blocks.
2. Multilevel index. A variant of linked representation uses a first-level index block
to point to a set of second-level index blocks, which in turn point to the file
blocks. To access a block, the operating system uses the first-level index to find a
second-level index block and then uses that block to find the desired data block
3. Combined scheme. Another alternative, used in the UFS, is to keep the first, say,
15 pointers of the index block in the file's inode. The first 12 of these pointers
point to direct blocks; that is, they contain addresses of blocks that contain data of
the file. Thus, the data for small files (of no more than 12 blocks) do not need a
separate index block.
Performance
The allocation methods that we have discussed vary in their storage efficiency and data-
block access times.
For any type of access, contiguous allocation requires only one access to get a disk
block.
For linked allocation, we can also keep the address of the next block in memory and read
it directly. This method is fine for sequential access some systems support direct-access
files by using contiguous allocation and sequential-access files by using linked allocation.
38
Operating Systems
For these systems, the type of access to be made must be declared when the file is
created.
Indexed allocation is more complex. If the index block is already in memory, then the
access can be made directly. However, keeping the index block in memory requires
considerable space.
39
Operating Systems
3. Grouping
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.
4. Counting
When there are multiple contiguous blocks of free space then the system can keep track
of the starting address of the group and the number of contiguous free blocks.
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.
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.
40
Memory Management
June/July 2017
1. Explain the multistep processing of a user program with a neat block diagram. (5)
2. Distinguish between internal and external fragmentation. (2)
3. Explain segmentation with an example. (6)
4. Consider the following segment table: (7)
Segment Base Length
0 219 600
1 2300 14
2 90 100
3 1327 580
4 1952 96
What are the physical address for the following logical address?
i) 0,430 ii) 1,10 iii) 2,500 iv) 3,400 v) 4,112
May 2017
1 Explain internal and external fragmentation with examples. (6)
2 Explain with a diagram, how TLB is used to solve the problem of simple paging scheme. (8)
3 What is thrashing? How does the system detect thrashing? (6)
Dec-2015
1) Discuss paging with an example. (8)
June-2015
4) With a supporting paging hardware, explain in detail concept of paging with an example for a 32-byte
memory with 4-type pages with a process being 16-bytes. How many bits are reserved for page number
and page offset in the logical address. Suppose the logical address is 5, calculate the corresponding
physical address, after populating memory and page table. (10)
5) Discuss on the performance of demand paging. (5)
6) What is Belady’s anomaly? Explain with an example. (5)
Dec - 2014
7) What is locality of reference ? (2)
Dec –Jan2013
8) Why are translation look-aside buffers(TLB) important? In a simple paging system what information is
stored in TLB? Explain. (8)
9) Given memory partitions of 100K,500K,200K,300K and 600K, apply first fit and best fit algorithm to
place 212K,417 K,112 K and 426 K. (4)
10) What is swapping? Does this increase the operating systems overhead? Justify your answer. (8).
June-July 2013
11) What are the draw backs of contiguous memory allocation?
12) Consider a paging system with the page table stored in memory.
i) if a memory reference takes 200 nano seconds, how long does a paged memory reference take?
ii) if we add associative register and 75 percentage of all page table references are found in the associative
registers, what is the effective memory access time? (Assume that finding a page table entry in the
associative memory/registers takes zero time, if the entry is found).
June 2012
13) Distinguish between:
i) Logical address space and physical address space.
ii) Internal fragmentation and external fragmentation.
iii) Paging and segmentation. (6)
14) Explain with the help of supporting hardware diagram how the TLB improves the performance of a
demand paging system. (10)
15) Given memory partitions of 100K,500K,200K,300K and 600K (in order) how would each of the first fit,
best fit and worst fit algorithms work place processes of 212K,417 K,112 K and 426 K (in order)? Which
algorithm makes the most efficient use of memory? (4)
Dec 2012
16) On a system using simple segmentation, compute the physical address for each of the logical address,
logical address is given in the following segment table. If the address generates a segment fault, indicate it
as “segment fault”
June-July 2011
21) Consider the following page reference string.
1, 2, 3, 5, 2, 3, 5, 7, 2, 1, 2, 3, 8, 6, 4, 3, 2, 2, 3, 6.
How many page faults would cocur in the case of
i) LRU
ii) FIFO
iii) Optimal algorithms assuming 3 frames.
(Intially all frames are empty). (8)
Dec-2011
22) What is dynamic storage allocation? Explain the commonly used strategies for dynamic storage allocation.
(12)
23) Explain the buddy- system, used for managing free memory assigned to kernel process.(8)
Dec -2010
24) What do you mean by address binding? Explain with the necessary steps, the binding of instructions and
data to memory addresses. (8)
25) On a system using demand paged memory it takes 0.12us to satisfy a memory request, if the page is in
memory. If the page is not in memory the request takes 5000us. What would the page fault rate need to be
to achieve an effective access time 1000us? Assume the system is only running a single process and the
CPU is idle during the page swaps. (8)
26) What do you mean by a copy-on-write? Where is it used? Explain in brief. (4)
May 2017
1. What is a file? Explain the different allocation methods. (10)
2. Explain different approaches to managing free space on disk storage. (10)
Dec-2015
1) Explain different file access methods. (6)
2) Describe the various directory structures. (8)
3) Write a note on any four different methods for managing free space. (6)
June-2015
4) With supporting diagrams distinguish between single-level and two-level directory structure.(5)
5) Compare contiguous and linked allocation methods for disk space. (5)
6) Explain bit vector free-space management technique. (5)
Dec –Jan2014
7) What is a file? Explain the different allocation methods. (10)
8) What are directories? Write a brief note on mounting file systems. (5)
9) How is free space managed? Explain (5)
June-July-2013
10) Explain the various file operations supported by the operating system, also differentiate mandatory lick
and advisory lock mechanisms used on files by the operating system. (5)
11) Explain various file protection mechanisms. (7)
June 2012
Dec 2012
15) Explain the different types of directory structures, with examples and mention their advantages and
disadvantages. (8)
16) With supporting diagrams, explain linked and indexed method of allocating disk space (8)
17) Describe the different access methods on files. (9)
Dec-2011
18) How do the modern operating systems concurrently support multiple types of file system? Explain its
implementation in detail. (10)
Dec-Jan 2010
19) Explain the following:
i) File Types.
ii) File operations
iii) File attributs.
iv) Tree directory structure.
v) Thrashing
vi) Monitors.
vii) Acyclic-graph direcotory.
Dec -2010
20) What do you mean by a free space list? With suitable examples, explain any two methods of
implementation of a free space list.
May-June 2010
21) What is meant by ‘consistency semantics’. Explain the consistency semantics as implemented in a modern
OS (7)
22) Explain Virtual File System.(VFS) (5)