Wrote priority queue for processes

This commit is contained in:
2020-08-01 16:12:55 -05:00
parent fec2817883
commit c75ca1999e
3 changed files with 120 additions and 0 deletions

23
src/process.hpp Normal file
View File

@@ -0,0 +1,23 @@
#ifndef PROCESS_H
#define PROCESS_H
#include <stddef.h>
namespace kernel
{
class Process
{
public:
Process();
size_t priority;
void* stack;
};
};
#endif

62
src/processqueue.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include "processqueue.hpp"
kernel::ProcessQueue::ProcessQueue(Process** array)
{
m_array = array;
m_size = 0;
}
kernel::Process* kernel::ProcessQueue::extractMin()
{
if(m_size == 0)
return NULL;
m_size--;
Process* p = m_array[0];
m_array[0] = m_array[m_size];
heapify(0);
}
void kernel::ProcessQueue::insert(Process* n)
{
size_t i = m_size;
m_size++;
for(; i > 0 && m_array[(i - 1) / 2]->priority > n->priority; i = (i - 1) / 2)
{
m_array[i] = m_array[(i - 1) / 2];
}
m_array[i] = n;
}
void kernel::ProcessQueue::remove(Process* n)
{
for(size_t i = 0; i < m_size; i++)
{
if(m_array[i] == n)
{
m_size--;
m_array[i] = m_array[m_size];
heapify(i);
break;
}
}
}
void kernel::ProcessQueue::heapify(size_t i)
{
if(i * 2 + 1 >= m_size)
return;
if (m_array[i]->priority >= m_array[i * 2 + 1]->priority && m_array[i * 2 + 1]->priority <= m_array[i * 2 + 2]->priority)
{
Process* buffer = m_array[i];
m_array[i] = m_array[i * 2 + 1];
m_array[i * 2 + 1] = buffer;
heapify(i * 2 + 1);
}
else if (m_array[i]->priority >= m_array[i * 2 + 2]->priority && m_array[i * 2 + 2]->priority <= m_array[i * 2 + 1]->priority)
{
Process* buffer = m_array[i];
m_array[i] = m_array[i * 2 + 2];
m_array[i * 2 + 2] = buffer;
heapify(i * 2 + 2);
}
}

35
src/processqueue.hpp Normal file
View File

@@ -0,0 +1,35 @@
#ifndef HEAP_H
#define HEAP_H
#include <stddef.h>
#include "process.hpp"
namespace kernel
{
class ProcessQueue
{
public:
ProcessQueue(Process** array);
Process* extractMin();
void insert(Process* n);
void remove(Process* n);
private:
void heapify(size_t index);
Process** m_array;
size_t m_size;
};
}
#endif