-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
2819 lines (2733 loc) · 109 KB
/
main.cpp
File metadata and controls
2819 lines (2733 loc) · 109 KB
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 <QCoreApplication>
#include <iostream>
#include "wtypes.h"
#include <windows.h>
#include <ctime>
#include <limits>
#include "dirent.h"
#include <algorithm>
#include <string>
#include <unistd.h>
#include <queue>
#include <fstream>
#include <algorithm>
#include <cctype>
#include <string>
#include <cstddef>
#include <time.h>
#define APP true
#define SYS false
#define RUNNING 0
#define READY 1
#define BLOCKED 2
#define NOT_SUSPENDED 0
#define SUSPENDED 1
using namespace std;
void InputLoop();
void DisplayDate(int year, int month, int day);
void RestoreCurrentDate(int &year, int &month, int &day);
int GetValidInt(int min, int max);
void DirectoryFiles();
void DisplayVersion();
void MaximizeWindow();
void ShowHelp();
void ExitProgram();
void AutoTests(int &year, int &month, int &day);
//PART 2 FUNCTIONS
struct PCB* AllocatePCB();
void FreePCB(struct PCB* inPCB);
struct PCB* SetupPCB(string name, int priority, bool classType);
struct PCB* FindPCB(string name, deque<struct PCB*> ready, deque<struct PCB*> blocked);
void InsertPCB(struct PCB* inPCB, deque<struct PCB*> *ready, deque<struct PCB*> *blocked);
void RemovePCB(struct PCB* inPCB, deque<struct PCB*> *ready, deque<struct PCB*> *blocked);
string GetPCBname();
void ShowPCB(deque<struct PCB*> ready, deque<struct PCB*> blocked);
void ShowAll(deque<struct PCB*> readyQueue, deque<struct PCB*> blockedQueue);
void ShowReady(deque<struct PCB *> ready);
void ShowBlocked(deque<struct PCB *> blocked);
//PART 3 FUNCTIONS
void ShortestJobFirst(deque<struct PCB*> *ready);
void FirstInFirstOut(deque<struct PCB*> *ready);
void ShortestTimeToCompletion(deque<struct PCB*> *ready);
void FixedPriority(deque<struct PCB*> *ready);
void RoundRobin(deque<struct PCB*> *ready);
void Multilevel(deque<struct PCB*> *ready);
void Lottery(deque<struct PCB*> *ready);
//PART 4 FUNCTIONS
void ShowMemory(struct MemorySpace memory);
void Coalesce(struct MemorySpace *memory);
void Compact(struct MemorySpace *memory);
//NOTE* the memory allocation algorithms have not been implemented with the schedulers, rather they only have a test function that proves each works
//since i could not seem to get them to work right within the schedulers
bool FirstFit(struct MemorySpace *memory, struct MemoryBlock *inBlock);
bool NextFit(struct MemorySpace *memory, struct MemoryBlock *inBlock, int position);
bool WorstFit(struct MemorySpace *memory, struct MemoryBlock *inBlock);
bool BestFit(struct MemorySpace *memory, struct MemoryBlock *inBlock);
void MemoryAllocation();
struct PCB{
string processName;
bool processClass;//application(true) or system(false) type
int priority;//int from -127 to +128
int stateOne;//0=running, 1=ready, 2=blocked
int stateTwo;//0=not suspended, 1=suspended
int memory;
int timeRemaining;
int timeOfArrival;
int percentCPU;
};
struct MemoryBlock{
string processName;//stores process name that it is tied to so we know which block represents which process
int min;//once the block is placed in memory, this will hold its starting point
int max;//holds the total size of the block before it is place in memory so we know how much memory it needs, once placed it is changed to represent the ending point
bool free;//tells whether or not the process associated with this block is free
};
struct MemorySpace{
int min;
int max;
vector<struct MemoryBlock> occupiedMemory;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//Call the AutoTests function to show some functionality automatically
//AutoTests(year, month, day);
InputLoop();
return a.exec();
}
//a big loop set up to implement the command prompt, lets the user repeatedly enter in commands
void InputLoop()
{
deque<struct PCB*> readyQueue;
deque<struct PCB*> blockedQueue;
//display a welcome message and the version number as well as set up the date variables and initialize them to the current date retrieved from the OS
cout << "Welcome to potatOS!\n";
DisplayVersion();
int year;
int month;
int day;
RestoreCurrentDate(year, month, day);
while (1)
{
string in;
cout << "\nEnter a command: ";
cin >> in;
transform(in.begin(), in.end(), in.begin(), ::tolower);
if (in.compare("version") == 0)
{
DisplayVersion();
}
else if (in.compare("displaydate") == 0)
{
DisplayDate(year, month, day);
}
else if (in.compare("setdate") == 0)
{
//Prompt the user to enter a custom date
day = GetValidInt(1,31);
month = GetValidInt(1,12);
year = GetValidInt(1,9999);
DisplayDate(year, month, day);
}
else if (in.compare("restoredate") == 0)
{
RestoreCurrentDate(year, month, day);
}
else if (in.compare("directoryfiles") == 0)
{
DirectoryFiles();
}
else if (in.compare("help") == 0)
{
ShowHelp();
}
else if (in.compare("exit") == 0)
{
ExitProgram();
}
/*USER COMMAND FOR ASSIGNMENT 2
*
else if (in.compare("createpcb") == 0)
{
while (1)
{
//check to see if the user enters in a valid, unique name for the PCB
string name = GetPCBname();
if (FindPCB(name, readyQueue, blockedQueue) == NULL)
{
while (1)
{
//check to see if the user enters in a valid class type
int classTypeIn;
cout << "\nEnter the class type of the new PCB (*must be application or system type, enter '1' for application or '0' for system*): ";
cin >> classTypeIn;
if (classTypeIn == 1 || classTypeIn == 0)
{
//set class type boolean variable accordingly
bool classType;
if (classTypeIn == 1)
{
classType = APP;
}
else
{
classType = SYS;
}
while (1)
{
//check to see if the user enters a valid integer for priority
int priority;
cout << "\nEnter the priority of the new PCB (*must be in the range of -127 to +128*): ";
cin >> priority;
if (priority >= -127 && priority <= 128)
{
struct PCB* newPCB = SetupPCB(name, priority, classType);
InsertPCB(newPCB, &readyQueue, &blockedQueue);
goto OUTER;
}
else
{
cout << "*ERROR: The input priority value was invalid. Try entering an integer between -127 and +128*" << endl;
}
}
}
else
{
cout << "*ERROR: The input class type was invalid. Try entering in either '1' for app or '2' for sys type*" << endl;
}
}
}
else
{
cout << "*ERROR: The input name was not unique, try entering a new name*" << endl;
}
}
OUTER:
cout << "PCB Successfully created" << endl;
}
*/
/*USER COMMAND FOR ASSIGNMENT 2
*
else if (in.compare("deletepcb") == 0)
{
//check to see if the user enters in an existing name for the PCB
string name = GetPCBname();
struct PCB* removed = FindPCB(name, readyQueue, blockedQueue);
if (removed != NULL)
{
RemovePCB(removed, &readyQueue, &blockedQueue);
FreePCB(removed);
cout << "PCB Successfully deleted" << endl;
}
else
{
cout << "*ERROR: No PCB found with that name*" << endl;
}
}
*/
/*USER COMMAND FOR ASSIGNMENT 2
*
else if (in.compare("block") == 0)
{
//check to see if the user enters in an existing name for the PCB
string name = GetPCBname();
struct PCB* removed = FindPCB(name, readyQueue, blockedQueue);
if (removed != NULL)
{
RemovePCB(removed, &readyQueue, &blockedQueue);
removed->stateOne = BLOCKED;
InsertPCB(removed, &readyQueue, &blockedQueue);
cout << "Successfully changed the PCB to the blocked status" << endl;
}
else
{
cout << "*ERROR: No PCB found with that name*" << endl;
}
}
*/
/*USER COMMAND FOR ASSIGNMENT 2
*
else if (in.compare("unblock") == 0)
{
//check to see if the user enters in an existing name for the PCB
string name = GetPCBname();
struct PCB* removed = FindPCB(name, readyQueue, blockedQueue);
if (removed != NULL)
{
RemovePCB(removed, &readyQueue, &blockedQueue);
removed->stateOne = READY;
InsertPCB(removed, &readyQueue, &blockedQueue);
cout << "Successfully changed the PCB to the unblocked status" << endl;
}
else
{
cout << "*ERROR: No PCB found with that name*" << endl;
}
}
*/
else if (in.compare("suspend") == 0)
{
//check to see if the user enters in an existing name for the PCB
string name = GetPCBname();
struct PCB* change = FindPCB(name, readyQueue, blockedQueue);
if (change != NULL)
{
change->stateTwo = SUSPENDED;
cout << "Successfully suspended the PCB" << endl;
}
else
{
cout << "*ERROR: No PCB found with that name*" << endl;
}
}
else if (in.compare("resume") == 0)
{
//check to see if the user enters in an existing name for the PCB
string name = GetPCBname();
struct PCB* change = FindPCB(name, readyQueue, blockedQueue);
if (change != NULL)
{
change->stateTwo = NOT_SUSPENDED;
cout << "Successfully resumed the PCB" << endl;
}
else
{
cout << "*ERROR: No PCB found with that name*" << endl;
}
}
else if (in.compare("setpriority") == 0)
{
//check to see if the user enters in an existing name for the PCB
string name = GetPCBname();
struct PCB* change = FindPCB(name, readyQueue, blockedQueue);
if (change != NULL)
{
int newPriority;
cout << "\nEnter the new priority level for this PCB (must be between -127 to +128): ";
cin >> newPriority;
if (newPriority >= -127 && newPriority <= 128)
{
change->priority = newPriority;
cout << "Successfully changed the PCB's priority" << endl;
}
else
{
cout << "*ERROR: The input priority value is invalid*" << endl;
}
}
else
{
cout << "*ERROR: No PCB found with that name*" << endl;
}
}
else if (in.compare("showpcb") == 0)
{
ShowPCB(readyQueue, blockedQueue);
}
else if (in.compare("showall") == 0)
{
ShowAll(readyQueue, blockedQueue);
}
else if (in.compare("showready") == 0)
{
ShowReady(readyQueue);
}
else if (in.compare("showblocked") == 0)
{
ShowBlocked(blockedQueue);
}
else if (in.compare("sjf") == 0)
{
ShortestJobFirst(&readyQueue);
}
else if (in.compare("fifo") == 0)
{
FirstInFirstOut(&readyQueue);
}
else if (in.compare("stcf") == 0)
{
ShortestTimeToCompletion(&readyQueue);
}
else if (in.compare("fpps") == 0)
{
FixedPriority(&readyQueue);
}
else if (in.compare("rr") == 0)
{
RoundRobin(&readyQueue);
}
else if (in.compare("mlfq") == 0)
{
Multilevel(&readyQueue);
}
else if (in.compare("ls") == 0)
{
Lottery(&readyQueue);
}
else if (in.compare("memorymethods") == 0)
{
MemoryAllocation();
}
else
{
cout << "Invalid command (try 'help' for a list of valid commands)" << endl;
}
}
}
//Simple print date function
void DisplayDate(int year, int month, int day)
{
cout << "DATE:" << month << '-' << day << '-' << year << "\n\n";
}
//get current date info and set variable accordingly
void RestoreCurrentDate(int &year, int &month, int &day)
{
time_t t = time(0);
struct tm * now = localtime( & t );
year = now->tm_year + 1900;
month = now->tm_mon + 1;
day = now->tm_mday;
}
//check to see if user input number is valid based on bounds. Code borrowed from comment section of: http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/
int GetValidInt(int min, int max)
{
int number;
if (max == 31)
{
cout << "Changing Day: ";
}
else if (max == 12)
{
cout << "Changing Month: ";
}
else
{
cout << "Changing Year: ";
}
while ( (cout << "Enter a valid number: " && !(cin >> number) ) || (number < min) || (number > max) || cin.peek() != '\n')
{
cout << "You must input a whole number between " << min << " and " << max << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return number;
}
//print all of the file names in the current directory
//for this function to work properly you may need to go to the projects tab, then the build & run tab, then the run tab, and then set the working directory to the proper directory
//I am unsure as to whether or not the working directory is set automatically when first running this on a new machine
void DirectoryFiles()
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("./")) != NULL)
{
//print all the files and directories within directory
cout << "\nDIRECTORY FILES: ";
while ((ent = readdir (dir)) != NULL)
{
printf ("%s\n", ent->d_name);
}
closedir (dir);
}
else
{
/* could not open directory */
perror ("");
}
}
//simple print function to display version number
void DisplayVersion()
{
cout << "\n--potatOS Version 0.3.0--\n\n";
}
//display each valid user command as well as what they do
void ShowHelp()
{
cout << "VALID COMMANDS:\n";
cout << "*note-- commands are NOT case sensitive *\n\n";
cout << "version -------- Displays the current version of the OS.\n";
cout << "displaydate ---- Displays the date.\n";
cout << "setdate -------- Allows user to set the date.\n";
cout << "restoredate ---- Sets the date to the current date.\n";
cout << "directoryfiles - Displays a list of all of the files in the OS directory.\n";
//cout << "createpcb ------ Allows the user to create a PCB.\n";
//cout << "deletepcb ------ Deletes a PCB with a user specified name.\n";
//cout << "block ---------- Changes a user specified PCB's state to blocked.\n";
//cout << "unblock -------- Changes a user specified PCB's state to ready.\n";
cout << "suspend -------- Changes a user specified PCB's state to suspended.\n";
cout << "resume --------- Changes a user specified PCB's state to not suspended.\n";
cout << "setpriority ---- Changes a user specified PCB's priority\n";
cout << "showpcb -------- Displays all information about a specified PCB\n";
cout << "showall -------- Displays some information about all PCBs\n";
cout << "showready ------ Displays some information about all PCBs in the ready state\n";
cout << "showblocked ---- Displays some information about all PCBs in the blocked state\n";
cout << "help ----------- Displays a list of all valid user commands as well as their function.\n";
cout << "sjf ------------ Performs the Shortest Job First scheduler.\n";
cout << "fifo ----------- Performs the First In First Out scheduler.\n";
cout << "stcf ----------- Performs the Shortest Time to Completion First scheduler.\n";
cout << "fpps ----------- Performs the Fixed Priority Pre-Emptive Scheduling scheduler.\n";
cout << "rr ------------- Performs the Round-Robin scheduler.\n";
cout << "mlfq ----------- Performs the Multilevel Feecback Queue scheduler.\n";
cout << "ls ------------- Performs the Lottery Scheduling scheduler.\n";
cout << "memorymethods -- Performs some memory allocation method tests.\n";
cout << "exit ----------- Ends the current session and exits the OS.\n\n";
}
//Exits the program
void ExitProgram()
{
while (1)
{
string in;
cout << "\nARE YOU SURE YOU WISH TO EXIT?\n'y' or 'yes' will terminate the program\n'n' or 'no' will return back to the command prompt: ";
cin >> in;
transform(in.begin(), in.end(), in.begin(), ::tolower);
if (in.compare("yes") == 0 || in.compare("y") == 0)
{
break;
}
else if (in.compare("no") == 0 || in.compare("n") == 0)
{
cout << "==== PROGRAM EXIT ABORTED ====" << endl;
return;
}
else
{
cout << "ERROR: You have entered an invalid response." << endl;
}
}
cout << "\n\n==== PROGRAM EXECUTION COMPLETE ====\n\n";
exit(0);
}
//Perform some automated tests to show that certain functions work
void AutoTests(int &year, int &month, int &day)
{
cout << "\n==AUTOMATED TESTS==\n\n";
//default set date values to current date and then display date.
RestoreCurrentDate(year, month, day);
DisplayDate(year, month, day);
//Displays all of the files located in the OS directory
DirectoryFiles();
//Example to show that commands are not case sensitive
string in;
cout << "Enter a command: ";
in = "VERSION";
//transform function changes to all lowercase
transform(in.begin(), in.end(), in.begin(), ::tolower);
cout << in << endl;
if (in.compare("version") == 0)
{
DisplayVersion();
}
ShowHelp();
}
/**************************************************************************/
/**************************PART 2 FUNCTIONS********************************/
/**************************************************************************/
//if malloc doesn't return 0, return malloc, else return NULL
struct PCB* AllocatePCB(){
struct PCB* temp = new PCB;
if (temp != 0)
{
return temp;
}
else
{
return NULL;
}
}
//if the pcb is null, return error, else free its memory
void FreePCB(struct PCB* inPCB){
if (inPCB == NULL)
{
cout << "ERROR: Something went wrong when attempting to free memory." << endl;
}
else
{
delete inPCB;
}
}
//initializes a PCB's content(name, priority,class)
struct PCB* SetupPCB(string name, int priority, bool classType, int memory, int timeRemaining, int timeArrival, int percentCPU)
{
struct PCB* inPCB = AllocatePCB();
inPCB->processName = name;
inPCB->priority = priority;
inPCB->processClass = classType;
inPCB->memory = memory;
inPCB->timeRemaining = timeRemaining;
inPCB->timeOfArrival = timeArrival;
inPCB->percentCPU = percentCPU;
inPCB->stateOne = READY;
inPCB->stateTwo = NOT_SUSPENDED;
return inPCB;
}
//Search for a PCB with a given name, return a pointer to it if found or NULL if not found
struct PCB* FindPCB(string name, deque<struct PCB*> ready, deque<struct PCB*> blocked)
{
//if both queues are empty return null
if (ready.size() == 0 && blocked.size() == 0)
{
return NULL;
}
else
{
//if ready queue isn't empty, go through it and search for PCB with the given name
if (ready.size() != 0)
{
for (unsigned int i = 0; i < ready.size(); i++)
{
struct PCB* temp = ready.at(i);
if (temp->processName == name)
{
return temp;
}
}
}
//if blocked queue isn't empty, go through it and search for PCB with the given name
if (blocked.size() != 0)
{
for (unsigned int i = 0; i < blocked.size(); i++)
{
struct PCB* temp = blocked.at(i);
if (temp->processName == name)
{
return temp;
}
}
}
}
//if the PCB isn't found in either queue, return NULL
return NULL;
}
//Insert a PCB into its appropriate queue
void InsertPCB(struct PCB* inPCB, deque<struct PCB*> *ready, deque<struct PCB*> *blocked)
{
//see if the PCB belongs in the ready queue
if (inPCB->stateOne == READY)
{
ready->push_back(inPCB);
}
//check to see if the PCB belongs in the blocked queue
else if (inPCB->stateOne == BLOCKED)
{
blocked->push_back(inPCB);
}
//show an error since the PCB has an invalid state
else
{
cout << "ERROR: PROCESS IS EITHER ALREADY RUNNING OR HAS AN INVALID STATE STATUS" << endl;
}
}
//Remove a PCB from its queue
void RemovePCB(struct PCB* inPCB, deque<struct PCB*> *ready, deque<struct PCB*> *blocked)
{
//check to see if the PCB exists
if (FindPCB(inPCB->processName, *ready, *blocked) == NULL)
{
cout << "ERROR: COULD NOT FIND PCB" << endl;
}
else
{
//if ready queue isn't empty, go through it and search for the PCB to remove it
if (ready->size() != 0)
{
for (unsigned int i = 0; i < ready->size(); i++)
{
//if temp's name = inPCB's name, then the PCB is in this queue
struct PCB* temp = ready->at(i);
if (temp->processName == inPCB->processName)
{
ready->erase(ready->begin() + i);
return;
}
}
}
//if blocked queue isn't empty, go through it and search for the PCB to remove it
if (blocked->size() != 0)
{
for (unsigned int i = 0; i < blocked->size(); i++)
{
struct PCB* temp = blocked->at(i);
if (temp->processName == inPCB->processName)
{
blocked->erase(ready->begin() + i);
return;
}
}
}
}
}
//get a string name for a pcb from the user
string GetPCBname()
{
string name;
cout << "\nEnter the name of the PCB: ";
cin >> name;
transform(name.begin(), name.end(), name.begin(), ::tolower);
return name;
}
//show info for a pcb
void ShowPCB(deque<struct PCB*> ready, deque<struct PCB*> blocked)
{
string name = GetPCBname();
struct PCB* toShow = FindPCB(name, ready, blocked);
if (toShow != NULL)
{
cout << " ******* PCB INFORMATION *******" << endl;
cout << " Name ---------- " << toShow->processName << endl;
if (toShow->processClass == APP)
{
cout << " Class --------- Application" << endl;
}
else
{
cout << " Class --------- System" << endl;
}
cout << " Priority ------ " << toShow->priority << endl;
if (toShow->stateOne == RUNNING)
{
cout << " State One ----- Running" << endl;
}
else if (toShow->stateOne == READY)
{
cout << " State One ----- Ready" << endl;
}
else
{
cout << " State One ----- Blocked" << endl;
}
if (toShow->stateTwo == SUSPENDED)
{
cout << " State Two ----- Suspended" << endl;
}
else
{
cout << " State Two ----- Not Suspended" << endl;
}
cout << " Memory -------- " << toShow->memory << endl;
cout << " ***** END PCB INFORMATION *****" << endl;
}
else
{
cout << "*ERROR: No PCB found with that name*" << endl;
}
}
//show info from all pcbs
void ShowAll(deque<struct PCB*> readyQueue, deque<struct PCB*> blockedQueue)
{
cout << " ******* PCB INFORMATION *******" << endl;
if (readyQueue.size() == 0 && blockedQueue.size() == 0)
{
cout << "*ERROR: There are currently no PCBs to show information*" << endl;
}
if (readyQueue.size() != 0)
{
for (unsigned int i = 0; i < readyQueue.size(); i++)
{
struct PCB* toShow = readyQueue.at(i);
cout << " Name -- " << toShow->processName << " || ";
cout << " Priority -- " << toShow->priority << " || ";
if (toShow->stateOne == RUNNING)
{
cout << " State One -- Running" << " || ";
}
else if (toShow->stateOne == READY)
{
cout << " State One -- Ready" << " || ";
}
else
{
cout << " State One -- Blocked" << " || ";
}
if (toShow->stateTwo == SUSPENDED)
{
cout << " State Two -- Suspended" << endl;
}
else
{
cout << " State Two -- Not Suspended" << endl;
}
}
}
if (blockedQueue.size() != 0)
{
for (unsigned int i = 0; i < blockedQueue.size(); i++)
{
struct PCB* toShow = blockedQueue.at(i);
cout << " Name -- " << toShow->processName << " || ";
cout << " Priority -- " << toShow->priority << " || ";
if (toShow->stateOne == RUNNING)
{
cout << " State One -- Running" << " || ";
}
else if (toShow->stateOne == READY)
{
cout << " State One -- Ready" << " || ";
}
else
{
cout << " State One -- Blocked" << " || ";
}
if (toShow->stateTwo == SUSPENDED)
{
cout << " State Two -- Suspended" << endl;
}
else
{
cout << " State Two -- Not Suspended" << endl;
}
}
}
cout << " ***** END PCB INFORMATION *****" << endl;
}
//show all pcbs in readyqueue
void ShowReady(deque<struct PCB*> ready)
{
cout <<" *****READY QUEUE*****" << endl;
if (ready.size() != 0)
{
for (unsigned int i = 0; i < ready.size(); i++)
{
struct PCB* toShow = ready.at(i);
cout << " Name -- " << toShow->processName << " ||";
cout << " Priority -- " << toShow->priority << " ||";
cout << " State One -- Ready" << " ||";
if (toShow->stateTwo == SUSPENDED)
{
cout << " State Two -- Suspended" << " ||";
}
else
{
cout << " State Two -- Not Suspended" << " ||";
}
cout << " Time Remaining -- " << toShow->timeRemaining << endl;
}
cout << endl;
}
else
{
cout << "*ERROR: There are no PCBs in the ready state*" << endl;
}
}
//show all pcbs in blockedqueue
void ShowBlocked(deque<struct PCB *> blocked)
{
cout <<" *****BLOCKED QUEUE*****" << endl;
if (blocked.size() != 0)
{
for (unsigned int i = 0; i < blocked.size(); i++)
{
struct PCB* toShow = blocked.at(i);
cout << " Name -- " << toShow->processName << " || ";
cout << " Priority -- " << toShow->priority << " || ";
cout << " State One -- Blocked" << " || ";
if (toShow->stateTwo == SUSPENDED)
{
cout << " State Two -- Suspended" << endl;
}
else
{
cout << " State Two -- Not Suspended" << endl;
}
}
cout << endl;
}
else
{
cout << "*ERROR: There are no PCBs in the blocked state*" << endl;
}
}
//
//ASSIGNMENT 3 FUNCTIONS
//
//perform the shortest job first scheduler
void ShortestJobFirst(deque<struct PCB*> *ready)
{
ifstream inFile;
string line = "";
string fileName;
cout << "Enter the data file name : ";
cin >> fileName;
//check to see if a file is open and if not, then oopen it and read in the info
if (!inFile.is_open())
{
inFile.open(fileName.c_str());
if (inFile.is_open())
{
//read in file line by line
while (getline(inFile, line))
{
int startPos=0;
int endPos=0;
vector<string> words;
//go through the line read in and store each word dilimeted by spaces into a string vecor
for (unsigned int i = 0; i < line.size(); i++)
{
char at = line.at(i);
if (at == ' ')
{
int size = endPos - startPos;
string word = line.substr(startPos, size);
words.push_back(word);
endPos++;
startPos = endPos;
}
else
{
endPos++;
}
}
//store the last value into the string vector
int size = endPos - startPos;
string word = line.substr(startPos, size);
words.push_back(word);
//change string values to proper types and setup the pcb
string temp = words.at(1);
bool classType;
if (temp == "A")
{
classType = APP;
}
else
{
classType = SYS;
}
temp = words.at(2);
int priority = atoi(temp.c_str());
temp = words.at(3);
int memory = atoi(temp.c_str());
temp = words.at(4);
int timeRemaining = atoi(temp.c_str());
temp = words.at(5);
int timeArrival = atoi(temp.c_str());
temp = words.at(6);
int percentCPU = atoi(temp.c_str());
struct PCB* newPCB = SetupPCB(words.at(0), priority, classType, memory, timeRemaining, timeArrival, percentCPU);
//check to see where the new PCB should be in the deque
if (ready->size() == 0)
{
ready->push_back(newPCB);
}
else
{
bool inserted = false;
for (unsigned int i = 0; i < ready->size(); i++)
{
struct PCB* previous = ready->at(i);
if (newPCB->timeRemaining < previous->timeRemaining)
{
ready->insert(ready->begin() + i, newPCB);
inserted = true;
break;
}
}
if (inserted == false)
{
ready->push_back(newPCB);
}
}
}
inFile.close();
}
else
{
cout << "*ERROR: Unable to open file*\n";
return;
}
}
else
{
cout << "*ERROR: File is already open*\n";
return;
}
//show ready queue to display the scheduler is ordered properly and then execute processes
ShowReady(*ready);
deque<struct PCB*> completedProcesses;
int completion = 0; //total time to completion
float average = 0; //average turnaround time
int numJobs = 0; //total number of jobs
if(ready->size() != 0)
{
while(ready->size() != 0)
{
struct PCB* running = ready->front();
ready->pop_front();
completion = completion + running->timeRemaining;
numJobs++;
Sleep(running->timeRemaining * 1000); //parameter time is in milliseconds so i am multiplying by 1000 to wait for temp.timeremaining seconds
completedProcesses.push_back(running);
cout << " ** " << running->processName << " completed **" << endl;
}
}