Browse Source

Normalement c'est réglé, enfin

master
Antoine Bartuccio 1 year ago
parent
commit
b00cb030be
Signed by: klmp200 <klmp200@klmp200.net> GPG Key ID: E7245548C53F904B
3 changed files with 21 additions and 15 deletions
  1. 15
    13
      SharedData/SharedData.c
  2. 5
    1
      Visitor/Visitor.c
  3. 1
    1
      main.c

+ 15
- 13
SharedData/SharedData.c View File

@@ -38,7 +38,7 @@ void start_all_threads_SharedData(THIS(SharedData)){
38 38
 		else {
39 39
 			residents = this->main_building->residents;
40 40
 			visitors = this->main_building->visitors;
41
-			this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB;
41
+			this->threads_nb = residents->get_size(residents) + visitors->get_size(visitors) + ELEVATOR_NB + 1;
42 42
 			this->threads = malloc_or_die(sizeof(pthread_t*) * this->threads_nb);
43 43
 
44 44
 			for (i=0; i < this->threads_nb; i++)
@@ -49,35 +49,37 @@ void start_all_threads_SharedData(THIS(SharedData)){
49 49
 
50 50
 			
51 51
 			/* starts threading elevators */
52
-			for (i=1; i<ELEVATOR_NB; i++) {
52
+			for (i=0; i<ELEVATOR_NB; i++) {
53 53
 				this->start_thread(this, this->main_building->elevators[i]->runnable,
54 54
 								   this->main_building->elevators[i], i);
55 55
 				this->main_building->elevators[i]->set_thread_number(this->main_building->elevators[i], i);
56 56
 			}
57 57
 
58
-			/* starts threading visitors */
59
-			current = visitors->get_head(visitors);
58
+			/* starting the elevator breaker */
59
+			this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, i);
60
+			this->elevator_breaker->set_thread_number(this->elevator_breaker, i);
61
+			i++;
62
+
63
+			/* starts threading residents */
64
+			current = residents->get_head(residents);
60 65
 			while (current != NULL){
61
-				this->start_thread(this, ((Visitor*)current->get_data(current))->runnable, current->get_data(current), i);
62
-				((Visitor*)current->get_data(current))->set_thread_number((Visitor*)current->get_data(current), i);
66
+				this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i);
67
+				((Resident*)current->get_data(current))->set_thread_number((Resident*)current->get_data(current), i);
63 68
 				current = current->get_next(current);
64 69
 				this->active_passengers++;
65 70
 				i++;
66 71
 			}
67 72
 
68
-			/* starts threading residents */
69
-			current = residents->get_head(residents);
73
+			/* starts threading visitors */
74
+			current = visitors->get_head(visitors);
70 75
 			while (current != NULL){
71
-				this->start_thread(this, ((Resident*)current->get_data(current))->runnable, current->get_data(current), i);
72
-				((Resident*)current->get_data(current))->set_thread_number((Resident*)current->get_data(current), i);
76
+				this->start_thread(this, ((Visitor*)current->get_data(current))->runnable, current->get_data(current), i);
77
+				((Visitor*)current->get_data(current))->set_thread_number((Visitor*)current->get_data(current), i);
73 78
 				current = current->get_next(current);
74 79
 				this->active_passengers++;
75 80
 				i++;
76 81
 			}
77 82
 
78
-			/* starting the elevator breaker */
79
-			this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, 0);
80
-			this->elevator_breaker->set_thread_number(this->elevator_breaker, 0);
81 83
 
82 84
 			pthread_mutex_unlock(&this->mutex_threads);
83 85
 			pthread_mutex_unlock(&this->mutex_active_passengers);

+ 5
- 1
Visitor/Visitor.c View File

@@ -23,7 +23,11 @@ void * runnable_Visitor(void * void_this){
23 23
 
24 24
 	printf("Visiteur %s : Je souhaite rendre visite a %s\n", this->name, this->contact_name);
25 25
 	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)));
26
-	data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
26
+	if (this->destination == this->position)
27
+		printf("Visiteur %s : pas besoin de prendre l'ascenseur pour cet étage, je vais y aller à pied\n", this->name);
28
+	else
29
+		data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
30
+
27 31
 	data->decrement_active_passengers(data);
28 32
 	data->unregister_thread(data, this->thread_number);
29 33
 	return NULL;

+ 1
- 1
main.c View File

@@ -23,7 +23,7 @@ int main(int argc, char* argv[]) {
23 23
 	if(argc == 3){
24 24
 		shared_data->set_main_building(shared_data, NEW(Building, argv[1], argv[2]));
25 25
 	} else if (argc == 1){
26
-		shared_data->set_main_building(shared_data, NEW(Building, "../residents.txt", "../visitors.txt"));
26
+		shared_data->set_main_building(shared_data, NEW(Building, "../Scripts/residents_500.txt", "../Scripts/visitors_500.txt"));
27 27
 	} else{
28 28
 		CRASH("Arguments invalides\nUsage : ./LO41 [residents_file visitors_file]\n");
29 29
 	}

Loading…
Cancel
Save