Shared memory and semaphore setup as well as correct free() calls

This commit is contained in:
zenon 2018-05-30 17:22:26 +02:00
parent b9ca106eb1
commit b43a600510

View File

@ -10,6 +10,8 @@
static const char *pname;
static struct circ_buf *shared;
struct sigaction sa;
struct AdjListNode {
@ -26,6 +28,8 @@ struct Graph {
struct AdjList *array;
};
static struct Graph graph;
static volatile sig_atomic_t quit = 0;
static void sig_handler(int signum)
@ -50,24 +54,21 @@ struct AdjListNode *newAdjListNode(int dest)
return newNode;
}
struct Graph *createGraph(int V)
void createGraph(int V)
{
struct Graph *graph = malloc(sizeof(struct Graph));
graph->V = V;
graph.V = V;
graph->array = malloc(V * sizeof(struct AdjList));
graph.array = malloc(V * sizeof(struct AdjList));
for (int i = 0; i < V; i++)
graph->array[i].head = NULL;
return graph;
graph.array[i].head = NULL;
}
void addEdge(struct Graph *graph, int src, int dest)
void addEdge(int src, int dest)
{
struct AdjListNode *newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
newNode->next = graph.array[src].head;
graph.array[src].head = newNode;
}
int strToInt(char *str)
@ -89,11 +90,9 @@ int strToInt(char *str)
return (int) val;
}
struct Graph *parse(int argc, char *argv[])
void parse(int argc, char *argv[])
{
char *edge;
char *token;
char *str;
int V = 0;
int src;
int dest;
@ -101,10 +100,17 @@ struct Graph *parse(int argc, char *argv[])
int edges[(argc - 1) * 2];
for (int i = 1; i < argc; i++) {
edge = argv[i];
str = strdup(edge);
char const *edge = argv[i];
char *str, *orig_copy;
str = orig_copy = strdup(edge);
int count = 0;
while ((token = strsep(&str, "-"))) {
if (str == NULL) {
fprintf(stderr, "[%s] An edge consists of two "
"nodes separated by a dash\n", pname);
free(orig_copy);
return;
}
if (count == 0) {
src = strToInt(token);
edges[edges_counter] = src;
@ -122,18 +128,18 @@ struct Graph *parse(int argc, char *argv[])
if (count > 1) {
fprintf(stderr, "[%s] An edge consists of two "
"nodes separated by a dash\n", pname);
free(str);
return NULL;
free(orig_copy);
return;
}
switch (src) {
case -2:
free(str);
return NULL;
free(orig_copy);
return;
break;
case -1:
fprintf(stderr, "%s\n", edge);
free(str);
return NULL;
free(orig_copy);
return;
break;
default:
break;
@ -141,21 +147,19 @@ struct Graph *parse(int argc, char *argv[])
count++;
}
free(str);
free(orig_copy);
}
struct Graph *graph = createGraph(V+1);
createGraph(V+1);
for (int i = 0; i < (sizeof(edges) / sizeof(int)); i+=2) {
addEdge(graph, edges[i], edges[i+1]);
addEdge(edges[i], edges[i+1]);
}
return graph;
}
void printGraph(struct Graph *graph)
void printGraph()
{
for (int v = 0; v < graph->V; v++) {
struct AdjListNode* pCrawl = graph->array[v].head;
for (int v = 0; v < graph.V; v++) {
struct AdjListNode* pCrawl = graph.array[v].head;
printf("\n Adjacency list of vertex %d\n head ", v);
while (pCrawl) {
printf("-> %d", pCrawl->dest);
@ -165,6 +169,27 @@ void printGraph(struct Graph *graph)
}
}
void putItem(int itemValue)
{
shared->validItems++;
shared->data[shared->tail] = itemValue;
shared->tail = (shared->tail + 1) % MAX_ITEMS;
}
void cleanup()
{
for (int v = 0; v < graph.V; v++) {
struct AdjListNode *pCrawl = graph.array[v].head;
struct AdjListNode *tmp;
while (pCrawl) {
tmp = pCrawl;
pCrawl = pCrawl->next;
free(tmp);
}
}
free(graph.array);
}
int main(int argc, char *argv[])
{
if (argc == 1)
@ -177,23 +202,36 @@ int main(int argc, char *argv[])
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
struct Graph *graph;
while (quit == 0) {
graph = parse(argc, argv);
if (graph == NULL) {
for (int i = 0; i < graph->V; i++) {
free(graph->array[i].head);
}
free(graph->array);
free(graph);
/*
int shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT, PERMISSION);
if (shmfd == -1) {
perror(pname);
exit(EXIT_FAILURE);
}
printGraph(graph);
free(graph->array);
free(graph);
shared = mmap(NULL, sizeof(*shared), PROT_WRITE, MAP_SHARED, shmfd, 0);
if (shared == MAP_FAILED) {
perror(pname);
exit(EXIT_FAILURE);
}
sem_t *sFreeSpace = sem_open(SEM_FREE_SPACE, 0);
sem_t *sUsedSpace = sem_open(SEM_USED_SPACE, 0);
sem_t *sWriteEnd = sem_open(SEM_WRITE_END, O_CREAT, PERMISSION, 1);
if (sFreeSpace == SEM_FAILED || sUsedSpace == SEM_FAILED || sWriteEnd == SEM_FAILED) {
perror(pname);
exit(EXIT_FAILURE);
}
*/
parse(argc, argv);
while (quit == 0) {
printGraph();
break;
}
cleanup();
return 0;
}