From b675fb4053bfd292ffb3b3ba590c3c31904862b7 Mon Sep 17 00:00:00 2001 From: Rui Ribeiro <42305006+ruiribeiro04@users.noreply.github.com> Date: Wed, 8 Oct 2025 22:31:22 +0100 Subject: [PATCH] Added new exercices --- rendu/limited_resources.c | 108 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 rendu/limited_resources.c diff --git a/rendu/limited_resources.c b/rendu/limited_resources.c new file mode 100644 index 0000000..8ce6aa1 --- /dev/null +++ b/rendu/limited_resources.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* limited_resources.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ruiferna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/10/08 19:31:58 by ruiferna #+# #+# */ +/* Updated: 2025/10/08 21:10:18 by ruiferna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include +#include +#include + +#define STUDENTS 10 + +/* +- 3 computers (limited access) +- Only 3 students can use the PC at same time +- Implement a "semaphore" (using mutex, counter and condictions) +*/ + +typedef struct s_shared +{ + int pcs_available; + int students; + pthread_mutex_t pcs_available_mutex; + pthread_mutex_t students_mutex; +} t_shared; + +void *student_routine(void *arg) +{ + /* + Print when students: + - Arrive at the library + - Get a computer + - Finish using the computer + - Leave the library + */ + t_shared *shared; + int student_id; + shared = (t_shared *) arg; + srand(time(NULL)); + pthread_mutex_lock(&shared->students_mutex); + shared->students += 1; + student_id = shared->students; + printf("[%i] Student %i arrived at the library.\n", student_id, student_id); + pthread_mutex_unlock(&shared->students_mutex); + while (1) + { + pthread_mutex_lock(&shared->pcs_available_mutex); + if (shared->pcs_available > 0) + { + shared->pcs_available -= 1; + printf("[%i] Student %i got a computer.\n", student_id, student_id); + pthread_mutex_unlock(&shared->pcs_available_mutex); + break; + } + else + { + pthread_mutex_unlock(&shared->pcs_available_mutex); + usleep(100); + } + } + int use_time = 2 + rand() % 4; + usleep(use_time * 1000000); + pthread_mutex_lock(&shared->pcs_available_mutex); + shared->pcs_available += 1; + printf("[%i] Student %i finished using a computer.\n", student_id, student_id); + pthread_mutex_unlock(&shared->pcs_available_mutex); + printf("[%i] Student %i leaved the library.\n", student_id, student_id); + return (NULL); +} + +int main() +{ + pthread_t *students; + t_shared shared; + + shared.pcs_available = 3; + shared.students = 0; + pthread_mutex_init(&shared.pcs_available_mutex, NULL); + pthread_mutex_init(&shared.students_mutex, NULL); + students = (pthread_t *) malloc (sizeof(pthread_t) * STUDENTS); + + int i = 0; + while (i < STUDENTS) + { + pthread_create(&students[i], NULL, student_routine, &shared); + i++; + } + i = 0; + while (i < STUDENTS) + { + pthread_join(students[i], NULL); + i++; + } + pthread_mutex_destroy(&shared.pcs_available_mutex); + pthread_mutex_destroy(&shared.students_mutex); + free(students); + return (0); +} \ No newline at end of file