Lukas Jiriste [Fri, 24 May 2024 08:13:27 +0000 (10:13 +0200)]
Implement the hunger watcher, rename function
The hunger watcher watches for starvation in calling philo
making him die when he starves (is hungry for too long).
The function print_timestamp is split to make it possible to
ensure the "philo died" message is the last one.
Lukas Jiriste [Thu, 23 May 2024 07:52:46 +0000 (09:52 +0200)]
Add semaphore to each philo
To react fast to starving philosopher, there has to be another thread
checking the state of the philo. Because of this there has to be
a mechanism to prevent data race. Because mutexes are forbidden
it has to be a semaphore.
This commit contains the semaphore and some code that dictates
its usage.
Lukas Jiriste [Sun, 12 May 2024 19:56:51 +0000 (21:56 +0200)]
Fix most of compilation issues.
I didn't want to make all the changes in a single commit so I split the
work. The project could not be built because of the partial changes
hence I didn't think about trying to make.
I've tried to build today and found many minor problems with the code
produced since commit 60f1edc (the copy to bonus).
I don't have much time and strength today but I wanted to be productive
so I've at least done this.
Lukas Jiriste [Fri, 10 May 2024 09:43:11 +0000 (11:43 +0200)]
Implement seat_philosophers function
The terminal semaphore is held to be able to exit without philosophers
outputing in case of an error.
Mae some minor changes, fix minor problems, remove unneeded (?) code.
Lukas Jiriste [Fri, 10 May 2024 07:34:36 +0000 (09:34 +0200)]
Change philo to guide the future changes
Reorganize the structures to reflect how I think the bonus should
be approached. Add comments to explain he thinking behind the changes.
Also remove mutex.c as mutexes are forbidden.
Lukas Jiriste [Thu, 9 May 2024 10:42:34 +0000 (12:42 +0200)]
Further change cleanup order
To make helgrind errors disappear, delete every mutex shared between
threads only after joining all the threads. This ensures that
no thread can access a mutex after the removal of the mutex because
no thread exists then.
Lukas Jiriste [Thu, 9 May 2024 09:19:16 +0000 (11:19 +0200)]
Add missing index increment, change cleanup order
The program only checked the forst philo for starvation.
The change in the cleanup order is needed because without the change
(forks) mutexes were deinitialized before joining the (philo) threads.
This could lead to a philo waiting for a fork, which has just been
cleared, leading to undefined behavior (endless wait). When the main
thread joins all the threads it gets gets stuck in the one waitin for
a fork.
Lukas Jiriste [Thu, 28 Mar 2024 13:56:54 +0000 (14:56 +0100)]
Implement seat_philosophers
Thanks to the change of how the program keeps track of time
it is possible to set the "last_eaten" member of philo in advance
(to zero - the start) without slowing the seating.
Thanks to this, no delay is necessary before the program checks whether
philos died of hunger. (It was needed in old branch, because
philosophers initialized their own time at the start of their
existance, and the check could happen before the time was set)
Lukas Jiriste [Thu, 28 Mar 2024 10:40:14 +0000 (11:40 +0100)]
Implement conditional compilation
Before this commit, compilation would stop at file parsing.c
with error (warning + -Werror) caused by -Wtype-limits.
Conditional compilation is added so that the offending if statement
is not present, when not needed.
The #if directive could have only been set up around the problematic
if statement. The 42 Norm however forbids using macros anywhere but
the global scope.
This made me write the the affected functions twice, which then broke
the 5 function limit per .c file. Hence this commit is quite large
for such a small change.
Lukas Jiriste [Thu, 28 Mar 2024 09:55:38 +0000 (10:55 +0100)]
Implement initialization, cleanup and mutex struct
The mutex struct is implemented because pthread_mutex_destroy
produces undefined behaviour when supplied with uninitialized mutex.
The struct holds information whether the mutex has been initialized
succesfully.
The header file philo.h has also been filled with declarations of
functions used in main so that Makefiles compiles every file to object.
This is done for easier checking of compile errors.
Lukas Jiriste [Tue, 26 Mar 2024 08:55:55 +0000 (09:55 +0100)]
Change the context of the time variables
This change is preliminary as the code that handles this is yet to be
written. I decided to do this because I think it will not add any
complexity but will make the code more portable (as useconds_t needs
not be huge this way)?