38 lines
1.8 KiB
Plaintext
38 lines
1.8 KiB
Plaintext
Assignment name: process_philosophers
|
|
Expected files: process_philosophers.c
|
|
Allowed functions: memset, printf, malloc, free, write, fork, kill, exit,
|
|
pthread_create, pthread_detach, pthread_join, waitpid,
|
|
sem_open, sem_close, sem_post, sem_wait, sem_unlink,
|
|
usleep, gettimeofday
|
|
-------------------------------------------------------------------------------
|
|
|
|
Implement a simplified philosophers problem using processes and semaphores:
|
|
1. Create N child processes (one per philosopher)
|
|
2. Use a semaphore initialized to N to represent the forks in the middle of the table
|
|
3. Each philosopher process:
|
|
- Waits to acquire 2 forks (sem_wait called twice)
|
|
- Eats for a specified time
|
|
- Releases 2 forks (sem_post called twice)
|
|
- Sleeps for a specified time
|
|
- Repeats for a specified number of meals
|
|
4. Each philosopher has a monitoring thread that checks if it's starving
|
|
5. Parent process waits for all children; if any dies, kills all others
|
|
|
|
Arguments: `number_of_philosophers time_to_die time_to_eat time_to_sleep number_of_meals`
|
|
|
|
Output format:
|
|
- "[timestamp] Philosopher <id>: has taken a fork"
|
|
- "[timestamp] Philosopher <id>: is eating"
|
|
- "[timestamp] Philosopher <id>: is sleeping"
|
|
- "[timestamp] Philosopher <id>: is thinking"
|
|
- "[timestamp] Philosopher <id>: died"
|
|
|
|
Usage: `./process_philosophers 5 800 200 200 7`
|
|
|
|
Hint: This combines everything: processes, semaphores, monitoring, and termination.
|
|
Each philosopher is a process with its own monitoring thread (just like bonus).
|
|
The semaphore represents all forks in the middle (different from mandatory version).
|
|
Use pthread_create within each child process for the death monitor thread.
|
|
Parent monitors children with waitpid(WNOHANG) and kills all if one dies.
|
|
This is almost the complete philosophers_bonus, but simplified.
|