Compare commits
65 Commits
f93eba6eb9
...
master
Author | SHA1 | Date | |
---|---|---|---|
cae1791eba | |||
6ff99deff7 | |||
c6dd294bc0 | |||
6a6fd9c37c | |||
112535e8e7 | |||
93635bc1d6 | |||
530ab9a135 | |||
bea609e01c | |||
136a4fe960 | |||
123e0f56bc | |||
e79158e3f1 | |||
73234d0764 | |||
0a18d16eb0 | |||
862af7192c | |||
a62d97f575 | |||
d81bbe423e | |||
9f9d7a1925 | |||
2769651fe1 | |||
3aa01cdc2b | |||
3aad46748a | |||
18669ebf1c | |||
b95fd534fb | |||
f049e8aeff | |||
025b2640fc | |||
64f9b63215 | |||
8a5b7ce7fc | |||
06b028bfb2 | |||
7d4ecdaba6 | |||
4494f47a32 | |||
5ebb6a96cc | |||
6189cf6a3b | |||
fb6ec1dc98 | |||
ca2d32dc34 | |||
9a170e3334 | |||
0668095e82 | |||
4cf7a4e698 | |||
531c063830 | |||
131210cbab | |||
152e31b38d | |||
9f1f82ad43 | |||
0bff959f1b | |||
b760363dc9 | |||
5f01274dc4 | |||
50d03ce45f | |||
78ef1e6d48 | |||
f8cbf9f064 | |||
7a3a6bd256 | |||
29e6cbb041 | |||
e43bf362d4 | |||
40bb239616 | |||
fc04a578ba | |||
e497514110 | |||
71342900db | |||
dbca562065 | |||
e7fed25b0a | |||
fd4de4cefe | |||
823d811f53 | |||
7234eaaf32 | |||
56c5587ee9 | |||
0b91991d8c | |||
5dcbbf5b79 | |||
a534f0d577 | |||
c3faf35400 | |||
552f95fe99 | |||
e0a1232987 |
@ -4,4 +4,5 @@
|
||||
4. The print thread will wait on the sim sem for each CPU thread, and this should leave the sem at 0 again.
|
||||
5. At the end of the print function, before the simulation is allowed to print again, the print thread will increase the time, and then post to the sim sem for each CPU thread, so the simulation can start again.
|
||||
6. Repeat steps 3-5 until the simulation is done.
|
||||
7.
|
||||
|
||||
This is the general process, at least at the time I wrote this down lmao
|
23
Assignment5/README.md
Normal file
23
Assignment5/README.md
Normal file
@ -0,0 +1,23 @@
|
||||
# How to run
|
||||
|
||||
To compile please run:
|
||||
```shell
|
||||
gcc -Ilib lib/process.c lib/queue.c hard_affinity.c
|
||||
```
|
||||
|
||||
And to run please run:
|
||||
```shell
|
||||
./a.out < example.txt
|
||||
```
|
||||
|
||||
# In case of error
|
||||
|
||||
If that command to compile does not work, please place all C and C header files in the same directory and run:
|
||||
```shell
|
||||
gcc process.c queue.c hard_affinity.c
|
||||
```
|
||||
|
||||
In case you need to move the files, for easy copying:
|
||||
```shell
|
||||
mv lib/process.c . & mv lib/queue.c . & mv lib/queue.h . & mv lib/process.h .
|
||||
```
|
@ -9,10 +9,9 @@ int *QUANTUM;
|
||||
int CPUS;
|
||||
|
||||
sem_t print_sem;
|
||||
sem_t sim_sem;
|
||||
sem_t *sim_sems;
|
||||
pthread_mutex_t finish_mutex;
|
||||
pthread_mutex_t time_mutex;
|
||||
|
||||
pthread_mutex_t summary_mutex;
|
||||
|
||||
int finish_count = 0;
|
||||
@ -99,11 +98,10 @@ void *print(void *args) {
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
bool finished = false;
|
||||
|
||||
while (finished == false) {
|
||||
while (getFinishCount() < CPUS) {
|
||||
// Wait for all the simulation threads to finish
|
||||
for (int i = getFinishCount(); i < CPUS; ++i) {
|
||||
for (int i = 0; i < CPUS; ++i) {
|
||||
sem_wait(&print_sem);
|
||||
}
|
||||
|
||||
@ -116,17 +114,12 @@ void *print(void *args) {
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Check if every process is done
|
||||
if (getFinishCount() == CPUS) {
|
||||
finished = true;
|
||||
}
|
||||
|
||||
// Essentially increase the time right before simulating
|
||||
incrementTime();
|
||||
|
||||
// Increment the simulation semaphore to let the simulation threads run
|
||||
for (int i = getFinishCount(); i < CPUS; ++i) {
|
||||
sem_post(&sim_sem);
|
||||
for (int i = 0; i < CPUS; ++i) {
|
||||
sem_post(&sim_sems[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -135,6 +128,13 @@ void *print(void *args) {
|
||||
pthread_mutex_lock(&summary_mutex);
|
||||
printList(thread_args->summary_queue);
|
||||
pthread_mutex_unlock(&summary_mutex);
|
||||
|
||||
// let the simulation threads finish
|
||||
// for some reason, if this is not here, the simulation threads will not finish
|
||||
for (int i = 0; i < CPUS; ++i) {
|
||||
sem_post(&sim_sems[i]);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -151,8 +151,8 @@ void *simulation(void *args) {
|
||||
int quantum = QUANTUM[cpu_id];
|
||||
int addedJobs = 0;
|
||||
int numberOfJobsForThisCPU = 0;
|
||||
int time = 0;
|
||||
int previousTime;
|
||||
int time;
|
||||
bool doneSimulating = false;
|
||||
Process *process = NULL;
|
||||
|
||||
// Count number of jobs this CPU has to do
|
||||
@ -164,59 +164,63 @@ void *simulation(void *args) {
|
||||
process = process->prev_elem;
|
||||
}
|
||||
|
||||
bool finished = false;
|
||||
// Create a queue for the simulation
|
||||
Queue *sim_queue = createQueue();
|
||||
while (finished == false) {
|
||||
while (getFinishCount() < CPUS) {
|
||||
// Only simulate if the time has changed
|
||||
previousTime = time;
|
||||
time = getTime();
|
||||
if (previousTime != time) {
|
||||
sem_wait(&sim_sem);
|
||||
// Begin going through all jobs and enqueueing them if they have arrived
|
||||
process = in_queue->end;
|
||||
for (int i = 0; i < in_queue->size; i++) {
|
||||
if (process->affinity == cpu_id && process->arrival_time == time) {
|
||||
// Create copy to keep the queues separate
|
||||
Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration, process->affinity);
|
||||
enqueue(sim_queue, copy);
|
||||
addedJobs++;
|
||||
sem_wait(&sim_sems[cpu_id]);
|
||||
if (!doneSimulating) {
|
||||
time = getTime();
|
||||
// Begin going through all jobs and enqueueing them if they have arrived
|
||||
process = in_queue->end;
|
||||
for (int i = 0; i < in_queue->size; i++) {
|
||||
if (process->affinity == cpu_id && process->arrival_time == time) {
|
||||
// Create copy to keep the queues separate
|
||||
Process *copy = createProcess(process->username, process->job, process->arrival_time, process->duration, process->affinity);
|
||||
enqueue(sim_queue, copy);
|
||||
addedJobs++;
|
||||
}
|
||||
process = process->prev_elem;
|
||||
}
|
||||
process = process->prev_elem;
|
||||
}
|
||||
|
||||
// Begin simulating the current job
|
||||
process = sim_queue->end;
|
||||
if (sim_queue->size != 0) {
|
||||
print_buffer[cpu_id] = process->job;
|
||||
process->duration--;
|
||||
quantum--;
|
||||
if (process->duration == 0) { // If the process is done, delete it
|
||||
Process *temp = dequeue(sim_queue); // Store the process in a temp variable for deletion
|
||||
pthread_mutex_lock(&summary_mutex);
|
||||
search(summary_queue, temp->username)->finish_time = time; // Set the finish time for the summary queue
|
||||
pthread_mutex_unlock(&summary_mutex);
|
||||
destroyProcess(temp); // This should be called on every process
|
||||
quantum = QUANTUM[cpu_id]; // Make sure to reset the quantum when a process is done
|
||||
} else if (quantum == 0) { // If the quantum is 0, then we need to dequeue the process and enqueue it again
|
||||
process = dequeue(sim_queue);
|
||||
enqueue(sim_queue, process);
|
||||
quantum = QUANTUM[cpu_id];
|
||||
}
|
||||
} else { //If there is nothing in sim_queue, put '-' in the print buffer
|
||||
print_buffer[cpu_id] = '-';
|
||||
if (addedJobs >= numberOfJobsForThisCPU) {
|
||||
finished = true; // If all jobs have been added, and the simulation queue is empty, then we are done
|
||||
// Begin simulating the current job
|
||||
process = sim_queue->end;
|
||||
if (sim_queue->size != 0) {
|
||||
print_buffer[cpu_id] = process->job;
|
||||
process->duration--;
|
||||
quantum--;
|
||||
if (process->duration == 0) { // If the process is done, delete it
|
||||
Process *temp = dequeue(sim_queue); // Store the process in a temp variable for deletion
|
||||
pthread_mutex_lock(&summary_mutex);
|
||||
search(summary_queue, temp->username)->finish_time = time; // Set the finish time for the summary queue
|
||||
pthread_mutex_unlock(&summary_mutex);
|
||||
destroyProcess(temp); // This should be called on every process
|
||||
quantum = QUANTUM[cpu_id]; // Make sure to reset the quantum when a process is done
|
||||
} else if (quantum == 0) { // If the quantum is 0, then we need to dequeue the process and enqueue it again
|
||||
process = dequeue(sim_queue);
|
||||
enqueue(sim_queue, process);
|
||||
quantum = QUANTUM[cpu_id];
|
||||
}
|
||||
} else { //If there is nothing in sim_queue, put '-' in the print buffer
|
||||
print_buffer[cpu_id] = '-';
|
||||
if (addedJobs >= numberOfJobsForThisCPU) {
|
||||
// Need to do this while finish is locked, otherwise the print thread will exit early
|
||||
incrementFinishCount();
|
||||
// If all jobs have been added, and the simulation queue is empty, then we are done
|
||||
doneSimulating = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// Allow the print thread to print because the simulation for this tick is done
|
||||
sem_post(&print_sem);
|
||||
}
|
||||
}
|
||||
// Let the print thread one last time
|
||||
sem_post(&print_sem);
|
||||
// Free memory for the simulation queue. There should be nothing left in it
|
||||
stop(sim_queue);
|
||||
// Signal that the thread is done
|
||||
incrementFinishCount();
|
||||
|
||||
//printf("sim thread done: %d\n", cpu_id);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -227,7 +231,13 @@ int main() {
|
||||
|
||||
// Make sure sem is init right after getting cpus, which is done in input_queue
|
||||
sem_init(&print_sem, 0, 0); // Initialize the semaphore
|
||||
sem_init(&sim_sem, 0, CPUS); // Initialize the semaphore
|
||||
|
||||
// Array of semaphores, one for each CPU
|
||||
sim_sems = malloc(sizeof(sem_t) * CPUS);
|
||||
for (int i = 0; i < CPUS; ++i) {
|
||||
sem_init(&sim_sems[i], 0, 1);
|
||||
}
|
||||
|
||||
pthread_mutex_init(&finish_mutex, NULL); // Initialize the mutex
|
||||
pthread_mutex_init(&time_mutex, NULL); // Initialize the mutex
|
||||
pthread_mutex_init(&summary_mutex, NULL); // Initialize the mutex
|
||||
@ -257,19 +267,36 @@ int main() {
|
||||
pthread_create(&threads[i], NULL, &simulation, args[i]);
|
||||
}
|
||||
|
||||
// Wait for print thread to finish
|
||||
pthread_join(print_thread, NULL);
|
||||
free(print_args);
|
||||
|
||||
// Threads simulate, then print
|
||||
for (int i = 0; i < CPUS; i++) {
|
||||
pthread_join(threads[i], NULL);
|
||||
free(args[i]);
|
||||
}
|
||||
|
||||
// Wait for print thread to finish
|
||||
pthread_join(print_thread, NULL);
|
||||
free(print_args);
|
||||
|
||||
|
||||
// Stop semaphores
|
||||
for (int i = 0; i < CPUS; ++i) {
|
||||
sem_destroy(&sim_sems[i]);
|
||||
}
|
||||
free(sim_sems);
|
||||
sem_destroy(&print_sem);
|
||||
|
||||
|
||||
// Stop mutexes
|
||||
pthread_mutex_destroy(&finish_mutex);
|
||||
pthread_mutex_destroy(&summary_mutex);
|
||||
pthread_mutex_destroy(&time_mutex);
|
||||
|
||||
// Free memory
|
||||
stop(in_queue); // Free memory for input queue
|
||||
stop(summary_queue); // Free memory for summary queue
|
||||
free(print_buffer); // Free memory for print buffer
|
||||
free(QUANTUM); // Free memory for quantum array
|
||||
return 0;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
BIN
Assignment5/submissions/Assignment5 v1.zip
Normal file
BIN
Assignment5/submissions/Assignment5 v1.zip
Normal file
Binary file not shown.
BIN
Assignment5/submissions/Assignment5 v2.zip
Normal file
BIN
Assignment5/submissions/Assignment5 v2.zip
Normal file
Binary file not shown.
49
Assignment5/testing/output3_3.txt
Normal file
49
Assignment5/testing/output3_3.txt
Normal file
@ -0,0 +1,49 @@
|
||||
Time CPU0 CPU1
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
1 - -
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
2 A B
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
3 A B
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
4 A C
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
5 A C
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
6 A D
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
7 E D
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
8 E C
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
9 - C
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
10 - D
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
11 - D
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
12 - D
|
||||
sim post cpu_id: 0
|
||||
sim post cpu_id: 1
|
||||
sim thread done: 1
|
||||
13 - -
|
||||
|
||||
Summary
|
||||
Jim 6
|
||||
Mary 9
|
||||
Sue 12
|
||||
print thread done
|
||||
sim post cpu_id: 0
|
||||
sim thread done: 0
|
@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
counter=1
|
||||
|
||||
# Do this forever
|
||||
while true; do
|
||||
# Capture output of the command
|
||||
@ -11,5 +13,6 @@ while true; do
|
||||
echo "$output"
|
||||
exit 1
|
||||
fi
|
||||
echo "Test 2 passed"
|
||||
echo "Test 2 passed $counter times"
|
||||
counter=$((counter+1))
|
||||
done
|
10
Assignment5/testing/test2simple.sh
Normal file
10
Assignment5/testing/test2simple.sh
Normal file
@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
counter=0
|
||||
|
||||
# Do this forever
|
||||
while true; do
|
||||
(../build/Assignment5 < ../documentation/a5_sample_input_2.txt)
|
||||
counter=$((counter+1))
|
||||
echo $counter
|
||||
done
|
@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
counter=1
|
||||
|
||||
# Do this forever
|
||||
while true; do
|
||||
# Capture output of the command
|
||||
@ -11,4 +13,6 @@ while true; do
|
||||
echo "$output"
|
||||
exit 1
|
||||
fi
|
||||
echo "Test 3 passed $counter times"
|
||||
counter=$((counter+1))
|
||||
done
|
5
Assignment6/.gitignore
vendored
Normal file
5
Assignment6/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
# Built program
|
||||
program1
|
||||
|
||||
# Testing output
|
||||
student_out/*
|
8
Assignment6/.idea/.gitignore
generated
vendored
Normal file
8
Assignment6/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
20
Assignment6/.idea/misc.xml
generated
Normal file
20
Assignment6/.idea/misc.xml
generated
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MakefileSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<MakefileProjectSettings>
|
||||
<option name="buildDirectory" value="" />
|
||||
<option name="buildTarget" value="build" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="version" value="2" />
|
||||
</MakefileProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
<component name="MakefileWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
7
Assignment6/.idea/runConfigurations/3.xml
generated
Normal file
7
Assignment6/.idea/runConfigurations/3.xml
generated
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
generated
Normal file
7
Assignment6/.idea/runConfigurations/7b.xml
generated
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>
|
8
Assignment6/.idea/runConfigurations/all.xml
generated
Normal file
8
Assignment6/.idea/runConfigurations/all.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="all" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="$PROJECT_DIR$/Makefile" target="all" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
Assignment6/.idea/runConfigurations/build.xml
generated
Normal file
8
Assignment6/.idea/runConfigurations/build.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="build" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="$PROJECT_DIR$/Makefile" target="build" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
Assignment6/.idea/runConfigurations/clean.xml
generated
Normal file
8
Assignment6/.idea/runConfigurations/clean.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="clean" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="$PROJECT_DIR$/Makefile" target="clean" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
Assignment6/.idea/runConfigurations/test.xml
generated
Normal file
8
Assignment6/.idea/runConfigurations/test.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="test" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="$PROJECT_DIR$/Makefile" target="test" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
6
Assignment6/.idea/vcs.xml
generated
Normal file
6
Assignment6/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
41
Assignment6/Makefile
Normal file
41
Assignment6/Makefile
Normal file
@ -0,0 +1,41 @@
|
||||
.PHONY: clean build test 1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
||||
|
||||
all: clean build test
|
||||
|
||||
test: 1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
||||
|
||||
clean:
|
||||
rm -rf ./program1
|
||||
rm -rf student_out/*
|
||||
|
||||
build:
|
||||
gcc -g -O0 ./code/*.c -o program1
|
||||
|
||||
1:
|
||||
./program1 -s 10 -f < ./in/test1.in > ./student_out/test1-a.out && diff ./student_out/test1-a.out ./out/test1.out
|
||||
2:
|
||||
./program1 -s 10 -f < ./in/test2.in > ./student_out/test2-a.out && diff ./student_out/test2-a.out ./out/test2.out
|
||||
3:
|
||||
./program1 -s 10 -f < ./in/test3.in > ./student_out/test3-a.out && diff ./student_out/test3-a.out ./out/test3.out
|
||||
4:
|
||||
./program1 -s 10 -f < ./in/test4.in > ./student_out/test4-a.out && diff ./student_out/test4-a.out ./out/test4.out
|
||||
5:
|
||||
./program1 -s 10 -f < ./in/test5.in > ./student_out/test5-a.out && diff ./student_out/test5-a.out ./out/test5.out
|
||||
6:
|
||||
./program1 -s 10 -f < ./in/test6.in > ./student_out/test6-a.out && diff ./student_out/test6-a.out ./out/test6.out
|
||||
7:
|
||||
./program1 -s 5 -f < ./in/test7.in > ./student_out/test7-a.out && diff ./student_out/test7-a.out ./out/test7-a.out
|
||||
8:
|
||||
./program1 -s 5 -b < ./in/test7.in > ./student_out/test7-b.out && diff ./student_out/test7-b.out ./out/test7-b.out
|
||||
9:
|
||||
./program1 -s 5 -w < ./in/test7.in > ./student_out/test7-c.out && diff ./student_out/test7-c.out ./out/test7-c.out
|
||||
10:
|
||||
./program1 -s 5 -f < ./in/test8.in > ./student_out/test8-a.out && diff ./student_out/test8-a.out ./out/test8-a.out
|
||||
11:
|
||||
./program1 -s 5 -b < ./in/test8.in > ./student_out/test8-b.out && diff ./student_out/test8-b.out ./out/test8-b.out
|
||||
12:
|
||||
./program1 -s 5 -w < ./in/test8.in > ./student_out/test8-c.out && diff ./student_out/test8-c.out ./out/test8-c.out
|
||||
13:
|
||||
./program1 -s 5 -w < ./in/test9.in > ./student_out/test13.out && diff ./student_out/test13.out ./out/test13.out
|
||||
14:
|
||||
./program1 -s 103 -f < ./in/test10.in > ./student_out/test14.out && diff ./student_out/test14.out ./out/test14.out
|
246
Assignment6/code/main.c
Normal file
246
Assignment6/code/main.c
Normal file
@ -0,0 +1,246 @@
|
||||
#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 startIndex = -1;
|
||||
|
||||
int size = 0;
|
||||
|
||||
int smallestSoFar = memSize;
|
||||
int biggestSoFar = 0;
|
||||
for (int i = 0; i < memSize; ++i) {
|
||||
if (memory[i] == 0) {
|
||||
if (size == 0) {
|
||||
startIndex = i;
|
||||
}
|
||||
size++;
|
||||
} else {
|
||||
if (size >= howMuchToAllocate) {
|
||||
// On first fit, set the start index
|
||||
if (FirstFitIndex == -1) {
|
||||
FirstFitIndex = startIndex;
|
||||
}
|
||||
// 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 = startIndex;
|
||||
biggestSoFar = size;
|
||||
}
|
||||
// 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 (size > 0 && smallestSoFar > size) {
|
||||
BestFitIndex = startIndex;
|
||||
smallestSoFar = size;
|
||||
}
|
||||
}
|
||||
size = 0;
|
||||
startIndex = -1;
|
||||
}
|
||||
}
|
||||
// Check if the last chunk is the first fit
|
||||
if (FirstFitIndex == -1 && size >= howMuchToAllocate) {
|
||||
FirstFitIndex = startIndex;
|
||||
}
|
||||
|
||||
// Check if the last chunk is the smallest
|
||||
if (size > 0 && smallestSoFar >= size) {
|
||||
BestFitIndex = startIndex;
|
||||
}
|
||||
// Check if the last chunk is the biggest
|
||||
if (size >= biggestSoFar) {
|
||||
WorstFitIndex = startIndex;
|
||||
}
|
||||
|
||||
// Since first fit is always set if there is space, we can error on that for any algorithm
|
||||
if (FirstFitIndex == -1 || 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;
|
||||
}
|
||||
|
||||
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 <= size) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
// Free the memory, just for good measure
|
||||
free(memory);
|
||||
return 0;
|
||||
}
|
BIN
Assignment6/documentation/Assignment 6-v2.pdf
Normal file
BIN
Assignment6/documentation/Assignment 6-v2.pdf
Normal file
Binary file not shown.
BIN
Assignment6/documentation/sample_test_suite-2023.zip
Normal file
BIN
Assignment6/documentation/sample_test_suite-2023.zip
Normal file
Binary file not shown.
2
Assignment6/in/test1.in
Normal file
2
Assignment6/in/test1.in
Normal file
@ -0,0 +1,2 @@
|
||||
N 1 5
|
||||
S
|
102
Assignment6/in/test10.in
Normal file
102
Assignment6/in/test10.in
Normal file
@ -0,0 +1,102 @@
|
||||
N 1 1
|
||||
N 2 1
|
||||
N 3 1
|
||||
N 4 1
|
||||
N 5 1
|
||||
N 6 1
|
||||
N 7 1
|
||||
N 8 1
|
||||
N 9 1
|
||||
N 10 1
|
||||
N 11 1
|
||||
N 12 1
|
||||
N 13 1
|
||||
N 14 1
|
||||
N 15 1
|
||||
N 16 1
|
||||
N 17 1
|
||||
N 18 1
|
||||
N 19 1
|
||||
N 20 1
|
||||
N 21 1
|
||||
N 22 1
|
||||
N 23 1
|
||||
N 24 1
|
||||
N 25 1
|
||||
N 26 1
|
||||
N 27 1
|
||||
N 28 1
|
||||
N 29 1
|
||||
N 30 1
|
||||
N 31 1
|
||||
N 32 1
|
||||
N 33 1
|
||||
N 34 1
|
||||
N 35 1
|
||||
N 36 1
|
||||
N 37 1
|
||||
N 38 1
|
||||
N 39 1
|
||||
N 40 1
|
||||
N 41 1
|
||||
N 42 1
|
||||
N 43 1
|
||||
N 44 1
|
||||
N 45 1
|
||||
N 46 1
|
||||
N 47 1
|
||||
N 48 1
|
||||
N 49 1
|
||||
N 50 1
|
||||
N 51 1
|
||||
N 52 1
|
||||
N 53 1
|
||||
N 54 1
|
||||
N 55 1
|
||||
N 56 1
|
||||
N 57 1
|
||||
N 58 1
|
||||
N 59 1
|
||||
N 60 1
|
||||
N 61 1
|
||||
N 62 1
|
||||
N 63 1
|
||||
N 64 1
|
||||
N 65 1
|
||||
N 66 1
|
||||
N 67 1
|
||||
N 68 1
|
||||
N 69 1
|
||||
N 70 1
|
||||
N 71 1
|
||||
N 72 1
|
||||
N 73 1
|
||||
N 74 1
|
||||
N 75 1
|
||||
N 76 1
|
||||
N 77 1
|
||||
N 78 1
|
||||
N 79 1
|
||||
N 80 1
|
||||
N 81 1
|
||||
N 82 1
|
||||
N 83 1
|
||||
N 84 1
|
||||
N 85 1
|
||||
N 86 1
|
||||
N 87 1
|
||||
N 88 1
|
||||
N 89 1
|
||||
N 90 1
|
||||
N 91 1
|
||||
N 92 1
|
||||
N 93 1
|
||||
N 94 1
|
||||
N 95 1
|
||||
N 96 1
|
||||
N 97 1
|
||||
N 98 1
|
||||
N 99 1
|
||||
N 100 1
|
||||
N 101 1
|
||||
S
|
3
Assignment6/in/test2.in
Normal file
3
Assignment6/in/test2.in
Normal file
@ -0,0 +1,3 @@
|
||||
N 1 5
|
||||
T 1
|
||||
S
|
4
Assignment6/in/test3.in
Normal file
4
Assignment6/in/test3.in
Normal file
@ -0,0 +1,4 @@
|
||||
N 1 5
|
||||
N 2 5
|
||||
N 3 5
|
||||
S
|
3
Assignment6/in/test4.in
Normal file
3
Assignment6/in/test4.in
Normal file
@ -0,0 +1,3 @@
|
||||
N 1 5
|
||||
T 7 5
|
||||
S
|
5
Assignment6/in/test5.in
Normal file
5
Assignment6/in/test5.in
Normal file
@ -0,0 +1,5 @@
|
||||
N 1 5
|
||||
N 2 5
|
||||
N 3 5
|
||||
T 3
|
||||
S
|
6
Assignment6/in/test6.in
Normal file
6
Assignment6/in/test6.in
Normal file
@ -0,0 +1,6 @@
|
||||
N 1 5
|
||||
N 2 5
|
||||
T 2
|
||||
T 1
|
||||
N 1 3
|
||||
S
|
8
Assignment6/in/test7.in
Normal file
8
Assignment6/in/test7.in
Normal file
@ -0,0 +1,8 @@
|
||||
N 1 1
|
||||
N 2 1
|
||||
N 3 1
|
||||
N 4 1
|
||||
T 2
|
||||
T 1
|
||||
N 5 1
|
||||
S
|
8
Assignment6/in/test8.in
Normal file
8
Assignment6/in/test8.in
Normal file
@ -0,0 +1,8 @@
|
||||
N 1 1
|
||||
N 2 1
|
||||
N 3 1
|
||||
N 4 1
|
||||
T 2
|
||||
T 4
|
||||
N 5 1
|
||||
S
|
9
Assignment6/in/test9.in
Normal file
9
Assignment6/in/test9.in
Normal file
@ -0,0 +1,9 @@
|
||||
N 1 1
|
||||
N 2 1
|
||||
N 3 1
|
||||
N 4 1
|
||||
T 4
|
||||
T 2
|
||||
N 5 1
|
||||
T 5
|
||||
S
|
3
Assignment6/out/test1.out
Normal file
3
Assignment6/out/test1.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 1, Total allocated memory 5, Total Processes
|
||||
terminated 0, Total freed memory 0, Final memory available 5, Final
|
||||
smallest and largest fragmented memory sizes 5 and 5, total failed requests:0, number of memory chunks: 1
|
3
Assignment6/out/test13.out
Normal file
3
Assignment6/out/test13.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 5, Total allocated memory 5, Total Processes
|
||||
terminated 3, Total freed memory 3, Final memory available 3, Final
|
||||
smallest and largest fragmented memory sizes 1 and 2, total failed requests:0, number of memory chunks: 2
|
3
Assignment6/out/test14.out
Normal file
3
Assignment6/out/test14.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 101, Total allocated memory 101, Total Processes
|
||||
terminated 0, Total freed memory 0, Final memory available 2, Final
|
||||
smallest and largest fragmented memory sizes 2 and 2, total failed requests:0, number of memory chunks: 1
|
3
Assignment6/out/test2.out
Normal file
3
Assignment6/out/test2.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 1, Total allocated memory 5, Total Processes
|
||||
terminated 1, Total freed memory 5, Final memory available 10, Final
|
||||
smallest and largest fragmented memory sizes 10 and 10, total failed requests:0, number of memory chunks: 1
|
4
Assignment6/out/test3.out
Normal file
4
Assignment6/out/test3.out
Normal file
@ -0,0 +1,4 @@
|
||||
Process 3 failed to allocate 5 memory
|
||||
Total Processes created 2, Total allocated memory 10, Total Processes
|
||||
terminated 0, Total freed memory 0, Final memory available 0, Final
|
||||
smallest and largest fragmented memory sizes 0 and 0, total failed requests:1, number of memory chunks: 0
|
4
Assignment6/out/test4.out
Normal file
4
Assignment6/out/test4.out
Normal file
@ -0,0 +1,4 @@
|
||||
Process 7 failed to free memory
|
||||
Total Processes created 1, Total allocated memory 5, Total Processes
|
||||
terminated 0, Total freed memory 0, Final memory available 5, Final
|
||||
smallest and largest fragmented memory sizes 5 and 5, total failed requests:1, number of memory chunks: 1
|
5
Assignment6/out/test5.out
Normal file
5
Assignment6/out/test5.out
Normal file
@ -0,0 +1,5 @@
|
||||
Process 3 failed to allocate 5 memory
|
||||
Process 3 failed to free memory
|
||||
Total Processes created 2, Total allocated memory 10, Total Processes
|
||||
terminated 0, Total freed memory 0, Final memory available 0, Final
|
||||
smallest and largest fragmented memory sizes 0 and 0, total failed requests:2, number of memory chunks: 0
|
3
Assignment6/out/test6.out
Normal file
3
Assignment6/out/test6.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 3, Total allocated memory 13, Total Processes
|
||||
terminated 2, Total freed memory 10, Final memory available 7, Final
|
||||
smallest and largest fragmented memory sizes 7 and 7, total failed requests:0, number of memory chunks: 1
|
3
Assignment6/out/test7-a.out
Normal file
3
Assignment6/out/test7-a.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 5, Total allocated memory 5, Total Processes
|
||||
terminated 2, Total freed memory 2, Final memory available 2, Final
|
||||
smallest and largest fragmented memory sizes 1 and 1, total failed requests:0, number of memory chunks: 2
|
3
Assignment6/out/test7-b.out
Normal file
3
Assignment6/out/test7-b.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 5, Total allocated memory 5, Total Processes
|
||||
terminated 2, Total freed memory 2, Final memory available 2, Final
|
||||
smallest and largest fragmented memory sizes 2 and 2, total failed requests:0, number of memory chunks: 1
|
3
Assignment6/out/test7-c.out
Normal file
3
Assignment6/out/test7-c.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 5, Total allocated memory 5, Total Processes
|
||||
terminated 2, Total freed memory 2, Final memory available 2, Final
|
||||
smallest and largest fragmented memory sizes 1 and 1, total failed requests:0, number of memory chunks: 2
|
3
Assignment6/out/test8-a.out
Normal file
3
Assignment6/out/test8-a.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 5, Total allocated memory 5, Total Processes
|
||||
terminated 2, Total freed memory 2, Final memory available 2, Final
|
||||
smallest and largest fragmented memory sizes 2 and 2, total failed requests:0, number of memory chunks: 1
|
3
Assignment6/out/test8-b.out
Normal file
3
Assignment6/out/test8-b.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 5, Total allocated memory 5, Total Processes
|
||||
terminated 2, Total freed memory 2, Final memory available 2, Final
|
||||
smallest and largest fragmented memory sizes 2 and 2, total failed requests:0, number of memory chunks: 1
|
3
Assignment6/out/test8-c.out
Normal file
3
Assignment6/out/test8-c.out
Normal file
@ -0,0 +1,3 @@
|
||||
Total Processes created 5, Total allocated memory 5, Total Processes
|
||||
terminated 2, Total freed memory 2, Final memory available 2, Final
|
||||
smallest and largest fragmented memory sizes 1 and 1, total failed requests:0, number of memory chunks: 2
|
BIN
Assignment6/submissions/Assignment6 v1.zip
Normal file
BIN
Assignment6/submissions/Assignment6 v1.zip
Normal file
Binary file not shown.
2
Assignment7/.gitignore
vendored
Normal file
2
Assignment7/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
student_out
|
||||
a.out
|
8
Assignment7/.idea/.gitignore
generated
vendored
Normal file
8
Assignment7/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
18
Assignment7/.idea/misc.xml
generated
Normal file
18
Assignment7/.idea/misc.xml
generated
Normal file
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MakefileSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<MakefileProjectSettings>
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="version" value="2" />
|
||||
</MakefileProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
<component name="MakefileWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
7
Assignment7/.idea/runConfigurations/Test1.xml
generated
Normal file
7
Assignment7/.idea/runConfigurations/Test1.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Test1" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="3" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/algo-check.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment7" TARGET_NAME="all" CONFIG_NAME="all" version="1" RUN_PATH="$PROJECT_DIR$/a.out">
|
||||
<method v="2">
|
||||
<option name="CLION.COMPOUND.BUILD" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
8
Assignment7/.idea/runConfigurations/all.xml
generated
Normal file
8
Assignment7/.idea/runConfigurations/all.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="all" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment7\makefile" target="all" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
Assignment7/.idea/runConfigurations/test.xml
generated
Normal file
8
Assignment7/.idea/runConfigurations/test.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="test" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment7\makefile" target="test" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
6
Assignment7/.idea/vcs.xml
generated
Normal file
6
Assignment7/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
156
Assignment7/code/main.c
Normal file
156
Assignment7/code/main.c
Normal file
@ -0,0 +1,156 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define OFFSETBITS 12
|
||||
#define PAGESIZE (1 << OFFSETBITS)
|
||||
#define PAGETABLEBITS 10
|
||||
#define ARCH 32
|
||||
int framesSize;
|
||||
unsigned int pageFault = 0;
|
||||
unsigned int pageHits = 0;
|
||||
unsigned int pagesSwapped = 0;
|
||||
int time = 0;
|
||||
|
||||
typedef struct frameEntry {
|
||||
unsigned int pageNumber;
|
||||
int lastUsed;
|
||||
bool dirty;
|
||||
} frameEntry;
|
||||
|
||||
frameEntry *frames;
|
||||
|
||||
int getPageOffsetAddress(unsigned long logicalAddress) {
|
||||
// Last 12 bits of logical address
|
||||
// Page size is 4096 bytes, and we bitwise AND with 4095 (111111111111) to get the last 12 bits
|
||||
return logicalAddress & (PAGESIZE - 1);
|
||||
}
|
||||
|
||||
int getPageNumber(unsigned long logicalAddress) {
|
||||
// First 20 bits of logical address
|
||||
// Bitwise shift right 12 bits to get the first 20 bits, as the last 12 bits are discarded
|
||||
return logicalAddress >> OFFSETBITS;
|
||||
|
||||
}
|
||||
|
||||
void printPhysicalAddress(int frameID, unsigned long logicalAddress) {
|
||||
printf("%lu -> %i\n", logicalAddress, frameID * PAGESIZE + getPageOffsetAddress(logicalAddress));
|
||||
}
|
||||
|
||||
double computeFormula() {
|
||||
// probabilityOfNoPageFault ∗ 10 + probabilityOfMajorPageFaultWithOneCopy ∗ 1000 + probabilityOfMajorPageFaultWithTwoCopyOperations ∗ 3000
|
||||
double probabilityOfNoPageFault = 1 - (double) pageFault / (pageFault + pageHits);
|
||||
double probabilityOfMajorPageFaultWithOneCopy = (double) pageFault / (pageFault + pageHits) * (1 - (double) pagesSwapped / pageFault);
|
||||
double probabilityOfMajorPageFaultWithTwoCopyOperations = (double) pageFault / (pageFault + pageHits) * (double) pagesSwapped / pageFault;
|
||||
return probabilityOfNoPageFault * 10 + probabilityOfMajorPageFaultWithOneCopy * 1000 + probabilityOfMajorPageFaultWithTwoCopyOperations * 3000;
|
||||
}
|
||||
|
||||
// Int so we can return the index of the frame if it is in the frames array
|
||||
int isInFrames(int pageNumber) {
|
||||
for (int i = 0; i < framesSize; i++) {
|
||||
if (frames[i].pageNumber == pageNumber) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Swap out the least recently used frame
|
||||
// Returns the index of the frame that was swapped out
|
||||
int swapOut() {
|
||||
// Find the least recently used frame
|
||||
int leastRecentlyUsed = 0x7FFFFFFF;
|
||||
int leastRecentlyUsedIndex = -1;
|
||||
for (int i = 0; i < framesSize; i++) {
|
||||
if (frames[i].lastUsed < leastRecentlyUsed) {
|
||||
leastRecentlyUsed = frames[i].lastUsed;
|
||||
leastRecentlyUsedIndex = i;
|
||||
}
|
||||
}
|
||||
// Swap out is implied, so we increment pageSwapped
|
||||
// If the frame is dirty, we need to write it to disk, so we increment pagesSwapped
|
||||
if (frames[leastRecentlyUsedIndex].dirty) {
|
||||
pagesSwapped++;
|
||||
}
|
||||
|
||||
return leastRecentlyUsedIndex;
|
||||
}
|
||||
|
||||
// Swap in a page
|
||||
// Returns the index of the frame that was swapped in
|
||||
int swapIn(int pageNumber) {
|
||||
// Find the first empty frame
|
||||
for (int i = 0; i < framesSize; i++) {
|
||||
if (frames[i].pageNumber == -1) {
|
||||
pageFault++;
|
||||
frames[i].pageNumber = pageNumber;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
// If no empty frames, swap out a frame
|
||||
int frameID = swapOut();
|
||||
frames[frameID].pageNumber = pageNumber;
|
||||
pageFault++;
|
||||
return frameID;
|
||||
}
|
||||
|
||||
// Read from an address
|
||||
void readFromAddress(unsigned long logicalAddress) {
|
||||
unsigned int pageNumber = getPageNumber(logicalAddress);
|
||||
int frameID = isInFrames(pageNumber);
|
||||
if (isInFrames(pageNumber) == -1) {
|
||||
frameID = swapIn(pageNumber);
|
||||
} else {
|
||||
pageHits++;
|
||||
}
|
||||
frames[frameID].lastUsed = time;
|
||||
printPhysicalAddress(frameID, logicalAddress);
|
||||
}
|
||||
|
||||
// Write to an address, only difference is that we set the dirty bit to true
|
||||
void writeToAddress(unsigned long logicalAddress) {
|
||||
unsigned int pageNumber = getPageNumber(logicalAddress);
|
||||
int frameID = isInFrames(pageNumber);
|
||||
if (isInFrames(pageNumber) == -1) {
|
||||
frameID = swapIn(pageNumber);
|
||||
} else {
|
||||
pageHits++;
|
||||
}
|
||||
frames[frameID].dirty = true;
|
||||
frames[frameID].lastUsed = time;
|
||||
printPhysicalAddress(frameID, logicalAddress);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
//Disable printf buffering for easier debugging
|
||||
setbuf(stdout, NULL);
|
||||
|
||||
framesSize = atoi(argv[1]);
|
||||
frames = calloc(framesSize, sizeof(frameEntry));
|
||||
// Initialize frames to -1, so we can check if a frame is empty
|
||||
for (int i = 0; i < framesSize; i++) {
|
||||
frames[i].pageNumber = -1;
|
||||
frames[i].lastUsed = -1;
|
||||
frames[i].dirty = false;
|
||||
}
|
||||
unsigned long logicalAddress;
|
||||
char operation;
|
||||
printf("Logical addresses -> Physical addresses:\n");
|
||||
while (EOF != scanf("%c %lu\n", &operation, &logicalAddress)) {
|
||||
if (operation == 'r') {
|
||||
readFromAddress(logicalAddress);
|
||||
} else {
|
||||
writeToAddress(logicalAddress);
|
||||
}
|
||||
time++;
|
||||
}
|
||||
|
||||
printf("\nStats:\nmajor page faults = %u\npage hits = %u\npages swapped out = %u\nEffective Access Time = %.3lf\n",
|
||||
pageFault,
|
||||
pageHits,
|
||||
pagesSwapped,
|
||||
computeFormula()
|
||||
);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
BIN
Assignment7/documentation/Assignment 7.pdf
Normal file
BIN
Assignment7/documentation/Assignment 7.pdf
Normal file
Binary file not shown.
BIN
Assignment7/documentation/a7-tests-v2.zip
Normal file
BIN
Assignment7/documentation/a7-tests-v2.zip
Normal file
Binary file not shown.
7
Assignment7/in/algo-check-2.in
Normal file
7
Assignment7/in/algo-check-2.in
Normal file
@ -0,0 +1,7 @@
|
||||
r 1124955998
|
||||
w 794845611
|
||||
w 1540976830
|
||||
w 1124957700
|
||||
w 1081961846
|
||||
w 794845440
|
||||
r 1540976645
|
7
Assignment7/in/algo-check-o.in
Normal file
7
Assignment7/in/algo-check-o.in
Normal file
@ -0,0 +1,7 @@
|
||||
r 1124955998
|
||||
r 794845611
|
||||
r 1540976830
|
||||
r 1081961846
|
||||
r 1124957700
|
||||
r 794845440
|
||||
r 1540976645
|
7
Assignment7/in/algo-check.in
Normal file
7
Assignment7/in/algo-check.in
Normal file
@ -0,0 +1,7 @@
|
||||
r 1124955998
|
||||
r 794845611
|
||||
r 1540976830
|
||||
r 1124957700
|
||||
r 1081961846
|
||||
r 794845440
|
||||
r 1540976645
|
8
Assignment7/in/discard-test.in
Normal file
8
Assignment7/in/discard-test.in
Normal file
@ -0,0 +1,8 @@
|
||||
r 1622650073
|
||||
r 1144108930
|
||||
r 101027544
|
||||
r 1784484492
|
||||
r 823378840
|
||||
r 197493099
|
||||
r 1954899097
|
||||
r 530511967
|
7
Assignment7/in/fromText.in
Normal file
7
Assignment7/in/fromText.in
Normal file
@ -0,0 +1,7 @@
|
||||
r 1124955998
|
||||
r 794845611
|
||||
r 1540976830
|
||||
w 1081961846
|
||||
r 1124957700
|
||||
r 794845440
|
||||
r 1540976645
|
8
Assignment7/in/major-test.in
Normal file
8
Assignment7/in/major-test.in
Normal file
@ -0,0 +1,8 @@
|
||||
w 1622650073
|
||||
w 1144108930
|
||||
w 101027544
|
||||
w 1784484492
|
||||
w 823378840
|
||||
w 197493099
|
||||
w 1954899097
|
||||
w 530511967
|
8
Assignment7/in/page-hit-test.in
Normal file
8
Assignment7/in/page-hit-test.in
Normal file
@ -0,0 +1,8 @@
|
||||
r 1725896583
|
||||
r 1725896583
|
||||
r 1725898287
|
||||
r 1725894961
|
||||
r 1725896008
|
||||
r 1725898705
|
||||
r 1725898485
|
||||
r 1725897033
|
42
Assignment7/makefile
Normal file
42
Assignment7/makefile
Normal file
@ -0,0 +1,42 @@
|
||||
SDIR=./code
|
||||
INDIR=./in
|
||||
OUTDIR=./out
|
||||
STDOUTDIR=./student_out
|
||||
|
||||
_OBJ=./a.out
|
||||
|
||||
CC=gcc
|
||||
CFLAGS= -O0 -g -lm -std=c99
|
||||
DIFF=diff
|
||||
|
||||
all:
|
||||
$(CC) $(CFLAGS) $(SDIR)/*.c -o $(_OBJ)
|
||||
1:
|
||||
./a.out 3 < ./in/algo-check.in > ./student_out/sample_output.out
|
||||
diff ./student_out/sample_output.out ./out/sample_output.out
|
||||
2:
|
||||
./a.out 3 < ./in/algo-check-2.in > ./student_out/sample_output_2.out
|
||||
diff ./student_out/sample_output_2.out ./out/sample_output_2.out
|
||||
3:
|
||||
./a.out 1 < ./in/discard-test.in > ./student_out/discard-test-1.out
|
||||
diff ./student_out/discard-test-1.out ./out/discard-test-1.out
|
||||
4:
|
||||
./a.out 2 < ./in/discard-test.in > ./student_out/discard-test-2.out
|
||||
diff ./student_out/discard-test-2.out ./out/discard-test-2.out
|
||||
5:
|
||||
./a.out 1 < ./in/major-test.in > ./student_out/major-test-1.out
|
||||
diff ./student_out/major-test-1.out ./out/major-test-1.out
|
||||
6:
|
||||
./a.out 2 < ./in/major-test.in > ./student_out/major-test-2.out
|
||||
diff ./student_out/major-test-2.out ./out/major-test-2.out
|
||||
7:
|
||||
./a.out 5 < ./in/page-hit-test.in > ./student_out/page-hit-test.out
|
||||
diff ./student_out/page-hit-test.out ./out/page-hit-test.out
|
||||
8:
|
||||
./a.out 3 < ./in/algo-check-o.in > ./student_out/algo-check-o-l.out
|
||||
diff ./student_out/algo-check-o-l.out ./out/algo-check-o-l.out
|
||||
9:
|
||||
./a.out 3 < ./in/fromText.in > ./student_out/fromText.out
|
||||
diff ./student_out/fromText.out ./out/fromText.out
|
||||
|
||||
test: all 1 2 3 4 5 6 7 8 9
|
14
Assignment7/out/algo-check-o-l.out
Normal file
14
Assignment7/out/algo-check-o-l.out
Normal file
@ -0,0 +1,14 @@
|
||||
Logical addresses -> Physical addresses:
|
||||
1124955998 -> 1886
|
||||
794845611 -> 4523
|
||||
1540976830 -> 8382
|
||||
1081961846 -> 3446
|
||||
1124957700 -> 7684
|
||||
794845440 -> 8448
|
||||
1540976645 -> 5
|
||||
|
||||
Stats:
|
||||
major page faults = 7
|
||||
page hits = 0
|
||||
pages swapped out = 0
|
||||
Effective Access Time = 1000.000
|
15
Assignment7/out/discard-test-1.out
Normal file
15
Assignment7/out/discard-test-1.out
Normal file
@ -0,0 +1,15 @@
|
||||
Logical addresses -> Physical addresses:
|
||||
1622650073 -> 3289
|
||||
1144108930 -> 1922
|
||||
101027544 -> 3800
|
||||
1784484492 -> 652
|
||||
823378840 -> 920
|
||||
197493099 -> 363
|
||||
1954899097 -> 1177
|
||||
530511967 -> 2143
|
||||
|
||||
Stats:
|
||||
major page faults = 8
|
||||
page hits = 0
|
||||
pages swapped out = 0
|
||||
Effective Access Time = 1000.000
|
15
Assignment7/out/discard-test-2.out
Normal file
15
Assignment7/out/discard-test-2.out
Normal file
@ -0,0 +1,15 @@
|
||||
Logical addresses -> Physical addresses:
|
||||
1622650073 -> 3289
|
||||
1144108930 -> 6018
|
||||
101027544 -> 3800
|
||||
1784484492 -> 4748
|
||||
823378840 -> 920
|
||||
197493099 -> 4459
|
||||
1954899097 -> 1177
|
||||
530511967 -> 6239
|
||||
|
||||
Stats:
|
||||
major page faults = 8
|
||||
page hits = 0
|
||||
pages swapped out = 0
|
||||
Effective Access Time = 1000.000
|
14
Assignment7/out/fromText.out
Normal file
14
Assignment7/out/fromText.out
Normal file
@ -0,0 +1,14 @@
|
||||
Logical addresses -> Physical addresses:
|
||||
1124955998 -> 1886
|
||||
794845611 -> 4523
|
||||
1540976830 -> 8382
|
||||
1081961846 -> 3446
|
||||
1124957700 -> 7684
|
||||
794845440 -> 8448
|
||||
1540976645 -> 5
|
||||
|
||||
Stats:
|
||||
major page faults = 7
|
||||
page hits = 0
|
||||
pages swapped out = 1
|
||||
Effective Access Time = 1285.714
|
15
Assignment7/out/major-test-1.out
Normal file
15
Assignment7/out/major-test-1.out
Normal file
@ -0,0 +1,15 @@
|
||||
Logical addresses -> Physical addresses:
|
||||
1622650073 -> 3289
|
||||
1144108930 -> 1922
|
||||
101027544 -> 3800
|
||||
1784484492 -> 652
|
||||
823378840 -> 920
|
||||
197493099 -> 363
|
||||
1954899097 -> 1177
|
||||
530511967 -> 2143
|
||||
|
||||
Stats:
|
||||
major page faults = 8
|
||||
page hits = 0
|
||||
pages swapped out = 7
|
||||
Effective Access Time = 2750.000
|
15
Assignment7/out/major-test-2.out
Normal file
15
Assignment7/out/major-test-2.out
Normal file
@ -0,0 +1,15 @@
|
||||
Logical addresses -> Physical addresses:
|
||||
1622650073 -> 3289
|
||||
1144108930 -> 6018
|
||||
101027544 -> 3800
|
||||
1784484492 -> 4748
|
||||
823378840 -> 920
|
||||
197493099 -> 4459
|
||||
1954899097 -> 1177
|
||||
530511967 -> 6239
|
||||
|
||||
Stats:
|
||||
major page faults = 8
|
||||
page hits = 0
|
||||
pages swapped out = 6
|
||||
Effective Access Time = 2500.000
|
15
Assignment7/out/page-hit-test.out
Normal file
15
Assignment7/out/page-hit-test.out
Normal file
@ -0,0 +1,15 @@
|
||||
Logical addresses -> Physical addresses:
|
||||
1725896583 -> 1927
|
||||
1725896583 -> 1927
|
||||
1725898287 -> 3631
|
||||
1725894961 -> 305
|
||||
1725896008 -> 1352
|
||||
1725898705 -> 4049
|
||||
1725898485 -> 3829
|
||||
1725897033 -> 2377
|
||||
|
||||
Stats:
|
||||
major page faults = 1
|
||||
page hits = 7
|
||||
pages swapped out = 0
|
||||
Effective Access Time = 133.750
|
14
Assignment7/out/sample_output.out
Normal file
14
Assignment7/out/sample_output.out
Normal file
@ -0,0 +1,14 @@
|
||||
Logical addresses -> Physical addresses:
|
||||
1124955998 -> 1886
|
||||
794845611 -> 4523
|
||||
1540976830 -> 8382
|
||||
1124957700 -> 3588
|
||||
1081961846 -> 7542
|
||||
794845440 -> 8448
|
||||
1540976645 -> 5
|
||||
|
||||
Stats:
|
||||
major page faults = 6
|
||||
page hits = 1
|
||||
pages swapped out = 0
|
||||
Effective Access Time = 858.571
|
14
Assignment7/out/sample_output_2.out
Normal file
14
Assignment7/out/sample_output_2.out
Normal file
@ -0,0 +1,14 @@
|
||||
Logical addresses -> Physical addresses:
|
||||
1124955998 -> 1886
|
||||
794845611 -> 4523
|
||||
1540976830 -> 8382
|
||||
1124957700 -> 3588
|
||||
1081961846 -> 7542
|
||||
794845440 -> 8448
|
||||
1540976645 -> 5
|
||||
|
||||
Stats:
|
||||
major page faults = 6
|
||||
page hits = 1
|
||||
pages swapped out = 3
|
||||
Effective Access Time = 1715.714
|
BIN
Assignment7/submissions/Submission v1.zip
Normal file
BIN
Assignment7/submissions/Submission v1.zip
Normal file
Binary file not shown.
2
Assignment8/.gitignore
vendored
Normal file
2
Assignment8/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
a.out
|
||||
student_out
|
8
Assignment8/.idea/.gitignore
generated
vendored
Normal file
8
Assignment8/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
86
Assignment8/.idea/editor.xml
generated
Normal file
86
Assignment8/.idea/editor.xml
generated
Normal file
@ -0,0 +1,86 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="BackendCodeEditorSettings">
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppClangFormat/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/EditorConfig/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_BINARY_EXPRESSIONS_CHAIN/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_CALLS_CHAIN/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXPRESSION/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_FOR_STMT/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTIPLE_DECLARATION/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_TERNARY/@EntryValue" value="ALIGN_ALL" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_CLASS_DEFINITION/@EntryValue" value="1" type="int" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue" value="2" type="int" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_CODE/@EntryValue" value="2" type="int" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_USER_LINEBREAKS/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CASE_FROM_SWITCH/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_COMMENT/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INT_ALIGN_EQ/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SIMPLE_BLOCK_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_ARGS/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_PARAMS/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_SEMICOLON/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_SEMICOLON/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_UNARY_OPERATOR/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_ARRAY_ACCESS_BRACKETS/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_CAST_EXPRESSION_PARENTHESES/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_METHOD_PARENTHESES/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPECIAL_ELSE_IF_TREATMENT/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_BINARY_OPSIGN/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_TERNARY_OPSIGNS/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TYPE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/OTHER_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CASE_BLOCK_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DECLARATION/@EntryValue" value="1" type="int" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DEFINITION/@EntryValue" value="1" type="int" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_INDENTATION/@EntryValue" value="All" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_ARGUMENT/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXTENDS_LIST/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_PARAMETER/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_ARGUMENT/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_PARAMETER/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_DECLARATIONS/@EntryValue" value="0" type="int" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_ACCESS_SPECIFIERS_FROM_CLASS/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CLASS_MEMBERS_FROM_ACCESS_SPECIFIERS/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/LINE_BREAK_AFTER_COLON_IN_MEMBER_INITIALIZER_LISTS/@EntryValue" value="ON_SINGLE_LINE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/MEMBER_INITIALIZER_LIST_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_NAMESPACE_DEFINITIONS_ON_SAME_LINE/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_COLON/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_COLON/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_DATA_MEMBER/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_DATA_MEMBERS/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_DECLARATION_PARENTHESES/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_BLOCKS/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_RPAR/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_RPAR/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_ARGUMENTS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_PARAMETERS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BREAK_TEMPLATE_DECLARATION/@EntryValue" value="LINE_BREAK" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/FREE_BLOCK_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INVOCABLE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INITIALIZER_BRACES/@EntryValue" value="END_OF_LINE_NO_SPACE" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_STYLE/@EntryValue" value="Tab" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_SIZE/@EntryValue" value="4" type="int" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CONTINUOUS_LINE_INDENT/@EntryValue" value="Double" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TAB_WIDTH/@EntryValue" value="4" type="int" />
|
||||
</component>
|
||||
</project>
|
19
Assignment8/.idea/misc.xml
generated
Normal file
19
Assignment8/.idea/misc.xml
generated
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MakefileSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<MakefileProjectSettings>
|
||||
<option name="buildTarget" value="build" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="version" value="2" />
|
||||
</MakefileProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
<component name="MakefileWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
7
Assignment8/.idea/runConfigurations/1.xml
generated
Normal file
7
Assignment8/.idea/runConfigurations/1.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="1" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="F" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/all_known_in_advance_spaces.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment8" TARGET_NAME="build" CONFIG_NAME="build" version="1" RUN_PATH="a.out">
|
||||
<method v="2">
|
||||
<option name="CLION.COMPOUND.BUILD" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
8
Assignment8/.idea/runConfigurations/2.xml
generated
Normal file
8
Assignment8/.idea/runConfigurations/2.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="2" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="2" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
Assignment8/.idea/runConfigurations/3.xml
generated
Normal file
8
Assignment8/.idea/runConfigurations/3.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="3" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="3" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
Assignment8/.idea/runConfigurations/4.xml
generated
Normal file
8
Assignment8/.idea/runConfigurations/4.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="4" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="4" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
7
Assignment8/.idea/runConfigurations/5.xml
generated
Normal file
7
Assignment8/.idea/runConfigurations/5.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="5" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="F" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/time_dependent_second_spaces.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment8" TARGET_NAME="build" CONFIG_NAME="build" version="1" RUN_PATH="a.out">
|
||||
<method v="2">
|
||||
<option name="CLION.COMPOUND.BUILD" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
7
Assignment8/.idea/runConfigurations/6.xml
generated
Normal file
7
Assignment8/.idea/runConfigurations/6.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="6" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="C" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/time_dependent_second_spaces.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment8" TARGET_NAME="build" CONFIG_NAME="build" version="1" RUN_PATH="a.out">
|
||||
<method v="2">
|
||||
<option name="CLION.COMPOUND.BUILD" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
8
Assignment8/.idea/runConfigurations/all.xml
generated
Normal file
8
Assignment8/.idea/runConfigurations/all.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="all" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="all" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
Assignment8/.idea/runConfigurations/build.xml
generated
Normal file
8
Assignment8/.idea/runConfigurations/build.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="build" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="build" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
Assignment8/.idea/runConfigurations/clean.xml
generated
Normal file
8
Assignment8/.idea/runConfigurations/clean.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="clean" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="clean" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
Assignment8/.idea/runConfigurations/test.xml
generated
Normal file
8
Assignment8/.idea/runConfigurations/test.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="test" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="test" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
6
Assignment8/.idea/vcs.xml
generated
Normal file
6
Assignment8/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
104
Assignment8/code/disk.c
Normal file
104
Assignment8/code/disk.c
Normal file
@ -0,0 +1,104 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "disk.h"
|
||||
|
||||
|
||||
DiskQueue *createDiskQueue() {
|
||||
DiskQueue *queue = malloc(sizeof(DiskQueue));
|
||||
if (queue == NULL) {
|
||||
printf("Error allocating memory for queue\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
queue->head = NULL;
|
||||
queue->size = 0;
|
||||
return queue;
|
||||
}
|
||||
|
||||
void enqueue(DiskQueue *queue, int time, int position) {
|
||||
DiskRequest *request = malloc(sizeof(DiskRequest));
|
||||
if (request == NULL) {
|
||||
printf("Error allocating memory for request\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
request->time = time;
|
||||
request->position = position;
|
||||
request->next = NULL;
|
||||
|
||||
if (queue->head == NULL) {
|
||||
queue->head = request;
|
||||
} else {
|
||||
DiskRequest *current = queue->head;
|
||||
while (current->next != NULL) {
|
||||
current = current->next;
|
||||
}
|
||||
current->next = request;
|
||||
}
|
||||
queue->size++;
|
||||
}
|
||||
|
||||
void delete(DiskQueue *queue, DiskRequest request) {
|
||||
DiskRequest *current = queue->head;
|
||||
DiskRequest *previous = NULL;
|
||||
while (current != NULL) {
|
||||
if (current->position == request.position && current->time == request.time) {
|
||||
if (previous == NULL) {
|
||||
queue->head = current->next;
|
||||
} else {
|
||||
previous->next = current->next;
|
||||
}
|
||||
queue->size--;
|
||||
free(current);
|
||||
return;
|
||||
}
|
||||
previous = current;
|
||||
current = current->next;
|
||||
}
|
||||
printf("Error: Request not found\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
DiskRequest *dequeue(DiskQueue *queue) {
|
||||
DiskRequest *head = queue->head;
|
||||
queue->head = head->next;
|
||||
queue->size--;
|
||||
return head;
|
||||
}
|
||||
|
||||
void destroyDiskQueue(DiskQueue *queue) {
|
||||
DiskRequest *current = queue->head;
|
||||
DiskRequest *next = NULL;
|
||||
while (current != NULL) {
|
||||
next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
free(queue);
|
||||
}
|
||||
|
||||
int calcDistance(int position, int destination) {
|
||||
if (position > destination) {
|
||||
return destination + (9999 + 1 - position);
|
||||
} else {
|
||||
return destination - position;
|
||||
}
|
||||
}
|
||||
|
||||
DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
||||
DiskRequest *current = queue->head;
|
||||
DiskRequest *closest = NULL;
|
||||
while (current != NULL) {
|
||||
if (current->time <= time) { // Filter out requests that haven't come in yet
|
||||
if (closest == NULL) { // If there is no closest yet, set it to the current
|
||||
closest = current;
|
||||
} else {
|
||||
int closestDistance = calcDistance(position, closest->position); // Distance from the closest to the current position, negative means down (bad)
|
||||
int currentDistance = calcDistance(position, current->position); // Distance from the current to the current position, negative means down (bad
|
||||
if (currentDistance < closestDistance) { // If the current is closer than the closest
|
||||
closest = current;
|
||||
}
|
||||
}
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
return closest;
|
||||
}
|
30
Assignment8/code/disk.h
Normal file
30
Assignment8/code/disk.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
typedef struct DiskRequest {
|
||||
struct DiskRequest *next;
|
||||
int position;
|
||||
int time;
|
||||
} DiskRequest;
|
||||
|
||||
typedef struct DiskQueue {
|
||||
DiskRequest *head;
|
||||
int size;
|
||||
} DiskQueue;
|
||||
|
||||
typedef enum DiskDirection {
|
||||
UP, DOWN
|
||||
} DiskDirection;
|
||||
|
||||
DiskQueue *createDiskQueue();
|
||||
|
||||
void enqueue(DiskQueue *queue, int time, int position);
|
||||
|
||||
void delete(DiskQueue *queue, DiskRequest request);
|
||||
|
||||
DiskRequest *dequeue(DiskQueue *queue);
|
||||
|
||||
DiskRequest *findClosest(DiskQueue *queue, int position, double time);
|
||||
|
||||
void destroyDiskQueue(DiskQueue *queue);
|
||||
|
||||
int calcDistance(int position, int destination);
|
95
Assignment8/code/main.c
Normal file
95
Assignment8/code/main.c
Normal file
@ -0,0 +1,95 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "disk.h"
|
||||
|
||||
// Global tracking variables
|
||||
DiskDirection currentDirection = UP;
|
||||
int movement = 0;
|
||||
double seekTime = 0;
|
||||
|
||||
// Array for storing the requests
|
||||
DiskQueue *queue;
|
||||
|
||||
double timeToProcessRequest(int position, int destination) {
|
||||
//The time (a floating point number) required to process a request is computed by distance the head travels divided by 5
|
||||
//Plus additional 15 milliseconds penalty if the direction has to change (for FCFS)
|
||||
double time = 0;
|
||||
// Calculated this way so that positive means up and negative means down
|
||||
int distance = destination - position;
|
||||
DiskDirection direction = (distance > 0) ? UP : DOWN;
|
||||
time += abs(distance) / 5.0;
|
||||
if (direction != currentDirection) {
|
||||
time += 15;
|
||||
currentDirection = direction;
|
||||
}
|
||||
return time;
|
||||
}
|
||||
|
||||
int diskMovement(int position, int destination) {
|
||||
int distance = destination - position;
|
||||
return abs(distance);
|
||||
}
|
||||
|
||||
// Your simulated disk is of size 10000, numbered from 0 to 9999.
|
||||
|
||||
// In first come first the time the request comes in is irrelevant
|
||||
void fcfs(int start) {
|
||||
int position = start;
|
||||
while (queue->size > 0) {
|
||||
DiskRequest *request = dequeue(queue);
|
||||
while (seekTime < request->time) { seekTime++; }
|
||||
seekTime += timeToProcessRequest(position, request->position);
|
||||
movement += diskMovement(position, request->position);
|
||||
position = request->position;
|
||||
free(request);
|
||||
}
|
||||
}
|
||||
|
||||
void cscan(int start) {
|
||||
int position = start;
|
||||
|
||||
while (queue->size > 0) {
|
||||
DiskRequest *request = findClosest(queue, position, seekTime);
|
||||
if (request == NULL) {
|
||||
seekTime++;
|
||||
} else {
|
||||
if (request->position < position) {
|
||||
// + 1 simulates the time it takes to move to the end of the disk (it is a circular disk)
|
||||
seekTime += timeToProcessRequest(position, 9999 + 1);
|
||||
movement += diskMovement(position, 9999 + 1);
|
||||
position = 0;
|
||||
// Rescan for closest, so we can efficiently handle requests
|
||||
request = findClosest(queue, position, seekTime);
|
||||
}
|
||||
seekTime += timeToProcessRequest(position, request->position);
|
||||
movement += diskMovement(position, request->position);
|
||||
position = request->position;
|
||||
}
|
||||
if (request != NULL) {
|
||||
delete(queue, *request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int position, time;
|
||||
char algorithm = argv[1][0];
|
||||
int start = 0;
|
||||
|
||||
queue = createDiskQueue();
|
||||
while (EOF != (scanf("%i %i\n", &position, &time))) {
|
||||
enqueue(queue, time, position);
|
||||
}
|
||||
if (algorithm == 'F') {
|
||||
fcfs(start);
|
||||
printf("Movement:%i Time:%.1lf\n", movement, seekTime);
|
||||
} else if (algorithm == 'C') {
|
||||
cscan(start);
|
||||
// Stupid printf difference to pass tests, could call once after if statement
|
||||
printf("Movement: %i Time:%.1lf\n", movement, seekTime);
|
||||
}
|
||||
destroyDiskQueue(queue);
|
||||
|
||||
return 0;
|
||||
}
|
BIN
Assignment8/documentation/Assignment 8.pdf
Normal file
BIN
Assignment8/documentation/Assignment 8.pdf
Normal file
Binary file not shown.
BIN
Assignment8/documentation/assign8.zip
Normal file
BIN
Assignment8/documentation/assign8.zip
Normal file
Binary file not shown.
14
Assignment8/documentation/verify.md
Normal file
14
Assignment8/documentation/verify.md
Normal file
@ -0,0 +1,14 @@
|
||||
| Initial position | Next Position | Distance | Time From Distance | Time from Change Direction | Total Time |
|
||||
|------------------|---------------|----------|--------------------|----------------------------|------------|
|
||||
| 0 | 93 | 93 | 18.6 | 0 | 18.6 |
|
||||
| 93 | 183 | 90 | 18 | 0 | 18 |
|
||||
| 183 | 37 | 146 | 29.2 | 15 | 44.2 |
|
||||
| 37 | 122 | 85 | 17 | 0 | 17 |
|
||||
| 122 | 14 | 108 | 21.6 | 15 | 36.6 |
|
||||
| 14 | 124 | 110 | 22 | 0 | 22 |
|
||||
| 124 | 65 | 59 | 11.8 | 15 | 26.8 |
|
||||
| 65 | 67 | 2 | 0.4 | 0 | 0.4 |
|
||||
|
||||
| Total Distance | Total Time |
|
||||
|----------------|------------|
|
||||
| 693 | 183 |
|
8
Assignment8/in/all_known_in_advance_spaces.in
Normal file
8
Assignment8/in/all_known_in_advance_spaces.in
Normal file
@ -0,0 +1,8 @@
|
||||
93 0
|
||||
183 0
|
||||
37 0
|
||||
122 0
|
||||
14 0
|
||||
124 0
|
||||
65 0
|
||||
67 0
|
14
Assignment8/in/time_dependent_second_spaces.in
Normal file
14
Assignment8/in/time_dependent_second_spaces.in
Normal file
@ -0,0 +1,14 @@
|
||||
7394 61
|
||||
1217 63
|
||||
3898 70
|
||||
4254 74
|
||||
8951 75
|
||||
4764 86
|
||||
5517 95
|
||||
6765 100
|
||||
151 108
|
||||
5637 121
|
||||
4245 125
|
||||
8742 130
|
||||
1703 138
|
||||
879 140
|
8
Assignment8/in/time_dependent_spaces.in
Normal file
8
Assignment8/in/time_dependent_spaces.in
Normal file
@ -0,0 +1,8 @@
|
||||
93 10
|
||||
183 12
|
||||
37 15
|
||||
122 17
|
||||
14 18
|
||||
124 20
|
||||
65 23
|
||||
67 25
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user