You are on page 1of 10

Ex No: 14 Reg No: RA2112703010017

Date: 04. 11. 2022 Name: Thamizhiniyan C S

File Allocation
Sequence and Indexed File Allocation

Aim:
To simulate the Sequence and Indexed file Allocation Algorithm.

Description of the exercise:


Sequence File Allocation Algorithm:
In this allocation strategy, each file occupies a set of contiguously blocks on
the disk. This strategy is best suited. For sequential files. The file allocation table
consists of a single entry for each file. It shows the filenames, staring block of the file
and size of the file. The main problem of this strategy is, it is difficult to find the
contiguous free blocks in the disk and some free blocks could happen between two
files.

Indexed File Allocation Algorithm:


Indexed allocation supports both sequential and direct access files. Te
file indexes are not physically stored as a part of the file allocation table. Whenever
the file size increases, we can easily add some more blocks to the index. In this
strategy, the file allocation table contains a single entry for each file. The entry
consisting of one index block, the index blocks having the pointers to the other
blocks. No external fragmentation.

Algorithm:

Sequential File Allocation:

Step 1: Start the program.


Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4:
A) Allocate the required locations to each in sequential order.
B) Check whether the required locations are free from the selected
location.
Step 5: Print the results fileno, length, Blocks allocated.
Step 6: Stop the program

Indexed File Allocation:


Program (Sequential File Allocation):

# Initialising an Objects list


objects = []

# Defining a class Sequence


class File(object):
# Defining a constructor
def __init__(self, name, size, starting_memory_block):
self.name = name # Class Members
self.size = size # Class Members
self.starting_memory_block = starting_memory_block # Class Members
self.ending_memory_block = self.starting_memory_block + self.size # Class Members

# Function for Creating a File


def create(variable):
switch = True # Temporary Variable
switch_1 = True # Temporary Variable
name = None # Initialising an empty variable
starting_allocated_location = None # Initialising an empty variable

while switch:
name = input("Enter the File Name : ")
for i in range(len(objects)):
# Checking whether the entered file name already exists
if name == objects[i].name:
print("File name already exists, please enter another name")
switch_1 = False
break
switch_1 = True

if switch_1:
switch = False

switch_2 = True # Temporary Variable


switch_3 = True # Temporary Variable

while switch_2:
starting_allocated_location = int(input("Enter the Starting Memory Location : "))
for i in range(len(objects)):
# Checking whether the entered file name already exists
if objects[i].starting_memory_block <= starting_allocated_location <=
objects[i].ending_memory_block:
print("The entered memory location is already allocated, please enter another
memory location")
switch_3 = False
break
switch_3 = True

if switch_3:
switch_2 = False

try:
size = int(input("Enter the size : "))
except ValueError:
print("Enter a Valid Size")
size = int(input("Enter the size : "))

objects.append(File(
name,
size,
starting_allocated_location
))
variable += 1

# Function for Deleting a File


def delete(variable):
variable -= 1
objects.remove(objects[variable])

# Function for Displaying all the files


def display():
print("".center(70, '-'))
print("|", "File Name".center(20), "|", "Size".center(20), "|", "Allocated
Memory".center(20), "|", end="\n")
print("".center(70, '-'))
for obj in objects:
print("".center(70, '-'))
print(
"|",
f"{obj.name} ".center(20),
"|",
f"{obj.size} MB".center(20),
"|",
f"{obj.starting_memory_block} - {obj.ending_memory_block}".center(20),
"|"
)
print("".center(70, '-'))

# Main Function
def main():
variable = 0
while True:
print("""\nSequential File Allocation
1. Creation
2. Delete
3. Display
4. Exit
""")
try:
option = int(input("Enter the Option : "))

except ValueError:
print("Enter a Valid Option")
option = int(input("Enter the Option : "))

if option == 1:
create(variable)
elif option == 2:
delete(variable)
elif option == 3:
display()
elif option == 4:
exit()
else:
print("Wrong Option")

if __name__ == "__main__":
main()
Output (Sequential File Allocation):
Program (Indexed File Allocation):

# Initialising an Objects list


objects = []

# Defining a class Sequence


class File(object):
# Defining a constructor
def __init__(self, name, size):
self.name = name # Class Members
self.size = size # Class Members
self.allocated = []

for i in range(10):
self.allocated.append(self.size / 10)

# Function for Creating a File


def create(variable):
switch = True # Temporary Variable
switch_1 = True # Temporary Variable
name = None # Initialising an empty variable
while switch:
name = input("Enter the File Name : ")
for i in range(len(objects)):
# Checking whether the entered file name already exists
if name == objects[i].name:
print("File name already exists, please enter another name")
switch_1 = False
break
switch_1 = True

if switch_1:
switch = False

try:
size = int(input("Enter the size : "))
except ValueError:
print("Enter a Valid Size")
size = int(input("Enter the size : "))

objects.append(File(
name,
size
))
variable += 1

# Function for Deleting a File


def delete(variable):
variable -= 1
objects.remove(objects[variable])

# Function for Displaying all the files


def display():
print("".center(100, '-'))
print(
"|",
"File Name".center(20),
"|",
"Size".center(20),
"|",
"Pointer".center(20),
"|",
"Allocated Memory Address".center(27),
"|",
end="\n"
)
print("".center(100, '-'))
for obj in objects:
print("".center(100, '-'))
print(
"|",
f"{obj.name} ".center(20),
"|",
f"{obj.size} MB".center(20),
"|",
f"{id(obj.allocated)}".center(20),
"|",
f"{id(obj.allocated[0])}".center(27),
"|"
)
for i in range(1, len(obj.allocated)):
print(
"|",
"".center(20),
"|",
"".center(20),
"|",
"".center(20),
"|",
end=''
)
print(f" {id(obj.allocated[i])}".center(28), "|")
print("".center(100, '-'))

# Main Function
def main():
variable = 0
while True:
print("""\nIndexed File Allocation
1. Creation
2. Delete
3. Display
4. Exit
""")
try:
option = int(input("Enter the Option : "))

except ValueError:
print("Enter a Valid Option")
option = int(input("Enter the Option : "))

if option == 1:
create(variable)
elif option == 2:
delete(variable)
elif option == 3:
display()
elif option == 4:
exit()
else:
print("Wrong Option")

if __name__ == "__main__":
main()

Output (Indexed File Allocation):


Result:
Hence, the Sequence and Indexed file Allocation Algorithms is successfully
simulated.

You might also like