Start assignment 4
This commit is contained in:
parent
f92a5a8725
commit
c6c7caeae7
8
Assignment4/.idea/.gitignore
vendored
Normal file
8
Assignment4/.idea/.gitignore
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
|
2
Assignment4/.idea/Assignment4.iml
Normal file
2
Assignment4/.idea/Assignment4.iml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
5
Assignment4/.idea/codeStyles/codeStyleConfig.xml
Normal file
5
Assignment4/.idea/codeStyles/codeStyleConfig.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||||
|
</state>
|
||||||
|
</component>
|
4
Assignment4/.idea/misc.xml
Normal file
4
Assignment4/.idea/misc.xml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||||
|
</project>
|
8
Assignment4/.idea/modules.xml
Normal file
8
Assignment4/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/Assignment4.iml" filepath="$PROJECT_DIR$/.idea/Assignment4.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
Assignment4/.idea/vcs.xml
Normal file
6
Assignment4/.idea/vcs.xml
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>
|
12
Assignment4/CMakeLists.txt
Normal file
12
Assignment4/CMakeLists.txt
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.22)
|
||||||
|
project(Assignment4 C)
|
||||||
|
|
||||||
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
||||||
|
add_executable(Assignment4
|
||||||
|
round_robin.c
|
||||||
|
lib/queue.h
|
||||||
|
lib/queue.c
|
||||||
|
lib/process.h
|
||||||
|
lib/process.c
|
||||||
|
)
|
BIN
Assignment4/documentation/Assignment 4-v2.pdf
Normal file
BIN
Assignment4/documentation/Assignment 4-v2.pdf
Normal file
Binary file not shown.
6
Assignment4/documentation/a4_sample_input_1.txt
Normal file
6
Assignment4/documentation/a4_sample_input_1.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
3
|
||||||
|
User Process Arrival Duration
|
||||||
|
Jim A 2 5
|
||||||
|
Mary B 2 2
|
||||||
|
Sue C 5 5
|
||||||
|
Mary D 6 2
|
7
Assignment4/documentation/a4_sample_input_2.txt
Normal file
7
Assignment4/documentation/a4_sample_input_2.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
3
|
||||||
|
User Process Arrival Duration
|
||||||
|
Jim A 2 5
|
||||||
|
Mary B 2 2
|
||||||
|
Mary D 6 2
|
||||||
|
Sue C 7 5
|
||||||
|
Hassan E 22 3
|
22
Assignment4/documentation/a4_sample_output_1.txt
Normal file
22
Assignment4/documentation/a4_sample_output_1.txt
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
Time Job
|
||||||
|
1 -
|
||||||
|
2 A
|
||||||
|
3 A
|
||||||
|
4 A
|
||||||
|
5 B
|
||||||
|
6 B
|
||||||
|
7 A
|
||||||
|
8 A
|
||||||
|
9 C
|
||||||
|
10 C
|
||||||
|
11 C
|
||||||
|
12 D
|
||||||
|
13 D
|
||||||
|
14 C
|
||||||
|
15 C
|
||||||
|
16 -
|
||||||
|
|
||||||
|
Summary
|
||||||
|
Jim 8
|
||||||
|
Mary 13
|
||||||
|
Sue 15
|
32
Assignment4/documentation/a4_sample_output_2.txt
Normal file
32
Assignment4/documentation/a4_sample_output_2.txt
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
Time Job
|
||||||
|
1 -
|
||||||
|
2 A
|
||||||
|
3 A
|
||||||
|
4 A
|
||||||
|
5 B
|
||||||
|
6 B
|
||||||
|
7 A
|
||||||
|
8 A
|
||||||
|
9 D
|
||||||
|
10 D
|
||||||
|
11 C
|
||||||
|
12 C
|
||||||
|
13 C
|
||||||
|
14 C
|
||||||
|
15 C
|
||||||
|
16 -
|
||||||
|
17 -
|
||||||
|
18 -
|
||||||
|
19 -
|
||||||
|
20 -
|
||||||
|
21 -
|
||||||
|
22 E
|
||||||
|
23 E
|
||||||
|
24 E
|
||||||
|
25 -
|
||||||
|
|
||||||
|
Summary
|
||||||
|
Jim 8
|
||||||
|
Mary 10
|
||||||
|
Sue 15
|
||||||
|
Hassan 24
|
36
Assignment4/lib/process.c
Normal file
36
Assignment4/lib/process.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "process.h"
|
||||||
|
|
||||||
|
//Assume that the string passed in is null terminated
|
||||||
|
|
||||||
|
Process *createProcess(char *username, char job, int arrival_time, int duration) {
|
||||||
|
// Allocate memory for the process, and check if it was successful
|
||||||
|
Process *node = calloc(1, sizeof(Process));
|
||||||
|
if (node == NULL) {
|
||||||
|
printf("Error allocating memory for process\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// Free the data first, then allocate memory for the new data
|
||||||
|
node->username = calloc(strlen(username) + 1, sizeof(char));
|
||||||
|
if (node->username == NULL) {
|
||||||
|
printf("Error allocating memory for username data\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// Copy data from the string passed in to the process's data
|
||||||
|
// Makes sure if the string passed in is changed, the process's data is not changed
|
||||||
|
// Also makes sure that if the data is on the stack, it is not freed at some other point
|
||||||
|
strcpy(node->username, username);
|
||||||
|
node->job = job;
|
||||||
|
node->arrival_time = arrival_time;
|
||||||
|
node->duration = duration;
|
||||||
|
node->next_elem = NULL;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroyProcess(Process *node) {
|
||||||
|
// Free the data first, then free the process
|
||||||
|
free(node->username);
|
||||||
|
free(node);
|
||||||
|
}
|
17
Assignment4/lib/process.h
Normal file
17
Assignment4/lib/process.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef NODE_H
|
||||||
|
#define NODE_H
|
||||||
|
|
||||||
|
typedef struct Process {
|
||||||
|
struct process *prev_elem;
|
||||||
|
struct process *next_elem;
|
||||||
|
char *username;
|
||||||
|
char job;
|
||||||
|
int arrival_time;
|
||||||
|
int duration;
|
||||||
|
} Process;
|
||||||
|
|
||||||
|
Process *createProcess(char *username, char job, int arrival_time, int duration);
|
||||||
|
|
||||||
|
void destroyProcess(Process *node);
|
||||||
|
|
||||||
|
#endif //NODE_H
|
109
Assignment4/lib/queue.c
Normal file
109
Assignment4/lib/queue.c
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "process.h"
|
||||||
|
#include "queue.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Queue implementation
|
||||||
|
* 5 4 3 2 1
|
||||||
|
* START END
|
||||||
|
*
|
||||||
|
* If you were to visualize the queue as a line, the end is the first person in line and the start is the last person in line
|
||||||
|
* So when you enqueue, you are adding to the start of the line
|
||||||
|
* And when you dequeue, you are exiting from the end of the line
|
||||||
|
*/
|
||||||
|
|
||||||
|
//TODO: Refactor
|
||||||
|
int contains(Queue *queue, char *job) {
|
||||||
|
Process *current = queue->start;
|
||||||
|
while (current != NULL) {
|
||||||
|
if (strcmp(current->username, job) == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
current = current->next_elem;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: Refactor
|
||||||
|
Process *search(Queue *queue, char *job) {
|
||||||
|
Process *current = queue->start;
|
||||||
|
|
||||||
|
while (current != NULL) {
|
||||||
|
if (strcmp(current->username, job) == 0) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
current = current->next_elem;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void enqueue(Queue *queue, Process *process) {
|
||||||
|
if (queue->end == NULL) { // If the queue is empty, set the start and end to the new process
|
||||||
|
queue->end = process;
|
||||||
|
queue->start = queue->end;
|
||||||
|
} else {
|
||||||
|
process->next_elem = queue->start; // Set the next element of the new process to the start of the queue
|
||||||
|
queue->start->prev_elem = process; // Set the previous element of the start of the queue to the new process
|
||||||
|
queue->start = process; // Set the start of the queue to the new process
|
||||||
|
}
|
||||||
|
queue->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// WARNING: Returns a pointer to a process that is not in the queue, it is your responsibility to free it
|
||||||
|
Process *dequeue(Queue *queue) {
|
||||||
|
if (queue->end == NULL) { // If the queue is empty, return NULL
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Process *temp = queue->end; // Store the end of the queue for returning later
|
||||||
|
|
||||||
|
queue->end = queue->end->prev_elem; // Set the end to the previous element
|
||||||
|
queue->end->next_elem = NULL; // Set the next element of the new end to NULL
|
||||||
|
|
||||||
|
if (queue->end == NULL) { // If the queue is empty, set the start to NULL
|
||||||
|
queue->start = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
temp->prev_elem = NULL; // The dequeued element should not point to anything
|
||||||
|
queue->size--;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: Refactor
|
||||||
|
void printList(Queue *queue) {
|
||||||
|
Process *current = queue->start;
|
||||||
|
while (current != NULL) {
|
||||||
|
// printf("%s %d\n", current->username, current->duration);
|
||||||
|
current = current->next_elem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: Refactor
|
||||||
|
int stop(Queue *queue) {
|
||||||
|
Process *current = queue->end;
|
||||||
|
while (current != NULL) {
|
||||||
|
Process *next = current->prev_elem;
|
||||||
|
destroyProcess(current);
|
||||||
|
current = next;
|
||||||
|
}
|
||||||
|
free(queue);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Queue *createQueue() {
|
||||||
|
Queue *queue = calloc(1, sizeof(Queue));
|
||||||
|
if (queue == NULL) {
|
||||||
|
printf("Error allocating memory for queue\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
queue->start = NULL;
|
||||||
|
queue->end = NULL;
|
||||||
|
queue->size = 0;
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
27
Assignment4/lib/queue.h
Normal file
27
Assignment4/lib/queue.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#ifndef QUEUE_H
|
||||||
|
#define QUEUE_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "process.h"
|
||||||
|
|
||||||
|
typedef struct Queue {
|
||||||
|
Process *start;
|
||||||
|
Process *end;
|
||||||
|
int size;
|
||||||
|
} Queue;
|
||||||
|
|
||||||
|
int contains(Queue *queue, char *job);
|
||||||
|
|
||||||
|
Process *search(Queue *queue, char *job);
|
||||||
|
|
||||||
|
void enqueue(Queue *queue, Process *process);
|
||||||
|
|
||||||
|
Process *dequeue(Queue *queue);
|
||||||
|
|
||||||
|
void printList(Queue *queue);
|
||||||
|
|
||||||
|
int stop(Queue *queue);
|
||||||
|
|
||||||
|
Queue* createQueue();
|
||||||
|
|
||||||
|
#endif //QUEUE_H
|
53
Assignment4/round_robin.c
Normal file
53
Assignment4/round_robin.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "lib/queue.h"
|
||||||
|
|
||||||
|
#define MAX_USERNAME_LENGTH 100
|
||||||
|
int QUANTUM;
|
||||||
|
|
||||||
|
typedef struct Summary {
|
||||||
|
char username[MAX_USERNAME_LENGTH];
|
||||||
|
int last_time;
|
||||||
|
} Summary;
|
||||||
|
|
||||||
|
Queue* input_queue() {
|
||||||
|
Queue* queue = createQueue();
|
||||||
|
|
||||||
|
scanf("%d", &QUANTUM);
|
||||||
|
char username[MAX_USERNAME_LENGTH];
|
||||||
|
char job;
|
||||||
|
int arrival_time, duration;
|
||||||
|
while(scanf("%s100 %c %d %d", username, &job, &arrival_time, &duration) != EOF) {
|
||||||
|
Process *process = createProcess(username, job, arrival_time, duration);
|
||||||
|
enqueue(queue, process);
|
||||||
|
}
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void simulation(Queue* queue) {
|
||||||
|
printf("Time\tJob\n");
|
||||||
|
int time = 0;
|
||||||
|
Queue *sim_queue = createQueue();
|
||||||
|
|
||||||
|
//Take note of all usernames at the start, and then create an array of summary
|
||||||
|
//When a job finishes, take note of its username, and then go through the array of summary and find the username
|
||||||
|
//Once you find the username, assign the last_time to the current time.
|
||||||
|
//TODO: May need to change as the summary should be sorted by username arrival time
|
||||||
|
|
||||||
|
//Go through list and check for arrival times at current time
|
||||||
|
//If arrival time is at current time, add to sim_queue
|
||||||
|
//If there is nothing in sim_queue, print -
|
||||||
|
//If there is something in sim queue, store the process in a variable, so we can check the quantum
|
||||||
|
//If the quantum is 0, then we need to dequeue the process and enqueue it again
|
||||||
|
//If the quantum is not 0, then we need to print the process and decrement the quantum
|
||||||
|
//If the process is done, then we need to print the process and dequeue it
|
||||||
|
//If the process is not done, then we need to print it and leave the queue alone
|
||||||
|
//Once we reach the point where time is greater than the last arrival time, we need to check if sim_queue is empty
|
||||||
|
//Once sim_queue is empty, we need to break out of the loop
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Queue* input = input_queue();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user