philosophers_prep/subjects/4_state_monitor.txt

24 lines
1008 B
Plaintext

Assignment name: state_monitor
Expected files: state_monitor.c
Allowed functions: memset, printf, malloc, free, write, pthread_create,
pthread_detach, pthread_join, pthread_mutex_init, pthread_mutex_destroy,
pthread_mutex_lock, pthread_mutex_unlock, usleep, gettimeofday
---------------------------------------------------------------------------
Create a program with 3 "worker" threads and 1 "monitor" thread:
Worker threads: Each thread alternates between 3 states:
- WORKING (2 seconds)
- RESTING (1 second)
- THINKING (1.5 seconds)
Each state change should be printed with timestamp: `[timestamp] Worker X is WORKING`
Monitor thread: Checks if any worker has been in the same state for more than 3 seconds.
If so, prints: `[timestamp] WARNING: Worker X stuck in STATE`
Usage: `./state_monitor`
Hint: Use enums for the states. The monitor thread simulates death checking in Philosophers,
you need to check continuously without interfering with workers. Use mutex to protect access to states.