-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfs.c
More file actions
134 lines (117 loc) · 4.07 KB
/
Copy pathfcfs.c
File metadata and controls
134 lines (117 loc) · 4.07 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
void calculateWaitingTime(int n, int arrival[], int burst[], int waiting[]) {
int completion[n];
completion[0] = arrival[0] + burst[0];
waiting[0] = 0;
for (int i = 1; i < n; i++) {
if (arrival[i] > completion[i - 1]) {
completion[i] = arrival[i] + burst[i];
} else {
completion[i] = completion[i - 1] + burst[i];
}
waiting[i] = completion[i] - arrival[i] - burst[i];
if (waiting[i] < 0) waiting[i] = 0;
}
}
void calculateTurnaroundTime(int n, int arrival[], int burst[], int waiting[], int turnaround[]) {
for (int i = 0; i < n; i++)
turnaround[i] = waiting[i] + burst[i];
}
void displayResults(int n, int pid[], int arrival[], int burst[], int waiting[], int turnaround[]) {
float total_waiting = 0, total_turnaround = 0;
printf("\nPID\tArrival\tBurst\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++) {
total_waiting += waiting[i];
total_turnaround += turnaround[i];
printf("P%d\t%d\t%d\t%d\t%d\n", pid[i], arrival[i], burst[i], waiting[i], turnaround[i]);
}
printf("\nAverage Waiting Time: %.2f", total_waiting / n);
printf("\nAverage Turnaround Time: %.2f\n", total_turnaround / n);
}
void displayGanttChart(int n, int pid[], int arrival[], int burst[]) {
printf("\nGantt Chart:\n");
// Top border of the Gantt chart
printf(" ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < burst[i]; j++) printf("--");
printf(" ");
}
printf("\n|");
// Process sequence in the Gantt chart
int time = arrival[0];
for (int i = 0; i < n; i++) {
if (arrival[i] > time) {
printf(" Idle |");
time = arrival[i];
}
for (int j = 0; j < burst[i] - 1; j++) printf(" ");
printf("P%d", pid[i]);
for (int j = 0; j < burst[i] - 1; j++) printf(" ");
printf("|");
time += burst[i];
}
// Bottom border of the Gantt chart
printf("\n ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < burst[i]; j++) printf("--");
printf(" ");
}
// Timeline below the Gantt chart
printf("\n%d", arrival[0]);
time = arrival[0];
for (int i = 0; i < n; i++) {
if (arrival[i] > time) {
printf(" %d", arrival[i]);
time = arrival[i];
}
for (int j = 0; j < burst[i]; j++) printf(" ");
time += burst[i];
printf("%d", time);
}
printf("\n");
}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int *arrival = (int *)malloc(n * sizeof(int));
int *burst = (int *)malloc(n * sizeof(int));
int *waiting = (int *)malloc(n * sizeof(int));
int *turnaround = (int *)malloc(n * sizeof(int));
int *pid = (int *)malloc(n * sizeof(int)); // Array to store process IDs
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &arrival[i], &burst[i]);
pid[i] = i + 1; // Assign process IDs (P1, P2, ...)
}
// Sort processes by arrival time (if not already sorted)
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arrival[j] > arrival[j + 1]) {
// Swap arrival times
int temp = arrival[j];
arrival[j] = arrival[j + 1];
arrival[j + 1] = temp;
// Swap burst times
temp = burst[j];
burst[j] = burst[j + 1];
burst[j + 1] = temp;
// Swap process IDs
temp = pid[j];
pid[j] = pid[j + 1];
pid[j + 1] = temp;
}
}
}
calculateWaitingTime(n, arrival, burst, waiting);
calculateTurnaroundTime(n, arrival, burst, waiting, turnaround);
displayResults(n, pid, arrival, burst, waiting, turnaround);
displayGanttChart(n, pid, arrival, burst);
free(arrival);
free(burst);
free(waiting);
free(turnaround);
free(pid);
return 0;
}