Skip to content

Instantly share code, notes, and snippets.

@knowbody
Created March 9, 2014 22:37
Show Gist options
  • Save knowbody/9456014 to your computer and use it in GitHub Desktop.
Save knowbody/9456014 to your computer and use it in GitHub Desktop.
Memory placement in OS: NextFit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NextFit
{
class Algo
{
static int previousFit = 0;
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)
{
if(previousFit==mem.Length-1)
previousFit=0;
for (int i = previousFit; i < mem.Length; i++)
{
if (mem[i].Size >= mNeed && 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));
previousFit = i;
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