From 212832523789adf3025da49ed2152068a6dc5fe5 Mon Sep 17 00:00:00 2001 From: Rui Ribeiro <42305006+ruiribeiro04@users.noreply.github.com> Date: Tue, 14 Oct 2025 16:37:24 +0100 Subject: [PATCH] Added new exercices to better cover `philosophers_bonus` requirements - Added exercices: - Added process basics. - Added semaphores basics. - Added process communication - Added process termination - Added process_philosophers - Renamed and reordered philosophers_bonus --- rendu/race_detector.c | 4 +- subjects/13_process_basics.txt | 25 ++ subjects/14_semaphore_basics.txt | 28 ++ subjects/15_process_communication.txt | 28 ++ subjects/16_process_termination.txt | 29 +++ subjects/17_process_philosophers.txt | 37 +++ ...rs_bonus.txt => 18_philosophers_bonus.txt} | 0 testers/run_all_tests.sh | 14 +- testers/test_12_process_basics.sh | 172 +++++++++++++ testers/test_13_semaphore_basics.sh | 176 +++++++++++++ testers/test_14_process_communication.sh | 190 ++++++++++++++ testers/test_15_process_termination.sh | 198 +++++++++++++++ testers/test_16_process_philosophers.sh | 240 ++++++++++++++++++ ...bonus.sh => test_17_philosophers_bonus.sh} | 0 14 files changed, 1137 insertions(+), 4 deletions(-) create mode 100644 subjects/13_process_basics.txt create mode 100644 subjects/14_semaphore_basics.txt create mode 100644 subjects/15_process_communication.txt create mode 100644 subjects/16_process_termination.txt create mode 100644 subjects/17_process_philosophers.txt rename subjects/{12_philosophers_bonus.txt => 18_philosophers_bonus.txt} (100%) create mode 100755 testers/test_12_process_basics.sh create mode 100755 testers/test_13_semaphore_basics.sh create mode 100755 testers/test_14_process_communication.sh create mode 100755 testers/test_15_process_termination.sh create mode 100755 testers/test_16_process_philosophers.sh rename testers/{test_12_philosophers_bonus.sh => test_17_philosophers_bonus.sh} (100%) diff --git a/rendu/race_detector.c b/rendu/race_detector.c index a82c290..ddbc00c 100644 --- a/rendu/race_detector.c +++ b/rendu/race_detector.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: ruiferna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/10/14 00:00:00 by ruiferna #+# #+# */ -/* Updated: 2025/10/14 15:38:47 by ruiferna ### ########.fr */ +/* Created: 2025/10/14 08:32:00 by ruiferna #+# #+# */ +/* Updated: 2025/10/14 16:30:18 by ruiferna ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/subjects/13_process_basics.txt b/subjects/13_process_basics.txt new file mode 100644 index 0000000..dfb714a --- /dev/null +++ b/subjects/13_process_basics.txt @@ -0,0 +1,25 @@ +Assignment name: process_basics +Expected files: process_basics.c +Allowed functions: memset, printf, malloc, free, write, fork, exit, waitpid, usleep, gettimeofday +------------------------------------------------------------------------------- + +Create a program that: +1. Creates 3 child processes using fork() +2. Each child process prints its process ID (PID) and a unique message 5 times +3. Each child sleeps for 100ms between messages +4. The parent process waits for all children to finish using waitpid() +5. The parent prints a summary when all children have finished + +Child messages should be: +- Child 1: "Child 1 (PID: ): Message " +- Child 2: "Child 2 (PID: ): Message " +- Child 3: "Child 3 (PID: ): Message " + +Parent message: "Parent: All 3 children have finished" + +Usage: `./process_basics` + +Hint: fork() creates a new process. It returns 0 in the child process and the child's PID in the parent. +Use exit() to terminate child processes properly. +waitpid() allows the parent to wait for specific children to finish. +Unlike threads, processes have separate memory spaces. diff --git a/subjects/14_semaphore_basics.txt b/subjects/14_semaphore_basics.txt new file mode 100644 index 0000000..c39927b --- /dev/null +++ b/subjects/14_semaphore_basics.txt @@ -0,0 +1,28 @@ +Assignment name: semaphore_basics +Expected files: semaphore_basics.c +Allowed functions: memset, printf, malloc, free, write, fork, exit, waitpid, + sem_open, sem_close, sem_post, sem_wait, sem_unlink, + usleep, gettimeofday +------------------------------------------------------------------------------- + +Create a program that demonstrates POSIX semaphores: +1. Create a named semaphore initialized to 3 (simulating 3 available resources) +2. Launch 5 child processes +3. Each child process tries to acquire a resource (sem_wait), uses it for 500ms, then releases it (sem_post) +4. Each child prints when it acquires and releases a resource +5. Parent waits for all children to finish +6. Properly cleanup the semaphore using sem_unlink() + +Output format: +- "Child (PID: ): Waiting for resource..." +- "Child (PID: ): Acquired resource (available: )" +- "Child (PID: ): Using resource..." +- "Child (PID: ): Released resource" + +Usage: `./semaphore_basics` + +Hint: Named POSIX semaphores (sem_open) work across processes, unlike thread mutexes. +sem_wait() decrements the semaphore (blocks if 0), sem_post() increments it. +Always call sem_unlink() to remove the semaphore from the system. +Semaphore names must start with '/' (e.g., "/my_semaphore"). +Use O_CREAT | O_EXCL flags with sem_open, and handle if semaphore already exists. diff --git a/subjects/15_process_communication.txt b/subjects/15_process_communication.txt new file mode 100644 index 0000000..f600ee5 --- /dev/null +++ b/subjects/15_process_communication.txt @@ -0,0 +1,28 @@ +Assignment name: process_communication +Expected files: process_communication.c +Allowed functions: memset, printf, malloc, free, write, fork, exit, waitpid, + sem_open, sem_close, sem_post, sem_wait, sem_unlink, + usleep, gettimeofday +------------------------------------------------------------------------------- + +Implement a producer-consumer pattern using processes and semaphores: +1. Create 2 semaphores: one for empty slots (init: 5) and one for filled slots (init: 0) +2. Launch 2 producer processes and 2 consumer processes +3. Producers "produce" items (increment a counter) and signal consumers +4. Consumers wait for items, "consume" them, and signal producers +5. Each process performs 10 operations +6. Use a third semaphore to protect the shared counter (mutual exclusion) + +Since processes don't share memory, print actions instead of actually sharing data: +- Producer: "Producer : Produced item (empty slots: )" +- Consumer: "Consumer : Consumed item (filled slots: )" + +Parent waits for all children and cleans up semaphores. + +Usage: `./process_communication` + +Hint: This demonstrates how semaphores coordinate processes (like mutexes for threads). +You need 3 semaphores: empty slots, filled slots, and mutex for critical sections. +The pattern is similar to producer_consumer but with processes instead of threads. +Since processes don't share memory, focus on demonstrating synchronization patterns. +sem_wait() on empty before producing, sem_post() on filled after producing. diff --git a/subjects/16_process_termination.txt b/subjects/16_process_termination.txt new file mode 100644 index 0000000..bd1d59d --- /dev/null +++ b/subjects/16_process_termination.txt @@ -0,0 +1,29 @@ +Assignment name: process_termination +Expected files: process_termination.c +Allowed functions: memset, printf, malloc, free, write, fork, kill, exit, waitpid, + sem_open, sem_close, sem_post, sem_wait, sem_unlink, + usleep, gettimeofday +------------------------------------------------------------------------------- + +Create a program that demonstrates process termination and cleanup: +1. Launch 5 child processes that run in a loop (each iteration takes 500ms) +2. Each child has a random "death time" between 2-5 seconds +3. Parent process monitors all children using waitpid() with WNOHANG +4. When a child "dies" (exits), parent detects it and kills all remaining children using kill() +5. Parent prints which child died first and performs cleanup + +Output format: +- "Child (PID: ): Iteration " +- "Child (PID: ): Dying after