/* * task-2-29994705.c * * FIT2100 Assignment 2 for S2/2020 * * Alexander Occhipinti * Student ID: 29994705 * Created: 18 Oct 2020 * Modified: 19 Oct 2020 * * 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. * */ #include #include #include #include #include "task-2-29994705.h" // NOTE: the structs provided in the assignment document are in the header file /* * Reads and interprets a file containing process PCBs to simulate in the given format: * [Process Name] [Arrival Time] [Service Time] [Deadline] * separated by spaces. * Writes these PCBs to a given array via a pointer to be simulated later. * * Args: * file_path: the path to the input file * pcb_array: an array to write PCBs into * array_size: the size of the given PCB array * * Returns (int): * The number of PCBs actually read from the file. (May be less than the maximum array size) * */ int pcbs_from_file(char* file_path, pcb_t *pcb_array, int array_size) { FILE *fp; fp = fopen(file_path, "r"); int i; /* Read PCBs in from file until array is full or the file ends */ for (i = 0; i 0) { 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) { /* Run the process for one cycle if it is already running*/ if (this_process->state == RUNNING) { /* 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++; distance_travelled = find_next_process(pcb_array, num_pcbs, &cursor, &this_process); if (procs_left < 0) break; /* 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++; distance_travelled = find_next_process(pcb_array, num_pcbs, &cursor, &this_process); } } /* Checks if the current process ready for execution and sets its status to RUNNING */ if (this_process->state == READY) { this_process->state = RUNNING; this_process->timeStarted = time; 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 { printf("Time %d: CPU is idle.\n", time); } // Sleep and continue to the next unit of time sleep(WAIT_PER_CYCLE); time++; } export_results(pcb_array, num_pcbs); } /* * The Main function, called when the program is started * Reads PCBs from file, simulates the scheduler on them, then exports the results. * * Interprets the following program arguments: * 0 args: runs with the DEFAULT_INPUT_PATH as the input file * 1 arg: (a path to a text file): exports the results to the given path */ int main(int argc, char *argv[]) { char in_path[MAX_PATH_SIZE] = DEFAULT_INPUT_PATH; // Initialise the PCB input file path to the default one if (argc > 1) strncpy(in_path, argv[1], MAX_PATH_SIZE); // Set the input path to the first argument if it was provided 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_rr_scheduling(pcb_array, num_pcbs); // Run the simulation on the read-in PCB array return 0; }