Skip to content

Instantly share code, notes, and snippets.

@knowbody
Created March 9, 2014 22:36
Show Gist options
  • Save knowbody/9455990 to your computer and use it in GitHub Desktop.
Save knowbody/9455990 to your computer and use it in GitHub Desktop.
Memory placement in OS: WorstFit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorstFit
{
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: ");
uint amount = uint.Parse(Console.ReadLine());
Find(amount, mem);
}
}
static void Find(uint mNeed, MGap[] mem)
{
uint diff = (uint)mem[0].Size - mNeed;
uint pos = 0;
for (uint i = 0; i < mem.Length; i++)
{
if (mem[i].Size - mNeed >= diff && mem[i].Available)
{
diff = (uint)mem[i].Size - mNeed;
pos = i;
}
}
mem[pos].Available = false;
Console.WriteLine("Found empty size {0} space at {1} position, wasted memory: {2}", mem[pos].Size, pos, diff);
}
}
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