Last active
April 3, 2025 08:56
-
-
Save icebarf/8815aaed5bcca117518369c6428d7cb3 to your computer and use it in GitHub Desktop.
Various Process scheduling algorithms asked to be implemented in OS Lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* FCFS Example Program by Amritpal Singh is marked with CC0 1.0 Universal. | |
* To view a copy of this license, visit | |
* https://creativecommons.org/publicdomain/zero/1.0/ | |
* Copy of Program at: | |
* https://gist.github.com/icebarf/8815aaed5bcca117518369c6428d7cb3/#file-fcfs_impl_from_class-cpp | |
*/ | |
#include <cstdint> | |
#include <cstdio> | |
// Registers on amd64 architecture | |
struct Registers { | |
uint64_t rax; | |
uint64_t rbx; | |
uint64_t rcx; | |
uint64_t rdx; | |
uint64_t rdi; | |
uint64_t rsi; | |
uint64_t rsp; | |
uint64_t rbp; | |
uint64_t r8; | |
uint64_t r9; | |
uint64_t r10; | |
uint64_t r11; | |
uint64_t r12; | |
uint64_t r13; | |
uint64_t r14; | |
uint64_t r15; | |
}; | |
// Process Control Block (minimal version) | |
struct Process { | |
int id; | |
enum State { Ready, Running, Waiting, Terminated } state; | |
uint64_t program_counter; | |
Registers ctx; | |
}; | |
constexpr int MAX_PROCESSES = 5; | |
Process queue[MAX_PROCESSES]; | |
int head = 0; | |
int tail = 0; | |
int enqueue(Process p) { | |
if ((tail + 1) % MAX_PROCESSES == head) { | |
printf("Attempting to overwrite processs data at idx %d\n", | |
(tail + 1) % MAX_PROCESSES); | |
printf ("Prcess id attempting to overwrite: %d\n", p.id); | |
return -1; | |
} | |
queue[tail] = p; | |
tail = (tail + 1) % MAX_PROCESSES; | |
return 0; | |
} | |
int dequeue(Process* p) { | |
if (head == tail) { | |
printf("Empty buffer\n"); | |
return -1; | |
} | |
*p = queue[head]; | |
head = (head + 1) % MAX_PROCESSES; | |
return 0; | |
} | |
void run_process(Process p) { | |
// actual operating system operating | |
// since this is an example | |
// i am not doing anything | |
} | |
void fcfs(Process pwaiting[], int len) { | |
enqueue_all: | |
for (int i = 0; i < MAX_PROCESSES - 1; i++) { | |
if (enqueue(pwaiting[i]) != 0) { | |
printf("Failed to queue processes\n"); | |
return; | |
}; | |
} | |
for (int i = 0; i < MAX_PROCESSES - 1; i++) { | |
Process p; | |
if (dequeue(&p) != 0) { | |
printf("Failed to dequeue process\n"); | |
return; | |
} | |
printf ("Process Dequeued with pid: %d\n", p.id); | |
run_process(p); | |
} | |
// enqueue and execute any remaining processes | |
if (len > MAX_PROCESSES) { | |
pwaiting = pwaiting + MAX_PROCESSES; | |
len = len - MAX_PROCESSES; | |
goto enqueue_all; | |
} | |
} | |
int main() { | |
Process plist[] = {{1, Process::State::Ready, 0x00f1006, {0}}, | |
{2, Process::State::Ready, 0x00f1f24, {3}}, | |
{3, Process::State::Ready, 0x00f1104, {1234}}, | |
{4, Process::State::Ready, 0x00f103a, {5346}}, | |
{5, Process::State::Ready, 0x00f1025, {23452}}, | |
{6, Process::State::Ready, 0x00f1003, {0}}, | |
{7, Process::State::Ready, 0x00f1035, {235}}, | |
{8, Process::State::Ready, 0x00f1012, {0}}}; | |
int len = sizeof(plist) / sizeof(plist[0]); | |
// run fcfs | |
fcfs(plist, len); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment