-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority.c
111 lines (107 loc) · 3.69 KB
/
priority.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
105
106
107
108
109
110
111
// 1. Start
// 2. Input the processes, their burst time , arrival time ,priority
// 3. Set currentTime=arrivalTime of smallest process
// 3. Sort the processes according to priority
// 4. Take the unvisited process with minimum burst time and check if it has arrived in ready queue
// 5. If it has arrived, find waiting time ,turnaround time,completion time and set it as visited. else move to next process
// a)waitingTime = currentTime – arrivalTime;
// b)turnAroundTime = waitingTime+ burstTime;
// c)completionTime= currentTime + burst[i];
// 7.If no process is there in queue, increment current time
// 8. Continue traversing till each process is scheduled
// 9. Find average waiting time = total_waiting_time / no_of_processes
// 10. Find average turnaround time = total_turnaround_time / no_of_processes
// 11. End
#include <stdio.h>
int n, i, j, w, yes, temp, completionTime[10], proc[10], loop, smallest, current_time, sorted[10], priority[10], arrival[10] = {0}, visited[10] = {0}, tat[10], order[10], wt[10], burst[10] = {0}, visited[10];
float total_tat = 0, total_wt = 0;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void Sort(int arr[], int n)
{
int i, j;
for (i = 1; i < n; i++)
for (j = i + 1; j <= n; j++)
if (arr[i] > arr[j])
{
swap(&priority[i], &priority[j]);
swap(&burst[i], &burst[j]);
swap(&proc[i], &proc[j]);
swap(&arrival[i], &arrival[j]);
}
}
int main()
{
printf("\n-------------------------------");
printf("\nPROGRAM : PRIORITY SCHEDULING \n\n");
printf("---------------------------------\n");
printf("Enter no of process : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("\nArrival time of process %d is :", i);
scanf("%d", &arrival[i]);
printf("\nBurst time of process %d is :", i);
scanf("%d", &burst[i]);
printf("\nPriority of process %d is :", i);
scanf("%d", &priority[i]);
proc[i] = i;
visited[i] = 0;
}
smallest = arrival[1];
j = smallest;
for (loop = 1; loop <= n; loop++)
{
if (smallest > arrival[loop])
{
smallest = arrival[loop];
j = loop;
}
}
current_time = smallest;
j = 1;
Sort(priority, n);
while (j <= n)
{
yes = 0;
for (int i = 1; i <= n; i++)
{
if (visited[i] == 0 && current_time >= arrival[i])
{
yes = 1;
order[j] = proc[i];
visited[i] = 1;
wt[i] = current_time - arrival[i];
tat[i] = wt[i] + burst[i];
completionTime[i] = current_time + burst[i];
total_wt += wt[i];
total_tat += tat[i];
current_time = completionTime[i];
++j;
break;
}
}
if (yes == 0)
{
++current_time;
}
}
total_wt = total_wt / n;
total_tat = total_tat / n;
printf("order in which process get executed :\t");
for (i = 1; i <= n; i++)
{
printf("%d \t ", order[i]);
}
printf("\n arrival_time Burst_time Turn_around_time waiting_time completion time");
for (i = 1; i <= n; i++)
{
printf("\nprocess%d %d %d %d %d %d ",proc[i], arrival[i], burst[i], tat[i],wt[i],completionTime[i]);
}
printf("\n\n Average waiting time is %.2f", total_wt);
printf("\n Average turn around time is %.2f", total_tat);
}