From b941fca02a635305ea541f92fd701d550704cbbf Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 28 Aug 2024 18:18:10 +0200 Subject: [PATCH] Modify PID Manager with threads --- threads/ex417.c | 31 ++++++++++++++ threads/pid_manager.c | 2 +- threads/pid_manager.h | 1 + threads/pid_manager_thread_chp4.c | 70 +++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 threads/ex417.c create mode 100644 threads/pid_manager_thread_chp4.c diff --git a/threads/ex417.c b/threads/ex417.c new file mode 100644 index 0000000..2e23c3b --- /dev/null +++ b/threads/ex417.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +int value = 0; +void *runner(void *param); + +int main(int argc, char *argv[]) { + pid_t pid; + pthread_t tid; + pthread_attr_t attr; + + pid = fork(); + + if (pid == 0) { + pthread_attr_init(&attr); + pthread_create(&tid, &attr, runner, NULL); + pthread_join(tid, NULL); + printf("CHILD: value = %d\n", value); + } else if (pid > 0) { + wait(NULL); + printf("PARENT: value = %d\n", value); + } +} + +void *runner(void *param) { + value = 5; + pthread_exit(0); +} diff --git a/threads/pid_manager.c b/threads/pid_manager.c index bd21294..7538b76 100644 --- a/threads/pid_manager.c +++ b/threads/pid_manager.c @@ -34,7 +34,7 @@ void release_pid(int pid) { int main(void) { if (allocate_map() != 1) { - perror("Failed to initialiaze PID Manager\n"); + perror("Failed to initialize PID Manager\n"); exit(1); } diff --git a/threads/pid_manager.h b/threads/pid_manager.h index a8011f8..e116507 100644 --- a/threads/pid_manager.h +++ b/threads/pid_manager.h @@ -1,5 +1,6 @@ #if !defined(__PID_MANGER_H__) #define __PID_MANGER_H__ +#define _GNU_SOURCE // Crea e inizializza una struttura per rappresentare i pid; // restituisce -1 in caso di insuccesso e 1 in caso di successo. diff --git a/threads/pid_manager_thread_chp4.c b/threads/pid_manager_thread_chp4.c new file mode 100644 index 0000000..761dcfd --- /dev/null +++ b/threads/pid_manager_thread_chp4.c @@ -0,0 +1,70 @@ +#include "pid_manager.h" +#include +#include +#include +#include +#include +#include + +#define MIN_PID 300 +#define MAX_PID 5000 +#define SIZE (MAX_PID - MIN_PID + 1) + +bool pids[SIZE]; + +void *runner(void *param); + +int allocate_map(void) { + for (int i = 0; i < SIZE; i++) { + pids[i] = false; + } + return 1; +} + +int allocate_pid(void) { + for (int i = 0; i < SIZE; i++) { + if (!pids[i]) { + pids[i] = true; + return i + MIN_PID; + } + } + return -1; +} + +void release_pid(int pid) { + if (pid >= MIN_PID && pid <= MAX_PID) { + pids[pid - MIN_PID] = false; + } +} + +void *runner(void *param) { + const pid_t self = gettid(); + const int pid = allocate_pid(); + printf("Run thread %d handle PID %d.\n", self, pid); + sleep(rand() % 31); + release_pid(pid); + printf("Thread %d ended\n", self); + pthread_exit(0); +} + +int main(void) { + const int thread_count = 100; + pthread_t threads[thread_count]; + + if (!allocate_map()) { + perror("Failed to allocate PID Manager\n"); + exit(EXIT_FAILURE); + } + + for (int i = 0; i < thread_count; i++) { + printf("Created thread %d\n", i); + pthread_create(&threads[i], NULL, runner, NULL); + } + + for (int i = 0; i < thread_count; i++) { + printf("Waiting thread %d\n", i); + pthread_join(threads[i], NULL); + } + + return EXIT_SUCCESS; +}