0% found this document useful (0 votes)
11 views

Program For Storage Allocatio1

The document describes three algorithms - first fit, best fit, and worst fit - for allocating processes of various sizes to blocks of memory. First fit allocates processes to the first block that fits, best fit allocates to the smallest fitting block, and worst fit allocates to the largest fitting block. The algorithms are implemented as functions that take block sizes, process sizes, and other parameters and return an allocation list and output.

Uploaded by

utsavarora1912
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Program For Storage Allocatio1

The document describes three algorithms - first fit, best fit, and worst fit - for allocating processes of various sizes to blocks of memory. First fit allocates processes to the first block that fits, best fit allocates to the smallest fitting block, and worst fit allocates to the largest fitting block. The algorithms are implemented as functions that take block sizes, process sizes, and other parameters and return an allocation list and output.

Uploaded by

utsavarora1912
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM FOR STORAGE ALLOCATION

FIRST FIT

def firstFit(blockSize, m, processSize, n):

allocation = [-1] * n

for i in range(n):

for j in range(m):

if blockSize[j] >= processSize[i]:

allocation[i] = j

blockSize[j] -= processSize[i]

break

print(" Process No. Process Size Block no.")

for i in range(n):

print(" ", i + 1, " ", processSize[i],

" ", end = " ")

if allocation[i] != -1:

print(allocation[i] + 1)

else:

print("Not Allocated")

if __name__ == '__main__':

blockSize = [100, 500, 200, 300, 600]

processSize = [212, 417, 112, 426]


m = len(blockSize)

n = len(processSize)

firstFit(blockSize, m, processSize, n)

OUTPUT:

Process No. Process Size Block no.

1 212 2

2 417 5

3 112 2

4 426 Not Allocated

BEST FIT

def bestFit(blockSize, m, processSize, n):

allocation = [-1] * n

for i in range(n):

bestIdx = -1

for j in range(m):

if blockSize[j] >= processSize[i]:

if bestIdx == -1:

bestIdx = j

elif blockSize[bestIdx] > blockSize[j]:

bestIdx = j
if bestIdx != -1:

allocation[i] = bestIdx

blockSize[bestIdx] -= processSize[i]

print("Process No. Process Size Block no.")

for i in range(n):

print(i + 1, " ", processSize[i],

end = " ")

if allocation[i] != -1:

print(allocation[i] + 1)

else:

print("Not Allocated")

if __name__ == '__main__':

blockSize = [100, 500, 200, 300, 600]

processSize = [212, 417, 112, 426]

m = len(blockSize)

n = len(processSize)

bestFit(blockSize, m, processSize, n)
OUTPUT

Process No. Process Size Block no.

1 212 4

2 417 2

3 112 3

4 426 5

WORST FIT

def worstFit(blockSize, m, processSize, n):

allocation = [-1] * n

for i in range(n):

wstIdx = -1

for j in range(m):

if blockSize[j] >= processSize[i]:

if wstIdx == -1:

wstIdx = j

elif blockSize[wstIdx] < blockSize[j]:

wstIdx = j

if wstIdx != -1:

allocation[i] = wstIdx

blockSize[wstIdx] -= processSize[i]

print("Process No. Process Size Block no.")

for i in range(n):
print(i + 1, " ",

processSize[i], end = " ")

if allocation[i] != -1:

print(allocation[i] + 1)

else:

print("Not Allocated")

if __name__ == '__main__':

blockSize = [100, 500, 200, 300, 600]

processSize = [212, 417, 112, 426]

m = len(blockSize)

n = len(processSize)

worstFit(blockSize, m, processSize, n)

OUTPUT:

Process No. Process Size Block no.

1 212 5

2 417 2

3 112 5

4 426 Not Allocated

You might also like