-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduling_FCFS.c
More file actions
98 lines (79 loc) · 2.4 KB
/
Copy pathscheduling_FCFS.c
File metadata and controls
98 lines (79 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
typedef struct {
int pid; // Process ID
int at; // Arrival Time
int bt; // Burst Time
int ct; // Completion Time
int tat; // Turnaround Time
int wt; // Waiting Time
} Process;
void sortByArrivalTime(Process p[], int n) {
for (int i = 0; i < n-1; ++i) {
for (int j = 0; j < n-i-1; ++j) {
if (p[j].at > p[j+1].at) {
Process temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
Process p[n];
// Input
for (int i = 0; i < n; ++i) {
p[i].pid = i + 1;
printf("Enter Arrival Time and Burst Time for Process P%d (AT BT): ", p[i].pid);
scanf("%d %d", &p[i].at, &p[i].bt);
}
// Sort processes by Arrival Time
sortByArrivalTime(p, n);
// FCFS Scheduling Logic
int current_time = 0;
float total_wt = 0, total_tat = 0;
for (int i = 0; i < n; ++i) {
if (current_time < p[i].at)
current_time = p[i].at;
p[i].ct = current_time + p[i].bt;
p[i].tat = p[i].ct - p[i].at;
p[i].wt = p[i].tat - p[i].bt;
total_wt += p[i].wt;
total_tat += p[i].tat;
current_time = p[i].ct;
}
// Display Table
printf("\n%-5s %-10s %-10s %-10s %-15s %-10s\n", "PID", "Arrival", "Burst", "Complete", "Turnaround", "Waiting");
for (int i = 0; i < n; ++i) {
printf("P%-4d %-10d %-10d %-10d %-15d %-10d\n", p[i].pid, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt);
}
// Display Averages
printf("\nAverage Turnaround Time: %.2f\n", total_tat / n);
printf("Average Waiting Time : %.2f\n", total_wt / n);
// Gantt Chart
printf("\nGantt Chart:\n");
// Top border
printf(" ");
for (int i = 0; i < n; i++) {
printf("--------");
}
printf("\n|");
// Process sequence
for (int i = 0; i < n; i++) {
printf(" P%d |", p[i].pid);
}
// Bottom border
printf("\n ");
for (int i = 0; i < n; i++) {
printf("--------");
}
// Time labels
printf("\n0");
for (int i = 0; i < n; i++) {
printf(" %d", p[i].ct);
}
printf("\n");
return 0;
}