diff --git a/src/nodes.c b/src/nodes.c index 7df28ca..ace2f3e 100644 --- a/src/nodes.c +++ b/src/nodes.c @@ -1,6 +1,7 @@ #include #include "routing4SITA.h" + NODE_t* getStartNode() { static NODE_t* start; diff --git a/src/routing4SITA b/src/routing4SITA index 1ae53e7..d2eff97 100755 Binary files a/src/routing4SITA and b/src/routing4SITA differ diff --git a/src/routing4SITA.c b/src/routing4SITA.c index 2685b65..bd5bf9f 100644 --- a/src/routing4SITA.c +++ b/src/routing4SITA.c @@ -4,14 +4,14 @@ #include "routing4SITA.h" +pthread_mutex_t mutex; void *Search(void* arguments); int main() { printf("hello world!\n"); - - + pthread_mutex_init (&mutex, NULL); pthread_t startThread; THREAD_DATA_t* data; @@ -25,20 +25,14 @@ int main() { data->bestpath->depth = 0; pthread_create(&startThread, NULL, Search, data); - pthread_join(startThread, NULL); - - printf("pathdepth: %i\n", data->bestpath->depth); - - printf("\nBest Path: %i", data->bestpath->predecessorsBestPath[0]); - - for(int i = 1; i <= data->bestpath->depth; i++){ - printf(" --> %i", data->bestpath->predecessorsBestPath[i]); + for(int i = 1; i <= data->bestpath->depth; i++) { + printf(" --> %i", data->bestpath->predecessorsBestPath[i]); } printf("\nWith cost of: %i\n", data->bestpath->lowestCost); - + for(;;) { } @@ -51,45 +45,45 @@ void *Search(void* arguments) { THREAD_DATA_t *data = (THREAD_DATA_t*) arguments; - printf("Number: %i\n", data->node->id); - printf("Cost to this node: %i\n", data->cost); + //printf("Number: %i\n", data->node->id); + //printf("Cost to this node: %i\n", data->cost); if(data->node->id == 8) { - printf("!!!End found! Cost: %i\n", data->cost); + //printf("!!!End found! Cost: %i\n", data->cost); //critical section start + pthread_mutex_lock (&mutex); if(data->bestpath->lowestCost == -1) { //first solution found - printf("first solution found! %i\n", data->depth); + // printf("first solution found! %i\n", data->depth); data->bestpath->lowestCost = data->cost; data->bestpath->depth = data->depth; for(int j = 0; j < data->depth; j++) { data->bestpath->predecessorsBestPath[j] = data->predecessors[j]; } - data->bestpath->predecessorsBestPath[(data->bestpath->depth)] = data->node->id; + data->bestpath->predecessorsBestPath[(data->bestpath->depth)] = data->node->id; } else { if(data->bestpath->lowestCost > data->cost) { //new best solution found - printf("new best solution found! %i\n", data->depth); + //printf("new best solution found! %i\n", data->depth); data->bestpath->lowestCost = data->cost; data->bestpath->depth = data->depth; for(int j = 0; j < data->depth; j++) { data->bestpath->predecessorsBestPath[j] = data->predecessors[j]; } - data->bestpath->predecessorsBestPath[(data->bestpath->depth)] = data->node->id; + data->bestpath->predecessorsBestPath[(data->bestpath->depth)] = data->node->id; } } - - - //critical section end + //critical section end + pthread_mutex_unlock (&mutex); return NULL; } if(data->node->nodescount == 0) { - printf("No next nodes\n"); + // printf("No next nodes\n"); return NULL; } @@ -105,19 +99,14 @@ void *Search(void* arguments) { for(int j = 0; j < data->depth; j++) { nextdata->predecessors[j] = data->predecessors[j]; - - printf("data->predecessors: %i\n", data->predecessors[j]); - } - nextdata->predecessors[((nextdata->depth) - 1)] = data->node->id; - pthread_create(&threads[i], NULL, Search, nextdata); } - - for(int i = 0; i < data->node->nodescount; i++) { + + for(int i = 0; i < data->node->nodescount; i++) { pthread_join(threads[i],NULL); - } + } return NULL; } diff --git a/src/routing4SITA.c.orig b/src/routing4SITA.c.orig deleted file mode 100644 index 19fd204..0000000 --- a/src/routing4SITA.c.orig +++ /dev/null @@ -1,113 +0,0 @@ -#include -#include -#include - -#include "routing4SITA.h" - - -void *Search(void* arguments); - -int main() { - printf("hello world!\n"); - - - - - pthread_t startThread; - THREAD_DATA_t* data; - data = (THREAD_DATA_t*) malloc(sizeof(THREAD_DATA_t)); - data->node = getStartNode(); - data->cost = 0; - data->depth = 0; - data->predecessors[0] = data->node->id; - data->bestpath = (BESTPATH_t*) malloc(sizeof(BESTPATH_t)); - data->bestpath->lowestCost = -1; - - pthread_create(&startThread, NULL, Search, data); - - for(;;) { - - } - - return 0; -} - - -void *Search(void* arguments) { - - THREAD_DATA_t *data = (THREAD_DATA_t*) arguments; - - printf("Number: %i\n", data->node->id); - printf("Cost to this node: %i\n", data->cost); - - if(data->node->id == 8) { - printf("End found! Cost: %i\n", data->cost); - - //critical section - - if(data->bestpath->lowestCost == -1){ - //first solution found - data->bestpath->lowestCost = data->cost; - for(int j = 0; j < data->depth; j++){ - nextdata->predecessors[j] = data->predecessors[j]; - } - }else{ - - if(data->bestpath->lowestCost > data->cost){ - //new best solution found - data->bestpath->lowestCost = data->cost; - for(int j = 0; j < data->depth; j++){ - nextdata->predecessors[j] = data->predecessors[j]; - } - - } - - - } - - - - return NULL; - } - - if(data->node->nodescount == 0) { - printf("No next nodes\n"); - return NULL; - } - - pthread_t threads[data->node->nodescount]; - - for(int i = 0; i < data->node->nodescount; i++) { - THREAD_DATA_t* nextdata; - nextdata = (THREAD_DATA_t*) malloc(sizeof(THREAD_DATA_t)); - nextdata->node = (data->node->nodesnext[i]); - nextdata->cost = data->node->nodescost[i] + data->cost; - nextdata->depth++; - - for(int j = 0; j < data->depth; j++){ - nextdata->predecessors[j] = data->predecessors[j]; - } - nextdata->predecessors[nextdata->depth] = data->node->id; - - pthread_create(&threads[i], NULL, Search, nextdata); - } - - return NULL; -} - - - - - - - - - - - - - - - - - diff --git a/src/routing4SITA.h b/src/routing4SITA.h index 400816a..1c2ee55 100644 --- a/src/routing4SITA.h +++ b/src/routing4SITA.h @@ -29,9 +29,9 @@ struct threadData { struct bestPath { - int lowestCost; - int predecessorsBestPath[MAX_DEPTH]; // array with all predecessor of the best path - int depth; //depth of the best path, aka. how many nodes + int lowestCost; + int predecessorsBestPath[MAX_DEPTH]; // array with all predecessor of the best path + int depth; //depth of the best path, aka. how many nodes }; #endif /* ROUTING4SITA_H */