Professional Documents
Culture Documents
Lab7-Memory Management
Lab7-Memory Management
| Lab 7
Memory Management
• Its advantage is that it is the fastest search as it searches only the first block i.e. enough to
assign a process.
• It may have problems of not allowing processes to take space even if it was possible to
allocate.
C# imaplementation of First - Fit algorithm
using System;
class GFG
{
// Method to allocate memory to
// blocks as per First fit algorithm
static void firstFit(int[] blockSize, int m,
int[] processSize, int n)
{
// Stores block id of the block
// allocated to a process
int[] allocation = new int[n];
// Initially no block is assigned to any process
for (int i = 0; i < allocation.Length; i++)
allocation[i] = -1;
Page | 1
4thLevel | Operating System. | Lab 7
if (blockSize[j] >= processSize[i])
{
// allocate block j to p[i] process
allocation[i] = j;
break;
}
}
}
Console.WriteLine("\nProcess No.\tProcess Size\tBlock no.");
for (int i = 0; i < n; i++)
{
Console.Write(" " + (i + 1) + "\t\t" +
processSize[i] + "\t\t");
if (allocation[i] != -1)
Console.Write(allocation[i] + 1);
else
Console.Write("Not Allocated");
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
int[] blockSize = { 100, 500, 200, 300, 600 };
int[] processSize = { 212, 417, 112, 426 };
int m = blockSize.Length;
int n = processSize.Length;
Page | 2
4thLevel | Operating System. | Lab 7
Example:
Input:
blockSize[] = {100, 500, 200, 300, 600};
processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 4
2 417 2
3 112 3
4 426 5
Page | 3
4thLevel | Operating System. | Lab 7
bestIdx = j;
else if (blockSize[bestIdx]
> blockSize[j])
bestIdx = j;
}
}
// If we could find a block for
// current process
if (bestIdx != -1)
{
Console.WriteLine("\nProcess No.\tProcess"
+ " Size\tBlock no.");
for (int i = 0; i < n; i++)
{
Console.Write(" " + (i + 1) + "\t\t"
+ processSize[i] + "\t\t");
if (allocation[i] != -1)
Console.Write(allocation[i] + 1);
else
Console.Write("Not Allocated");
Console.WriteLine();
}
}
// Driver Method
public static void Main()
{
int[] blockSize = { 100, 500, 200, 300, 600 };
int[] processSize = { 212, 417, 112, 426 };
int m = blockSize.Length;
int n = processSize.Length;
Page | 4
4thLevel | Operating System. | Lab 7
Is Best-Fit really best?
Although, best fit minimizes the wastage space, it consumes a lot of processor time for
searching the block which is close to required size. Also, Best-fit may perform poorer than
other algorithms in some cases.
Page | 5
4thLevel | Operating System. | Lab 7
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}
Output:
******************
Page | 6