View difference between Paste ID: SyH2MVNi and 9uJ3Wb2a
SHOW: | | - or go back to the newest paste.
1
#include <cstdlib>
2
#include <cstdio>
3
//#include <winbgim.h>
4
#include <fstream>
5
#include <iostream>
6
#include <iomanip>
7
#include <queue>
8
#define INP "input.txt"
9
using namespace std;
10
11
struct process
12
{
13
	char name[10];
14
	int timeRL, timeCPU, priority;
15
	int timeOUT, timeIN, timewait, timesave;
16
	int index; //the index of pr[i]
17
};
18
19
typedef process *ListP;
20
21
int quantum;
22
int set;
23
24
void input(ListP &pr, int &n, int &timeOUT);
25
26
void output_input(ListP pr, int n);
27
void output_FIFO(ListP pr, int n, int timeOUT);
28
void output_RR(ListP pr, ListP RL, int n, int m, int timeOUT);
29
void output_PRIORITY_preemptive(ListP pr, int n, int timeOUT);
30
void output_PRIORITY_nopreemptive(ListP pr, ListP RL, int n, int m, int timeOUT);
31
void output_SJF_preemptive(ListP pr, int n, int timeOUT);
32
void output_SJF_nopreemptive(ListP pr, ListP RL, int n, int m, int timeOUT);
33
34
void OUTPUT(ListP pr, ListP RL, int n, int m, int timeOUT, int set);
35
36
void process_FIFO(ListP &pr, int n, int timeOUT);
37
void process_RR(ListP &pr, ListP &RL, int n, int &m, int timeOUT, int quantum);
38
void process_PRIORITY_preemptive(ListP &pr, int n, int timeOUT);
39
void process_PRIORITY_nopreemptive(ListP &pr, ListP &RL, int n, int &m, int timeOUT);
40
void process_SJF_preemptive(ListP &pr, int n, int timeOUT);
41
void process_SJF_nopreemptive(ListP &pr, ListP &RL, int n, int &m, int timeOUT);
42
43
void PROCESS(ListP &pr, ListP &RL, int n, int &m, int timeOUT, int set);
44
45
46
void input(ListP &pr, int &n, int &timeOUT)
47
{
48
	timeOUT = 0;
49
	ifstream in(INP);
50
	if (in == NULL)
51
	{
52
		cout<<"Not input file !";
53
		return;
54
	}
55
	in>>n;
56
	in>>set;
57
	in>>quantum;	
58
	pr = new process[n];
59
	for (int i=0; i<n; i++)
60
	{
61
		in>> pr[i].name;
62
		in>> pr[i].timeRL;
63
		in>> pr[i].timeCPU;
64
		in>> pr[i].priority;
65
		if (timeOUT < pr[i].timeRL)
66
			timeOUT = pr[i].timeRL + pr[i].timeCPU;
67
		else timeOUT += pr[i].timeCPU;
68
		pr[i].index = i;
69
	}
70
}
71
72
void output_input(ListP pr, int n)
73
{
74
	cout<<"INPUT"<<endl<<endl;
75
	cout <<"Name" <<setw(10) <<"TimeRL" <<setw(10) <<"TimeCPU" <<setw(10) <<"Priority" <<endl;
76
	for (int i=0; i<n; i++)
77
		cout <<pr[i].name <<setw(10) <<pr[i].timeRL <<setw(10) <<pr[i].timeCPU <<setw(10) <<pr[i].priority<<endl;
78
	cout<<"quantum = "<<quantum<<endl;
79
	cout<<endl<<"-------------------------------"<<endl<<endl;
80
}
81
82
83
void output_FIFO(ListP pr, int n, int timeOUT)
84
{
85
	cout<<"FIFO"<<endl<<endl<<"PROCESS"<<endl<<endl;
86
	cout <<"Name" <<setw(10) <<"TimeRL" <<setw(10) <<"TimeCPU" <<setw(10) <<"Priority" <<setw(10) <<"TimeIN" <<setw(10) <<"TimeOUT" <<setw(10) <<"Timewait" <<setw(10) << "Timesave" <<endl;
87
	for (int i=0; i<n; i++)
88
		cout <<pr[i].name <<setw(10) <<pr[i].timeRL <<setw(10) <<pr[i].timeCPU <<setw(10) <<pr[i].priority <<setw(10) <<pr[i].timeIN <<setw(10) <<pr[i].timeOUT <<setw(10) <<pr[i].timewait <<setw(10) << pr[i].timesave <<endl;
89
	cout<<endl<<"-------------the end-------------"<<endl<<endl;		
90
}
91
92
void output_RR(ListP pr, ListP RL, int n, int m, int timeOUT)
93
{
94
	cout<<"ROUND ROBIN"<<endl<<endl<<"OUTPUT"<<endl<<endl;
95
	cout <<"Name" <<setw(10) <<"TimeOUT" <<setw(10) <<"Timewait" <<setw(10) << "Timesave" <<endl;
96
	for (int i=0; i<n; i++)
97
		cout <<pr[i].name <<setw(10) <<pr[i].timeOUT <<setw(10) <<pr[i].timewait <<setw(10) << pr[i].timesave <<endl;
98
	
99
	cout<<endl<<endl<<"---PROCESS---"<<endl<<endl;
100
	
101
	cout <<"Name" <<setw(10) <<"TimeRL" <<setw(10) <<"TimeCPU" <<setw(10) <<"Priority" <<setw(10) <<"TimeIN" <<setw(10) <<"TimeOUT" <<endl;
102
	for (int i=0; i<m; i++)
103
		cout <<RL[i].name <<setw(10) <<RL[i].timeRL <<setw(10) <<RL[i].timeCPU <<setw(10) <<RL[i].priority <<setw(10) <<RL[i].timeIN <<setw(10) <<RL[i].timeOUT <<endl;
104
	cout<<endl<<"-------------the end-------------"<<endl<<endl;	
105
}
106
107
void output_PRIORITY_preemptive(ListP pr, int n, int timeOUT)
108
{
109
	cout<<"PRIORITY preemptive"<<endl<<endl<<"PROCESS"<<endl<<endl;
110
	cout <<"Name" <<setw(10) <<"TimeRL" <<setw(10) <<"TimeCPU" <<setw(10) <<"Priority" <<setw(10) <<"TimeIN" <<setw(10) <<"TimeOUT" <<setw(10) <<"Timewait" <<setw(10) << "Timesave" <<endl;
111
	for (int i=0; i<n; i++)
112
		cout <<pr[i].name <<setw(10) <<pr[i].timeRL <<setw(10) <<pr[i].timeCPU <<setw(10) <<pr[i].priority <<setw(10) <<pr[i].timeIN <<setw(10) <<pr[i].timeOUT <<setw(10) <<pr[i].timewait <<setw(10) << pr[i].timesave <<endl;
113
	cout<<endl<<"-------------the end-------------"<<endl<<endl;		
114
}
115
116
void output_SJF_preemptive(ListP pr, int n, int timeOUT)
117
{
118
	cout<<"SJF preemptive"<<endl<<endl<<"PROCESS"<<endl<<endl;
119
	cout <<"Name" <<setw(10) <<"TimeRL" <<setw(10) <<"TimeCPU" <<setw(10) <<"Priority" <<setw(10) <<"TimeIN" <<setw(10) <<"TimeOUT" <<setw(10) <<"Timewait" <<setw(10) << "Timesave" <<endl;
120
	for (int i=0; i<n; i++)
121
		cout <<pr[i].name <<setw(10) <<pr[i].timeRL <<setw(10) <<pr[i].timeCPU <<setw(10) <<pr[i].priority <<setw(10) <<pr[i].timeIN <<setw(10) <<pr[i].timeOUT <<setw(10) <<pr[i].timewait <<setw(10) << pr[i].timesave <<endl;
122
	cout<<endl<<"-------------the end-------------"<<endl<<endl;		
123
}
124
125
void output_PRIORITY_nopreemptive(ListP pr, ListP RL, int n, int m, int timeOUT)
126
{
127
	cout<<"PRIORITY nopreemptive"<<endl<<endl<<"OUTPUT"<<endl<<endl;
128
	cout <<"Name" <<setw(10) <<"TimeOUT" <<setw(10) <<"Timewait" <<setw(10) << "Timesave" <<endl;
129
	for (int i=0; i<n; i++)
130
		cout <<pr[i].name <<setw(10) <<pr[i].timeOUT <<setw(10) <<pr[i].timewait <<setw(10) << pr[i].timesave <<endl;
131
	
132
	cout<<endl<<endl<<"---PROCESS---"<<endl<<endl;
133
	
134
	cout <<"Name" <<setw(10) <<"TimeRL" <<setw(10) <<"TimeCPU" <<setw(10) <<"Priority" <<setw(10) <<"TimeIN" <<setw(10) <<"TimeOUT" <<endl;
135
	for (int i=0; i<m; i++)
136
		cout <<RL[i].name <<setw(10) <<RL[i].timeRL <<setw(10) <<RL[i].timeCPU <<setw(10) <<RL[i].priority <<setw(10) <<RL[i].timeIN <<setw(10) <<RL[i].timeOUT <<endl;
137
	cout<<endl<<"-------------the end-------------"<<endl<<endl;	
138
}
139
140
void output_SJF_nopreemptive(ListP pr, ListP RL, int n, int m, int timeOUT)
141
{
142
	cout<<"SJF nopreemptive"<<endl<<endl<<"OUTPUT"<<endl<<endl;
143
	cout <<"Name" <<setw(10) <<"TimeOUT" <<setw(10) <<"Timewait" <<setw(10) << "Timesave" <<endl;
144
	for (int i=0; i<n; i++)
145
		cout <<pr[i].name <<setw(10) <<pr[i].timeOUT <<setw(10) <<pr[i].timewait <<setw(10) << pr[i].timesave <<endl;
146
	
147
	cout<<endl<<endl<<"---PROCESS---"<<endl<<endl;
148
	
149
	cout <<"Name" <<setw(10) <<"TimeRL" <<setw(10) <<"TimeCPU" <<setw(10) <<"Priority" <<setw(10) <<"TimeIN" <<setw(10) <<"TimeOUT" <<endl;
150
	for (int i=0; i<m; i++)
151
		cout <<RL[i].name <<setw(10) <<RL[i].timeRL <<setw(10) <<RL[i].timeCPU <<setw(10) <<RL[i].priority <<setw(10) <<RL[i].timeIN <<setw(10) <<RL[i].timeOUT <<endl;
152
	cout<<endl<<"-------------the end-------------"<<endl<<endl;	
153
}
154
155
156
void process_FIFO(ListP &pr, int n, int timeOUT)
157
{
158
	ListP RL = new process[n];
159
	int m = -1;
160
	for (int t=0; t<timeOUT; t++)
161
	{
162
		for (int i=0; i<n; i++)
163
			if (t == pr[i].timeRL)
164
				RL[++m] = pr[i];
165
	}
166
	timeOUT = 0;
167
	for (int i=0; i<=m; i++)
168
	{
169
		if (timeOUT <= RL[i].timeRL) 
170
		{
171
			timeOUT = RL[i].timeRL + RL[i].timeCPU;
172
			RL[i].timeIN = RL[i].timeRL;
173
		}
174
		else 
175
		{
176
			timeOUT += RL[i].timeCPU;
177
			RL[i].timeIN = RL[i-1].timeOUT;
178
		}
179
		RL[i].timeOUT = timeOUT;
180
		RL[i].timewait = RL[i].timeOUT - (RL[i].timeRL + RL[i].timeCPU);
181
		RL[i].timesave = RL[i].timeOUT - RL[i].timeRL;
182
	}
183
	pr = RL;
184
}
185
186
187
void process_RR(ListP &pr, ListP &RL, int n, int &m, int timeOUT, int quantum) //Round Robin
188
{
189
	RL = new process;
190
	ListP pr1 = pr; //list temp of pr
191
	m = 0; // the number of element in RL
192
	int count = 0; //count time quantum
193
	int j = 0;
194
	int temptime = 0;
195
	for (int t=0; t<=timeOUT; t++)
196
	{
197
		if (m > 0 && j<m)
198
		{
199
			count++;
200
			if (count <= quantum && RL[j].timeCPU - temptime >0)
201
				temptime++;
202
			if (count == quantum && RL[j].timeCPU - temptime > 0)
203
				{
204
					m++;
205
					RL = (process *) realloc(RL,m*sizeof(process));
206
					RL[m-1] = RL[j];
207
					RL[m-1].timeCPU -= temptime;
208
				}
209
			if (RL[j].timeCPU - temptime == 0)
210
				{
211
					
212
					pr1[RL[j].index].timeOUT = t;
213
					pr1[RL[j].index].timewait = pr1[RL[j].index].timeOUT - (pr1[RL[j].index].timeRL + pr1[RL[j].index].timeCPU);
214
					pr1[RL[j].index].timesave = pr1[RL[j].index].timeOUT - pr1[RL[j].index].timeRL;
215
				}
216
			if (count == quantum || RL[j].timeCPU - temptime == 0)
217
				{
218
					RL[j].timeOUT = t;
219
					RL[j].timeCPU = temptime;
220
					RL[j].timeIN = t - RL[j].timeCPU;
221
					j++;
222
					temptime = 0;
223
					count = 0;
224
				}
225
		}
226
			
227
		for (int i=0; i<n; i++)
228
			if (t == pr1[i].timeRL)
229
			{
230
				m++;
231
				RL = (process *) realloc(RL,m*sizeof(process));
232
				RL[m-1] = pr1[i];
233
			}
234
	}
235
}
236
237
void process_PRIORITY_preemptive(ListP &pr, int n, int timeOUT)
238
{
239
	ListP RL = new process[n];
240
	ListP pr1 = pr; //list temp of pr
241
	int j = 0, m = 0;
242
	int temptime = 0;
243
	for (int t=0; t<=timeOUT; t++)
244
	{
245
		if (m > 0 && j < m)
246
		{
247
			if (temptime < RL[j].timeCPU)
248
				temptime ++;
249
			if (temptime == RL[j].timeCPU)
250
			{
251
				RL[j].timeIN = t - RL[j].timeCPU;
252
				RL[j].timeOUT = RL[j].timeIN + RL[j].timeCPU;
253
				RL[j].timewait = RL[j].timeOUT - (RL[j].timeRL + RL[j].timeCPU);
254
				RL[j].timesave = RL[j].timeOUT - RL[j].timeRL;
255
				temptime = 0;
256
				j++;
257
			}		
258
		}	
259
		for (int i=0; i<n; i++)
260
			if (t == pr1[i].timeRL)
261
			{
262
				int k = m;
263
				while (k > j+1 && pr1[i].priority < RL[k-1].priority)
264
				{
265
					RL[k] = RL[k-1];
266
					k--;
267
				}
268
				RL[k] = pr1[i];
269
				m++;
270
			}		
271
	}
272
	pr = RL;
273
}
274
275
void process_PRIORITY_nopreemptive(ListP &pr, ListP &RL, int n, int &m, int timeOUT)
276
{
277
	RL = new process;
278
	ListP pr1 = pr; //list temp of pr
279
	process temp;
280
	int j = 0;
281
	m = 0;
282
	int temptime = 0;
283
	for (int t=0; t<=timeOUT; t++)
284
	{
285
		if (m > 0 && j < m)
286
		{
287
			if (temptime < RL[j].timeCPU)
288
				temptime ++;
289
			if (temptime == RL[j].timeCPU)
290
			{
291
				RL[j].timeIN = t - RL[j].timeCPU;
292
				RL[j].timeOUT = RL[j].timeIN + RL[j].timeCPU;
293
				RL[j].timewait = RL[j].timeOUT - (RL[j].timeRL + RL[j].timeCPU);
294
				RL[j].timesave = RL[j].timeOUT - RL[j].timeRL;
295
								
296
				pr1[RL[j].index].timeOUT = t;
297
				pr1[RL[j].index].timewait = pr1[RL[j].index].timeOUT - (pr1[RL[j].index].timeRL + pr1[RL[j].index].timeCPU);
298
				pr1[RL[j].index].timesave = pr1[RL[j].index].timeOUT - pr1[RL[j].index].timeRL;
299
				
300
				temptime = 0;
301
				j++;
302
			}		
303
		}	
304
		for (int i=0; i<n; i++)
305
			if (t == pr1[i].timeRL)
306
			{
307
				m++;
308
				int k = m-1;
309
				RL = (process *) realloc(RL,m*sizeof(process));
310
				if (temptime > 0 && pr1[i].priority < RL[j].priority)
311
				{
312
					m++;
313
					k = m - 1;
314
					RL = (process *) realloc(RL,m*sizeof(process));
315
					for (k=m-1; k>j+1; k--)
316
						RL[k] = RL[k-2];
317
					RL[j+1] = pr1[i];
318
							
319
					RL[j+2] = RL[j];
320
					RL[j+2].timeCPU -= temptime;
321
					
322
					RL[j].timeIN = t - temptime;
323
					RL[j].timeOUT = t;
324
					RL[j].timeCPU = temptime; 									
325
					temptime = 0;
326
					j++;	
327
				}
328
				else
329
				{
330
					while (k > j && pr1[i].priority < RL[k-1].priority)
331
					{
332
						RL[k] = RL[k-1];
333
						k--;
334
					}
335
					RL[k] = pr1[i];	
336
				}
337
			}		
338
	}
339
}
340
341
342
void process_SJF_preemptive(ListP &pr, int n, int timeOUT)
343
{
344
	ListP RL = new process[n];
345
	ListP pr1 = pr; //list temp of pr
346
	int j = 0, m = 0;
347
	int temptime = 0;
348
	for (int t=0; t<=timeOUT; t++)
349
	{
350
		if (m > 0 && j < m)
351
		{
352
			if (temptime < RL[j].timeCPU)
353
				temptime ++;
354
			if (temptime == RL[j].timeCPU)
355
			{
356
				RL[j].timeIN = t - RL[j].timeCPU;
357
				RL[j].timeOUT = RL[j].timeIN + RL[j].timeCPU;
358
				RL[j].timewait = RL[j].timeOUT - (RL[j].timeRL + RL[j].timeCPU);
359
				RL[j].timesave = RL[j].timeOUT - RL[j].timeRL;
360
				temptime = 0;
361
				j++;
362
			}		
363
		}	
364
		for (int i=0; i<n; i++)
365
			if (t == pr1[i].timeRL)
366
			{
367
				int k = m;
368
				while (k > j+1 && pr1[i].timeCPU < RL[k-1].timeCPU)
369
				{
370
					RL[k] = RL[k-1];
371
					k--;
372
				}
373
				RL[k] = pr1[i];
374
				m++;
375
			}		
376
	}
377
	pr = RL;
378
}
379
380
void process_SJF_nopreemptive(ListP &pr, ListP &RL, int n, int &m, int timeOUT)
381
{
382
	RL = new process;
383
	ListP pr1 = pr; //list temp of pr
384
	process temp;
385
	int j = 0;
386
	m = 0;
387
	int temptime = 0;
388
	for (int t=0; t<=timeOUT; t++)
389
	{
390
		if (m > 0 && j < m)
391
		{
392
			if (temptime < RL[j].timeCPU)
393
				temptime ++;
394
			if (temptime == RL[j].timeCPU)
395
			{
396
				RL[j].timeIN = t - RL[j].timeCPU;
397
				RL[j].timeOUT = RL[j].timeIN + RL[j].timeCPU;
398
				RL[j].timewait = RL[j].timeOUT - (RL[j].timeRL + RL[j].timeCPU);
399
				RL[j].timesave = RL[j].timeOUT - RL[j].timeRL;
400
								
401
				pr1[RL[j].index].timeOUT = t;
402
				pr1[RL[j].index].timewait = pr1[RL[j].index].timeOUT - (pr1[RL[j].index].timeRL + pr1[RL[j].index].timeCPU);
403
				pr1[RL[j].index].timesave = pr1[RL[j].index].timeOUT - pr1[RL[j].index].timeRL;
404
				
405
				temptime = 0;
406
				j++;
407
			}		
408
		}	
409
		for (int i=0; i<n; i++)
410
			if (t == pr1[i].timeRL)
411
			{
412
				m++;
413
				int k = m-1;
414
				RL = (process *) realloc(RL,m*sizeof(process));
415
				if (temptime > 0 && pr1[i].timeCPU < RL[j].timeCPU - temptime)
416
				{
417
					m++;
418
					k = m - 1;
419
					RL = (process *) realloc(RL,m*sizeof(process));
420
					for (k=m-1; k>j+1; k--)
421
						RL[k] = RL[k-2];
422
					RL[j+1] = pr1[i];
423
							
424
					RL[j+2] = RL[j];
425
					RL[j+2].timeCPU -= temptime;
426
					
427
					RL[j].timeIN = t - temptime;
428
					RL[j].timeOUT = t;
429
					RL[j].timeCPU = temptime; 									
430
					temptime = 0;
431
					j++;	
432
				}
433
				else
434
				{
435
					while (k > j+1 && pr1[i].timeCPU < RL[k-1].timeCPU)
436
					{
437
						RL[k] = RL[k-1];
438
						k--;
439
						if (k == j+1 && pr1[i].timeCPU < RL[k-1].timeCPU - temptime)
440
						{
441
							RL[k] = RL[k-1];
442
							k--;	
443
						}
444
					}
445
					RL[k] = pr1[i];	
446
				}
447
			}		
448
	}
449
}
450
451
void PROCESS(ListP &pr, ListP &RL, int n, int &m, int timeOUT, int select)
452
{
453
	switch (select)
454
	{
455
		case 1: process_FIFO(pr, n, timeOUT); break;
456
		case 2: process_RR(pr, RL, n, m, timeOUT, quantum);break;
457
		case 3: process_PRIORITY_preemptive(pr, n, timeOUT);break;
458
		case 4: process_PRIORITY_nopreemptive(pr, RL, n, m, timeOUT); break;
459
		case 5: process_SJF_preemptive(pr, n, timeOUT); break;
460
		case 6: process_SJF_nopreemptive(pr, RL, n, m, timeOUT); break;
461
	}
462
}
463
464
void OUTPUT(ListP pr, ListP RL, int n, int m, int timeOUT, int select)
465
{
466
	switch (select)
467
	{
468
		case 1: output_FIFO(pr, n, timeOUT); break;
469
		case 2: output_RR(pr, RL, n, m, timeOUT); break;
470
		case 3: output_PRIORITY_preemptive(pr, n, timeOUT); break;
471
		case 4: output_PRIORITY_nopreemptive(pr, RL, n, m, timeOUT); break;
472
		case 5: output_SJF_preemptive(pr, n, timeOUT); break;
473
		case 6: output_SJF_nopreemptive(pr, RL, n, m, timeOUT); break;
474
	}
475
}
476
477
int main()
478
{
479
	ListP pr, RL;
480
	int n, m, timeOUT;
481
	input(pr, n, timeOUT);
482
	output_input(pr, n);
483
	PROCESS(pr, RL, n, m, timeOUT, set);
484
	OUTPUT(pr, RL, n, m, timeOUT, set);
485
	system("pause");
486
	return 0;
487-
}
487+
488
489
/*README file
490
Trong file input gom co:
491
- Dong dau tien la so tien trinh.
492
- Dong thu 2 la  quantum va cach dieu phoi CPU
493
     1: FIFO
494
     2: RR
495
     3: Do uu tien doc quyen
496
     4: Do uu tien khong doc quyen
497
     5: SJF doc quyen
498
     6: SJF khong doc quyen
499
- Cac dong tiep theo la so lieu cua cac tien trinh, lan luot la: ten, thoi gian vao danh sach san sang, thoi gian 
500
can CPU xu ly, do uu tien: (quy uoc do uu tien 1>2>3>...)
501
502
VD:
503
4
504
1 2
505
P1  0 20 3
506
P2  3 6 2
507
P3  3 5 1
508
P4  7 4 0
509
510
=> Du lieu gom 4 tien trinh, cach dieu phoi CPU la FIFO, quantum =2
511
tien trinh P1 co timeRL = 0, timeCPU = 20, do uu tien = 3.
512
513
514
*/