A small CPU scheduling simulation written in C. This was written for an assignment for an unit on operating systems. I do not expect anyone to be impressed by the resulting program. I have decided to upload this assignment because I am proud of how well the code worked out. The commenting is extensive and the code is well modularised and debugged.
This program will take a list of 'processes' and simulate how long they would take to complete using either round robin, shortest job first or both scheduling algorithms.
##Input file format The file format for the input file is as follows: quantum arrivalTime burstTime arrivalTime burstTime ... arrivalTime burstTime
The quantum here is the time that the CPU should simulate a process for before context switching to the next queued process in a round robin system. The arrivalTime is the arrival time of the process. The burstTime is the time that the process will run for before it will block/terminate. Each row after the initial quantum represents a single process. All time is measured in ticks starting at 0.
##Original Readme The following is the original README written for the assignment:
- CONTENTS
-
Contents
-
Compiling and Running
- Compiling and Running Part 1
- Compiling and Running Part 2
- Compiling and Running Part 3
-
Source Code Overview
-
COMPILING AND RUNNING
-
-- Steps to compile part 1 (Round robin simulator):
- Navigate to the assignment root directory
- Use the following sequence of commands to compile part 1 cd rr make clean make
- Use the following command (while still inside the rr directory) to run part 1 ./rr
-
-- Steps to compile part 2 (Shortest job first simulator ):
- Navigate to the assignment root directory
- Use the following sequence of commands to compile part 2 cd sjf make clean make
- Use the following command (while still inside the sjf directory) to run part 2 ./sjf
-
-- Steps to compile part 3 (Threaded simulator ):
- Navigate to the assignment root directory
- Use the following sequence of commands to compile part 3 cd sim make clean make
- Use the following command (while still inside the sim directory) to run part 3 ./sim
Note well: The "assignment root directory" referenced from here on is the directory that contains the files for the assignment (including this README file).
- SOURCE CODE OVERVIEW
Following is a brief overview of the structure of the source code for this assignment. The module by module overview will proceed from the bottom up as I feel that the basic data structures are used over and over and assist more in the overall understanding.
-
-- General Comments There is a header file for every c file whose functions are called by another file. Files that contain an entry point (a main function) tend not to have header files. I call a c file and its corresponding header file (if applicable) a 'module'. For example, LList.c and LList.h constitute the LList module.
Files beginning with capital letters tend to define new data types. The internal structure of these types is not of anyone's concern and so these 'types' should not be accessed except through the functions defined for them. This rule is followed very strictly in the code and there are no exceptions to it. As a counter example, struct proc is NOT typedef'd as the fields inside it will be directly accessed by unrelated functions.
-
-- Module by Module overview
The common Module
Consists of a single header file that contains common definitions that are required in many files. This allows flags and types to be added incredibly easy. This file could most likely be removed for this project but the flexibility that it allows has convinced me to leave it in.The LList Module
Contains a simple singly-linked single tailed linked list implementation. This module is used in almost every other source file and has almost no dependencies. It provides arbitrary element addition/ access/removal. It is not overly efficient but efficiency is not an important goal in such a deterministic simple situation. Importantly, indexes into the list are 1-based instead of 0-based.The proc Module
Contains a very important function for reading in a table of processes from a file. The file should be in the format detailed in the specificatin. If the specification is not followed then the function should gracefully exit with an error code. This module also defines the very important struct proc record that is used throughout the program to keep information on processes.The Gantt Module
Contains a simple implementation of gantt chart data structure. This data structure is used in both RR and SJF simulations to calculate the average waiting and turnaround times. Gantt charts can only be added to in order. The blocks in the chart can be accessed by id and by index. Indexing is 1-based again here.The simcalc Module
Contains several helper functions for 'simulation calculations'. Notably, it contains functions to calculate the average waiting time and the average turnaround time given the gantt chart for the simulation.The rrsim Module
Contains functions for performing the round robin simulation. This is used in both part 1 and part 3 of the assignment. Notably this file contains the heart of part1 in rrSim(...).The sjfsim Module
Contains functions for performing the shortest job first simulation. This is used in both part 2 and part 3 of the assignment. Notably this file contains the heart of part2 in sjfSim(...).The rr/rr Module
Contains the entry point for part 1. Simply prompts the user for an input file, uses the proc module to load the file and then call rrSim(...).The sjf/sjf Module
Contains the entry point for part 2. Simply prompts the user for an input file, uses the proc module to load the file and then calls sjfSim(...).The sim/simulator Module
Contains the entry point for part 3. Has three threads: the main thread, the sjf thread and the rr thread. The two worker threads will load the file and then call sjfSim(...) and rrSim( ... ) respectively. Lots of care is taken in this file to make sure that synchronisation is preserved and deadlocks and race conditions are avoided.