Last active
February 20, 2021 16:14
-
-
Save coderaven/4501931 to your computer and use it in GitHub Desktop.
OS Project - First Come First Served
This file contains 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> | |
// Coded by Raven G. Duran | |
struct process { | |
int arrival_time; | |
int burst_time; | |
int waiting_time; | |
int turnaround_time; | |
int start_time; | |
int end_time; | |
}; | |
int main(int argc, const char *argv[]){ | |
int n,i,j; | |
float average_burst = 0, average_waiting = 0, average_turnaround = 0; | |
struct process p[20]; | |
printf("--- First come first served ---\n"); | |
printf("Number of processes: "); | |
scanf("%d",&n); | |
for (i = 0; i < n; i++) { | |
printf("\n::Process %d::\n",i+1); | |
printf("Arrival time: "); | |
scanf("%d",&p[i].arrival_time); | |
printf("Burst time: "); | |
scanf("%d",&p[i].burst_time); | |
} | |
// Simulate the processes and calculate for each attribute | |
for (i = 0; i < n; i++){ | |
if (i == 0) { // Special case set for the first process | |
p[i].start_time = p[i].arrival_time; | |
p[i].end_time = p[i].arrival_time + p[i].burst_time; | |
p[i].waiting_time = 0; | |
p[i].turnaround_time = p[i].burst_time; | |
} else { // Non first processes | |
p[i].start_time = p[i-1].end_time; | |
p[i].end_time = p[i].start_time + p[i].burst_time; | |
p[i].waiting_time = (p[i-1].start_time + p[i-1].burst_time) - p[i].arrival_time; | |
p[i].turnaround_time = p[i].burst_time + p[i].waiting_time; | |
} | |
average_burst += (float) p[i].burst_time; | |
average_waiting += (float) p[i].waiting_time; | |
average_turnaround += (float) p[i].turnaround_time; | |
} | |
// Calculate average | |
average_burst /= (float)n; | |
average_waiting /= (float)n; | |
average_turnaround /= (float)n; | |
printf("\n\n----- Results -----\n\n"); | |
//printf("P%d : Arrived at %d : Bursted for %d\n",i+1,p[i].arrival_time,p[i].burst_time); | |
printf("%-9s %-3s %-12s %-3s %-10s %-3s %-12s %-3s %-15s\n","Processes"," ","Arrival time"," ","Burst time"," ","Waiting time"," ","Turnaround time"); | |
for (i = 0; i < n; i++){ | |
printf("P%-8d %-3s %-12d %-3s %-10d %-3s %-12d %-3s %-15d\n",i+1," ",p[i].arrival_time," ",p[i].burst_time," ",p[i].waiting_time," ",p[i].turnaround_time); | |
for (j = 1; j <= 78; j++) printf("-"); // Prints a line | |
printf("\n"); | |
} | |
printf("\n\n"); | |
printf("%-9s %-3s %-12s %-3s %-10.2f %-3s %-12.2f %-3s %-15.2f\n","Average"," ","-"," ",average_burst," ",average_waiting," ",average_turnaround); | |
for (j = 1; j <= 78; j++) printf("-"); // Prints a line | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment