C++-ify it some more, begin readfile
This commit is contained in:
parent
f770d105b8
commit
476aec5734
3
.gitignore
vendored
3
.gitignore
vendored
@ -9,6 +9,9 @@
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# ---> Input files
|
||||
*input*.txt
|
||||
|
||||
# ---> C
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
2
Makefile
2
Makefile
@ -22,7 +22,7 @@ VPATH = $(SPATH) $(IPATH) $(DPATH) $(OPATH)
|
||||
|
||||
# compiler and compiler flags
|
||||
CC = g++
|
||||
CFLAGS = -I$(IPATH) -pthread -lrt
|
||||
CFLAGS = -I$(IPATH) -pthread -lrt -std=c++11
|
||||
|
||||
# list of object files
|
||||
SOURCES = $(wildcard $(SPATH)/*.$(STYPE))
|
||||
|
@ -1,57 +1,23 @@
|
||||
|
||||
// Gee, ain't pointers just the greatest?
|
||||
//? Rename to'graph'? Typedef to 'graph'?
|
||||
struct adjmatrix {
|
||||
int num_processes; // The number of processes in the matrix
|
||||
int num_resources; // The number of resources in the matrix
|
||||
int *resource_counts; // The counts of each resource in the matrix
|
||||
int **matrix; // Tell me, Mr. Anderson, what good is a phone call if you are unable to speak?
|
||||
};
|
||||
class graph {
|
||||
private:
|
||||
int num_processes;
|
||||
int num_resources;
|
||||
int *resource_counts;
|
||||
struct m{
|
||||
int x, y;
|
||||
int **data;
|
||||
} matrix; // Tell me, Mr. Anderson, what good is a phone call if you are unable to speak?
|
||||
|
||||
/* 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);
|
||||
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();// ?
|
||||
};
|
@ -1,3 +1,3 @@
|
||||
|
||||
|
||||
int read_file(char *filename, struct adjmatrix *graph);
|
||||
void read_file(std::string filename);
|
@ -1,22 +1,59 @@
|
||||
#include <cstdlib>
|
||||
#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: Use that function to implement the graph reduction algorithm
|
||||
//? Make sure when reducing the graph, you don't try to delete the
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
int knot_detect(struct adjmatrix *graph) {
|
||||
return 0;
|
||||
/* knotted:
|
||||
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) {
|
||||
for (int i = 0; i < matrix->num_processes + matrix->num_resources; i++) {
|
||||
free(matrix->matrix[i]);
|
||||
graph::graph (const int processes, const int resources) {
|
||||
num_processes = processes;
|
||||
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);
|
||||
}
|
||||
|
12
src/main.cpp
12
src/main.cpp
@ -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 |
|
||||
+-----------------------------------------------+ */
|
||||
#include <cstdio> // printf
|
||||
#include <string>
|
||||
#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
|
||||
|
||||
@ -12,16 +13,15 @@ int main(int argc, char** argv) {
|
||||
// TODO: Grab file name from args
|
||||
//? Command line argument structure?
|
||||
//? 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
|
||||
struct adjmatrix *graph;
|
||||
read_file(filename, graph);
|
||||
read_file(filename);
|
||||
|
||||
|
||||
// TODO: Implement graph reduction and/or knot detection
|
||||
|
||||
// TODO: Destroy the graph created by read_file
|
||||
destroy_adjmatrix(graph);
|
||||
return 0;
|
||||
}
|
@ -1,39 +1,58 @@
|
||||
//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 "read_input.hpp" // read_input
|
||||
|
||||
#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
|
||||
int read_file(char *filename, struct adjmatrix *graph) {
|
||||
// TODO: Open file with name filename as read-only
|
||||
FILE *f = fopen(filename, "r");
|
||||
void read_file(std::string filename) {
|
||||
// Open file with name filename as read-only
|
||||
std::fstream f; f.open(filename, f.in);
|
||||
// TODO: Check for file IO errors (I might have a solution for that in another project)
|
||||
|
||||
int num_processes = 0, num_resources = 0;
|
||||
char line[MAX_LINE_LEN]; // Lines can be no more than 1KB in size, a sensible limitation
|
||||
while (!feof(f)) {
|
||||
//* A line starting with a % is a comment, and should be skipped
|
||||
// if %:
|
||||
continue;
|
||||
//* A blank line should be skipped
|
||||
// if blank:
|
||||
continue;
|
||||
//* A line starting with `num_processes=` contains the number of processes
|
||||
//if "num_processes":
|
||||
num_processes = 0; // TODO: Read file
|
||||
//* A line starting with `num_resources=` contains the number of resources
|
||||
//if "num_resources":
|
||||
num_resources = 0;
|
||||
// TODO: create graph 'object' here
|
||||
//* A line containing num_resources numbers follows, indicating the number of each resource
|
||||
//* 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
|
||||
std::vector<int> rescount;
|
||||
std::vector<std::vector<int>> matrix;
|
||||
std::string line; // Lines can be no more than 1KB in size, a sensible limitation
|
||||
while (!f.eof()) {
|
||||
std::getline (f, line);
|
||||
std::smatch res;
|
||||
// Iterate over each pattern, and grab the associated data
|
||||
for (auto pattern: patterns) {
|
||||
if (std::regex_search (line, res, pattern)) {
|
||||
// get the pattern type, value
|
||||
std::string type = res.format("$1"), value = res.format("$2");
|
||||
// Handle the pattern
|
||||
if (type == "num_processes") { num_processes = std::stoi(value); } else
|
||||
if (type == "num_resources") { num_resources = std::stoi(value); } else
|
||||
if (type.length()/2 <= num_resources) {
|
||||
// This is the resource count structure
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Close file
|
||||
// Close file
|
||||
f.close();
|
||||
// TODO: Check for file IO errors (Shouldn't be any)
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user