In computer science, The Dining Philosopher Problem is an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them.
Philosophers is an individual project at 1337 school that requires us to solve The Dining Philosopher Problem problem using threads, processes, mutex and semaphores. The mandatory part must be solved using threads and mutex. The solution was implemented using a monitoring thread, which checks if any philosopher has died and warns others to stop (locking mutex). The bonus consists of using processes instead of threads and semaphores instead of mutex, to solve it the solution used was to implement the death check between the sleeping times, avoiding the problem of reporting the death of a philosopher with more than 10ms.
The problem was proposed by Edsger W. Dijkstra in 1965 and is considered one of the classic problems about operating systems. The problem consists of five philosophers sitting around a circular dinner table, where each philosopher has a plate for eating spaghetti and a fork to his right. To eat they need 2 forks but there is only one fork between each pair of plates. A philosopher has three actions: eating, thinking, or sleeping. When a philosopher gets hungry he will try to take the fork to his right and to his left, one at a time. If he manages to get both forks, he will eat the food at a certain time and will put the forks on the table, going to sleep and then going to think again.
All arguments working with mandatory and bonus
#1 | #2 | #3 | #4 | #5 |
---|---|---|---|---|
5 | 800 | 200 | 200 | 10 |
number_of_philosophers | time_to_die | time_to_eat | time_to_sleep | *number_of_times_each_must_eat |
*The last argument, number_of_times_each_must_eat is optional. |
name | prototype | description |
---|---|---|
usleep | int usleep(useconds_t usec); | shall cause the calling thread to be suspended from execution until either the number of realtime microseconds specified by the argument useconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested due to the scheduling of other activity by the system. |
gettimeofday | int gettimeofday( struct timeval *, struct tzp * ); | fills two structures with details about the current time of day |
pthread_create | int pthread_create(pthread_t * restrict thread, const pthread_attr_t * restrict attr, void ( start_routine)(void *), void * restrict arg); | create a new thread |
pthread_detach | int pthread_detach(pthread_t thread); | marks the thread identified by thread as detached. When a detached thread terminates, its resources are automatically released back to the system without the need for another thread to join with the terminated thread. Attempting to detach an already detached thread results in unspecified behavior. |
pthread_join | int pthread_join(pthread_t thread, void ** retval); | join with a terminated thread. The function waits for the thread specified by thread to terminate. If that thread has already terminated, then pthread_join() returns immediately. The thread specified by thread must be joinable. |
pthread_mutex_init | int pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t * attr); | initialises the mutex referenced by mutex with attributes specified by attr. If attr is NULL, the default mutex attributes are used; the effect is the same as passing the address of a default mutex attributes object. Upon successful initialisation, the state of the mutex becomes initialised and unlocked. |
pthread_mutex_destroy | int pthread_mutex_destroy(pthread_mutex_t * mutex); | destroys the mutex object referenced by mutex; the mutex object becomes, in effect, uninitialised. |
pthread_mutex_lock | int pthread_mutex_lock(pthread_mutex_t * mutex); | locks the object referenced by mutex. If the mutex is already locked, the calling thread blocks until the mutex becomes available. This operation returns with the mutex object referenced by mutex in the locked state with the calling thread as its owner |
pthread_mutex_unlock | int pthread_mutex_unlock(pthread_mutex_t * mutex); | releases the mutex object referenced by mutex. |
- Unix Threads in C -playlist by CodeVault
- Multi Threaded Programming Video by Arif Butt
- Synchronization among Threads Video by Arif Butt
- Multithreaded Programming
- ASCIImoji