-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedularsimulator.c
368 lines (321 loc) · 9.77 KB
/
schedularsimulator.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <getopt.h>
#include <stdbool.h>
#define LINE_MAX 1024
void Menu(void);
void MethodMenu(void);
// we have defined variable below
char PreemptiveMood='P'; // Preemptive Mod variable that keep A or P
// these definitions are defined for file manipulation
char *inputfile="input.txt";//input source
char *outputfile= "output.txt";
void ReadingData(void); // the function defined function reading data from the input source
void DataOutput(void); // the function defined function output data from the out source
// there are three structure is defined for value manupýlation
struct node
{
int bursttime,arrivaltime,priority,Pno;
struct node *next;
};
struct node * header;
struct result
{
int Pno,waiting,prebursttime;
struct result *next;
};
struct result * times;
struct headertemp
{
int Pno,waiting;
struct headertemp *next;
};
struct headertemp *tempwaiting;
struct result * initializeresult(struct result * times);
int is_empty(struct node *header);
struct node * createList(int b,int a,int p, int row);
struct node * insertBack(struct node *header, int b,int a, int p,int row);
int sizeofdata (struct node *header);
void display (struct node *header);
void sortburst(struct node *header);
void sortarrival(struct node *header);
void firstcomefss(struct node *header);
// this is main function
int main(){
Menu(); // the called function brings menu to screen
return 0; //this is default integer value of main function
}
void Menu(void) // we have created this function for print out main function
{
char choise=' ';
do
{ char flag=' ';
while(flag==' ') // we have use flag here to control while loop
{
ReadingData(); // the function is called for reading data from input file
printf(" Main Menu\n\n");
printf(" Please, Choose Your Option\n");
printf("1-) Scheduling Method\n");
if (PreemptiveMood=='A'){ //this function specifies which mode is active
printf("2-) Preemptive Mode (enable)\n" );
printf("3-) Non-preemptive Mode\n");
}else{
printf("2-) Preemptive Mode\n" );
printf("3-) Non-preemptive Mode (enable)\n");
}
printf("4-) Show Result\n");
printf("5-) End Program\n");
printf("Option =");
scanf("%c", &choise);
system("cls");
if(choise == '1'){
MethodMenu();
break;
}if(choise == '2'){
PreemptiveMood='A';
printf(" Preemptive Mode is active now. \n");
printf(" Preemptive Mode Scheduling can be simulate. \n");
sleep(2);
break;
}if(choise == '3'){
PreemptiveMood='P';
printf(" Non-preemptive Mode is active now. \n");
printf(" Non-preemptive Mode Scheduling can be simulate. \n");
sleep(2);
break;
}if(choise == '4'){
printf("4) The Results Are Showing Result\n");
DataOutput();
printf("press 5 To back main menu\n");
int key;
do{
scanf("%d", &key);
if(key == 5){
Menu();
break;
}
}
while(choise!='5');
break;
}if(choise == '5'){
break;
}else{
break;
}
scanf("%c", &flag);
}
}
while(choise!='5');
}
void MethodMenu(void) // we have created this function for print out main function
{
char choise=' ';
do
{ char flag=' ';
while(flag==' ') // we have use flag here to control while loop
{
ReadingData(); // the function is called for reading data from input file
printf(" Scheduling Method Menu\n\n");
printf("1-) First Come First Served Scheduling\n");
printf("2-) Shortest Job First Scheduling\n");
printf("3-) Priority Scheduling\n");
printf("4-) Round Robin Scheduling\n");
printf("5-) Back to Main Menu\n");
printf("6-) End Program\n");
printf("Option =");
scanf("%c", &choise);
system("cls");
if(choise == '1'){
printf("1-) First Come First Served Scheduling\n");
firstcomefss(header);
sleep(5);
break;
}if(choise == '2'){
printf("2-) Shortest Job First Scheduling\n");
break;
}if(choise == '3'){
printf("3-) Priority Scheduling\n");
break;
}if(choise == '4'){
printf("4-) Round Robin Scheduling\n");
break;
}if(choise == '5'){
printf("5-) Back to Main Menu\n");
Menu();
break;
}if(choise == '6'){
printf(" The CPU Shedule Similator Closed. ");
break;
}else{
break;
}
scanf("%c", &flag);
}
}
while(choise!='6');
}
// this function is wrote for that reading data from input file
void ReadingData(void)
{
FILE *fp;
char line[LINE_MAX];
int b,a,p;
int row =0;
if ((fp = fopen(inputfile,"r")) == NULL)
{
//return;
printf("Input file could not found.\n");
printf("Please, try again...\n");
exit(0);
}
else
{
while (fgets(line, LINE_MAX, fp) != NULL)
{
row++;
sscanf(line,"%d:%d:%d\n",&b,&a,&p);
// push data to linked list
header=insertBack(header,b,a,p,row);
// print to screen
printf("%u) %u %u %u\n",row,b,a,p);
}
fclose(fp);
printf(" Input file readed succesfully.\n\n");
}
}
void DataOutput(void){
FILE *fp;
char line[LINE_MAX];
if ((fp = fopen(outputfile,"r")) == NULL)
{
//return;
printf("Output file could not found.\n");
printf("Please, try again...\n");
exit(0);
}
else
{ // print to the screen
while (fgets(line, LINE_MAX, fp) != NULL)
{
// print line
printf("%s",line);
}
fclose(fp);
printf("\n Output file readed succesfully.\n");
}
}
int is_empty(struct node *header){
if(header==NULL)
return 1;
else
return 0;
}
struct node * createList(int b,int a,int p,int row)
{
struct node * temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->bursttime=b;
temp->arrivaltime=a;
temp->priority=p;
temp->Pno=row;
temp->next=NULL;
return temp;
}
struct node * insertBack(struct node *header, int b,int a,int p,int row)
{
struct node * temp = createList(b,a,p,row);
struct node * headertemp;
if (header == NULL)
{
header = temp;
return header;
}
headertemp=header;
while(headertemp->next != NULL)
headertemp=headertemp->next;
headertemp->next = temp;
return header;
}
//the function is wrote for initialize the result stack
struct result * initializeresult(struct result * times)
{
times=(struct result*)malloc(sizeof(struct result*));
times->Pno=0;
times->waiting=0;
times->prebursttime=0;
return times;
}
// this function is written for sorting the arrival time
void sortarrival(struct node *header)
{
int control=1;
struct node *headertemp=header;
struct node *tempSort;
if (headertemp == NULL)
exit(0);
while(control){
control=0;
headertemp=header;
while (headertemp->next!=NULL) {
if (headertemp->arrivaltime>headertemp->next->arrivaltime){
int temp_arrivaltime=headertemp->arrivaltime;
headertemp->arrivaltime=headertemp->next->arrivaltime;
headertemp->next->arrivaltime=temp_arrivaltime;
control=1;
}
headertemp=headertemp->next;
}
tempSort=headertemp;
}
header=tempSort;
}
void firstcomefss(struct node *header) //fisrt come first served scheduling method
{
sortarrival(header);
times=initializeresult(times);
struct node *temp;
struct result *temptimes;
double avgwaiting=0;
temp=header;
temptimes=times;
int i=1;
int SIZE = sizeofdata(temp);
FILE *fp;
fp = fopen(outputfile,"a");
fprintf(fp,"===========================================\n");
fprintf(fp,"Scheduling Method: First Come First Served\n");
fprintf(fp,"Process Waiting Times:\n");
printf("\n===========================================\n");
printf("Scheduling Method: First Come First Served\n");
printf("Process Waiting Times:\n");
for(i=1; i<=SIZE; i++)
{
avgwaiting+=temptimes->waiting-temp->arrivaltime;
temptimes->Pno=temp->Pno;
fprintf(fp,"P%d: %d ms\n",(temptimes->Pno),avgwaiting);
printf("P%d: %d ms\n",(temptimes->Pno),(temptimes->waiting));
temptimes->waiting+=temp->bursttime;
temp=temp->next;
}
avgwaiting=avgwaiting/SIZE;
fprintf(fp,"Average Waiting Time: %0.2f ms\n",avgwaiting);
fprintf(fp,"===========================================\n");
fclose(fp);
printf("Average Waiting Time: %0.2f ms\n",avgwaiting);
printf("\n");
printf("Simulation result saved to ' %s ' file \n",outputfile);
printf("\n\n");
}
int sizeofdata(struct node *header)
{
struct node *temp=header;
int line=0;
if(temp!=NULL)
while(temp!=NULL){
temp=temp->next;
line++;
}
free(temp);
return line;
}