Browse Source

added mutex and cleanup

master
Hendrik Schutter 3 years ago
parent
commit
c144c43593
  1. 1
      src/nodes.c
  2. BIN
      src/routing4SITA
  3. 49
      src/routing4SITA.c
  4. 113
      src/routing4SITA.c.orig
  5. 6
      src/routing4SITA.h

1
src/nodes.c

@ -1,6 +1,7 @@
#include <stdlib.h>
#include "routing4SITA.h"
NODE_t* getStartNode() {
static NODE_t* start;

BIN
src/routing4SITA

Binary file not shown.

49
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;
}

113
src/routing4SITA.c.orig

@ -1,113 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#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;
}

6
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 */

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close