diff --git a/Assignment6/.idea/runConfigurations/3.xml b/Assignment6/.idea/runConfigurations/3.xml
new file mode 100644
index 0000000..b96611d
--- /dev/null
+++ b/Assignment6/.idea/runConfigurations/3.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Assignment6/.idea/runConfigurations/7b.xml b/Assignment6/.idea/runConfigurations/7b.xml
new file mode 100644
index 0000000..f33c11c
--- /dev/null
+++ b/Assignment6/.idea/runConfigurations/7b.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Assignment6/code/main.c b/Assignment6/code/main.c
new file mode 100644
index 0000000..2e53e06
--- /dev/null
+++ b/Assignment6/code/main.c
@@ -0,0 +1,244 @@
+#include
+#include
+#include
+#include
+
+// 1 - best fit
+// 2 - worst fit
+// 3 - first fit
+enum Algorithm {
+ BEST_FIT = 1,
+ WORST_FIT = 2,
+ FIRST_FIT = 3
+};
+enum Algorithm algorithm;
+int memSize;
+int totalAllocated = 0;
+int totalMemAllocated = 0;
+int totalFailed = 0;
+int totalTerminated = 0;
+int totalFreedMemory = 0;
+
+int *memory;
+
+void allocateByIndex(int index, int howMuchToAllocate, int processId) {
+ for (int i = index; i < index + howMuchToAllocate; ++i) {
+ memory[i] = processId;
+ // Statistics
+ totalMemAllocated++;
+ }
+}
+
+bool doAllocate(int howMuchToAllocate, int processId) {
+ int FirstFitIndex = -1;
+ int WorstFitIndex = -1;
+ int BestFitIndex = -1;
+
+ int size = 0;
+ int smallestSoFar = memSize;
+ int biggestSoFar = 0;
+ for (int i = 0; i < memSize; ++i) {
+ if (memory[i] == 0) {
+ size++;
+ // GTE, because we need to keep track of all algorithm indexes
+ if (size == howMuchToAllocate) {
+ // On first fit, set the index
+ if (FirstFitIndex == -1) {
+ FirstFitIndex = i - size + 1;
+ biggestSoFar = size;
+ smallestSoFar = size;
+ }
+ }
+ // If the size of the contiguous empty memory is bigger than the largest contiguous empty memory so far, set the index and update the size
+ if (size >= biggestSoFar) {
+ WorstFitIndex = i - size + 1;
+ biggestSoFar = size;
+ }
+
+ } else {
+ // If the size of the contiguous empty memory is smaller than the smallest contiguous empty memory so far, set the index and update the size
+ if (smallestSoFar >= size) {
+ BestFitIndex = i - size + 1;
+ smallestSoFar = size;
+ }
+ size = 0;
+ }
+ }
+ // Check if the last chunk is the smallest
+ if (smallestSoFar >= size) {
+ BestFitIndex = memSize - size;
+ }
+
+ if (howMuchToAllocate > memSize) {
+ printf("Process %d failed to allocate %d memory\n", processId, howMuchToAllocate);
+ // Statistics
+ totalFailed++;
+
+ // Early exit because there is no space, so don't touch memory
+ return false;
+ }
+
+ // Since first fit is always set if there is space, we can error on that for any algorithm
+ if (FirstFitIndex == -1) {
+ printf("Process %d failed to allocate %d memory\n", processId, howMuchToAllocate);
+ // Statistics
+ totalFailed++;
+
+ // Early exit because there is no space, so don't touch memory
+ return false;
+ }
+
+ switch (algorithm) {
+ case BEST_FIT: {
+ allocateByIndex(BestFitIndex, howMuchToAllocate, processId);
+ break;
+ }
+ case WORST_FIT: {
+ allocateByIndex(WorstFitIndex, howMuchToAllocate, processId);
+ break;
+ }
+ case FIRST_FIT: {
+ allocateByIndex(FirstFitIndex, howMuchToAllocate, processId);
+ break;
+ }
+ default: {
+ printf("There was an error, the algorithm is uninitialized");
+ exit(0);
+ }
+ }
+ // Statistics
+ totalAllocated++;
+ return true;
+}
+
+bool doFree(int processId) {
+ bool found = false;
+ for (int i = 0; i < memSize; ++i) {
+ if (memory[i] == processId) {
+ memory[i] = 0;
+ found = true;
+ // Statistics
+ totalFreedMemory++;
+ }
+ }
+ if (found) {
+ // Statistics
+ totalTerminated++;
+
+ } else {
+ printf("Process %d failed to free memory\n", processId);
+ // Statistics
+ // Might not need to be counted as a failed request?
+ totalFailed++;
+ }
+ return found;
+}
+
+int calcFinalMemory() {
+ int total = 0;
+ for (int i = 0; i < memSize; ++i) {
+ if (memory[i] == 0) {
+ total++;
+ }
+ }
+ return total;
+}
+
+int getNumberOfChunks() {
+ int total = 0;
+ bool inChunk = false;
+ for (int i = 0; i < memSize; ++i) {
+ if (memory[i] == 0) {
+ if (!inChunk) {
+ inChunk = true;
+ total++;
+ }
+ } else {
+ inChunk = false;
+ }
+ }
+ return total;
+}
+
+int getSmallest() {
+ int smallestSize = memSize;
+ int size = 0;
+ for (int i = 0; i < memSize; ++i) {
+ if (memory[i] == 0) {
+ size++;
+ } else if (smallestSize > size && size != 0) {
+ smallestSize = size;
+ size = 0;
+ }
+ }
+ // Check if the last chunk is the smallest
+ if (smallestSize == memSize) {
+ smallestSize = size;
+ }
+ return smallestSize;
+}
+
+int getBiggest() {
+ int biggestSize = 0;
+ int size = 0;
+ for (int i = 0; i < memSize; ++i) {
+ if (memory[i] == 0) {
+ size++;
+ } else {
+ if (size > biggestSize) {
+ biggestSize = size;
+ }
+ size = 0;
+ }
+ }
+ // Check if the last chunk is the biggest
+ if (biggestSize == 0) {
+ biggestSize = size;
+ }
+ return biggestSize;
+}
+
+int main(int argc, char **argv) {
+ for (int i = 0; i < argc; i++) {
+ if (strcmp(argv[i], "-b") == 0) {
+ algorithm = BEST_FIT;
+ } else if (strcmp(argv[i], "-w") == 0) {
+ algorithm = WORST_FIT;
+ } else if (strcmp(argv[i], "-s") == 0) {
+ memSize = atoi(argv[i + 1]);
+ } else if (strcmp(argv[i], "-f") == 0) {
+ algorithm = FIRST_FIT;
+ }
+ }
+ if (memSize >= 0) {
+ // Use calloc to initialize memory to 0, which means empty in our case
+ memory = calloc(memSize, sizeof(int));
+ } else {
+ printf("The program requires size\n");
+ exit(0);
+ }
+ char operation;
+ int id = 1337;
+ int size;
+ while (EOF != scanf("%c", &operation)) {
+ switch (operation) {
+ case 'N':
+ scanf(" %d %d\n", &id, &size);
+ doAllocate(size, id);
+ break;
+ case 'T':
+ scanf(" %d\n", &id);
+ doFree(id);
+ break;
+ case 'S':
+ printf("Total Processes created %d, Total allocated memory %d, Total Processes\n"
+ "terminated %d, Total freed memory %d, Final memory available %d, Final\n"
+ "smallest and largest fragmented memory sizes %d and %d, total failed requests:%d, number of memory chunks: %d\n",
+ totalAllocated, totalMemAllocated,
+ totalTerminated, totalFreedMemory, calcFinalMemory(),
+ getSmallest(), getBiggest(), totalFailed, getNumberOfChunks());
+ break;
+ }
+ }
+ return 0;
+}
\ No newline at end of file
diff --git a/Assignment6/code/starter_code.c b/Assignment6/code/starter_code.c
deleted file mode 100644
index 322d815..0000000
--- a/Assignment6/code/starter_code.c
+++ /dev/null
@@ -1,102 +0,0 @@
-#include
-#include
-#include
-
-// 1 - best fit
-// 2 - worst fit
-// 3 - first fit
-enum Algorithm {
- BEST_FIT = 1,
- WORST_FIT = 2,
- FIRST_FIT = 3
-};
-int algorithm;
-int memSize;
-int totalAllocated = 0;
-int totalMemAllocated = 0;
-int totalFailed = 0;
-int totalTerminated = 0;
-int totalFreedMemory = 0;
-
-
-int doFree(int processId) {
- return 0;
-}
-
-
-int doAllocate(int howMuchToAllocate, int processId) {
-
- switch (algorithm) {
- case 1: {
- break;
- }
- case 2: {
- break;
- }
- case 3: {
- break;
- }
- default: {
- printf("There was an error, the algorithm is uninitialized");
- exit(0);
- }
- }
-}
-
-int calcFinalMemory() {
- return 0;
-}
-
-int getNumberOfChunks() {
- return 0;
-}
-
-int getSmallest() {
- return 0;
-}
-
-int getBiggest() {
- return 0;
-}
-
-int main(int argc, char **argv) {
- int i = 0;
- for (i = 0; i < argc; i++) {
- if (strcmp(argv[i], "-b") == 0) {
- algorithm = 1;
- } else if (strcmp(argv[i], "-w") == 0) {
- algorithm = 2;
- } else if (strcmp(argv[i], "-s") == 0) {
- memSize = atoi(argv[i + 1]);
- } else if (strcmp(argv[i], "-f") == 0) {
- algorithm = 3;
- }
- }
- if (memSize >= 0) {
- // initialize your memory here
- } else {
- printf("The program requires size\n");
- exit(0);
- }
- char operation;
- int id = 1337;
- int size;
- while (EOF != scanf("%c", &operation)) {
- switch (operation) {
- case 'N':
- scanf(" %d %d\n", &id, &size);
- doAllocate(size, id);
- break;
- case 'T':
- scanf(" %d\n", &id);
- doFree(id);
- break;
- case 'S':
- printf("Total Processes created %d, Total allocated memory %d, Total Processes\nterminated %d, Total freed memory %d, Final memory available %d, Final\nsmallest and largest fragmented memory sizes %d and %d, total failed requests:%d, number of memory chunks: %d\n",
- totalAllocated, totalMemAllocated, totalTerminated, totalFreedMemory, calcFinalMemory(), getSmallest(), getBiggest(), totalFailed,
- getNumberOfChunks());
- break;
- }
- }
- return 0;
-}
\ No newline at end of file