Finish impl'ing reading a file
This commit is contained in:
parent
aeeb33e699
commit
8234aca2e0
@ -1,23 +1,25 @@
|
|||||||
|
|
||||||
class graph {
|
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:
|
public:
|
||||||
// Constructors
|
// Constructors
|
||||||
|
graph() {}
|
||||||
graph(const int processes, const int resources);
|
graph(const int processes, const int resources);
|
||||||
// Destructors
|
|
||||||
~graph();
|
|
||||||
// Initializers:
|
// Initializers:
|
||||||
void init(const int **data);
|
void read(std::string filename);
|
||||||
// check functions:
|
// check functions:
|
||||||
// is the graph...
|
// is the graph...
|
||||||
bool reducible();// ?
|
bool reducible();// ?
|
||||||
bool knotted();// ?
|
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 <cstdlib>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "graph.hpp"
|
#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 <cstdlib>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "graph.hpp"
|
#include "graph.hpp"
|
||||||
|
|
||||||
|
@ -4,10 +4,10 @@
|
|||||||
+-------------+---------+-----------------------+
|
+-------------+---------+-----------------------+
|
||||||
| Created 2022-04-16 |
|
| Created 2022-04-16 |
|
||||||
+-----------------------------------------------+ */
|
+-----------------------------------------------+ */
|
||||||
|
#include <cstdio>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#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
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
// TODO: Grab file name from args
|
// TODO: Grab file name from args
|
||||||
@ -15,9 +15,10 @@ int main(int argc, char** argv) {
|
|||||||
//? Other flags? What other features should this have?
|
//? Other flags? What other features should this have?
|
||||||
if (argc < 2) return 1;
|
if (argc < 2) return 1;
|
||||||
std::string filename = argv[1];
|
std::string filename = argv[1];
|
||||||
|
graph g;
|
||||||
|
|
||||||
//TODO: Implement reading from a file
|
//TODO: Implement reading from a file
|
||||||
read_file(filename);
|
g.read(filename);
|
||||||
|
|
||||||
|
|
||||||
// TODO: Implement graph reduction and/or knot detection
|
// TODO: Implement graph reduction and/or knot detection
|
||||||
|
@ -1,51 +1,60 @@
|
|||||||
//TODO: Include C file IO header (stdio.h? Whatever it is)
|
//TODO: Include C file IO header (stdio.h? Whatever it is)
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include "graph.hpp" // struct adjmatrix
|
#include "graph.hpp"
|
||||||
#include "read_input.hpp" // read_input
|
|
||||||
|
|
||||||
#define MAX_LINE_LEN 1024
|
#define MAX_LINE_LEN 1024
|
||||||
|
|
||||||
// Vector of regices
|
// Vector of regices
|
||||||
std::vector<std::regex> patterns = {
|
std::vector<std::regex> patterns = {
|
||||||
//* A line starting with a % is a comment, and should be skipped
|
//* A line starting with a % is a comment, and should be skipped
|
||||||
//* A blank line should be skipped
|
//* A blank line should be skipped
|
||||||
//* A line starting with `num_processes=` contains the number of processes
|
//* A line starting with `num_processes=` contains the number of processes
|
||||||
std::regex("^.*(num_processes)\\s*=\\s*(\\d+).*"),
|
std::regex("^.*(num_processes)\\s*=\\s*(\\d+).*"),
|
||||||
//* A line starting with `num_resources=` contains the number of resources
|
//* A line starting with `num_resources=` contains the number of resources
|
||||||
std::regex("^.*(num_resources)\\s*=\\s*(\\d+)"),
|
std::regex("^.*(num_resources)\\s*=\\s*(\\d+)"),
|
||||||
//* A line containing comma-separated values should be returned as-is, for manual disassembly
|
//* 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
|
// 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
|
// Open file with name filename as read-only
|
||||||
std::fstream f; f.open(filename, f.in);
|
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;
|
// TODO: Check for file IO errors (I might have a solution for that in another project)
|
||||||
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
|
std::string line; // Lines can be no more than 1KB in size, a sensible limitation
|
||||||
|
|
||||||
while (!f.eof()) {
|
while (!f.eof()) {
|
||||||
std::getline (f, line);
|
std::getline (f, line);
|
||||||
|
|
||||||
std::smatch res;
|
std::smatch res;
|
||||||
// Iterate over each pattern, and grab the associated data
|
// Iterate over each pattern, and grab the associated data
|
||||||
for (auto pattern: patterns) {
|
for (auto pattern: patterns) {
|
||||||
if (std::regex_search (line, res, pattern)) {
|
if (std::regex_search (line, res, pattern)) {
|
||||||
// get the pattern type, value
|
// get the pattern type, value
|
||||||
std::string type = res.format("$1"), value = res.format("$2");
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,5 +63,25 @@ void read_file(std::string filename) {
|
|||||||
// Close file
|
// Close file
|
||||||
f.close();
|
f.close();
|
||||||
// TODO: Check for file IO errors (Shouldn't be any)
|
// 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;
|
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 <cstdlib>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "graph.hpp"
|
#include "graph.hpp"
|
||||||
|
|
||||||
/* reducible:
|
/* reducible:
|
||||||
Perform the graph reduction algorithm on the adjacency matrix to detect deadlocks
|
Perform the graph reduction algorithm on the adjacency matrix to detect deadlocks
|
||||||
This algorithm is described in section 5.2 of the Zybook
|
This algorithm is described in section 5.2 of the Zybook
|
||||||
(!THIS MODIFIES THE GRAPH!)
|
|
||||||
@params: none, uses class-internal data
|
@params: none, uses class-internal data
|
||||||
@returns:
|
@returns:
|
||||||
bool:
|
bool:
|
||||||
false if graph is not reducible (deadlock)
|
true if graph is not reducible (deadlock)
|
||||||
true if graph is reducible (no 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: 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 false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool graph::is_blocked(int process_id) {
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user