-
Notifications
You must be signed in to change notification settings - Fork 364
/
priority_scheduling.c
104 lines (89 loc) · 3.04 KB
/
priority_scheduling.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct priorityScheduling
{
char name[20];
int completionTime, arrivalTime, burstTime, turnAroundTime, responseTime, waitingTime, priority;
};
int curr = 0;
double avgTurnAroundTime = 0, avgwaitingTime = 0, avgresponseTime = 0;
void swap(struct priorityScheduling *process, int n)
{
int position;
struct priorityScheduling temp;
for (int i = 0; i < n; i++)
{
position = i;
for (int j = i + 1; j < n; j++)
{
if (curr >= process[j].arrivalTime && curr >= process[position].arrivalTime && process[j].priority < process[position].priority)
{
position = j;
}
}
temp = process[i];
process[i] = process[position];
process[position] = temp;
}
}
int main()
{
printf("Enter Number of Processes: ");
int n;
scanf("%d", &n);
fflush(stdin);
struct priorityScheduling *process = malloc(n * sizeof(struct priorityScheduling));
printf("Enter Names of Processes: ");
for (int i = 0; i < n; i++)
{
fgets(process[i].name, sizeof(process[i].name), stdin);
process[i].name[strcspn(process[i].name, "\n")] = '\0';
}
printf("Enter Arrival Times of Processes: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &process[i].arrivalTime);
}
printf("Enter Priority of Processes: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &process[i].priority);
}
printf("Enter Burst Times of Processes: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &process[i].burstTime);
}
for (int i = 0; i < n; i++)
{
swap(process, n);
process[i].responseTime = curr - process[i].arrivalTime;
process[i].completionTime = curr + process[i].burstTime;
curr = process[i].completionTime;
process[i].turnAroundTime = process[i].completionTime - process[i].arrivalTime;
process[i].waitingTime = process[i].turnAroundTime - process[i].burstTime;
avgTurnAroundTime += process[i].turnAroundTime;
avgresponseTime += process[i].responseTime;
avgwaitingTime += process[i].waitingTime;
}
printf(" AT\tP\tBT\tCT\tTAT\tWT\tRT\n");
for (int i = 0; i < n; i++)
{
printf("%s: %d\t%d\t%d\t%d\t%d\t%d\t%d\n", process[i].name, process[i].arrivalTime, process[i].priority, process[i].burstTime, process[i].completionTime, process[i].turnAroundTime, process[i].waitingTime, process[i].responseTime);
}
printf("\n");
printf("Average TurnAroundTime: %.2f\n", avgTurnAroundTime / n);
printf("Average WaitingTime: %.2f\n", avgwaitingTime / n);
printf("Average ResponseTime: %.2f\n", avgresponseTime / n);
printf("\n");
printf("Gantt Chart\n");
printf("%d |%s| ", process[0].arrivalTime, process[0].name);
for (int i = 0; i < n - 1; i++)
{
printf("%d |%s| ", process[i].completionTime, process[i + 1].name);
}
printf("%d\n", process[n - 1].completionTime);
free(process);
return 0;
}