$R101OHL
$R101OHL
Definition:
Indexed file organization uses an index table to store the address of each record in a file.
Each entry in the index contains a key field and a pointer to the actual location of the record
in memory or disk.
This allows direct access to the records without searching the entire file sequentially.
Working:
• An index is created using a primary key (like student ID, product code, etc.).
• The index contains key values and their corresponding addresses in the file.
Key Address
1001 Block A
1002 Block B
1003 Block C
Data File:
• Block A → Record 1001
Advantages:
1. Faster Access: You don’t have to scan the entire file. Access is direct via index.
2. Efficient for Searching: Especially helpful for large files where linear search would be
slow.
3. Saves Time: Especially when data is accessed frequently using a key.
4. Random Access Supported: You can retrieve any record without scanning from the
beginning.
Disadvantages:
Definition:
Indexed Sequential File Organization combines sequential file organization and
indexing.
Records are stored in sorted order (usually based on a key), and an index is
maintained to allow direct access to records.
Working:
• Records are kept in sorted order in the data file.
• A separate index holds the key values and pointers to blocks of records.
Structure:
1000 Block A
1010 Block B
1020 Block C
Data File:
Advantages:
2. Faster Range Queries: Useful when searching for a set of records within a key range.
3. Maintains Order: Good for applications needing sorted data (e.g., bank records).
4. Efficient Searching: Better than pure sequential method for large files.
Disadvantages:
Block-level search +
Index Search Direct key match
sequential
5. Applications
• Indexed File:
o Library systems
o Employee databases
6. Conclusion
Both indexed file organization and indexed sequential file organization are powerful
ways to store and retrieve data efficiently.
5. Simple Example
Assume we have a hash function:
h(key) = key % 10
Now let’s say we are storing these student IDs:
101 → 101 % 10 = 1
102 → 102 % 10 = 2
113 → 113 % 10 = 3
121 → 121 % 10 = 1 (Collision!)
Table Representation:
Bucket Record
0
1 101, 121
2 102
3 113
... ...
5. Hash Function
A hash function maps a key to a location in memory (or disk).
Good hash functions should:
• Distribute records uniformly
• Minimize collisions
• Be simple and fast to compute
Common Hash Functions:
1. Modulus Function: h(k) = k % m
2. Folding Method: Break key into parts and add
3. Mid-Square Method: Square the key and extract middle digits
6. Collision in Hashing
A collision occurs when two different keys hash to the same address.
Example:
101 % 10 = 1
121 % 10 = 1
Both keys are mapped to the same bucket (1).
To solve collisions, we use collision resolution techniques.
12. Conclusion
Hash file organization is highly efficient for direct access to records using a hash
function.
While it excels in speed, particularly for single-record queries, it is not suitable for
sorted data or range queries.
Understanding and handling collisions effectively is key to maintaining performance.
In the above image on the left side, we have a memory diagram where we can see
the blocks of memory. At first, we have a text file named file1.txt which is
allocated using contiguous memory allocation, it starts with the memory
block 0 and has a length of 4 so it takes the 4 contiguous blocks 0,1,2,3. Similarly,
we have an image file and video file named sun.jpg and mov.mp4 respectively,
which you can see in the directory that they are stored in the contiguous
blocks. 5,6,7 and 9,10,11 respectively.
Here the directory has the entry of each file where it stores the address of the
starting block and the required space in terms of the block of memory.
Advantages
Disadvantages
In the above image on the right, we have a memory diagram where we can see
memory blocks. On the left side, we have a directory where we have the
information like the address of the first memory block and the last memory block.
In this allocation, the starting block given is 0 and the ending block is 15, therefore
the OS searches the empty blocks between 0 and 15 and stores the files in available
blocks, but along with that it also stores the pointer to the next block in the present
block. Hence it requires some extra space to store that link.
Advantages
Disadvantages
Disadvantages
In an operating system, there are different types of directory structures that help
organize and manage files efficiently.
Directories in an OS can be single-level, two-level, or hierarchical.
1) Single-Level Directory
The single-level directory is the simplest directory structure. In it, all files are
contained in the same directory which makes it easy to support and understand.
A single level directory has a significant limitation, however, when the number of files
increases or when the system has more than one user. Since all the files are in the
same directory, they must have a unique name. If two users call their dataset test,
then the unique name rule violated.
Advantages
• Since it is a single directory, so its implementation is very easy.
• If the files are smaller in size, searching will become faster.
• The operations like file creation, searching, deletion, updating are very easy in such a
directory structure.
• Logical Organization : Directory structures help to logically organize files and
directories in a hierarchical structure. This provides an easy way to navigate and
manage files, making it easier for users to access the data they need.
• Increased Efficiency: Directory structures can increase the efficiency of the file
system by reducing the time required to search for files. This is because directory
structures are optimized for fast file access, allowing users to quickly locate the file
they need.
• Improved Security : Directory structures can provide better security for files by
allowing access to be restricted at the directory level. This helps to prevent
unauthorized access to sensitive data and ensures that important files are protected.
• Facilitates Backup and Recovery : Directory structures make it easier to backup and
recover files in the event of a system failure or data loss. By storing related files in the
same directory, it is easier to locate and backup all the files that need to be
protected.
• Scalability: Directory structures are scalable, making it easy to add new directories
and files as needed. This helps to accommodate growth in the system and makes it
easier to manage large amounts of data.
Disadvantages
• There may chance of name collision because two files can have the same name.
• Searching will become time taking if the directory is large.
This can not group the same type of files together.
2) Two-Level Directory
As we have seen, a single level directory often leads to confusion of files names
among different users. The solution to this problem is to create a separate directory
for each user.
In the two-level directory structure, each user has their own user files directory
(UFD). The UFDs have similar structures, but each lists only the files of a single user.
System's master file directory (MFD) is searched whenever a new user id is created.
Advantages
• The main advantage is there can be more than two files with same name, and would
be very helpful if there are multiple users.
• A security would be there which would prevent user to access other user's files.
• Searching of the files becomes very easy in this directory structure.
Disadvantages
• As there is advantage of security, there is also disadvantage that the user cannot
share the file with the other users.
• Unlike the advantage users can create their own files, users don't have the ability to
create subdirectories.
• Scalability is not possible because one user can't group the same types of files
together.
3) Tree Structure/ Hierarchical Structure
Tree directory structure of operating system is most commonly used in our personal
computers. User can create files and subdirectories too, which was a disadvantage in
the previous directory structures.
This directory structure resembles a real tree upside down, where the root
directory is at the peak. This root contains all the directories for each user. The users
can create subdirectories and even store files in their directory.
A user do not have access to the root directory data and cannot modify it. And, even
in this directory the user do not have access to other user's directories. The
structure of tree directory is given below which shows how there are files and
subdirectories in each user's directory.