philosophers_prep/subjects/3_precise_timing.txt

24 lines
1.0 KiB
Plaintext

Assignment name: precise_timing
Expected files: precise_timing.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
---------------------------------------------------------------------------------------
Implement the following functions:
- `long long get_current_time()` - returns the current time in milliseconds
- `void precise_sleep(int ms)` - sleeps for exactly X milliseconds
- `long long time_diff(long long start, long long end)` - calculates difference between times
Create a program that tests these functions:
1. Measure start time
2. Sleep for 100ms using precise_sleep
3. Measure end time
4. Print the difference (should be ~100ms)
Usage: `./precise_timing`
Hint: gettimeofday() is crucial in the Philosophers project.
usleep() is not always precise, so you need to check the time periodically.
In Philosophers, precise timing is essential for detecting deaths.