OS Unit 3 Complete
OS Unit 3 Complete
MEMORY MANAGEMENT
Logical and Physical Address space
Swapping
Contiguous Allocation
Paging
Segmentation
VIRTUAL MEMORY
Introduction to Virtual Memory
Demand Paging
Page Replacement
Allocation of frames
Thrashing
FILE SYSTEM
File Concepts – Attributes, Operations and Types of Files
File System
Directory Structure
Protection
1
DIAGRAM LIST
1. Swapping
Operating system
(OS)
User process
Wasted memory
space
Mainmemory Mainmemory
Operating Operating
system system
Process 5
Process 1 Free
Process 2 Process 2
Process 3 Free
Process 4 Process 4
wasted Free
2
4. Paging table architecture
5. Virtual Memory
3
6. Demand paging
4
Chapter - 1
MEMORY MANAGEMENT
Memory is central to the operation of a modern computer system.
Memory is a large array of words or bytes, each with its own address. The
CPU fetches instructions from memory according to the value of the
program counter. These instructions may cause additional loading from
and storing to specific memory addresses.
3. After the instruction has been executed on the operands, results may
be stored back in memory.
5
Instructions and data to memory addresses can be done in following ways
Dynamic Loading
Dynamic Linking
Virtual and physical addresses are the same in compile-time and load-
time address-binding schemes. Virtual and physical addresses differ in
execution-time address-binding scheme.
Swapping
7
Contiguous memory allocation:
8
Operating system 0 Wasted memory
(OS) space
User programs
Wasted memory Operating System
space (OS)
640K
Working:
9
Multiple – partition allocation (partitioned memory management or
partitioned memory allocation scheme)
Working:
In this case number of partitions at any given time dictate the degree
of multiprogramming. Thus if one or more partitions are free, processes
will be selected from an input ready queue and are loaded in to vacant
partitions on one-process, per-partition basis. When a process terminates its
execution successfully, the corresponding partition becomes free for loading
another process by overwriting.
Process 5
Process 1 Free
Process 2 Process 2
Process 3 Free
Process 4 Process 4
Free
wasted
10
sizes (big, small and moderate sizes) and are scattered across memory
at any given time.
Once a process arrives with a memory request, then OS scans
through this set of memory holes, and it will attempt to find a hole
that is large enough (appropriate size) to accommodate the process.
Suppose a hole is too much large in size, it can be halved or bisected
into two partitions – one hole of appropriate size is allocated for
loading the requested process (arrived process) and the remaining part
is returned to pool of unused memory holes.
Similarly once a process terminates, the occupied memory block will
be released automatically and is then returned to pool of free (unused)
memory holes.
Suppose when a memory block is released such that it is adjacent to
another free (vacant) hole, then they will be merged together to form a
higher size memory hole.
This kind of memory allocation procedure reflect a particular instance
of the general dynamic storage allocation problem. The problem
highlights on how to fulfill a memory request of size ‘n’ from a given
list of free memory holes.
For which the set of free holes can be searched to locate as to which
hole is the best one (or more appropriate one in size) to allocate as per
the popular strategies as the First-fit, best-fit and worst-fit.
First Fit: The first hole that is big enough is allocated to program.
Best Fit: The smallest hole that is big enough is allocated to program.
Worst Fit: The largest hole that is big enough is allocated to program.
11
partitions where each partition should
contain only one process. When a partition is
free, a process is selected from the input
queue and is loaded into the free partition.
When the process terminates, the partition
becomes available for another process
Fragmentation:
As processes are loaded and removed from memory, the free memory
space is broken into little pieces. It happens after sometimes that processes
cannot be allocated to memory blocks considering their small size and
memory blocks remains unused. This problem is known as Fragmentation.
Fragmentation is of two types
Paging:
Page number (p) -- page number is used as an index into a page table
which contains base address of each page in physical memory.
12
Page offset (d) -- page offset is combined with base address to define
the physical memory address.
Segmentation
Unlike paging, segments are having varying sizes and thus eliminates
internal fragmentation. External fragmentation still exists but to lesser
extent.
13
Segment offset (o) -- segment offset is first checked against limit and
then is combined with base address to define the physical memory
address.
g p
s
Segment number Deals with protection
Indicate GDT\LDT
14
****************************** End of Chapter 1 ******************************
15
Chapter - 2
Virtual memory
Memory is hardware that your computer uses to load the operating
system and run programs. It consists of one or more RAM chips that each
have several memory modules. The amount of real memory in a computer is
limited to the amount of RAM installed. Common memory sizes are 256MB,
512MB, and 1GB.
The process of mapping data back and forth between the hard drive
and the RAM takes longer than accessing it directly from the memory. This
means that the more virtual memory is used, the more it will slow your
computer down.
16
Following are the situations, when entire program is not required to be
loaded fully in main memory.
User written error handling routines are used only when an error
occurred in the data or computation.
Certain options and features of a program may be used rarely.
Many tables are assigned a fixed amount of address space even
though only a small amount of the table is actually used.
The ability to execute a program that is only partially in memory
would counter many benefits.
Less number of I/O would be needed to load or swap each user
program into memory.
A program would no longer be constrained by the amount of physical
memory that is available.
Each user program could take less physical memory, more programs
could be run the same time, with a corresponding increase in CPU
utilization and throughput.
Demand Paging
Access to a page marked invalid causes a page-fault trap. This trap is the
result of the operating system's failure to bring the desired page into
memory. But page fault can be handled as following
18
Step Description
2 If the reference was invalid, terminate the process. If it was valid, but
page have not yet brought in, page in the latter.
4 Schedule a disk operation to read the desired page into the newly
allocated frame
5 When the disk read is complete, modify the internal table kept with
the process and the page table to indicate that the page is now in
memory
19
Advantages
Disadvantages
PAGE REPLACEMENT
However, if all page frames are full for a given fixed size main memory
and a page fault occurs demanding a page to be brought into main memory
then page replacement policy or algorithm has to determine as to which
page (page frame) from physical main memory must be removed (or swapped
–out to hard disk). Therefore, page replacement is fundamental to demand
paging. Although we have a wide variety of page-replacement algorithms, the
one with good paging performance and that yields the lowest page –fault
rate will be preferred.
20
1. First In First Out page replacement algorithm
The oldest page is at the head of queue (First –in) and most
recently arrived page is at the tail of the queue.
Drawback:
Thus FIFO kind of page replacement will increase the page-fault rate
and slow-down process execution.
21
Limitations of this algorithm:
This algorithm can be used for simulating and evaluating other new
algorithms for their comparative studies.
Working:
Advantages:
Limitations:
22
Allocation of Frames:
1. You cannot assign more frames than the total number of frames
available.
2. A specific number of frames should be assigned to each process. This
limitation is due to two factors. The first is that when the number of
frames assigned drops, the page fault ratio grows, decreasing the
process's execution performance. Second, there should be sufficient
frames to hold all the multiple pages that any instruction may
reference.
There are mainly five ways of frame allocation algorithms in the OS. These
are as follows:
23
assigned to any system process may be used as a free-frame buffer pool in
the system.
Disadvantage
Disadvantage
Advantages
24
Disadvantages
The process itself may not solely control the page fault ratio of a
process. The paging behavior of other processes also influences the number
of pages in memory for a process.
Advantages
Disadvantages
Thrashing:
o Page fault: We know every program is divided into some pages. A page
fault occurs when a program attempts to access data or code in its
address space but is not currently located in the system RAM.
o Swapping: Whenever a page fault happens, the operating system will
try to fetch that page from secondary memory and try to swap it with
one of the pages in RAM. This process is called swapping.
Thrashing is when the page fault and swapping happens very frequently
at a higher rate, and then the operating system has to spend more time
swapping these pages. This state in the operating system is known as
thrashing. Because of thrashing, the CPU utilization is going to be reduced
or negligible.
The long-term scheduler would then try to improve the CPU utilization
by loading some more processes into the memory, thereby increasing the
degree of multiprogramming. Unfortunately, this would result in a further
decrease in the CPU utilization, triggering a chained reaction of higher page
faults followed by an increase in the degree of multiprogramming, called
thrashing.
Since global page replacement can bring any page, it tries to bring
more pages whenever thrashing is found. But what actually will happen is
that no process gets enough frames, and as a result, the thrashing will
increase more and more. Therefore, the global page replacement algorithm is
not suitable when thrashing happens.
27
Chapter – 3
File System
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.
File Concept:
Computers can store information on various storage media, such as
magnetic disks, magnetic tapes and optical disks. So that the computer
system will be convenient to use, the OS provides a uniform logical view of
stored information. The OS abstracts from the physical properties of its
storage devices to define a logical storage unit, the file. Files are mapped by
the OS onto physical devices. These storage devices are usually non-
volatile, so the contents are persistent between system reboots.
28
File Attributes
A file is named, for the convenience of its human users, and is
referred to by its name. a name is usually a string of characters such as
example.c. Some systems differentiate between uppercase and lowercase
characters in names, whereas other systems do not. When a file is named, it
becomes independent of the process, the user and even the system that
created it.
Name – The symbolic file name is the only information kept in human
readable form.
Size – The current size of the file (in bytes, words or blocks) and
possibly the maximum allowed size are included in this attribute.
29
File Operations
A file is an abstract data type. To define a file properly, we need to
consider the operations that can be performed on files. The OS can provide
system calls to create, write, read, reposition, delete and truncate files.
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.
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 files 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.
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. Again, the directory is searched for the associated entry and the system
needs to keep a read pointer to the location in the file where the next read
is to take place. Once a read has taken place, the read pointer is updated.
Because a process is usually either reading from or writing to a file, the
current operation location can be kept as a per-process current file
position pointer. Both the read and write operations use this same pointer,
saving space and reducing system complexity.
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, 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 the file length – but lets the file be reset to length zero and its file space
released.
These six basic operations comprise the minimal set of required file
operations. Other common operations include appending new information
to the end of an existing file and renaming an existing file.
30
Types of Files
A common technique for implementing file types is to include the type
as part of the file name. The name is split into two parts – a name and an
extension, usually separated by a period. In this way, the user and the OS
can tell from the name alone what the type of a file ie., Most OS allow users
to specify a file name as a sequence of characters followed by a period and
terminated by an extension made up of additional characters. Eg:
mysore.doc, factorial.c, fibo.java etc.,
Multimedia mpeg, mov, mp3, mp4, avi Binary file containing audio
or A/V information
31
File System
An integral part of an OS is what is called a File system. A file system
is a data structure that stores data and information on storage devices (hard
drives, floppy disk etc) making them easily retrievable. Different OS’s use
different file systems, but all have similar features.
1. FAT (File Allocation Table): An older file system used by older versions
of Windows and other OS
5. APFS (Apple File System): A new file system introduced by Apple for
their Macs and OS devices.
32
Disadvantages of using a file system:
b) Disk space overhead – File systems may use some disk space to store
metadata and other overhead information, reducing the amount of
space available for user data.
d) Sequential Access:
e) Direct Access:
33
Direct access files are of great use for immediate access to
large amount of information. Databases are often of this
type. When a query concerning a particular subject
arrives, we compute which block contains the answer and
then read that block directly to provide the desired
information.
With large files, the index file itself may become too large to be kept in
memory. The solution is to create an index for the index file. The primary
index file contains pointers to secondary index files, which point to the
actual data items.
Directory Structure
The directory can be viewed as a symbol table that translates file
names into their directory entries. If we take such a view, we see that the
directory itself can be organized in many ways. The organization must allow
us to insert entries, to delete entries, to search for a named entry and to
list all the entries in the directory.
2. Create a file: New files need to be created and added to the directory
34
3. Delete a file: When a file is no longer needed, we want to be able to
remove it from the directory.
6. Traverse the file system: We may wish to access every directory and
every file within a directory structure. For reliability it is a good idea to
save file contents and structure of the entire file system at regular
intervals. Often, we do this by copying all files to magnetic tape. This
technique provides a backup copy in case of system failure. In
addition, if a file is no longer in use, the file can be copied to tape and
the disk space of that file released for reuse by another file.
35
Two – Level directory:
In the two-level directory structure, each user has his own User File
Directory (UFD). The UFD’s have similar structure but each lists only the
files of a single user. When a user job starts or a user logs in, the systems
Master File Directory (MFD) is searched. The MFD is indexed by user name
on account number and each entry points to the UFD for that user.
When a user refers to a particular file, only his own UFD is searched.
Thus, different users may have files with the same name, as long as all the
file names within each UFD are unique. To create a file for a user, the OS
searches only that users UFD to ascertain whether another file of that name
exists. To delete a file, the OS confines its search to the local UFD, thus it
cannot accidentally delete another users file that has the same name.
Thus, a user name and a file name define a path name. Every file in
the system has a path name. to name, a file uniquely a user must know the
path name of the file desired.
MFD
UFD
36
Other directories include: Tree-structured directories, Acyclic – Graph
directories, General Graph directory etc.,
Protection
When information is stored in a computer system, we want to keep it
safe from physical damage (the issue of reliability) and improper access.
Types of Access:
Delete: Delete the file and free its space for possible reuse
Other operations such as renaming, copying and editing the file, may also
be controlled.
37
File System Implementation:
Disks provide most of the secondary storage on which file systems are
maintained. Two characteristics make them convenient for this purpose:
To improve I/O efficiency, I/O transfers between memory and disk are
performed in units of blocks. Each block has one or more sectors.
Depending on the disk drive, sector size varies from 32 bytes to 4,096
bytes; the usual size is 512 bytes.
38
3. Data Retrieval: The file system implementation determines how the
data is read from and written to the physical storage device. This
includes strategies such as buffering and caching to optimize file I/O
performance.
Application programs
I/O control
Devices
39
I/O Control level – Device drivers acts as interface between devices
and OS, they help to transfer data between disk and main memory. It
takes block number as input and as output it gives low level hardware
specific instruction.
Advantages:
2. Each file system can have its own logical file system.
40
7. Improved Performance: File system implementation includes
techniques such as buffering and caching to optimize file I/O
performance. This results in faster access to data and improved overall
system performance.
Disadvantages:
10. Hash Table – The hash table takes a value computed from the file
name and returns a pointer to the file. It decreases the directory search
time. The insertion and deletion process of files is easy. The major
difficulty is hash tables are its generally fixed size and hash tables are
dependent on hash function on that size.
42
methods for allocating storage space, including contiguous, linked, and
indexed allocation.
4. File operations: The file system provides a set of operations that can
be performed on files and directories, including create, delete, read,
write, open, close, and seek. These operations are implemented using
the file system structures and the storage allocation methods.
Allocation Methods
In almost every case, many files are stored on the same disk. The
main problem is how to allocate space to these files so that disk space
is utilized effectively and files can be accessed quickly. Three major
methods of allocating disk space are in wide use.
1. Contiguous allocation
2. Linked allocation
3. Indexed allocation
1. Contiguous Allocation
2. Linked Allocation:
44
There is no external fragmentation with linked allocation.
A file can continue to grow as long as free blocks are available.
Consequently, it is never necessary to compact disk space.
45
3. Indexed Allocation:
46
Free Space Management
Free space management is a critical aspect of operating systems as it
involves managing the available storage space on the hard disk or other
secondary storage devices. The operating system uses various techniques
to manage free space and optimize the use of storage devices. Here are
some of the commonly used free space management techniques:
1. Linked Allocation: In this technique, each file is represented by a
linked list of disk blocks. When a file is created, the operating system
finds enough free space on the disk and links the blocks of the file to
form a chain. This method is simple to implement but can lead to
fragmentation and wastage of space.
47
3. Indexed Allocation: In this technique, a separate index block is
used to store the addresses of all the disk blocks that make up a file.
When a file is created, the operating system creates an index block and
stores the addresses of all the blocks in the file. This method is efficient
in terms of storage space and minimizes fragmentation.
48
blocks are allocated can be represented by a bitmap of 16 bits
as: 0000111000000110.
2. Advantages –
Simple to understand.
Finding the first free block is efficient. It requires scanning the
words (a group of 8 bits) in a bitmap for a non-zero word. (A 0-valued
word has all bits 0). The first free block is then found by scanning for
the first 1 bit in the non-zero word.
3. Linked List – In this approach, the free disk blocks are linked
together i.e. a free block contains a pointer to the next free block. The
block number of the very first disk block is stored at a separate location
on disk and is also cached in
49
memory. In Figure-2, the free
space list head points to Block 5 which points to Block 6, the next free
block and so on. The last free block would contain a null pointer
indicating the end of free list. A drawback of this method is the I/O
required for free space list traversal.
5. Counting – This approach stores the address of the first free disk
block and a number n of free contiguous disk blocks that follow the first
block. Every entry in the list would contain:
1. Address of first free disk block
2. A number n
50
Advantages:
Disadvantages:
51