24 lines
1.1 KiB
Plaintext
24 lines
1.1 KiB
Plaintext
Assignment name: deadlock_demo
|
|
Expected files: deadlock_demo.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 that demonstrates deadlock and then shows how to solve it:
|
|
|
|
Part 1 - Deadlock:
|
|
2 threads, 2 mutexes (mutex_a, mutex_b)
|
|
- Thread 1: lock mutex_a → sleep 100ms → lock mutex_b → unlock both
|
|
- Thread 2: lock mutex_b → sleep 100ms → lock mutex_a → unlock both
|
|
|
|
Part 2 - Solution: Both threads lock the mutexes in the same order.
|
|
|
|
The program must accept an argument: 0 for deadlock, 1 for solution.
|
|
|
|
Usage: `./deadlock_demo 0` (demonstrates deadlock)
|
|
Usage: `./deadlock_demo 1` (demonstrates solution)
|
|
|
|
Hint: Deadlock is a real risk in Philosophers when philosophers try to grab forks in different orders.
|
|
The most common solution is to order the resources (forks with lower IDs first).
|