Skip to content

Instantly share code, notes, and snippets.

@knowbody
Created March 9, 2014 22:37
Show Gist options
  • Save knowbody/9456003 to your computer and use it in GitHub Desktop.
Save knowbody/9456003 to your computer and use it in GitHub Desktop.
Memory placement in OS: FirstFit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FirstFit
{
class Algo
{
static void Main(string[] args)
{
// enter amount of memory gaps and value of memory gap into each array
MGap[] mem = new MGap[4];
mem[0] = new MGap(5);
mem[1] = new MGap(15);
mem[2] = new MGap(30);
mem[3] = new MGap(10);
while (true)
{
Console.WriteLine("Enter the amount of memory: ");
int amount = int.Parse(Console.ReadLine());
Find(amount, mem);
}
}
static void Find(int mNeed, MGap[] mem)
{
for (int i = 0; i < mem.Length; i++)
{
if (mNeed <= mem[i].Size && mem[i].Available)
{
mem[i].Available = false;
Console.WriteLine("Found empty size {0} space at {1} position, wasted memory: {2}", mem[i].Size, i, (mem[i].Size - mNeed));
break;
}
}
}
}
class MGap
{
public bool Available { get; set; }
public int Size { get; set; }
public MGap(int size)
{
Size = size;
Available = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment