From 5398b9dccd9d0e69ef963644b3b5a93494694a15 Mon Sep 17 00:00:00 2001 From: zenon Date: Fri, 8 Jun 2018 11:21:03 +0200 Subject: [PATCH] Calculate bestSolutionLength correctly --- supervisor.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/supervisor.c b/supervisor.c index f4aaf88..7d0369a 100644 --- a/supervisor.c +++ b/supervisor.c @@ -106,12 +106,17 @@ void getSolution(sem_t *sUsedSpace, sem_t *sFreeSpace) int bestSolutionLength = 0; int newSolutionLength = 0; - for (int i = 0; i < MAX_ITEMS; i++) { - if ((bestSolution[i] == -1) && (i != 0)) { - bestSolutionLength = i+1; - break; + // Compute length of current bestSolution + if (bestSolution[0] != -1) { + for (int i = 0; i < MAX_ITEMS; i++) { + if (bestSolution[i] == -1) { + bestSolutionLength = i; + break; + } } } + + // Compute length of newSolution for (int i = 0; i < MAX_ITEMS; i++) { if ((solution[i] == -1) && (i != 0)) { newSolutionLength = i+1; @@ -121,6 +126,7 @@ void getSolution(sem_t *sUsedSpace, sem_t *sFreeSpace) if (newSolutionLength == 0) { printf("[%s] The graph is acyclic!\n", pname); + quit = 1; return; }