-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_final.c
109 lines (100 loc) · 2.46 KB
/
priority_final.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
#include<stdio.h>
#include <stdlib.h>
#include<time.h>
struct Process
{
int pno;
int arrival; // non-pre-emptive program
int burst;
int priority;
int turn;
int wait;
}P[20];
void swap(struct Process *p1, struct Process *p2)
{
struct Process tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
void sorting(int n)
{
for(int i=0;i<n-1;i++)
for (int j=0;j<n-i-1;j++)
if(P[j].priority > P[j+1].priority)
swap(&P[j], &P[j+1]);
}
/*void sorting(int n)
{
for(int i=0;i<n-1;i++)
for (int j=0;j<n-i-1;j++)
if(P[j].arrival>P[j+1].arrival)
swap(&P[j], &P[j+1]);
else if(P[j].arrival==P[j+1].arrival)
if(P[j].priority>P[j+1].priority)
swap(&P[j], &P[j+1]);
}*/
void waiting(int n)
{
float wavg=0.0, tavg, tburst=P[0].burst;
P[0].wait=0;
P[0].turn=P[0].burst;
for(int i=1;i<n;i++)
{
P[i].wait= tburst;
if(P[i].wait<0)
P[i].wait=0;
wavg += P[i].wait;
tburst += P[i].burst;
P[i].turn = tburst;
}
tavg = (wavg + tburst)/n;
printf("\nProcess\t Priority\t Burst Time\t Waiting Time\t Turnaround Time\n");
for(int i=0;i<n;i++)
{
printf("\nP%d\t\t%d\t\t%d\t\t%d\t\t%d\n",P[i].pno,P[i].priority,P[i].burst,P[i].wait,P[i].turn);
}
printf("\nAverage waiting time: %f", wavg/n);
printf("\nAverage turnaround time: %f", tavg);
}
int main(){
int n;
char ch, fl;
printf("Continue with random input? (y/n): ");
scanf("%c",&ch);
scanf("%c",&fl);
if(ch=='y')
{
printf("continuing with random inputs");
srand(time(0));
n = rand()%5 + 1;
for (int i=0; i<n; i++)
{
P[i].pno=i+1;
P[i].burst = rand()%10 + 1;
//P[i].arrival = rand()%10 + 1;
P[i].priority = rand()%10 + 1;
}
}
else if(ch=='n')
{
printf("Enter no of processes: ");
scanf("%d", &n);
printf("Enter values: ");
for (int i=0; i<n; i++)
{
P[i].pno=i+1;
printf("P[%d]: ", i);
printf("Burst time: ");
scanf("%d", &P[i].burst);
//printf("Arrival time: ");
//scanf("%d", &P[i].arrival);
printf("Priority: ");
scanf("%d", &P[i].priority);
}
}
else {printf("Invalid Input\n"); return 0;}
sorting(n);
waiting(n);
printf("\n");
return 0;
}