Finish impl'ing reading a file
This commit is contained in:
parent
aeeb33e699
commit
8234aca2e0
@ -1,23 +1,25 @@
|
||||
|
||||
class graph {
|
||||
private:
|
||||
int num_processes;
|
||||
int num_resources;
|
||||
int *resource_counts;
|
||||
struct m{
|
||||
int x, y;
|
||||
std::vector<std::vector<int>> data;
|
||||
} matrix; // Tell me, Mr. Anderson, what good is a phone call if you are unable to speak?
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
graph() {}
|
||||
graph(const int processes, const int resources);
|
||||
// Destructors
|
||||
~graph();
|
||||
// Initializers:
|
||||
void init(const int **data);
|
||||
void read(std::string filename);
|
||||
// check functions:
|
||||
// is the graph...
|
||||
bool reducible();// ?
|
||||
bool knotted();// ?
|
||||
|
||||
struct m{
|
||||
int x, y;
|
||||
std::vector<std::vector<int>> data;
|
||||
};
|
||||
|
||||
private:
|
||||
int num_processes = 0;
|
||||
int num_resources = 0;
|
||||
std::vector<int> resource_counts;
|
||||
struct m matrix; // Tell me, Mr. Anderson, what good is a phone call if you are unable to speak?
|
||||
bool is_blocked(int process_id);
|
||||
};
|
@ -1,3 +0,0 @@
|
||||
|
||||
|
||||
void read_file(std::string filename);
|
@ -1,4 +1,5 @@
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "graph.hpp"
|
||||
|
||||
@ -20,7 +21,3 @@ graph::graph (const int processes, const int resources) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
graph::~graph() {
|
||||
/* clean up..? */
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "graph.hpp"
|
||||
|
||||
|
@ -4,10 +4,10 @@
|
||||
+-------------+---------+-----------------------+
|
||||
| Created 2022-04-16 |
|
||||
+-----------------------------------------------+ */
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#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
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// TODO: Grab file name from args
|
||||
@ -15,9 +15,10 @@ int main(int argc, char** argv) {
|
||||
//? Other flags? What other features should this have?
|
||||
if (argc < 2) return 1;
|
||||
std::string filename = argv[1];
|
||||
graph g;
|
||||
|
||||
//TODO: Implement reading from a file
|
||||
read_file(filename);
|
||||
g.read(filename);
|
||||
|
||||
|
||||
// TODO: Implement graph reduction and/or knot detection
|
||||
|
@ -1,11 +1,11 @@
|
||||
//TODO: Include C file IO header (stdio.h? Whatever it is)
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <regex>
|
||||
#include "graph.hpp" // struct adjmatrix
|
||||
#include "read_input.hpp" // read_input
|
||||
#include "graph.hpp"
|
||||
|
||||
#define MAX_LINE_LEN 1024
|
||||
|
||||
@ -18,34 +18,43 @@ std::vector<std::regex> patterns = {
|
||||
//* 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, ]+)")
|
||||
};
|
||||
std::regex("^([\\-0-9, ]+)")}; // matches comma-separated space-separated signed decimal integers
|
||||
|
||||
// convert comma-separated string s to vector<int>
|
||||
std::vector<int> stovi (const std::string &s);
|
||||
|
||||
// TODO: Implement reading from a file
|
||||
void read_file(std::string filename) {
|
||||
void graph::read(std::string filename) {
|
||||
printf("graph::read(%s)\n", filename.c_str());
|
||||
|
||||
// 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;
|
||||
std::vector<int> rescount;
|
||||
std::vector<std::vector<int>> matrix;
|
||||
// TODO: Check for file IO errors (I might have a solution for that in another project)
|
||||
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
|
||||
|
||||
}
|
||||
// Handle the pattern
|
||||
// If num_processes= matched, assign value to num_processes
|
||||
if (type == "num_processes") { num_processes = std::stoi(value); } else
|
||||
// If num_resources= matched, assign value to num_resources
|
||||
if (type == "num_resources") { num_resources = std::stoi(value); } else
|
||||
// If this line is a comma-separated list of numbers,
|
||||
// and this is the first match, assign it to resource counts
|
||||
if (!resource_counts.size()) { resource_counts = stovi(type); } else
|
||||
// and this is a subsequent match, push it onto the matrix
|
||||
{ matrix.data.push_back(stovi(type)); }
|
||||
|
||||
// If a pattern is matched, go to the next line and skip evaluating other patterns
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -54,5 +63,25 @@ void read_file(std::string filename) {
|
||||
// Close file
|
||||
f.close();
|
||||
// TODO: Check for file IO errors (Shouldn't be any)
|
||||
|
||||
// print information about the graph
|
||||
printf("np: %d\tnr: %d\n", num_processes, num_resources);
|
||||
printf("resource_counts:\n"); for (auto e: resource_counts) printf("%d\t", e); printf("\n");
|
||||
printf("matrix:\n"); for (auto x: matrix.data) {for (auto y: x) printf("%d\t", y); printf("\n");}
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<int> stovi (const std::string &s) {
|
||||
std::stringstream ss(s);
|
||||
printf("%s\n", s.c_str());
|
||||
size_t idx = 0;
|
||||
std::vector<int> vi;
|
||||
// reserve enough space for an entire string of one-character ints
|
||||
vi.reserve((s.length()+1)/2);
|
||||
while (!ss.eof()) {
|
||||
char integer[16]; // " -WXXXYYYZZZ,"
|
||||
ss.getline(integer, 16, ',');
|
||||
vi.push_back(atoi( (const char* ) &integer));
|
||||
}
|
||||
return vi;
|
||||
}
|
||||
|
@ -1,21 +1,24 @@
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "graph.hpp"
|
||||
|
||||
/* 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)
|
||||
true if graph is not reducible (deadlock)
|
||||
false if graph is reducible (no deadlock)
|
||||
*/
|
||||
bool graph::reducible()
|
||||
{
|
||||
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 false;
|
||||
}
|
||||
|
||||
bool graph::is_blocked(int process_id) {
|
||||
return true;
|
||||
}
|
Loading…
Reference in New Issue
Block a user