/os-kernel-c

A multi-threaded implementation of an operating system kernel with options for 4 different scheduling algorithms.

Primary LanguageC++

What is the Spark Kernel?

A model of an operating system which uses a multithreaded approach to handle processes. This kernel can schedule processes using 4 different types of algorithms:

• First Come First Serve
• Round Robin
• Preemptive Priority
• Shortest Job First

The aim, of embedding various scheduling algorithms in this operating system kernel, was to analyze which scheduling algorithm works better (keeping the conditions in mind). Spark Kernel uses C++ with a twist of POSIX Threads to make processing and execution fast and efficient.

How does Spark Kernel work?

Spark kernel has an easy-to-use command-line interface. This is how you can use Spark Kernel command line interface to apply different scheduling techniques:

• First Come First Serve: ./os-kernel <input_file> <#cpus> f <output_file>
• Round Robin: ./os-kernel <input_file> <#cpus> r <time_slice> <output_file>
• Preemptive Priority: ./os-kernel <input_file> <#cpus> p <output_file>
• Shortest Job First: ./os-kernel <input_file> <#cpus> s <output_file>

Spark Kernel parses these command line arguments and takes the appropriate route from there. The input file should have the following format:

Process | Name | Priority | Arrival Time | Process Type | CPU Time | I/O Times

If you have a valid input file and use the command-line interface as instructed, you will be able to see a live Gannt Chart of the whole operating system model. After complete execution of all processes, you will receive the total executed time, total time spent in ready state and number of context switches. This complete output will also be written to the text file you provide at the time of execution. Here is a sneak peek:

fcfs

Program Structure

os-kernel.cpp

int main(int, char*[]) //driver code

os-kernel.h

void init_proc(vector&, string) //initializes text file input to pcb entries
void sortVec(vector&) //sorts all entries according to arrival time

process.h

struct entry //stores pcb-related data of a process
struct compareEntry: bool operator()(entry*, entry*) //operator overload for priority queue
struct compareCpu: bool operator()(entry*, entry*) //operator overload for priority queue
struct sortEntry:inline bool operator()(const entry&, const entry&) //operator overload for sorting

scheduler.h

struct fcfs{ //structure for first come first serve scheduling
 void context_switch() //adds to number of context switches
 void yield(entry*) //sends process to I/O
 void terminate(entry*) //terminates process after execution
 void wakeup(entry*) //sends process back to CPU from I/O
 void cpu_thread(int) //CPU thread
 void io_thread(int) // I/O thread
 string idle(entry*) //decides if a process is idle or not
 void schedule(char*) //schedules all processes
 fcfs(vector, int, char) //start function to initialize scheduler
}

struct round_robin{ //structure for round robin scheduling
 void context_switch() //adds to number of context switches
 void yield(entry*) //sends process to I/O
 void terminate(entry*) //terminates process after execution
 void preempt(entry*) force-fully takes CPU for another process
 void wakeup(entry*) //sends process back to CPU from I/O
 void cpu_thread(int) //CPU thread
 void io_thread(int) // I/O thread
 string idle(entry*) //decides if a process is idle or not
 void schedule(char*) //schedules all processes
 round_robin(vector, int, int, char) //start function to initialize scheduler
}

struct priority{ //structure for preemptive priority-based scheduling
 void context_switch() //adds to number of context switches
 void preempt() //force-fully takes CPU for another process
 void cpu_thread(int) //CPU thread
 void io_thread(int) // I/O thread
 string idle(entry*) //decides if a process is idle or not
 void schedule(char*) //schedules all processes
 priority(vector, int, char) //start function to initialize scheduler
}

struct sjf{
 void context_switch() //adds to number of context switches
 void preempt() //force-fully takes CPU for another process
 void cpu_thread(int) //CPU thread
 void io_thread(int) // I/O thread
 string idle(entry*) //decides if a process is idle or not
 void schedule(char*) //schedules all processes
 sjf(vector, int, char) //start function to initialize scheduler
}

Color.hpp

An open-source GitHub library to bring color to your terminal.
Repository: https://github.com/hugorplobo/colors.hpp
Colors used for data visualization:

• Red: IDLE
• Yellow: I/O Bound Process
• Green: CPU Bound Process


The code provided is well-commented. If you have any confusions after reading the above structure, the commented code can be used as reference.

Conclusion

This project has helped us strengthen all our core concepts of the Linux operating system including multiprocessing, multithreading, scheduling and synchronization. It has made us confident about being able to work on any operating systems programming task/project in the future. As our field is cyber security, the Linux operating system is very important for us to learn and this project helped us to do exactly that.