Priority schedueling with & without preemtion, with aging, with different arrival times

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
215
216
217
218
219
220
#include"stdio.h"
#include"conio.h"
typedef struct{
 
int arrival;
int burst;
int priorty;
int index;
int status;
}process;
 
void swap(process *x,process *y)
{
process temp;
temp = *x;
*x = *y;
*y = temp;
}
 
void bublesort(process p[], int n)
{
 
int i,j;
for(i=0;i<(n-1);i++)
for(j=0;j<(n-(i+1));j++)
if(p[j].arrival > p[j+1].arrival||(p[j].arrival==p[j+1].arrival&&p[j].priorty>p[j+1].priorty))
swap(&p[j],&p[j+1]);
}
 
void sortpr(process p[],int n)
{
int i,j;
for(i=0;i<(n-1);i++)
for(j=0;j<(n-(i+1));j++)
if(p[j].priorty > p[j+1].priorty)
swap(&p[j],&p[j+1]);
}
 
void deletep(process p[],int size,int index)
{ 
int i=index;
while(i<size-1)
{
p[i]=p[i+1];
i++;
}
return;
}
 
bool nonew(process p[],int size)
{ 
for(int i=0;i<size;i++)
{
if(p[i].status==0 )return false;
}
return true;
}
 
bool noready(process p[],int size)
{
for(int i=0;i<size;i++)
if(p[i].status==1)
return false;
return true;
}
 
int ready(process p[],int size)
{ 
int min=0;
for(int i=0;i<size;i++)
if(p[i].status==1&&p[i].priorty<p[min].priorty)
min=i;
return min;
}
 
void NonPreemtive(process p[],int size)
{ 
int i,j,minpr,current=p[0].arrival;
for(i=0;i<size-1;i++)
{
printf("p%d from %d to %d\n",p[i].index,current,current+p[i].burst);
current=current+p[i].burst;
if (p[i+1].arrival==current)
{
continue;
}
else{
if(p[i+1].arrival>current){
current=p[i+1].arrival;}
else{ j=i+1;
minpr=p[j].priorty;
for(j=i+1;j<size;j++)
{ 
if(p[j].arrival<current){
if (p[j].priorty<minpr){
minpr=p[j].priorty;
swap(&p[i+1],&p[j]);
}
}
}
}
}
}
printf("p%d from %d to %d\n",p[i].index,current,current+p[i].burst);
}
 
void NonPreemtiveAging(process p[],int size)
{
int i,j,minpr,current=p[0].arrival;
for(i=0;i<size-1;i++)
{ 
printf("p%d from %d to %d\n",p[i].index,current,current+p[i].burst);
current=current+p[i].burst;
if (p[i+1].arrival==current)
{
continue;
}
else{
if(p[i+1].arrival>current){
current=p[i+1].arrival;
}
else{ j=i+1;
minpr=p[j].priorty-(current-p[j].arrival)/5;
for(j=i+1;j<size;j++)
{ 
if(p[j].arrival<current){
if (p[j].priorty-(current-p[j].arrival)/5<minpr)
{
minpr=p[j].priorty-(current-p[j].arrival)/5;
swap(&p[i+1],&p[j]);
}
}
}
}
}
}
printf("p%d from %d to %d\n",p[i].index,current,current+p[i].burst);
}
 
void Preemtive(process p[],int size)
{
int i=0,current=p[0].arrival,j,test;
while(size!=0)
{
printf("p%d from %d to ",p[i].index,current);
if(nonew(p,size)){
printf("%d\n",current+p[i].burst);
current=current+p[i].burst;
deletep(p,size,i);
size--;
if(size==0) break;
i=ready(p,size);
continue;
}
else{test=0;
for(j=0;j<size;j++)
{
if(p[j].arrival<current+p[i].burst)    
{
if(p[j].status==1)continue;
if(p[j].priorty<p[i].priorty)
{
printf(" %d\n",p[j].arrival);
p[i].burst=p[i].burst-(p[j].arrival-current);
p[i].status=1;
current=p[j].arrival;
test=1;
i=j;
break;
}else p[j].status=1;
}else if(p[j].arrival==current+p[i].burst)p[j].status=1;
}
if(test==1)continue;
printf("%d\n",current+p[i].burst);
current=current+p[i].burst;
deletep(p,size,i);
size--;
if (!noready(p,size))
{
i=ready(p,size);
}
else {
current=p[i].arrival;}
}
}
return;
}
 
void main()
{
process p[5];
int i,c;
printf("Welcome\n");
printf( "Choose the mode of scedueling:\npress 1 for non preemtive\npress 2 for non preemtive with aging\npress 3 for preemptive\n");
scanf("%d",&c);
for (i=0;i<5;i++)
{
printf("\nenter the arrival time of p %d:",i);
scanf("%d",&p[i].arrival);
printf("enter the burst time of p %d:",i);
scanf("%d",&p[i].burst);
printf("enter the priorty of p %d:",i);
scanf("%d",&p[i].priorty);
p[i].status=0;
p[i].index=i;
}
bublesort(p,5);
printf("\nsorted:\nprocess arrival time burst time priorty\n");
for (i=0;i<5;i++)
{
printf( " p%d %d %d %d\n",p[i].index,p[i].arrival,p[i].burst,p[i].priorty);
}
if(c==1)
NonPreemtive(p,5);
else{
if(c==2) NonPreemtiveAging(p,5);
else if (c==3)Preemtive(p,5);
}
getch();
}
rayan daou

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in