Shared memory and semaphore setup as well as correct free() calls
This commit is contained in:
parent
b9ca106eb1
commit
b43a600510
124
generator.c
124
generator.c
@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
static const char *pname;
|
static const char *pname;
|
||||||
|
|
||||||
|
static struct circ_buf *shared;
|
||||||
|
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
|
|
||||||
struct AdjListNode {
|
struct AdjListNode {
|
||||||
@ -26,6 +28,8 @@ struct Graph {
|
|||||||
struct AdjList *array;
|
struct AdjList *array;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static struct Graph graph;
|
||||||
|
|
||||||
static volatile sig_atomic_t quit = 0;
|
static volatile sig_atomic_t quit = 0;
|
||||||
|
|
||||||
static void sig_handler(int signum)
|
static void sig_handler(int signum)
|
||||||
@ -50,24 +54,21 @@ struct AdjListNode *newAdjListNode(int dest)
|
|||||||
return newNode;
|
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++)
|
for (int i = 0; i < V; i++)
|
||||||
graph->array[i].head = NULL;
|
graph.array[i].head = NULL;
|
||||||
|
|
||||||
return graph;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void addEdge(struct Graph *graph, int src, int dest)
|
void addEdge(int src, int dest)
|
||||||
{
|
{
|
||||||
struct AdjListNode *newNode = newAdjListNode(dest);
|
struct AdjListNode *newNode = newAdjListNode(dest);
|
||||||
newNode->next = graph->array[src].head;
|
newNode->next = graph.array[src].head;
|
||||||
graph->array[src].head = newNode;
|
graph.array[src].head = newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
int strToInt(char *str)
|
int strToInt(char *str)
|
||||||
@ -89,11 +90,9 @@ int strToInt(char *str)
|
|||||||
return (int) val;
|
return (int) val;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Graph *parse(int argc, char *argv[])
|
void parse(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char *edge;
|
|
||||||
char *token;
|
char *token;
|
||||||
char *str;
|
|
||||||
int V = 0;
|
int V = 0;
|
||||||
int src;
|
int src;
|
||||||
int dest;
|
int dest;
|
||||||
@ -101,10 +100,17 @@ struct Graph *parse(int argc, char *argv[])
|
|||||||
int edges[(argc - 1) * 2];
|
int edges[(argc - 1) * 2];
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
edge = argv[i];
|
char const *edge = argv[i];
|
||||||
str = strdup(edge);
|
char *str, *orig_copy;
|
||||||
|
str = orig_copy = strdup(edge);
|
||||||
int count = 0;
|
int count = 0;
|
||||||
while ((token = strsep(&str, "-"))) {
|
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) {
|
if (count == 0) {
|
||||||
src = strToInt(token);
|
src = strToInt(token);
|
||||||
edges[edges_counter] = src;
|
edges[edges_counter] = src;
|
||||||
@ -122,18 +128,18 @@ struct Graph *parse(int argc, char *argv[])
|
|||||||
if (count > 1) {
|
if (count > 1) {
|
||||||
fprintf(stderr, "[%s] An edge consists of two "
|
fprintf(stderr, "[%s] An edge consists of two "
|
||||||
"nodes separated by a dash\n", pname);
|
"nodes separated by a dash\n", pname);
|
||||||
free(str);
|
free(orig_copy);
|
||||||
return NULL;
|
return;
|
||||||
}
|
}
|
||||||
switch (src) {
|
switch (src) {
|
||||||
case -2:
|
case -2:
|
||||||
free(str);
|
free(orig_copy);
|
||||||
return NULL;
|
return;
|
||||||
break;
|
break;
|
||||||
case -1:
|
case -1:
|
||||||
fprintf(stderr, "%s\n", edge);
|
fprintf(stderr, "%s\n", edge);
|
||||||
free(str);
|
free(orig_copy);
|
||||||
return NULL;
|
return;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -141,21 +147,19 @@ struct Graph *parse(int argc, char *argv[])
|
|||||||
|
|
||||||
count++;
|
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) {
|
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()
|
||||||
}
|
|
||||||
|
|
||||||
void printGraph(struct Graph *graph)
|
|
||||||
{
|
{
|
||||||
for (int v = 0; v < graph->V; v++) {
|
for (int v = 0; v < graph.V; v++) {
|
||||||
struct AdjListNode* pCrawl = graph->array[v].head;
|
struct AdjListNode* pCrawl = graph.array[v].head;
|
||||||
printf("\n Adjacency list of vertex %d\n head ", v);
|
printf("\n Adjacency list of vertex %d\n head ", v);
|
||||||
while (pCrawl) {
|
while (pCrawl) {
|
||||||
printf("-> %d", pCrawl->dest);
|
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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
@ -177,23 +202,36 @@ int main(int argc, char *argv[])
|
|||||||
sigaction(SIGINT, &sa, NULL);
|
sigaction(SIGINT, &sa, NULL);
|
||||||
sigaction(SIGTERM, &sa, NULL);
|
sigaction(SIGTERM, &sa, NULL);
|
||||||
|
|
||||||
struct Graph *graph;
|
/*
|
||||||
|
int shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT, PERMISSION);
|
||||||
while (quit == 0) {
|
if (shmfd == -1) {
|
||||||
graph = parse(argc, argv);
|
perror(pname);
|
||||||
if (graph == NULL) {
|
|
||||||
for (int i = 0; i < graph->V; i++) {
|
|
||||||
free(graph->array[i].head);
|
|
||||||
}
|
|
||||||
free(graph->array);
|
|
||||||
free(graph);
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
printGraph(graph);
|
|
||||||
free(graph->array);
|
shared = mmap(NULL, sizeof(*shared), PROT_WRITE, MAP_SHARED, shmfd, 0);
|
||||||
free(graph);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user