Not working but save point here
This commit is contained in:
parent
e7fed25b0a
commit
dbca562065
7
Assignment6/.idea/runConfigurations/3.xml
Normal file
7
Assignment6/.idea/runConfigurations/3.xml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="3" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="-s 10 -f" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/test3.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment6" TARGET_NAME="build" CONFIG_NAME="build" version="1" RUN_PATH="program1">
|
||||||
|
<method v="2">
|
||||||
|
<option name="CLION.COMPOUND.BUILD" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
7
Assignment6/.idea/runConfigurations/7b.xml
Normal file
7
Assignment6/.idea/runConfigurations/7b.xml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="7b" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="-s 5 -b" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/test7.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment6" TARGET_NAME="build" CONFIG_NAME="build" version="1" RUN_PATH="program1">
|
||||||
|
<method v="2">
|
||||||
|
<option name="CLION.COMPOUND.BUILD" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
244
Assignment6/code/main.c
Normal file
244
Assignment6/code/main.c
Normal file
@ -0,0 +1,244 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
@ -1,102 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user