Last active
January 4, 2016 12:35
-
-
Save naezith/b90264b809fe862be231 to your computer and use it in GitHub Desktop.
Round Robin process scheduling simulation
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
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct Process { | |
unsigned int id; | |
int cpu_time; | |
int last_start_time; | |
int preemption_count; | |
} Process; | |
// main function - Simulates the Round Robin process scheduling | |
int main() { | |
// Take the process count input | |
printf("Process count: "); | |
unsigned int process_count; | |
scanf("%d", &process_count); | |
// Allocate memory for the processes | |
Process* processes = (Process*)malloc(sizeof(Process) * process_count); | |
if(processes == NULL) { | |
printf("Could not allocate the memory for processes."); | |
exit(-1); | |
} | |
// Take Quantum value and CPU time input for every processor | |
unsigned int quantum; | |
{ | |
unsigned int i; | |
for(i = 0; i < process_count; ++i) { | |
processes[i].id = i + 1; | |
processes[i].last_start_time = 0; | |
processes[i].preemption_count = 0; | |
printf("CPU time of process #%d: ", i + 1); | |
scanf("%d", &processes[i].cpu_time); | |
} | |
printf("Q value: "); | |
scanf("%d", &quantum); | |
} | |
// Open the output file for writing | |
FILE* fp; | |
if((fp = fopen("cikti.txt", "w")) == NULL) { | |
printf("Could not open the cikti.txt for writing."); | |
exit(-1); | |
} | |
// Simulate the process scheduling | |
{ | |
fprintf(fp, "Q\t: %d", quantum); | |
unsigned int elapsed_time = 0, curr_pid = 0, p_count_in_queue = process_count; | |
// Start running processes | |
while(p_count_in_queue > 0) { | |
processes[curr_pid].last_start_time = elapsed_time; | |
// Process will get preempted | |
if(processes[curr_pid].cpu_time > quantum) { | |
elapsed_time += quantum; | |
processes[curr_pid].cpu_time -= quantum; | |
++processes[curr_pid].preemption_count; | |
} | |
// Process will end in this burst | |
else if(processes[curr_pid].cpu_time <= quantum) { | |
elapsed_time += processes[curr_pid].cpu_time; | |
processes[curr_pid].cpu_time = 0; | |
--p_count_in_queue; | |
} | |
// Log the time slice | |
fprintf(fp, "\nP%d\t: %d-%d", curr_pid + 1, processes[curr_pid].last_start_time, elapsed_time); | |
// Start the next process in queue | |
do curr_pid = (curr_pid + 1) % process_count; | |
while(p_count_in_queue >= 1 && processes[curr_pid].cpu_time == 0); | |
} | |
} | |
// Calculate and log the average waiting time | |
{ | |
unsigned int sum_wt = 0, i; | |
for(i = 0; i < process_count; ++i) | |
sum_wt += processes[i].last_start_time - processes[i].preemption_count * quantum; | |
fprintf(fp, "\nawt\t: %f", (float)sum_wt / process_count); | |
} | |
// Clean-up | |
fclose(fp); | |
free(processes); | |
processes = NULL; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment