-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.c
2201 lines (2071 loc) · 111 KB
/
scheduler.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <math.h>
#define True 1
#define False 0
#define MAX 1000
/* type definitions --------------------------------------------- -------*/
typedef struct node node_t;
struct node {
//time-arrived, process-id, memory-size-req, job-time
long long int time_arrived;
long long int process_id;
long long int memory_size_req;
long long int job_time;
long long int remain_time;
long long int quantum;
long long int load_time;//load time = page_num/2 = memory_size/2
long long int page_num;//page_num = memory_size/4
long long int last_stop_time;
long long int *take_up_page;
long long int allocated_page;
long long int frequency;
node_t *next; /* the pointer that point to the next node */
};
typedef struct sta sta_t;
struct sta {
//time-arrived, process-id, memory-size-req, job-time
long long int time_arrived;
long long int process_id;
long long int job_time;
long long int time_complete;
long long int interval;
sta_t *next; /* the pointer that point to the next node */
};
typedef struct {
node_t *head; /* the start node of this link list */
node_t *foot; /* the end node of this link list */
} list_t;
typedef struct {
sta_t *head; /* the start node of this link list */
sta_t *foot; /* the end node of this link list */
} statistic_t;
/* type definitions --------------------------------------------- -------*/
/*-----------------------------------function declaration-------------------------------*/
/*read in all the file*/
void read_in_cmd(char *argv[], int argc, char *cmd_arg[]);
void read_file_into_list(char* filename, list_t *process_input, long long int quantum);
/*only the scheduling method*/
long long int run_ff_algorithm(list_t *process_input,statistic_t *statistics);
long long int run_cs_algorithm(list_t *process_input,statistic_t *statistics);
long long int run_rr_algorithm(list_t *process_input, long long int quantum, statistic_t *statistics);
long long int run_p_on_ff_algorithm(list_t *process_input,long long int memory_size, statistic_t *statistics);
long long int run_p_on_cs_algorithm(list_t *process_input,long long int memory_size, statistic_t *statistics);
long long int run_v_on_cs_algorithm(list_t *process_input,long long int memory_size, statistic_t *statistics);
long long int run_p_on_rr_algorithm(list_t *process_input,long long int memory_size, long long int quantum, statistic_t *statistics);
long long int run_v_on_rr_algorithm(list_t *process_input,long long int memory_size, long long int quantum, statistic_t *statistics);
long long int run_cm_on_rr_algorithm(list_t *process_input,long long int memory_size, long long int quantum, statistic_t *statistics);
long long int check_arrival(long long int simulator_timer, list_t *process_input, list_t *to_do_list, long long int num_processes_arrived);
node_t *find_least_rec_pro(list_t *to_do_list);
node_t *find_least_rec_pro_for_v(list_t *to_do_list);
node_t *find_most_rec_pro_for_v(list_t *to_do_list);
void assigned_value_to_empty_page(long long int* pages,long long int num_of_page,long long int value);
long long int find_none_empty_space(long long int* pages,long long int num_of_page);
void apply_shortest_job(list_t *to_do_list);
node_t * find_shortest_job(list_t *to_do_list);
/*node operation*/
node_t *create_a_node(long long int time_arrived, long long int process_id, long long int memory_size_req, long long int job_time, long long int remain_time, long long int quantum,long long int page_num, long long int load_time, long long int* take_up_page, long long int last_stop_time, long long int allocated_page, long long int frequency);
/* list operations */
list_t *make_empty_list(void);
long long int is_empty_list(list_t*);
void free_list(list_t*);
list_t *insert_at_head(list_t *list, node_t *new);
list_t *insert_at_foot(list_t *list, node_t *new);
long long int get_head(list_t *list);
list_t *get_tail(list_t *list);
long long int get_weight(list_t *list);
void delete_a_node(list_t *list, long long int del_process_id);
void print_list(list_t *list);
long long int length_of_list(list_t *list);
void print_take_up_page(long long int* take_up_page, long long int num_of_page);
sta_t *create_sta_node(long long int time_arrived, long long int process_id, long long int job_time, long long int time_complete, long long int interval);
statistic_t *insert_a_sta(statistic_t *list, sta_t *new);
statistic_t *make_a_statisitics(void);
void perform_statistics(statistic_t *statistics, long long int finish_at);
//function for sort the list
int comp(const void*a, const void*b){
return *(long long int*)a-*(long long int*)b;
}
/*-----------------------------------function declaration-------------------------------*/
int
main(int argc, char *argv[]){
// read in commend line, and create a string array cmd_arg to record.
char* cmd_arg[5];
cmd_arg[4] = NULL;
read_in_cmd(argv, argc, cmd_arg);
/*read in the file*/
list_t *process_input = make_empty_list();
if(strcmp(cmd_arg[1], "rr")==0){
long long int quantum=atoi(cmd_arg[4]);
read_file_into_list(cmd_arg[0], process_input, quantum);
}else{
read_file_into_list(cmd_arg[0], process_input, -1);
}
statistic_t *statistics = make_a_statisitics();
long long int finish_at = 0;
if(strcmp(cmd_arg[1], "ff")==0 && strcmp(cmd_arg[2], "u")==0){
finish_at=run_ff_algorithm(process_input,statistics);
}else if(strcmp(cmd_arg[1], "cs")==0 && strcmp(cmd_arg[2], "u")==0){
finish_at=run_cs_algorithm(process_input,statistics);
}else if(strcmp(cmd_arg[1], "rr")==0 && strcmp(cmd_arg[2], "u")==0){
finish_at=run_rr_algorithm(process_input, atoi(cmd_arg[4]),statistics);
}else if(strcmp(cmd_arg[1], "ff")==0 && strcmp(cmd_arg[2], "p")==0){
finish_at=run_p_on_ff_algorithm(process_input, atoi(cmd_arg[3]), statistics);
}else if(strcmp(cmd_arg[1], "cs")==0 && strcmp(cmd_arg[2], "p")==0){
finish_at=run_p_on_cs_algorithm(process_input, atoi(cmd_arg[3]), statistics);
}else if(strcmp(cmd_arg[1], "rr")==0 && strcmp(cmd_arg[2], "p")==0){
finish_at=run_p_on_rr_algorithm(process_input,atoi(cmd_arg[3]),atoi(cmd_arg[4]),statistics);
}else if(strcmp(cmd_arg[1], "ff")==0 && strcmp(cmd_arg[2], "v")==0){
//they should run with the same as the algorithm p
finish_at=run_p_on_ff_algorithm(process_input, atoi(cmd_arg[3]), statistics);
}else if(strcmp(cmd_arg[1], "rr")==0 && strcmp(cmd_arg[2], "v")==0){
finish_at=run_v_on_rr_algorithm(process_input,atoi(cmd_arg[3]),atoi(cmd_arg[4]),statistics);
}else if(strcmp(cmd_arg[1], "cs")==0 && strcmp(cmd_arg[2], "v")==0){
finish_at=run_v_on_cs_algorithm(process_input,atoi(cmd_arg[3]),statistics);
}else if(strcmp(cmd_arg[1], "rr")==0 && strcmp(cmd_arg[2], "cm")==0){
finish_at=run_cm_on_rr_algorithm(process_input,atoi(cmd_arg[3]),atoi(cmd_arg[4]),statistics);
}else if(strcmp(cmd_arg[1], "ff")==0 && strcmp(cmd_arg[2], "cm")==0){
finish_at=run_p_on_ff_algorithm(process_input, atoi(cmd_arg[3]), statistics);
}else if(strcmp(cmd_arg[1], "cs")==0 && strcmp(cmd_arg[2], "cm")==0){
finish_at=run_p_on_cs_algorithm(process_input, atoi(cmd_arg[3]), statistics);
}else{
printf("Sorry, I didn't implement this function so far\n");
exit(0);
}
/*do the performance statistics*/
perform_statistics(statistics, finish_at);
free_list(process_input);
return 0;
}
long long int
run_ff_algorithm(list_t *process_input,statistic_t *statistics){
long long int interval = 0;
long long int simulator_timer = 0;
//information about the arriving job
long long int num_processes_arrived = 0;
list_t *to_do_list = make_empty_list();
node_t *curr_process=NULL;
while(!is_empty_list(process_input)){
//set the interval for statisics
if((simulator_timer-1)%60==0&&simulator_timer!=1&&simulator_timer!=0){
interval++;
}
/*accept the arriving process*/
num_processes_arrived = check_arrival(simulator_timer, process_input, to_do_list, num_processes_arrived);
/*executing the process*/
if(curr_process!=NULL && curr_process->remain_time!=0){
/*continue dealing with current process*/
curr_process->remain_time--;
}else{
if(curr_process!=NULL){
/*this process has already finished*/
num_processes_arrived--;
printf("%lld, FINISHED, id=%lld, proc-remaining=%lld\n", simulator_timer,curr_process->process_id, num_processes_arrived);
//record the statistics;
sta_t *this_process = create_sta_node(curr_process->time_arrived, curr_process->process_id, curr_process->job_time, simulator_timer,interval);
statistics = insert_a_sta(statistics, this_process);
delete_a_node(process_input,curr_process->process_id);
delete_a_node(to_do_list,curr_process->process_id);
}
/*execute the next process*/
//find the right one
if(!is_empty_list(to_do_list)){
//there are still process need to execute in to-do-list
curr_process = to_do_list->head;
printf("%lld, RUNNING, id=%lld, remaining-time=%lld\n", simulator_timer, curr_process->process_id, curr_process->remain_time);
curr_process->remain_time--;
curr_process->frequency++;
}else if(!is_empty_list(process_input)){
//there are still process need to execute, but they just haven't arrived yet
//so do nothing just wait
curr_process = NULL;
}else{
//there is nothing to do here, so break out
break;
}
}
simulator_timer++;
}
free_list(to_do_list);
return simulator_timer;
}
long long int
run_cs_algorithm(list_t *process_input,statistic_t *statistics){
long long int interval = 0;
long long int simulator_timer = 0;
//information about the arriving job
long long int num_processes_arrived = 0;
list_t *to_do_list = make_empty_list();
node_t *curr_process=NULL;
while(!is_empty_list(process_input)){
//set the interval for statisics
if((simulator_timer-1)%60==0&&simulator_timer!=1&&simulator_timer!=0){
interval++;
}
/*accept the arriving process*/
num_processes_arrived = check_arrival(simulator_timer, process_input, to_do_list, num_processes_arrived);
/*executing the process*/
if(curr_process!=NULL && curr_process->remain_time!=0){
/*continue dealing with current process*/
curr_process->remain_time--;
}else{
if(curr_process!=NULL){
/*this process has already finished*/
num_processes_arrived--;
printf("%lld, FINISHED, id=%lld, proc-remaining=%lld\n", simulator_timer,curr_process->process_id, num_processes_arrived);
//record the statistics;
sta_t *this_process = create_sta_node(curr_process->time_arrived, curr_process->process_id, curr_process->job_time, simulator_timer,interval);
statistics = insert_a_sta(statistics, this_process);
delete_a_node(process_input,curr_process->process_id);
delete_a_node(to_do_list,curr_process->process_id);
}
/*execute the next process*/
//find the right one
if(!is_empty_list(to_do_list)){
//there are still process need to execute in to-do-list
apply_shortest_job(to_do_list);
curr_process = to_do_list->head;
printf("%lld, RUNNING, id=%lld, remaining-time=%lld\n", simulator_timer, curr_process->process_id, curr_process->remain_time);
curr_process->remain_time--;
curr_process->frequency++;
}else if(!is_empty_list(process_input)){
//there are still process need to execute, but they just haven't arrived yet
//so do nothing just wait
curr_process = NULL;
}else{
//there is nothing to do here, so break out
break;
}
}
simulator_timer++;
}
free_list(to_do_list);
return simulator_timer;
}
long long int
run_rr_algorithm(list_t *process_input, long long int quantum,statistic_t *statistics){
long long int interval = 0;
long long int simulator_timer = 0;
//information about the arriving job
long long int num_processes_arrived = 0;
list_t *to_do_list = make_empty_list();
node_t *curr_process=NULL;
while(!is_empty_list(process_input)){
//set the interval for statisics
if((simulator_timer-1)%60==0&&simulator_timer!=1&&simulator_timer!=0){
interval++;
}
//accept the arriving process
num_processes_arrived = check_arrival(simulator_timer, process_input, to_do_list, num_processes_arrived);
//executing the process
if(curr_process!=NULL && curr_process->remain_time!=0 && curr_process->quantum!=0){
//continue dealing with current process
curr_process->remain_time--;
curr_process->quantum--;
}else{
if(curr_process!=NULL){
//this process has already finished
if(curr_process->remain_time==0){
num_processes_arrived--;
printf("%lld, FINISHED, id=%lld, proc-remaining=%lld\n", simulator_timer,curr_process->process_id, num_processes_arrived);
sta_t *this_process = create_sta_node(curr_process->time_arrived, curr_process->process_id, curr_process->job_time, simulator_timer,interval);
statistics = insert_a_sta(statistics, this_process);
delete_a_node(process_input,curr_process->process_id);
delete_a_node(to_do_list,curr_process->process_id);
}else{
//delete from the head of the to-do-list, put at the end of the queue
//delete from head
//printf("%lld, ELASPED, id=%lld, proc-remaining=%lld, and the project still remain %lld time\n", simulator_timer,curr_process->process_id, num_processes_arrived,curr_process->remain_time);
node_t *next_round_process = create_a_node(curr_process->time_arrived,
curr_process->process_id,
curr_process->memory_size_req,
curr_process->job_time,
curr_process->remain_time,
quantum, 0, 0, NULL, simulator_timer,
curr_process->allocated_page,
curr_process->frequency);
delete_a_node(to_do_list,curr_process->process_id);
//insert at the end of the
to_do_list = insert_at_foot(to_do_list, next_round_process);
}
}
//execute the next process
//find the right one
if(!is_empty_list(to_do_list)){
//there are still process need to execute in to-do-list
curr_process = to_do_list->head;
printf("%lld, RUNNING, id=%lld, remaining-time=%lld\n", simulator_timer, curr_process->process_id, curr_process->remain_time);
curr_process->remain_time--;
curr_process->quantum--;
curr_process->frequency++;
}else if(!is_empty_list(process_input)){
//there are still process need to execute, but they just haven't arrived yet
//so do nothing just wait
curr_process = NULL;
}else{
//there is nothing to do here, so break out
break;
}
}
simulator_timer++;
}
free_list(to_do_list);
return simulator_timer;
}
long long int
run_p_on_ff_algorithm(list_t *process_input, long long int memory_size, statistic_t *statistics){
long long int interval = 0;
long long int simulator_timer = 0;
//remain page also equal total memory page minus empty_space_start.
//or total memory=empty_space_start+remain_page
long long int memory_page = memory_size/4; //const
long long int ram[MAX];
long long int remain_page = memory_page;
long long int empyt_space_start = 0;
// initialize the all the memory page as -1
long long int counter=0;
for(counter = 0; counter<MAX; counter++){
ram[counter] = -1;
}
//information about the arriving job
long long int num_processes_arrived = 0;
list_t *to_do_list = make_empty_list();
node_t *curr_process=NULL;
while(!is_empty_list(process_input)){
//set the interval for statisics
if((simulator_timer-1)%60==0&&simulator_timer!=1&&simulator_timer!=0){
interval++;
}
/*accept the arriving process*/
num_processes_arrived = check_arrival(simulator_timer, process_input, to_do_list, num_processes_arrived);
/*executing the process*/
if(curr_process!=NULL && curr_process->remain_time!=0 && curr_process->load_time!=0){
/*continue dealing with load time*/
curr_process->load_time--;
}else if(curr_process!=NULL && curr_process->remain_time!=0 && curr_process->load_time==0){
/*continue dealing with current process*/
curr_process->remain_time--;
}else{
//FINISH A PROGRESS
if(curr_process!=NULL){
/*this process has already finished*/
num_processes_arrived--;
empyt_space_start = curr_process->take_up_page[0];
remain_page = remain_page+curr_process->page_num;
printf("%lld, EVICTED, mem-addresses=", simulator_timer);
print_take_up_page(curr_process->take_up_page, curr_process->page_num);
long long int i=0;
for(i=0; i<curr_process->page_num; i++){
long long int assign_page=curr_process->take_up_page[i];
ram[assign_page] = -1;
}
printf("%lld, FINISHED, id=%lld, proc-remaining=%lld\n", simulator_timer,curr_process->process_id, num_processes_arrived);
//record the statistics;
sta_t *this_process = create_sta_node(curr_process->time_arrived, curr_process->process_id, curr_process->job_time, simulator_timer,interval);
statistics = insert_a_sta(statistics, this_process);
delete_a_node(process_input,curr_process->process_id);
delete_a_node(to_do_list,curr_process->process_id);
}
/*START A NEW PROGRASS*/
if(!is_empty_list(to_do_list)){
//there are still process need to execute in to-do-list
//choose a process to do.
curr_process = to_do_list->head;
//assign the memory to process
//There must be enough space for p mode, so we just assign the space to the process directly
long long int page_need= curr_process->page_num;
int memory_take_up_per;
if(remain_page>=page_need){
long long int i=0;
for(i=0; i<page_need; i++){
long long int assign_page=empyt_space_start+i;
curr_process->take_up_page[i]=assign_page;
ram[assign_page] = curr_process->process_id;
}
//change the empty_space_start
empyt_space_start = empyt_space_start+page_need;
remain_page = remain_page-page_need;
//calculate the memory take up percentage
//memory_take_up_per = ((double)page_need/memory_page)*100;
memory_take_up_per = ceil(((double)(memory_page-remain_page)/memory_page)*100); //this is (2)
}else{
printf("Even the memory is empty, there is still no space for the process, try other input!\n");
exit(0);
}
printf("%lld, RUNNING, id=%lld, remaining-time=%lld, load-time=%lld, mem-usage=%d%%, mem-addresses=", simulator_timer, curr_process->process_id, curr_process->remain_time, curr_process->load_time,memory_take_up_per);
print_take_up_page(curr_process->take_up_page, curr_process->page_num);
curr_process->load_time--;
curr_process->frequency++;
}else if(!is_empty_list(process_input)){
//there are still process need to execute, but they just haven't arrived yet
//so do nothing just wait
curr_process = NULL;
}else{
//there is nothing to do here, so break out
break;
}
}
simulator_timer++;
}
free_list(to_do_list);
return simulator_timer;
}
long long int
run_p_on_cs_algorithm(list_t *process_input,long long int memory_size, statistic_t *statistics){
long long int interval = 0;
long long int simulator_timer = 0;
//remain page also equal total memory page minus empty_space_start.
//or total memory=empty_space_start+remain_page
long long int memory_page = memory_size/4; //const
long long int ram[MAX];
long long int remain_page = memory_page;
long long int empyt_space_start = 0;
// initialize the all the memory page as -1
long long int counter=0;
for(counter = 0; counter<MAX; counter++){
ram[counter] = -1;
}
//information about the arriving job
long long int num_processes_arrived = 0;
list_t *to_do_list = make_empty_list();
node_t *curr_process=NULL;
while(!is_empty_list(process_input)){
//set the interval for statisics
if((simulator_timer-1)%60==0&&simulator_timer!=1&&simulator_timer!=0){
interval++;
}
/*accept the arriving process*/
num_processes_arrived = check_arrival(simulator_timer, process_input, to_do_list, num_processes_arrived);
/*executing the process*/
if(curr_process!=NULL && curr_process->remain_time!=0 && curr_process->load_time!=0){
/*continue dealing with load time*/
curr_process->load_time--;
}else if(curr_process!=NULL && curr_process->remain_time!=0 && curr_process->load_time==0){
/*continue dealing with current process*/
curr_process->remain_time--;
}else{
//FINISH A PROGRESS
if(curr_process!=NULL){
/*this process has already finished*/
num_processes_arrived--;
empyt_space_start = curr_process->take_up_page[0];
remain_page = remain_page+curr_process->page_num;
printf("%lld, EVICTED, mem-addresses=", simulator_timer);
print_take_up_page(curr_process->take_up_page, curr_process->page_num);
long long int i=0;
for(i=0; i<curr_process->page_num; i++){
long long int assign_page=curr_process->take_up_page[i];
ram[assign_page] = -1;
}
printf("%lld, FINISHED, id=%lld, proc-remaining=%lld\n", simulator_timer,curr_process->process_id, num_processes_arrived);
//record the statistics;
sta_t *this_process = create_sta_node(curr_process->time_arrived, curr_process->process_id, curr_process->job_time, simulator_timer,interval);
statistics = insert_a_sta(statistics, this_process);
delete_a_node(process_input,curr_process->process_id);
delete_a_node(to_do_list,curr_process->process_id);
}
/*START A NEW PROGRASS*/
if(!is_empty_list(to_do_list)){
//there are still process need to execute in to-do-list
//choose a process to do.
apply_shortest_job(to_do_list);
curr_process = to_do_list->head;
//assign the memory to process
//There must be enough space for p mode, so we just assign the space to the process directly
long long int page_need= curr_process->page_num;
int memory_take_up_per;
if(remain_page>=page_need){
long long int i=0;
for(i=0; i<page_need; i++){
long long int assign_page=empyt_space_start+i;
curr_process->take_up_page[i]=assign_page;
ram[assign_page] = curr_process->process_id;
}
//change the empty_space_start
empyt_space_start = empyt_space_start+page_need;
remain_page = remain_page-page_need;
//calculate the memory take up percentage
//memory_take_up_per = ((double)page_need/memory_page)*100;
memory_take_up_per = ceil(((double)(memory_page-remain_page)/memory_page)*100); //this is (2)
}else{
printf("Even the memory is empty, there is still no space for the process, try other input!\n");
exit(0);
}
printf("%lld, RUNNING, id=%lld, remaining-time=%lld, load-time=%lld, mem-usage=%d%%, mem-addresses=", simulator_timer, curr_process->process_id, curr_process->remain_time, curr_process->load_time,memory_take_up_per);
print_take_up_page(curr_process->take_up_page, curr_process->page_num);
curr_process->load_time--;
curr_process->frequency++;
}else if(!is_empty_list(process_input)){
//there are still process need to execute, but they just haven't arrived yet
//so do nothing just wait
curr_process = NULL;
}else{
//there is nothing to do here, so break out
break;
}
}
simulator_timer++;
}
free_list(to_do_list);
return simulator_timer;
}
long long int
run_p_on_rr_algorithm(list_t *process_input,long long int memory_size, long long int quantum, statistic_t *statistics){
long long int interval = 0;
long long int simulator_timer = 0;
//remain page also equal total memory page minus empty_space_start.
//or total memory=empty_space_start+remain_page
long long int memory_page = memory_size/4; //const
long long int ram[MAX];
long long int remain_page = memory_page;
long long int empyt_space_start = 0;
// initialize the all the memory page as -1
long long int counter=0;
for(counter = 0; counter<MAX; counter++){
ram[counter] = -1;
}
//information about the arriving job
long long int num_processes_arrived = 0;
list_t *to_do_list = make_empty_list();
node_t *curr_process=NULL;
while(!is_empty_list(process_input)){
//printf("time %lld :\n", simulator_timer);
//set the interval for statisics
if((simulator_timer-1)%60==0&&simulator_timer!=1&&simulator_timer!=0){
interval++;
}
//keep eye on the arriving process
num_processes_arrived = check_arrival(simulator_timer, process_input, to_do_list, num_processes_arrived);
if(curr_process!=NULL && curr_process->remain_time!=0 && curr_process->quantum!=0 && curr_process->load_time!=0){
/*continue dealing with load time*/
//printf("loading process %lld, there are %lld time need to load\n", curr_process->process_id,curr_process->load_time);
curr_process->load_time--;
}else if(curr_process!=NULL && curr_process->remain_time!=0 && curr_process->quantum!=0 && curr_process->load_time==0){
/*continue dealing with current process*/
//printf("executing process %lld, there are %lld time need to execute, and %lld time in quantum \n", curr_process->process_id,curr_process->remain_time, curr_process->quantum);
curr_process->remain_time--;
curr_process->quantum--;
}else{
//this is not the first process, then we need to finish the current process first
if(curr_process!=NULL){
printf("this is the only place");
//printf("current process %lld need to finish\n", curr_process->process_id);
//printf("another round assignment happened at %lld time", simulator_timer);
//this process has already finished
if(curr_process->remain_time==0){
remain_page = remain_page+curr_process->page_num;
empyt_space_start = curr_process->take_up_page[0];
printf("%lld, EVICTED, mem-addresses=", simulator_timer);
print_take_up_page(curr_process->take_up_page, curr_process->page_num);
long long int i=0;
for(i=0; i<curr_process->page_num; i++){
long long int assign_page=curr_process->take_up_page[i];
ram[assign_page] = -1;
}
num_processes_arrived--;
printf("%lld, FINISHED, id=%lld, proc-remaining=%lld\n", simulator_timer,curr_process->process_id, num_processes_arrived);
//record for the statistics
sta_t *this_process = create_sta_node(curr_process->time_arrived, curr_process->process_id, curr_process->job_time, simulator_timer,interval);
statistics = insert_a_sta(statistics, this_process);
//delete from the process input list
delete_a_node(process_input,curr_process->process_id);
//delete from the todo list
delete_a_node(to_do_list,curr_process->process_id);
}else{
// 3. make it load time as 0, because it has already being loaded inside the ram
node_t *next_round_process = create_a_node(curr_process->time_arrived,
curr_process->process_id,
curr_process->memory_size_req,
curr_process->job_time,
curr_process->remain_time,
quantum, 0,
curr_process->page_num,
curr_process->take_up_page,
simulator_timer,
curr_process->allocated_page,
curr_process->frequency);
//printf("the evict process: ");
//print_take_up_page(next_round_process->take_up_page, next_round_process->page_num);
//2. mark the process with its stop time
next_round_process->last_stop_time = simulator_timer;
//1. delete it from the head of the list
delete_a_node(to_do_list,curr_process->process_id);
//4. insert at the end of the
to_do_list = insert_at_foot(to_do_list, next_round_process);
}
}
//execute the next process
//then load a new one
//printf("check whether there is process on the to do list\n");
//print_list(to_do_list);
if(!is_empty_list(to_do_list)){
//printf("there are process need to process\n");
//printf("I start to find a new process in the to do list to load\n");
//there are still process need to execute in to-do-list
//choose a process to do.
curr_process = to_do_list->head;
//assign the memory to process
//If there is no
long long int page_need= curr_process->page_num;
if(memory_page<page_need){
printf("there is not enought space, even the memory is empty!\n");
exit(0);
}
//case 1: if there are enough pages for the new process, then we just assign memory to the processes directly
//case 2: otherwise, we need to evict other process according to when they finish(least recentely executed).
// there is another condition, that is the process has already being loaded already. no need to assign extra space
if(curr_process->load_time==0){
//I guess do nothing
}else if(remain_page>=page_need){
long long int found_page=0;
while(found_page!=page_need){
if(empyt_space_start==memory_page){
empyt_space_start=0;
}
if(ram[empyt_space_start]==-1){
ram[empyt_space_start]=curr_process->process_id;
curr_process->take_up_page[found_page]=empyt_space_start;
found_page++;
}
empyt_space_start++;
}
remain_page = remain_page-page_need;
}else{
long long int found_page=0;
while(found_page!=remain_page){
if(empyt_space_start==memory_page){
//that's end of the pages
empyt_space_start=0;
}
if(ram[empyt_space_start]==-1){
ram[empyt_space_start]=curr_process->process_id;
curr_process->take_up_page[found_page]=empyt_space_start;
found_page++;
}
empyt_space_start++;
}
//change the remain page value
remain_page = 0;
//then we need to start evict some other loaded processes.
while(found_page<page_need){
printf("I run here\n\n");
//printf("I sucked here last night to find new space for the new processes\n");
//OK, start the find the process
node_t *least_rec_pro = find_least_rec_pro(to_do_list);
//we already find out this process, let's evict it
//firstly, we make its load time back to origin
least_rec_pro->load_time=least_rec_pro->memory_size_req/2;
long long int this_pro_page = least_rec_pro->page_num;
printf("%lld, EVICTED, mem-addresses=", simulator_timer);
print_take_up_page(least_rec_pro->take_up_page, least_rec_pro->page_num);
//the page still need to find
//printf("I guess it means we have already find the right process to evict\n");
long long int diff=this_pro_page-(page_need-found_page);
long long int i=0;
if(diff==0||diff<0){
for(i=0;i<this_pro_page;i++){
// printf("i=%lld ", i);
// printf("found_page=%lld\n", found_page);
// printf("the old take up ram page is %lld\n",least_rec_pro->take_up_page[i]);
// printf("the ram %lld used to be take up by process %lld\n",ram[least_rec_pro->take_up_page[i],least_rec_pro->process_id);
curr_process->take_up_page[found_page]=least_rec_pro->take_up_page[i];
ram[least_rec_pro->take_up_page[i]]=curr_process->process_id;
least_rec_pro->take_up_page[i]=-1;
found_page++;
}
}else if(diff>0){
for(i=0;i<this_pro_page;i++){
if(i<=this_pro_page-page_need){
curr_process->take_up_page[found_page]=least_rec_pro->take_up_page[i];
ram[least_rec_pro->take_up_page[i]]=curr_process->process_id;
least_rec_pro->take_up_page[i]=-1;
found_page++;
}else{
ram[least_rec_pro->take_up_page[i]]=-1;
least_rec_pro->take_up_page[i]=-1;
remain_page++;
}
}
}
}
}
//calculate the memory take up percentage
int memory_take_up_per;
// I am not sure the precentage is of the process take how much of the whole memory(1)
// or of how much taked up memory out of the whole memory(2), take (1) at here, can change later
//memory_take_up_per = ((double)page_need/memory_page)*100; //this is (1)
memory_take_up_per = ceil(((double)(memory_page-remain_page)/memory_page)*100); //this is (2)
printf("%lld, RUNNING, id=%lld, remaining-time=%lld, load-time=%lld, mem-usage=%d%%, mem-addresses=", simulator_timer, curr_process->process_id, curr_process->remain_time, curr_process->load_time, memory_take_up_per);
//printf("something wrong happened here!!!!\n");
print_take_up_page(curr_process->take_up_page, curr_process->page_num);
//printf("something wrong happened here!!!!\n");
curr_process->frequency++;
if(curr_process->load_time==0){
curr_process->remain_time--;
curr_process->quantum--;
}else{
curr_process->load_time--;
}
//printf("Nothing wrong happened here!!!!\n");
}else if(!is_empty_list(process_input)){
//there are still process need to execute, but they just haven't arrived yet
//so do nothing just wait
curr_process = NULL;
}else{
//there is nothing to do here, so break out
break;
}
}
simulator_timer++;
//printf("time+1 is %lld :\n", simulator_timer);
}
free_list(to_do_list);
return simulator_timer;
}
long long int
run_v_on_rr_algorithm(list_t *process_input,long long int memory_size, long long int quantum, statistic_t *statistics){
long long int interval = 0;
long long int simulator_timer = 0;
//remain page also equal total memory page minus empty_space_start.
//or total memory=empty_space_start+remain_page
long long int memory_page = memory_size/4; //const
long long int ram[MAX];
long long int remain_page = memory_page;
long long int empyt_space_start = 0;
// initialize the all the memory page as -1
long long int counter=0;
for(counter = 0; counter<MAX; counter++){
ram[counter] = -1;
}
//information about the arriving job
long long int num_processes_arrived = 0;
list_t *to_do_list = make_empty_list();
node_t *curr_process=NULL;
while(!is_empty_list(process_input)){
//printf("time %lld: \n", simulator_timer);
//this part is for the statisics
if((simulator_timer-1)%60==0&&simulator_timer!=1&&simulator_timer!=0){
interval++;
}
//keep eye on the arriving process
num_processes_arrived = check_arrival(simulator_timer, process_input, to_do_list, num_processes_arrived);
if(curr_process!=NULL && curr_process->remain_time!=0 && curr_process->quantum!=0 && curr_process->load_time!=0){
/*continue dealing with load time*/
curr_process->load_time--;
}else if(curr_process!=NULL && curr_process->remain_time!=0 && curr_process->quantum!=0 && curr_process->load_time==0){
/*continue dealing with current process*/
curr_process->remain_time--;
curr_process->quantum--;
}else{
//this is not the first process, then we need to finish the current process
if(curr_process!=NULL){
//this process has already finished
if(curr_process->remain_time==0){
//printf("This is the finish part\n");
remain_page = remain_page+curr_process->allocated_page;
//doesn't change this part
empyt_space_start = curr_process->take_up_page[0];
printf("%lld, EVICTED, mem-addresses=", simulator_timer);
print_take_up_page(curr_process->take_up_page, curr_process->page_num);
long long int i=0;
for(i=0; i<curr_process->page_num; i++){
long long int assign_page=curr_process->take_up_page[i];
if(assign_page!=-1){
ram[assign_page] = -1;
}
}
num_processes_arrived--;
printf("%lld, FINISHED, id=%lld, proc-remaining=%lld\n", simulator_timer,curr_process->process_id, num_processes_arrived);
//record for the statistics
sta_t *this_process = create_sta_node(curr_process->time_arrived, curr_process->process_id, curr_process->job_time, simulator_timer,interval);
statistics = insert_a_sta(statistics, this_process);
//delete from the process input list
delete_a_node(process_input,curr_process->process_id);
//delete from the todo list
delete_a_node(to_do_list,curr_process->process_id);
}else{
// make it load time as 0, because it has already being loaded inside the ram
node_t *next_round_process = create_a_node(curr_process->time_arrived, curr_process->process_id,curr_process->memory_size_req,curr_process->job_time, curr_process->remain_time,quantum, 0, curr_process->page_num, curr_process->take_up_page, simulator_timer, curr_process->allocated_page, curr_process->frequency);
//1. delete it from the head of the list
delete_a_node(to_do_list,curr_process->process_id);
//4. insert at the end of the
to_do_list = insert_at_foot(to_do_list, next_round_process);
}
}
//then load a new one to execute
if(!is_empty_list(to_do_list)){
//there are still process need to execute in to-do-list
//choose a process to do.
curr_process = to_do_list->head;
/*assign the memory to process*/
long long int page_need= curr_process->page_num;
if(memory_page<page_need){
printf("there is not enought space, even the memory is empty!\n");
exit(0);
}
/*OK let's assign the page for the new process*/
//create the varible to record the minimum that we need for this process
long long int min_require=4;//default as 4 pages
long long int actual_require = curr_process->page_num-curr_process->allocated_page;
long long int old_page_allocate = curr_process->allocated_page;
if(page_need<=4){
min_require=page_need-curr_process->allocated_page;
}else{
min_require=4-curr_process->allocated_page;
}
/* let's start the party */
//the most exciting condition is that the all the pages the process need has already being allocated
if(actual_require==0){
//we can do nothing for this section. let's continue
}else if(remain_page>=min_require){
/*assign the empty to this process */
long long int ass_num_page=min_require;//default as equal to the min_require
//if there are more empty space, assign to it.
if(actual_require<=remain_page){
ass_num_page=actual_require;
}else{
ass_num_page=remain_page;
}
long long int assigned_page=0;
while(assigned_page!=ass_num_page){
if(empyt_space_start==memory_page){
empyt_space_start=0;
}
if(ram[empyt_space_start]==-1){
ram[empyt_space_start]=curr_process->process_id;
assigned_value_to_empty_page(curr_process->take_up_page,curr_process->page_num,empyt_space_start);
curr_process->take_up_page[assigned_page]=empyt_space_start;
curr_process->allocated_page++;
assigned_page++;
}
empyt_space_start++;
}
remain_page = remain_page-assigned_page;
}else{
/*which is need evict the process to allocate the current one*/
//because the remain page in the memory is must smaller than the mininum require pages
//so assign all the remain page in the memory to the
long long int assigned_page=0;
while(assigned_page!=remain_page){
if(empyt_space_start==memory_page){
//that's end of the pages
empyt_space_start=0;
}
if(ram[empyt_space_start]==-1){
ram[empyt_space_start]=curr_process->process_id;
curr_process->take_up_page[assigned_page]=empyt_space_start;
curr_process->allocated_page++;
assigned_page++;
}
empyt_space_start++;
}
//start need to find
long long int* converted_pages_this_time;
converted_pages_this_time = (long long int *)malloc(sizeof(long long int)*min_require);
assert(converted_pages_this_time!=NULL);
long long int num_of_page_remove_this_round=0;
while(assigned_page<min_require){
node_t *least_rec_pro = find_least_rec_pro_for_v(to_do_list);
/*
printf("----------------------------------------------------------------------------------------\n");
printf("time at:%lld \n", simulator_timer);
print_list(to_do_list);
printf("Before converting:\n");
printf("the process %lld's page will give to %lld\n", least_rec_pro->process_id, curr_process->process_id);
printf("its old page allocation situation is like this:");
print_take_up_page(least_rec_pro->take_up_page, least_rec_pro->page_num);
printf("its process that need to sign's page allocation situation is like this:");
print_take_up_page(curr_process->take_up_page, curr_process->page_num);
*/
long long int num_of_ass = 0;
long long int require_now = min_require-assigned_page;
/*printf("the min_require is %lld\n", min_require);
printf("the assigned page value now is %lld\n", assigned_page);
printf("the page that we want the lea_rec_pro to lect is %lld\n", require_now);
*/
if(least_rec_pro->allocated_page>=require_now){
num_of_ass=require_now;
}else{
num_of_ass=least_rec_pro->allocated_page;
}
//printf("number of ass need to assign is %lld\n",num_of_ass);
long long int counter =0;
for(counter=0;counter<num_of_ass;counter++){
//printf("counter is %lld\n",counter);
long long int remove_page_num=find_none_empty_space(least_rec_pro->take_up_page,least_rec_pro->page_num);
assert(remove_page_num!=-1);
//printf("the next place's ram space %lld will be converted, ", remove_page_num);
//assign to the new process first
assigned_value_to_empty_page(curr_process->take_up_page,curr_process->page_num,least_rec_pro->take_up_page[remove_page_num]);
curr_process->allocated_page++;
//record the removed pages
converted_pages_this_time[num_of_page_remove_this_round]=least_rec_pro->take_up_page[remove_page_num];
num_of_page_remove_this_round++;
//remove from the old procee
ram[least_rec_pro->take_up_page[remove_page_num]]=curr_process->process_id;
least_rec_pro->take_up_page[remove_page_num]=-1;
least_rec_pro->allocated_page--;
least_rec_pro->load_time=least_rec_pro->load_time+2;
assigned_page++;
}
/*
printf("After converting:\n");
printf("the process %lld's page will give to %lld\n", least_rec_pro->process_id, curr_process->process_id);
printf("its old page allocation situation is like this:");
print_take_up_page(least_rec_pro->take_up_page, least_rec_pro->page_num);
printf("the new page page number is %lld\n",curr_process->page_num);
print_take_up_page(curr_process->take_up_page, curr_process->page_num);