Chapter No 5 and 6
Chapter No 5 and 6
Initially, reference 7, 0, 1 are store inside the three free frames. When the reference to page 4
occurs, LRU replacement checks for existing pages inside the frames for their last access. From
the three frames in the memory, page 7 was used least recently. Thus, the LRU algorithm
replaces page 7, not knowing that page 7 is about to be used. Advantage: Less number of
faults as compared to FIFO
Q2 Explain static and dynamic memory partioning with advantages and drawback. (Explanation-
static & dynamic 1Mark each, any one Advantage and disadvantage 1 Mark each for each type)
ANS Static Memory
Static Memory Partitioning Main memory is divided into multiple partitions of fixed size at the
time of system generation. A process may be loaded into a partition of equal size or greater
size. Partitions can be of equal size or unequal size
Advantages:
1)Simple to implement
2) It requires minimal operating system software and processing overhead as
partitions are fixed at the time of system generation.
Disadvantages:
1) Memory wastage
2) Inefficient use of memory due to internal fragmentation.
3) Maximum number of active processes is fixed.
Dynamic Memory
Dynamic Memory partitioning When a process enters in main memory, it is allocated exact
size that is required by that process. So in this method, partitions can vary in size depending on
memory space required by a process entering in main memory. Operating system maintains a
table indicating which parts of memory are available and which are occupied. When new
process arrives and it needs space, system searches for available memory space in main
memory. If it is available then memory is allocated to the process by creating a partition in
memory. Like this depending on size of process and available memory, partitions take place in
main memory.
Process of memory allocation:-
Total memory size is 64 M .From this 8 M partition is occupied by operating system and
remaining can be partitioned as per the size of processes
Advantages:
No internal fragmentation, More efficient use of main memory.
Disadvantages:
It suffers from external fragmentation, It needs compaction
Q3 Explain concept of page replacement with suitable diagram.
(Explanation-3Marks, Diagram- 3Marks) [Note:-explanation with any algorithm such as
FIFO,Optimal,LRU shall be considered]
ANS Basic Page Replacement:
Page replacement takes the following approach. If no frame is free, we find one that is not
currently being used and free it. We can free a frame by writing its contents to swap space
and changing the page table (and all other tables) to indicate that the page is no longer in
memory. We can now use the free frame to hold the page for which the process faulted.
We modify the page-fault service routine to include page replacement:
1. Find the location of the desired page on the disk.
2. Find a free frame:
a. If there is free frame, use it.
b. If there is no free frame, use a page-replacement algorithm to select a victim frame.
c. Write the victim frame to the dis; change the page and frame tables accordingly.
3. Read the desired page into the newly freed frame; change the page and frame
tables.
4. Restart the user process.
Notice that, if no frame 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
Q6 Explain any two file allocation methods with the help of diagram
ANS 1. Contiguous Allocation
The contiguous allocation method requires each file to occupy a set of contiguous
address on the disk. Disk addresses define a linear ordering on the disk. With this
ordering, accessing block b+1 after block b normally requires no head movement.
Contiguous allocation of a file is defined by the disk address and the length of the first
block. If the file is n blocks long, and starts at location b, then it occupies blocks b,
b+1, b+2, …, b+n-1. The directory entry for each file indicates the address of the
starting block and the length of the area allocated for this file.
Advantages of Contiguous File Allocation Method:
1. Supports both sequential and direct access methods.
2. Contiguous allocation is the best form of allocation for
sequential files. Multiple blocks can be brought in at a time to
improve I/O performance for sequential processing.
3. It is also easy to retrieve a single block from a file. For
example, if a file starts at block ‘n’ and the ith block of the file
is wanted, its location on secondary storage is simply n + i.
4. Reading all blocks belonging to each file is very fast.
5. Provides good performance.
Disadvantages of Contiguous File Allocation Method:
1. Suffers from external fragmentation.
2. Very difficult to find contiguous blocks of space for new files.
3. Also with pre-allocation, it is necessary to declare the size of
the file at the time of creation which many a times is difficult to
estimate.
4. Compaction may be required and it can be very expensive
2. linked Allocation:-
In this method, each file occupies disk blocks scattered anywhere on the
disk. It is a linked list of allocated blocks. When space has to be allocated to the file,
any free block can be used from the disk and system makes an entry in directory.
Directory entry for allocated file contains file name, a pointer to the first allocated
block and last allocated block of the file. The file pointer is initialized to nil value to
indicate empty file. A write to a file, causes search of free block. After getting free
block data is written to the file and that block is linked to the end of the file. To read
the file, read blocks by following the pointers from block to block starting with block
address specified in the directory entry.
For example, a file of five blocks starting with block 9 and continue with block
16,then block 1,then block 10 an finally block 25.each allocated block contains a
pointer to the next block
Total memory size is 64 M .From this 8 M partition is occupied by operating system and
remaining can be partitioned as per the size of processes.
Fig a: - the operating system is loaded in the memory. The entire remaining space is free.
Fig b: - process p 1 is loaded with 20 M memory size as space is available in memory. So
loading
process p1 create one partition in memory.
Fig c: - process p 2 is loaded with 14 M memory size as space is available in memory. So
loading
process p 2 creates one more partition in memory with 14 M size.
Fig d: - process p 3 is loaded with 18 M memory size as space is available in memory. So
loading
process p 3 creates one more partition in memory.
Fig e & f: - consider Process P 2 is currently blocked. After some time process P 4 with high
priority is ready for execution with memory of 8M.the existing free space is less than the
required
space. With priority scheduling, suppose P 2 is having less priority over P4 then system
performs
swapping of process P2 and process P 4.in this case, space occupied by process P2 is released
i.e.
14 M and P 4 occupies 8 M in that free space as shown in fig f.
Fig g: - Process P1 completes its job and then it releases its occupied space of 20 M.
Fig h:-Process P2 can be loaded again in the memory in the free partition released by process P
1.but P 2 requires only 20 M, so the free space of 20 M is divided into two partitions of 14 M
occupied by P2 and 6 M free space.
Q8 Consider the following page reference string-1,2,3,4,5,6,7,8,7,8,9,7,8,9,5,4,5. How many page
faults occur for FIFO replacement algorithm assuming 3 frames?
ANS
If you want to read a piece of data that is stored at the very end of the file, you have to read all
of the data that comes before it-you cannot jump directly to the desired data. This is similar to
the way cassette tape players work. If you want to listen to the last song on a cassette tape,
you have to either fast-forward over all of the songs that come before it or listen to them
Direct Access Method:
A file is made up of fixed-length logical records that allow
programs to read and write records rapidly in no particular order. Thus, we may read
block 14, then read block 53, and then write block 7. There are no restrictions on the
order of reading or writing for a direct-access file. The direct-access method is based on
a disk model of a file, since disks allow random access to any file block. Direct-access
files are of great use for immediate access to large amounts of information. Databases
are often of this type. For the direct-access method, the file operations must be modified
to include the block number as a parameter. The block number provided by the user to
the OS is normally a relative block number.
A relative block number is an index relative to the beginning of the file.
Thus, the first relative block of the file is 0, the next is 1, and so on, even though the
actual absolute disk address of the block may be 14703 for the first block and 3192
for the second. When you work with a direct access file (which is also known as a
random access file), you can jump directly to any piece of data in the file without
reading the data that comes before it. This is similar to the way a CD player or an
MP3 player works. You can jump directly to any song that you want to listen to.
Sequential access files are easy to work with, and you can use them to gain an
understanding of basic file operations.
2 Enlist different file allocation methods? Explain contiguous allocation method in detail
Ans From the user’s point of view, a file is an abstract data type. It can be created,
opened, written, read, closed and deleted without any real concern for its
implementation. The implementation of a file is a problem for the operating
system.
The main problem is how to allocate space to these files so that disk space is
effectively utilized and files can be quickly accessed.
Three major methods of allocating disk space are in wide use:
• Contiguous
• Linked
• Indexed
Contiguous Allocation
• The contiguous allocation method requires each file to occupy
a set of contiguous addresses on the disk. Disk addresses define
a linear ordering on the disk. Contiguous allocation of a file is
defined by the disk address of the first block and its length. If
the file is ‘n’ blocks long and starts at location ‘b’, then it
occupies blocks b, b+1, b+2, - - - - - b+n-1. The directory entry
for each file indicates the address of the starting block and the
length of the area allocated for this file.
• Contiguous allocation supports both sequential and direct
access.
• For direct access to block ‘i’ of a file, which starts at block ‘b’,
we can immediately access block b+i. The difficulty with
contiguous allocation is finding space for a new file.
• For direct access to block ‘i’ of a file, which starts at block ‘b’,
we can immediately access block b+i.
• The difficulty with contiguous allocation is finding space for a
new file.
• If file to be created are ‘n’ blocks long, we must search free
space list for ‘n’ free contiguous blocks.
Advantages of Contiguous File Allocation Method:
1. Supports both sequential and direct access methods.
2. Contiguous allocation is the best form of allocation for
sequential files. Multiple blocks can be brought in at a time to
improve I/O performance for sequential processing.
3. It is also easy to retrieve a single block from a file. For
example, if a file starts at block ‘n’ and the ith block of the file
is wanted, its location on secondary storage is simply n + i.
4. Reading all blocks belonging to each file is very fast.
5. Provides good performance.
Disadvantages of Contiguous File Allocation Method:
1. Suffers from external fragmentation.
2. Very difficult to find contiguous blocks of space for new files.
3. Also with pre-allocation, it is necessary to declare the size of
the file at the time of creation which many a times is difficult to
estimate.
4. Compaction may be required and it can be very expensive
3 List and explain any four attributes of file
Ans File attributes:
• Name: The symbolic file name is the only information kept in human readable form.
• Identifier: File system gives a unique tag or number that identifies file within file
system and which is used to refer files internally.
• Type: This information is needed for those systems that support different types.
• Location: This information is a pointer to a device and to the location of the file on
that device.
• Size: The current size of the file (in bytes, words or blocks) and possibly the maximum
allowed size are included in this attribute.
• Protection: Access control information determines that who can do reading, writing,
executing and so on.
• 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.
4 Explain any six file operations performed by OS
ANS Creating a file: Two steps are necessary to create a file. First space in the file system must be
found for the file. Second an entry for the new file must be made in the directory. The
directory entry records the name of the file and the location in the file system.
Writing a file: To write a file, we make a system call specifying 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 location of file then the write pointer must be updated
whenever a write occurs
Reading a file: To read from a file, we use a system call that specifies the name of the
file and where (in memory) the next block of the file should be put. System needs to
keep a read pointer to location in the file where the next read is to take place. Once the
read has taken place, the read pointer is updated.
Repositioning within a file: The directory is searched for the appropriate entry, and
the current file position is set to a given value. Repositioning within a file does not
need to involve any actual I/O. This file operation is also known as a file seeks.
Deleting a file: To delete a file, we search the directory for the named file. Having
found the associated directory entry, we release all file space and erase the directory
entry.
Truncating a file: Instead of deleting a file and then recreate it, this function allows all
attributes to remain unchanged but for the file to be reset to length zero. User wants to
erase the contents of the file.
Other common operations include appending new information to the end of an existing
file, and renaming an existing file.
5 Explain FIFO (First in First out) page replacement algorithm for reference string
7012030423103.
Ans First-In-First-Out (FIFO) Algorithm: A FIFO replacement associates with each page the time
when that page was bought into memory. When the page must be replaced, the oldest page is
chosen. It maintains a FIFO queue to hold all pages in memory. We replace the page at the
head of the queue. When a page is brought into the memory, we insert it at the tail of the
queue. Reference string: Consider three frames are available.
6