-
Notifications
You must be signed in to change notification settings - Fork 0
/
FINAL.cpp
214 lines (206 loc) · 6.76 KB
/
FINAL.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include<stdlib.h>
struct process{
int arr,burst,firstt,i,status,burl,compt,waitingt,turnaroundt;
}*run=NULL;
int n,tq=10,px=0;
struct node
{
struct process *q;
struct node *next;
}*start=NULL,*last,*cn;
int main()
{
printf("\nThe Number of the processes to be executed :");
fflush(stdout);
scanf("%d",&n);
fflush(stdin);
int pl=n; //processes to be created
struct process p[n];
for(int i=0;i<n;i++)
{
printf("\nGive Arrival and Burst Time of processes %d ",i+1);
fflush(stdout);
printf("\n\tArrival Time : ");
fflush(stdout);
scanf("%d",&p[i].arr);
fflush(stdin);
printf("\tBurst Time : ");
scanf("%d",&p[i].burst);
fflush(stdin);
p[i].burl=p[i].burst;
p[i].i=i+1;
p[i].status=0;
p[i].firstt=-1;
if(p[i].arr>3)
px++;
}
int time=4,y=1; //cpu idle for first 3 time units
while(pl!=0)
{
if(px!=0)
{
for(int i=0;i<n;i++)
{
if(p[i].arr>3 && p[i].arr<=time && p[i].status==0) //inside of the queue
{
if(start==NULL)
{
p[i].status=1;
cn=new node;
cn->q=&p[i];
cn->next=NULL;
last=cn;
start=cn;
}
else{
p[i].status=1;
cn=new node;
cn->q=&p[i];
node *x;
x=start;
if((start->q->burl)>(cn->q->burl))
{
cn->next=start;
start=cn;
}
else
{
while(x->next!=NULL && x->next->q->burl < cn->q->burl)
{
x=x->next;
}
cn->next=x->next;
x->next=cn;
}
}
px--;
}
}
}
if(px==0 && y==1)
{
for(int i=0;i<n;i++)
{
if(p[i].arr<=3 && p[i].status==0) //in the queue
{
if(start==NULL)
{
p[i].status=1;
cn=new node;
cn->q=&p[i];
cn->next=NULL;
last=cn;
start=cn;
}
else{
p[i].status=1;
cn=new node;
cn->q=&p[i];
node *x;
x=start;
if((start->q->burl)>(cn->q->burl))
{
cn->next=start;
start=cn;
}
else
{
while(x->next!=NULL && x->next->q->burl < cn->q->burl)
{
x=x->next;
}
cn->next=x->next;
x->next=cn;
}
}
}
}
y--;
}
if(run==NULL) //process execution
{
if(start!=NULL)
{
run=start->q;
start=start->next;
if(run->firstt==-1)
{
run->firstt=time;
}
if(run->burl<=tq)
{
printf("\nProcess P%d running from time= %d sec to time= %d sec.",run->i,time,time+run->burl);
fflush(stdout);
time=time+run->burl;
run->burl=0;
run->compt=time;
run->status=2;
pl--;
}
else if(run->burl>tq)
{
printf("\nProcess P%d running from time= %d sec to time= %d sec.",run->i,time,time+tq);
fflush(stdout);
time=time+tq;
run->burl=run->burl-tq;
run->compt=time;
if(start==NULL)
{
cn=new node;
cn->q=run;
cn->next=NULL;
last=cn;
start=cn;
}
else
{
cn=new node;
cn->q=run;
node *x;
x=start;
if((start->q->burl)>(cn->q->burl))
{
cn->next=start;
start=cn;
}
else
{
while(x->next!=NULL && x->next->q->burl < cn->q->burl)
{
x=x->next;
}
cn->next=x->next;
x->next=cn;
}
}
}
}
run=NULL;
}
}
int awt=0,att=0;
for(int i=0;i<n;i++)
{
p[i].turnaroundt=((p[i].compt)-(p[i].arr));
p[i].waitingt=((p[i].turnaroundt)-(p[i].burst));
awt=awt+p[i].waitingt;
att=att+p[i].turnaroundt;
}
awt=awt/n;
att=att/n;
printf("\n\n\nResult after scheduling :");
fflush(stdout);
printf("\n _______________________________________________________________________________________________");
printf("\n|\tPROCESS\t\t|\tARRIVAL TIME\t|\tBURST TIME\t|\tWAITING TIME \t|\tTURN AROUND TIME \t|");
printf("\n|_______________|___________________|_______________|___________________|_______________________|");
fflush(stdout);
for(int i=0;i<n;i++)
{
printf("\n|\t\tP%d\t\t|\t\t%d\t\t\t|\t\t%d\t\t|\t\t\t%d\t\t|\t\t\t%d\t\t\t|",p[i].i,p[i].arr,p[i].burst,p[i].waitingt,p[i].turnaroundt);
printf("\n|_______________|___________________|_______________|___________________|_______________________|");
}
printf("\nAverage waiting time = %d ",awt);
fflush(stdout);
printf("\nAverage Turn Around Time = %d \n",att);
}