diff --git a/supervisor.c b/supervisor.c index 8cf6595..96f8545 100644 --- a/supervisor.c +++ b/supervisor.c @@ -13,6 +13,8 @@ struct circ_buf *shared; struct sigaction sa; +static int bestSolution[MAX_ITEMS] = {-1}; + static volatile sig_atomic_t quit = 0; static void sig_handler(int signum) @@ -53,7 +55,44 @@ void getSolution(sem_t sUsedSpace, sem_t sFreeSpace, int *solution) } } - // Code for testing wether new solution is better than old + // Code for testing if new solution is better than old + int bestSolutionLength = 0; + int newSolutionLength = 0; + + for (int i = 0; i < MAX_ITEMS; i++) { + if ((bestSolution[i] == -1) && (i != 0)) { + bestSolutionLength = i+1; + break; + } + } + for (int i = 0; i < MAX_ITEMS; i++) { + if ((solution[i] == -1) && (i != 0)) { + newSolutionLength = i+1; + break; + } + } + + if (newSolutionLength == 0) { + printf("[%s] The graph is acyclic!\n", pname); + return; + } + + if (newSolutionLength < bestSolutionLength) { + // Set all entries of bestSolution to -1 + for (int i = 0; i < bestSolutionLength; i++) { + bestSolution[i] = -1; + } + // Copy all entries from solution to bestSolution + for (int i = 0; i < newSolutionLength; i++) { + bestSolution[i] = solution[i]; + } + + printf("[%s] Solution with %d edges:", pname, newSolutionLength/2); + for (int i = 0; i < newSolutionLength; i += 2) { + printf(" %d-%d", bestSolution[i], bestSolution[i+1]); + } + printf("\n"); + } } int main(int argc, char *argv[])