diff options
| author | akiyamn | 2020-10-19 20:10:48 +1100 |
|---|---|---|
| committer | akiyamn | 2020-10-19 20:10:48 +1100 |
| commit | 2a05c0d80dba9a46efbcfeb9c14cb7fc31a5c6ee (patch) | |
| tree | 159b53de81d4d2c276fbcd80680f58a7f4b3a72e /task-2-29994705.c | |
| parent | 7dfef31dfd2c64d15e8744e302a59735c80d3d09 (diff) | |
| download | fit2100_ass2-2a05c0d80dba9a46efbcfeb9c14cb7fc31a5c6ee.tar.gz fit2100_ass2-2a05c0d80dba9a46efbcfeb9c14cb7fc31a5c6ee.zip | |
Task 2 done ?????
Diffstat (limited to 'task-2-29994705.c')
| -rw-r--r-- | task-2-29994705.c | 132 |
1 files changed, 66 insertions, 66 deletions
diff --git a/task-2-29994705.c b/task-2-29994705.c index 9e946bb..2a63173 100644 --- a/task-2-29994705.c +++ b/task-2-29994705.c @@ -1,15 +1,15 @@ /* - * task-1-29994705.c + * task-2-29994705.c * * FIT2100 Assignment 2 for S2/2020 * * Alexander Occhipinti * Student ID: 29994705 - * Created: 14 Oct 2020 - * Modified: 15 Oct 2020 + * Created: 18 Oct 2020 + * Modified: 19 Oct 2020 * - * This is the source file for Task 1 of Assignment 2. - * This program simulates a FCFS (first-come-first-serve) scheduler with at most 10 simulated processes. + * This is the source file for Task 2 of Assignment 2. + * This program simulates a RR (round robin) scheduler with at most 10 simulated processes. * These processes are read from file, simulated, then results are exported to "process-data.txt". * These results include wait time, turnaround time and whether the deadline was met for each process. * The purpose of this is to compare various scheduling algorithms. @@ -25,26 +25,6 @@ // NOTE: the structs provided in the assignment document are in the header file /* - * Compares two pcb_t structs to determine the order between the two in a FCFS system. - * This function is designed to interface with C's qsort function. - * - * Args: - * pcb_a: any given process' PCB as a pcb_t - * pcb_b: any given process' PCB as a pcb_t - * - * Returns (int): - * -1 if pcb_a comes before b - * 0 if both equal - * 1 if pcb_a comes after b - * -*/ -int fcfs_compare(const void *pcb_a, const void *pcb_b) { - pcb_t *pcb_x = (pcb_t *) pcb_a; - pcb_t *pcb_y = (pcb_t *) pcb_b; - return pcb_x->entryTime - pcb_y->entryTime; -} - -/* * Reads and interprets a file containing process PCBs to simulate in the given format: * [Process Name] [Arrival Time] [Service Time] [Deadline] * separated by spaces. @@ -103,41 +83,48 @@ void export_results(pcb_t *pcb_array, int num_pcbs) { fclose(fp); } + /* - * The function that runs the main FCFS scheduling simulation loop, provided an array of process PCBs to simulate. - * Each loop represents a single unit of time, where processes may either be become ready, start running or finish. - * For each unit of time, the following happens (in order): - * Each process is checked to see if it is ready (i.e. simulating the user or system starting a new process) - * The current running process is checked to see if it has finished executing - * The next process in the array (determined by FCFS) is checked to see if it is able to be run (i.e. no other process running) - * The scheduler sleeps for a short amount of time (one second by default) and the simulated time is incremeneted by one unit - * The simulation terminates once every process has been completed, then the results are exported. + * Finds the next process from an array of PCBs, which needs is next for the RR scheduler to execute. + * This function starts from where the scheduler last left off in the array (the scheduler's cursor) and moves it to + * the new PCB's index. It also updates the scheduler's `this_process` pointer which always points to the process the scheduler + * is currently working on. * * Args: - * pcb_array: An array of process PCB structs to run the simulation on - * num_pcbs: the number of PCBs in the provided array + * pcb_array: an array of PCBs (pcb_t) to search through + * num_pcbs: the number of PCBs in the array provided + * cursor: a pointer to the scheduler's current cursor which indicates which index it last left off from in the array. + * It is set to the index of the chosen PCB. + * this_process: a pointer to the pointer which refers to the process that is to be processed by the scheduler. + * It is set to a reference to the chosen PCB * - * Returns void + * Returns (int): + * 0 or positive: the distance that the cursor moved to find the next process, wrapping around the end of the array if needed. + * -1: No process is READY, so can't chose one to execute. (i.e. idle) */ - int find_next_process(pcb_t *pcb_array, int num_pcbs, int *cursor, pcb_t **this_process) { int i; int started_from = *cursor; for (i = 0; i<num_pcbs; i++) { *cursor = (started_from + i) % num_pcbs; - // printf("(%d,%d)", *cursor, num_pcbs); - // printf("[%d]", pcb_array[*cursor].state); - // printf("{%d}", i); if (pcb_array[*cursor].state == READY) { *this_process = &pcb_array[*cursor]; - return 1; + return i; } } - return 0; + return -1; } - +/* + * Scan through an array of PCBs and given a time, sets those that are due to enter the system to READY. + * Intended to simulate processes in the system being started while the scheduler is running. + * + * Args: + * pcb_array: an array of PCBs (pcb_t) to search through + * num_pcbs: the number of PCBs in the array provided + * time: the current unit of time that the scheduler is up to. +*/ int scan_ready_procs(pcb_t *pcb_array, int num_pcbs, int time){ int total_ready = 0; for (int i = 0; i < num_pcbs; i++) { @@ -150,47 +137,59 @@ int scan_ready_procs(pcb_t *pcb_array, int num_pcbs, int time){ return total_ready; } -void perform_fcfs_scheduling(pcb_t *pcb_array, int num_pcbs) { +/* + * The function that runs the main FCFS scheduling simulation loop, provided an array of process PCBs to simulate. + * Each loop represents a single unit of time, where processes may either be become ready, start running or finish. + * For each unit of time, the following happens (in order): + * Each process is checked to see if it is ready (i.e. simulating the user or system starting a new process) + * The current running process is checked to see if it has finished executing + * The next process in the array (determined by FCFS) is checked to see if it is able to be run (i.e. no other process running) + * The scheduler sleeps for a short amount of time (one second by default) and the simulated time is incremeneted by one unit + * The simulation terminates once every process has been completed, then the results are exported. + * + * Args: + * pcb_array: An array of process PCB structs to run the simulation on + * num_pcbs: the number of PCBs in the provided array + * + * Returns void +*/ +void perform_rr_scheduling(pcb_t *pcb_array, int num_pcbs) { - qsort(pcb_array, num_pcbs, sizeof(pcb_t), fcfs_compare); // Sort the PCB array in the order determined by the FCFS algorithm (via comparison function) - int time = 0; - int cursor = 0; + /* Initialise values before starting */ + int time = 0, cursor = 0, distance_travelled = 0; int procs_left = num_pcbs; pcb_t *this_process = NULL; - // for (int i = 0; i < num_pcbs; i++) { - // if (pcb_array[i].entryTime <= time && pcb_array[i].state == UNENTERED) { - // pcb_array[i].state = READY; - // printf("Time %d: Process %s entered the system.\n", time, pcb_array[i].process_name); - // } - // } - - // this_process = find_next_process(pcb_array, num_pcbs, &cursor, &finished_flag); - - while (procs_left > 0) { // Loop until all processes are finished + /* Loop until all processes are finished */ + while (procs_left > 0) { - - int num_procs_ready = scan_ready_procs(pcb_array, num_pcbs, time); + scan_ready_procs(pcb_array, num_pcbs, time); // Scan and mark processes that are now ready as 'READY'. + /* If this_process is a null pointer, set it to the next process. (i.e. first run) */ if (!this_process) find_next_process(pcb_array, num_pcbs, &cursor, &this_process); + /* Simulate a single time cycle if there is a PCB to process */ if (this_process) { - /* Checks if the current process has completed, sets its status to EXIT if so */ + + /* Run the process for one cycle if it is already running*/ if (this_process->state == RUNNING) { - // printf("Time %d: Process %s is running.\n", time, this_process->process_name); + + /* Checks if the current process has completed, sets its status to EXIT if so */ if (--this_process->remainingTime == 0) { // Is there no remaining time left? this_process->state = EXIT; this_process->timeFinished = time; printf("Time %d: Process %s has finished executing.\n", time, this_process->process_name); procs_left--; cursor++; - find_next_process(pcb_array, num_pcbs, &cursor, &this_process); + distance_travelled = find_next_process(pcb_array, num_pcbs, &cursor, &this_process); if (procs_left < 0) break; - } else if ((this_process->serviceTime-this_process->remainingTime) % 2 == 0) { + + /* If this process' time quantum slice is over, switch to the next available process */ + } else if ((this_process->serviceTime-this_process->remainingTime) % TIME_QUANTUM == 0) { this_process->state = READY; cursor++; - find_next_process(pcb_array, num_pcbs, &cursor, &this_process); + distance_travelled = find_next_process(pcb_array, num_pcbs, &cursor, &this_process); } } @@ -198,7 +197,8 @@ void perform_fcfs_scheduling(pcb_t *pcb_array, int num_pcbs) { if (this_process->state == READY) { this_process->state = RUNNING; this_process->timeStarted = time; - printf("Time %d: Process %s is now running.\n", time, this_process->process_name); + if (distance_travelled == 0) // Only print a message if the process running has changed. + printf("Time %d: Process %s is now running.\n", time, this_process->process_name); } } else { @@ -228,6 +228,6 @@ int main(int argc, char *argv[]) { pcb_t pcb_array[MAX_NUM_PROCS]; // Init the array that will hold the PCBs int num_pcbs = pcbs_from_file(in_path, pcb_array, MAX_NUM_PROCS); // Read the PCBs from the provided file path, into the PCB array - perform_fcfs_scheduling(pcb_array, num_pcbs); // Run the simulation on the read-in PCB array + perform_rr_scheduling(pcb_array, num_pcbs); // Run the simulation on the read-in PCB array return 0; } |
