-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority.c
More file actions
97 lines (79 loc) · 2.76 KB
/
Copy pathpriority.c
File metadata and controls
97 lines (79 loc) · 2.76 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
#include <stdio.h>
void priorityScheduling(int n, int arrival[], int burst[], int priority[]) {
int remaining[n], completion[n], waiting[n], turnaround[n];
int time = 0, completed = 0, processOrder[100], ganttTime[100], index = 0;
int isCompleted[n];
for (int i = 0; i < n; i++) {
remaining[i] = burst[i];
isCompleted[i] = 0;
}
while (completed < n) {
int minPriority = 9999, minIndex = -1;
// Find the process with the highest priority (lower number = higher priority)
for (int i = 0; i < n; i++) {
if (arrival[i] <= time && remaining[i] > 0 && priority[i] < minPriority) {
minPriority = priority[i];
minIndex = i;
}
}
if (minIndex == -1) {
// If no process is available, move forward in time without adding idle to Gantt chart
time++;
continue;
}
// Store execution in Gantt chart order
if (index == 0 || processOrder[index - 1] != minIndex + 1) {
processOrder[index] = minIndex + 1;
ganttTime[index] = time;
index++;
}
remaining[minIndex]--;
time++;
if (remaining[minIndex] == 0) {
completion[minIndex] = time;
turnaround[minIndex] = completion[minIndex] - arrival[minIndex];
waiting[minIndex] = turnaround[minIndex] - burst[minIndex];
isCompleted[minIndex] = 1;
completed++;
}
}
// Print Process Table
printf("\nPID\tArrival\tBurst\tPriority\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++)
printf("P%d\t%d\t%d\t%d\t\t%d\t%d\n", i + 1, arrival[i], burst[i], priority[i], waiting[i], turnaround[i]);
// Printing Gantt Chart in a Box Format
printf("\nGantt Chart:\n ");
// Top Border
for (int i = 0; i < index; i++) {
printf("+----");
}
printf("+\n");
// Process Row
for (int i = 0; i < index; i++) {
printf("| P%d ", processOrder[i]);
}
printf("|\n");
// Bottom Border
for (int i = 0; i < index; i++) {
printf("+----");
}
printf("+\n");
// Time Labels
printf("%d", ganttTime[0]); // Start time of the first process
for (int i = 1; i < index; i++) {
printf(" %d", ganttTime[i]);
}
printf(" %d\n", time); // End time of the last process
}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int arrival[n], burst[n], priority[n];
for (int i = 0; i < n; i++) {
printf("Enter arrival time, burst time, and priority for process %d: ", i + 1);
scanf("%d %d %d", &arrival[i], &burst[i], &priority[i]);
}
priorityScheduling(n, arrival, burst, priority);
return 0;
}