From c144c43593e4d14be069d19c9a262dd24802371e Mon Sep 17 00:00:00 2001 From: localhorst Date: Wed, 8 May 2019 00:52:12 +0200 Subject: [PATCH] added mutex and cleanup --- src/nodes.c | 1 + src/routing4SITA | Bin 21336 -> 21568 bytes src/routing4SITA.c | 49 +++++++---------- src/routing4SITA.c.orig | 113 ---------------------------------------- src/routing4SITA.h | 6 +-- 5 files changed, 23 insertions(+), 146 deletions(-) delete mode 100644 src/routing4SITA.c.orig 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 1ae53e7265fab0edecf2e6a824a0a068c7bb897a..d2eff97e74b9ca7c5363e827776ea90c8b0a8ec8 100755 GIT binary patch delta 4068 zcma)9eQ*=U72iEcpc6Ef{H=iT*)iCdBH2)|T`)Rf1m`a*gEix}P=|o2=?B4#X*>iX z6UPwca7Sck8q&<9&@ja5B(05U$3Pnl3{gmvj$x)v6KKatOd}7F8W%d464dv0Pm1k7 zI^CPwx9|Pl*S_1^+xzQrI?zqSnaMqNm0%M+45|+m9=A15Lm45>(++*?k~hkN+sGW$ zZR??e>!GZQx@>KdU^7kZS-Rh`ToLSYMdgF#%qg8oosmM{uXwBS^C!{+)0#e(qyK1p z&(5M!o;(ATY;enLpCyGAnCxG}$QHQGa9dfqX&L=CJ7mhBJ9+;U?>n)#yapNy^GYdn z6_-B4Zkyb!&6G)tk!Ol|-l8AxYHez3+_rsNE7{T7yt8T3mPR4^(UY5hI7PH;I|^C3 zdBZ}9nD|8qjwF6jLP;}(v}0JfcRjid>S7`m7)3TKDFEP4yrvqiv0 zFTh9(`9`_p=#yr)+fhb$vy+aJ?_W#?UudI+BGJ2Ff}ZY+CIsI=L)|y3>1u(lnnUlL zbtozkZla(Z@jbw$cOyTc6nLO*zM`)Rqpp(B{XN^BoEI!e@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ARJmq)}EeoI$jMXGF7o7h-fCQ)4WGinX@b*Y!yBi-WdEW8d+$zY-u%*#ozg z+P=d%dqrUxIP#ok*}zR?*pQbV8#=7z^m4*BK;U>@sx@?`CHg~xZ5xc{Ijvp#N$=88 z3pnzfQ9U|}hxobt!bJ}X*qZFFqctL9T zXkCKO3Z53e0>?V1`TqZ#8`h;;Qyen$NO5q-6@A_7TmqhpQ7=wV=A;bv!gOn>US4`0 zuzA5zm;|kbiFgbestpath->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 */