-
Notifications
You must be signed in to change notification settings - Fork 1
/
Best_Fit.py
72 lines (59 loc) · 2.46 KB
/
Best_Fit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Dimension = int(input("How many Partitions: "))
Amount_Process = int(input("How many Process: "))
Partition_Count = 0
Process_Count = 0
Memory_Partitions = []
Processes = []
Order = []
Can_Fit = []
Sorted_Can_Fit = []
for x in range(0, Dimension):
Partitions_Prompt = int(input("Enter Partition {}: ".format(Partition_Count + 1)))
Memory_Partitions.append(Partitions_Prompt)
Partition_Count += 1
print("\n")
for x in range(0, Amount_Process):
Processes_Prompt = int(input("Enter Process {}: ".format(Process_Count + 1)))
Processes.append(Processes_Prompt)
Process_Count += 1
print("\n")
counter = 0
checker = 0
Memory_Partitions_Copy=tuple(Memory_Partitions)
while len(Processes) != 0:
Len_Memory_Partitions_Holder = len(Memory_Partitions)
for x in range(0, Len_Memory_Partitions_Holder):
"""
In this for loop determining of best fit is done by subtracting each index of memory partitions to
the current process, if the result is negative(<0) then the process can not fit to
that allocation, only the positive(<=0) are collected and used as evaluation to determine
which partition the process is best fit to.
"""
Difference = int(Memory_Partitions[counter]) - int(Processes[0])
if Memory_Partitions[counter] - int(Processes[0]) >= 0:
Can_Fit.append(Memory_Partitions[counter])
counter += 1
#Current processs cannot fit into any memory block since the for loop above
#determined that there are no suiting allocation for the current
#process, therefore no allocation
if len(Can_Fit) == 0:
Order.append(str(Processes[0]) + " No Allocation")
Processes.pop(0)
#Process can fit to x amount of memory block, can fit
#Line 58: Sorting the difference list, first index would be the best fit for the process
#Line 59: Get the index/Partition Number of the best fit memory block
#Line 61: Process succesfully allocated, move on to next process
#Line 62: Block has been occupied, can no longer be used by next process
elif len(Can_Fit) >= 1:
Sorted_Can_Fit = sorted(Can_Fit)
best_fit_index = Memory_Partitions_Copy.index(Sorted_Can_Fit[0])
Order.append(str(Processes[0]) + ": Partition " + str(best_fit_index+1))
Processes.pop(0)
Memory_Partitions.pop(0)
#reinitializing evaluation
counter = 0
Can_Fit = []
Sorted_Can_Fit = []
checker += 1
for x in range(0, len(Order)):
print(Order[x])