-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalcode.c
More file actions
178 lines (162 loc) · 3.98 KB
/
finalcode.c
File metadata and controls
178 lines (162 loc) · 3.98 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int count,i,j;
int time_quantum,n;
int time = 0;
float avg_wait_time(int wt[], int n);
void minptyinc(int pty[],int n);
float avg_turnaround_time(int tat[], int n);
void rearrange_process_queue(int pq[],int rt[],int pty[],int n,int running_processes);
printf("\nEnter Number of Processes =");
scanf("%d" ,&n);
printf("\nEnter Time Quantum =");
scanf("%d" ,&time_quantum);
if(n <= 0 || time_quantum <= 0)
{
printf("Invalid data!");
return 0;
}
printf("The number of processes are set to: %d\nThe time quantum is set to:%d\n" , n,time_quantum);
int at[10],bt[10],rt[10],pq[10],pty[10],pty1[10],pflag[10],tat[10],wt[10];
for(j=0;j<n;j++)
{
pq[j] = 0;
pflag[j] = 0;
}
for(count=0;count<n;count++)
{
printf("\nEnter Detail for Process = %d",count+1);
printf("\nEnter Arrival time = ");
scanf("%d",&at[count]);
printf("Enter Burst time = ");
scanf("%d",&bt[count]);
printf("Enter Priority = ");
scanf("%d",&pty[count]);
rt[count]=bt[count];
pty1[count]=pty[count];
if(at[count] < 0 || bt[count] <= 0)
{
printf("Invalid Data!");
return 0;
}
}
int current = 0;
int running_processes = 0;
int x=0;
pq[0] = 1;
running_processes = 1;
pflag[0] = 1;
int flag = 0;
while(running_processes!=0)
{
flag = 0;
x++;
if(rt[pq[0]-1]>time_quantum)
{
rt[pq[0]-1] = rt[pq[0]-1] - time_quantum;
time = time + time_quantum;
current = time;
for(j=0;j<n;j++)
{
printf("%d",rt[0]);
}
}
else
{
time = time + rt[pq[0]-1];
rt[pq[0]-1] = 0;
flag = 1;
current = time;
tat[pq[0]-1] = time - at[pq[0]-1];
wt[pq[0]-1] = tat[pq[0]-1] - bt[pq[0]-1];
for(j=0;j<n;j++)
{
printf("%d",rt[0]);
}
}
for(i=0;i<n;i++)
{
if(at[i] <= time && pflag[i]== 0)
{
pq[running_processes] = i+1;
running_processes = running_processes + 1;
pflag[i] = 1;
}
}
if(x%2==0)
minptyinc(pty,n);
rearrange_process_queue(pq,rt,pty,n,running_processes);
if(flag == 1)
running_processes = running_processes - 1;
}
printf("\n\nExecution Data:\n");
printf("|\tProcess\t|\tAT\t|\tBT\t| Priority |\tTAT\t|\tWT\t|\n");
for(i=0;i<n;i++)
{
printf("|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\n",i+1,at[i],bt[i],pty1[i],tat[i],wt[i]);
}
printf("\n\nAverage Waiting Time= %f\n",avg_wait_time(wt,n));
printf("Avg Turnaround Time = %f\n",avg_turnaround_time(tat,n));
return 0;
}
float avg_turnaround_time(int tat[], int n)
{
float x = 0;
int i,sum = 0;
for(i=0;i<n;i++)
sum = sum + tat[i];
x = sum * 1.0;
x = x / n;
return x;
}
void rearrange_process_queue(int pq[],int rt[],int pty[],int n,int running_processes)
{
int i;
if(pty[0]<pty[1])
{
int temp = pq[0];
for(i=0;i<running_processes;i++)
{
pq[i] = pq[i+1];
}
pq[running_processes-1] = temp;
}
if(rt[pq[0]-1]==0)
{
int temp = pq[0];
for(i=0;i<running_processes;i++)
{
pq[i] = pq[i+1];
}
pq[running_processes-1] = temp;
running_processes=running_processes-1;
}
}
void minptyinc(int pty[],int n)
{
int i,min=pty[0];
for(i=1;i<n;i++)
{
if(min>=pty[i])
min=pty[i];
}
for(i=0;i<n;i++)
{
if(pty[i]==min)
pty[i]=pty[i]+1;
printf("%drrr",pty[i]);
}
}
float avg_wait_time(int wt[], int n)
{
float x = 0;
int i,sum = 0;
for(i=0;i<n;i++)
sum = sum + wt[i];
x = sum * 1.0;
x = x / n;
return x;
}