C++-ify it some more, begin readfile

This commit is contained in:
John 2022-04-17 22:03:17 -05:00
parent f770d105b8
commit 476aec5734
7 changed files with 122 additions and 97 deletions

3
.gitignore vendored
View File

@ -9,6 +9,9 @@
# Local History for Visual Studio Code # Local History for Visual Studio Code
.history/ .history/
# ---> Input files
*input*.txt
# ---> C # ---> C
# Prerequisites # Prerequisites
*.d *.d

View File

@ -22,7 +22,7 @@ VPATH = $(SPATH) $(IPATH) $(DPATH) $(OPATH)
# compiler and compiler flags # compiler and compiler flags
CC = g++ CC = g++
CFLAGS = -I$(IPATH) -pthread -lrt CFLAGS = -I$(IPATH) -pthread -lrt -std=c++11
# list of object files # list of object files
SOURCES = $(wildcard $(SPATH)/*.$(STYPE)) SOURCES = $(wildcard $(SPATH)/*.$(STYPE))

View File

@ -1,57 +1,23 @@
// Gee, ain't pointers just the greatest? class graph {
//? Rename to'graph'? Typedef to 'graph'? private:
struct adjmatrix { int num_processes;
int num_processes; // The number of processes in the matrix int num_resources;
int num_resources; // The number of resources in the matrix int *resource_counts;
int *resource_counts; // The counts of each resource in the matrix struct m{
int **matrix; // Tell me, Mr. Anderson, what good is a phone call if you are unable to speak? int x, y;
int **data;
} matrix; // Tell me, Mr. Anderson, what good is a phone call if you are unable to speak?
public:
// Constructors
graph(const int processes, const int resources);
// Destructors
~graph();
// Initializers:
void init(const int **data);
// check functions:
// is the graph...
bool reducible();// ?
bool knotted();// ?
}; };
/* graph_reduce:
Perform the graph reduction algorithm on the adjacency matrix to detect deadlocks
This algorithm is described in section 5.2 of the Zybook
(!THIS MODIFIES THE GRAPH!)
@params:
graph: adjacency matrix containing all relevant information about the graph
@returns:
int:
1 if graph is not reducible (deadlock)
0 if graph is reducible (no deadlock)
*/
int graph_reduce(struct adjmatrix *graph);
/* knot_detect:
Perform the knot detection algorithm on the adjacency matrix to detect deadlocks
(!THIS MODIFIES THE GRAPH!)
@params:
graph: adjacency matrix containing all relevant information about the graph
@returns:
int:
1 if graph is knotted (deadlock)
0 if graph is not (no deadlock)
*/
int knot_detect(struct adjmatrix *graph);
/* create_adjmatrix:
Create a new adjmatrix (!THIS INVOLVES MEMORY ALLOCATION!) (Basically a constructor)
@params:
num_processes
num_resources
@returns:
struct adjmatrix:
Empty adjacency matrix of size (num_processes+num_resources)^2
Fill in the blanks yourself
*/
struct adjmatrix * create_adjmatrix(int num_processes, int num_resources);
/* destroy_adjmatrix:
Destroy an adjmatrix (!THIS INVOLVES MEMORY DEALLOCATION!) (Basically a destructor)
@params:
matrix:
A structure containing all relevant information about the graph
@returns:
int:
0 if operation completed successfully, else a value of type `errno`
*/
int destroy_adjmatrix(struct adjmatrix *matrix);

View File

@ -1,3 +1,3 @@
int read_file(char *filename, struct adjmatrix *graph); void read_file(std::string filename);

View File

@ -1,22 +1,59 @@
#include <cstdlib> #include <cstdlib>
#include "graph.hpp" #include "graph.hpp"
int graph_reduce(struct adjmatrix *graph) { /* reducible:
Perform the graph reduction algorithm on the adjacency matrix to detect deadlocks
This algorithm is described in section 5.2 of the Zybook
(!THIS MODIFIES THE GRAPH!)
@params: none, uses class-internal data
@returns:
bool:
false if graph is not reducible (deadlock)
true if graph is reducible (no deadlock)
*/
bool graph::reducible() {
// TODO: Implement a function which checks if a process is blocked // TODO: Implement a function which checks if a process is blocked
// TODO: Use that function to implement the graph reduction algorithm // TODO: Use that function to implement the graph reduction algorithm
//? Make sure when reducing the graph, you don't try to delete the //? Make sure when reducing the graph, you don't try to delete the
return 0; return false;
} }
int knot_detect(struct adjmatrix *graph) { /* knotted:
return 0; Perform the knot detection algorithm on the adjacency matrix to detect deadlocks
(!THIS MODIFIES THE GRAPH!)
@params: none, uses class-internal data
@returns:
int:
true if graph is knotted (deadlock)
false if graph is not (no deadlock)
*/
bool graph::knotted() {
return false;
} }
int destroy_adjmatrix(struct adjmatrix *matrix) { graph::graph (const int processes, const int resources) {
for (int i = 0; i < matrix->num_processes + matrix->num_resources; i++) { num_processes = processes;
free(matrix->matrix[i]); num_resources = resources;
matrix.x = processes + resources;
matrix.y = matrix.x;
matrix.data = new int*[matrix.x];
for (int x = 0; x < matrix.x; x++) {
matrix.data[x] = new int[matrix.y];
} }
free(matrix->matrix); }
free(matrix->resource_counts);
return 0; graph::~graph() {
/* clean up the 2D matrix */
if (matrix.data) {
if (matrix.x || matrix.y) {
// delete y axes
for (int x = 0; x < matrix.x; x++) {
delete matrix.data[x];
}
// delete x axis
delete matrix.data;
matrix.x = 0; matrix.y = 0;
}
}
free(resource_counts);
} }

View File

@ -1,9 +1,10 @@
/* +-------------+---------+-----------------------+ /* +-------------+---------+-----------------------+
| John Breaux | jab0910 | JohnBreaux@my.unt.edu | //TODO put your name here | John Breaux | jab0910 | JohnBreaux@my.unt.edu |
| | | | //TODO put your names here
+-------------+---------+-----------------------+ +-------------+---------+-----------------------+
| Created 2022-04-16 | | Created 2022-04-16 |
+-----------------------------------------------+ */ +-----------------------------------------------+ */
#include <cstdio> // printf #include <string>
#include "graph.hpp" // Graph reduction/Knot detection, struct adjmatrix #include "graph.hpp" // Graph reduction/Knot detection, struct adjmatrix
#include "read_input.hpp" //TODO: Read input in the associated C file, and provide an interface to that function here #include "read_input.hpp" //TODO: Read input in the associated C file, and provide an interface to that function here
@ -12,16 +13,15 @@ int main(int argc, char** argv) {
// TODO: Grab file name from args // TODO: Grab file name from args
//? Command line argument structure? //? Command line argument structure?
//? Other flags? What other features should this have? //? Other flags? What other features should this have?
char* filename = NULL; if (argc < 2) return 1;
std::string filename = argv[1];
//TODO: Implement reading from a file //TODO: Implement reading from a file
struct adjmatrix *graph; read_file(filename);
read_file(filename, graph);
// TODO: Implement graph reduction and/or knot detection // TODO: Implement graph reduction and/or knot detection
// TODO: Destroy the graph created by read_file // TODO: Destroy the graph created by read_file
destroy_adjmatrix(graph);
return 0; return 0;
} }

View File

@ -1,39 +1,58 @@
//TODO: Include C file IO header (stdio.h? Whatever it is) //TODO: Include C file IO header (stdio.h? Whatever it is)
#include <cstdio> #include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>
#include "graph.hpp" // struct adjmatrix #include "graph.hpp" // struct adjmatrix
#include "read_input.hpp" // read_input #include "read_input.hpp" // read_input
#define MAX_LINE_LEN 1024 #define MAX_LINE_LEN 1024
// Vector of regices
std::vector<std::regex> patterns = {
//* A line starting with a % is a comment, and should be skipped
//* A blank line should be skipped
//* A line starting with `num_processes=` contains the number of processes
std::regex("^.*(num_processes)\\s*=\\s*(\\d+).*"),
//* A line starting with `num_resources=` contains the number of resources
std::regex("^.*(num_resources)\\s*=\\s*(\\d+)"),
//* A line containing comma-separated values should be returned as-is, for manual disassembly
std::regex("^([0-9, ]+)")
};
// TODO: Implement reading from a file // TODO: Implement reading from a file
int read_file(char *filename, struct adjmatrix *graph) { void read_file(std::string filename) {
// TODO: Open file with name filename as read-only // Open file with name filename as read-only
FILE *f = fopen(filename, "r"); std::fstream f; f.open(filename, f.in);
// TODO: Check for file IO errors (I might have a solution for that in another project) // TODO: Check for file IO errors (I might have a solution for that in another project)
int num_processes = 0, num_resources = 0; int num_processes = 0, num_resources = 0;
char line[MAX_LINE_LEN]; // Lines can be no more than 1KB in size, a sensible limitation std::vector<int> rescount;
while (!feof(f)) { std::vector<std::vector<int>> matrix;
//* A line starting with a % is a comment, and should be skipped std::string line; // Lines can be no more than 1KB in size, a sensible limitation
// if %: while (!f.eof()) {
continue; std::getline (f, line);
//* A blank line should be skipped std::smatch res;
// if blank: // Iterate over each pattern, and grab the associated data
continue; for (auto pattern: patterns) {
//* A line starting with `num_processes=` contains the number of processes if (std::regex_search (line, res, pattern)) {
//if "num_processes": // get the pattern type, value
num_processes = 0; // TODO: Read file std::string type = res.format("$1"), value = res.format("$2");
//* A line starting with `num_resources=` contains the number of resources // Handle the pattern
//if "num_resources": if (type == "num_processes") { num_processes = std::stoi(value); } else
num_resources = 0; if (type == "num_resources") { num_resources = std::stoi(value); } else
// TODO: create graph 'object' here if (type.length()/2 <= num_resources) {
//* A line containing num_resources numbers follows, indicating the number of each resource // This is the resource count structure
//* Resources are comma-separated, so there can never be more than 1/2 as many as max line length
int resource_counts[MAX_LINE_LEN/2];
//* A comma-separated, newline-separated adjacency matrix with which to reduce
}
break;
}
}
} }
// TODO: Close file // Close file
f.close();
// TODO: Check for file IO errors (Shouldn't be any) // TODO: Check for file IO errors (Shouldn't be any)
return;
} }