From b00cb030be70d3c7eed6e8e465be1abb1571c420 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Fri, 22 Jun 2018 07:13:13 +0200 Subject: [PATCH] =?UTF-8?q?Normalement=20c'est=20r=C3=A9gl=C3=A9,=20enfin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SharedData/SharedData.c | 32 +++++++++++++++++--------------- Visitor/Visitor.c | 6 +++++- main.c | 2 +- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/SharedData/SharedData.c b/SharedData/SharedData.c index 0e64211..f02fd68 100644 --- a/SharedData/SharedData.c +++ b/SharedData/SharedData.c @@ -38,7 +38,7 @@ void start_all_threads_SharedData(THIS(SharedData)){ else { residents = this->main_building->residents; visitors = this->main_building->visitors; - this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB; + this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB + 1; this->threads = malloc_or_die(sizeof(pthread_t*) * this->threads_nb); for (i=0; i < this->threads_nb; i++) @@ -49,12 +49,27 @@ void start_all_threads_SharedData(THIS(SharedData)){ /* starts threading elevators */ - for (i=1; istart_thread(this, this->main_building->elevators[i]->runnable, this->main_building->elevators[i], i); this->main_building->elevators[i]->set_thread_number(this->main_building->elevators[i], i); } + /* starting the elevator breaker */ + this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, i); + this->elevator_breaker->set_thread_number(this->elevator_breaker, i); + i++; + + /* starts threading residents */ + current = residents->get_head(residents); + while (current != NULL){ + this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i); + ((Resident*)current->get_data(current))->set_thread_number((Resident*)current->get_data(current), i); + current = current->get_next(current); + this->active_passengers++; + i++; + } + /* starts threading visitors */ current = visitors->get_head(visitors); while (current != NULL){ @@ -65,19 +80,6 @@ void start_all_threads_SharedData(THIS(SharedData)){ i++; } - /* starts threading residents */ - current = residents->get_head(residents); - while (current != NULL){ - this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i); - ((Resident*)current->get_data(current))->set_thread_number((Resident*)current->get_data(current), i); - current = current->get_next(current); - this->active_passengers++; - i++; - } - - /* starting the elevator breaker */ - this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, 0); - this->elevator_breaker->set_thread_number(this->elevator_breaker, 0); pthread_mutex_unlock(&this->mutex_threads); pthread_mutex_unlock(&this->mutex_active_passengers); diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index f8d7475..fcfd147 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -23,7 +23,11 @@ void * runnable_Visitor(void * void_this){ printf("Visiteur %s : Je souhaite rendre visite a %s\n", this->name, this->contact_name); printf("Visiteur %s : J'apelle à l'interphone\nVisiteur %s : J'apprends que %s habite à l'étage %d\n", this->name, this->name, this->contact_name, (this->destination = data->use_call_box(data, this->contact_name))); - data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); + if (this->destination == this->position) + printf("Visiteur %s : pas besoin de prendre l'ascenseur pour cet étage, je vais y aller à pied\n", this->name); + else + data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); + data->decrement_active_passengers(data); data->unregister_thread(data, this->thread_number); return NULL; diff --git a/main.c b/main.c index 9adbb4d..381e4ae 100644 --- a/main.c +++ b/main.c @@ -23,7 +23,7 @@ int main(int argc, char* argv[]) { if(argc == 3){ shared_data->set_main_building(shared_data, NEW(Building, argv[1], argv[2])); } else if (argc == 1){ - shared_data->set_main_building(shared_data, NEW(Building, "../residents.txt", "../visitors.txt")); + shared_data->set_main_building(shared_data, NEW(Building, "../Scripts/residents_500.txt", "../Scripts/visitors_500.txt")); } else{ CRASH("Arguments invalides\nUsage : ./LO41 [residents_file visitors_file]\n"); }