42/philosophers.git
18 months agoChange watch_for_starvation to not print after end
Lukas Jiriste [Thu, 30 May 2024 12:29:08 +0000 (14:29 +0200)]
Change watch_for_starvation to not print after end

This change may prevent some printing but I am not sure whther it stops
it all. Further testing (or thinking) needed.

18 months agoAdd and fix error messaging, stop removal of files
Lukas Jiriste [Thu, 30 May 2024 11:35:30 +0000 (13:35 +0200)]
Add and fix error messaging, stop removal of files

The philo program checks whether it created the semaphores.
When not, it assumes another instance is running and errors out.
Until this commit it would erase the semaphore files nontheless
because the unlinking was not conditional.

18 months agoGet rid of philo_pids array as it is not needed
Lukas Jiriste [Thu, 30 May 2024 11:19:35 +0000 (13:19 +0200)]
Get rid of philo_pids array as it is not needed

18 months agoMake semaphores lose conditionally
Lukas Jiriste [Thu, 30 May 2024 11:06:51 +0000 (13:06 +0200)]
Make semaphores lose conditionally

This is done to make helgrind stop emitting an error about closing
non-existent semaphore hence saving me some explaining during
evaluation.

18 months agoFix well fed message not printing by initializing
Lukas Jiriste [Thu, 30 May 2024 10:55:13 +0000 (12:55 +0200)]
Fix well fed message not printing by initializing

18 months agoFurther improve philos memory, make 1 philo work
Lukas Jiriste [Thu, 30 May 2024 10:50:12 +0000 (12:50 +0200)]
Further improve philos memory, make 1 philo work

1 philo did not ever exit because he did not ever get out of waiting
for a fork. This is rectified by main posting new forks at the end.

Philo cleanup has been refactored to ease with the error printing.

18 months agoHandle philo errors better, fix philo "leaks"
Lukas Jiriste [Thu, 30 May 2024 10:12:08 +0000 (12:12 +0200)]
Handle philo errors better, fix philo "leaks"

18 months agoImplement the philo side of the protocol
Lukas Jiriste [Thu, 30 May 2024 09:45:43 +0000 (11:45 +0200)]
Implement the philo side of the protocol

The protocol is drafted in previous commit (commit 6980661).
The program now produces the correct output but needs to be checked
for bugs and races.

The hunger_watcher.c is renamed due to it containing more than
just the hunger watcher.

18 months agoImplement waiting for death/fed in the main
Lukas Jiriste [Thu, 30 May 2024 08:17:06 +0000 (10:17 +0200)]
Implement waiting for death/fed in the main

This is only half the protocol implementation. It is complicated
because I cannot use signal handlers to clean after the philosopers.

The basic idea is that an end condition posts the death semaphore.
It is that used and posted imediately by the threads waiting for it.

In main thread waiting for death this ends the thread_fed.
The thread then waits for all philosophers to switch to not alive
so that it can post the term semaphore without any printing.
The term semaphore has to be posted because philosophers may be waiting
for it and would be stuck there forever.

To be implemented in next commit:
In philosopher thread the death semahore couses the alive var to switch
to 0. Additionally the philosopher posts the end semaphore
which (when all philos do so) signals to the main process it can post
the term semaphore.

18 months agoRewrite semaphore initialization, add end semaphore
Lukas Jiriste [Tue, 28 May 2024 12:48:39 +0000 (14:48 +0200)]
Rewrite semaphore initialization, add end semaphore

It is better to create semaphores at 0 than at some value and then
subtracting them to 0.

The end semaphore will be used to signal to philosophers they should
end. This mechanism was chosen instead of kill function, because the
philosophers have to clean after themselves and signal
handlers cannot be used.

18 months agoAdd terminating null to name of philo's semaphore
Lukas Jiriste [Tue, 28 May 2024 12:31:03 +0000 (14:31 +0200)]
Add terminating null to name of philo's semaphore

Otherwise the program may reach memory it should not.

18 months agoFix the hunger_watcher loop
Lukas Jiriste [Tue, 28 May 2024 12:25:28 +0000 (14:25 +0200)]
Fix the hunger_watcher loop

The hunger_watcher should exit its loop when philo has no time left.

18 months agoFix philosopher initialization
Lukas Jiriste [Tue, 28 May 2024 12:24:11 +0000 (14:24 +0200)]
Fix philosopher initialization

18 months agoAdd "empty" handle_philosophers for testing
Lukas Jiriste [Fri, 24 May 2024 13:36:00 +0000 (15:36 +0200)]
Add "empty" handle_philosophers for testing

18 months agoChange semaphores array for a structure
Lukas Jiriste [Fri, 24 May 2024 13:26:10 +0000 (15:26 +0200)]
Change semaphores array for a structure

This change is mainly done because norminette sees the old declaration
of semaphores[sem_num] as VLA. Instead of changing the enum, it may be
better to reimplement semaphores as a structure.
This may make a little bit more sense syntactically also?
(compare semaphores[forks] with semaphores.forks)

18 months agoAdd create_philo_sem_name and supporting functions
Lukas Jiriste [Fri, 24 May 2024 12:25:46 +0000 (14:25 +0200)]
Add create_philo_sem_name and supporting functions

18 months agoImplement the hunger watcher, rename function
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.

18 months agoRename philo attribute lives to alive, minor fixes
Lukas Jiriste [Fri, 24 May 2024 07:22:59 +0000 (09:22 +0200)]
Rename philo attribute lives to alive, minor fixes

The attribute has not been initialized in previous commits.
It has aso not been a part of the philosopher structure...
This has been rectified.

18 months agoAdd fcntl.h as depedency for macros (eg. O_CREAT)
Lukas Jiriste [Fri, 24 May 2024 06:48:18 +0000 (08:48 +0200)]
Add fcntl.h as depedency for macros (eg. O_CREAT)

18 months agoImplement is_alive function
Lukas Jiriste [Fri, 24 May 2024 06:46:45 +0000 (08:46 +0200)]
Implement is_alive function

Also use it in print_timestamp so that the "philosopher died" message
is the last one.

18 months agoAdd semaphore to each philo
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.

18 months agoClear mutex.c as dependency from Makefile
Lukas Jiriste [Thu, 23 May 2024 07:33:01 +0000 (09:33 +0200)]
Clear mutex.c as dependency from Makefile

18 months agoMake semaphores close properly, fix array access
Lukas Jiriste [Thu, 23 May 2024 07:32:26 +0000 (09:32 +0200)]
Make semaphores close properly, fix array access

18 months agoFix most of compilation issues.
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.

18 months agoImplement be_a_philosopher and its helpers
Lukas Jiriste [Fri, 10 May 2024 10:13:10 +0000 (12:13 +0200)]
Implement be_a_philosopher and its helpers

Also implement kill_philosophers and other minor changes.

18 months agoImplement seat_philosophers function
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.

18 months agoChange main, parsing and mem_management
Lukas Jiriste [Fri, 10 May 2024 09:04:19 +0000 (11:04 +0200)]
Change main, parsing and mem_management

Switch from mutexes to semaphores in the functions main, init and
cleanup.

In addition change parsing a little. As semaphores can only
handle unsigned int value, philo_count should only be unsigned int
as well.

18 months agoChange philo to guide the future changes
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.

18 months agoCopy the philo as a base for the bonus
Lukas Jiriste [Fri, 10 May 2024 06:52:38 +0000 (08:52 +0200)]
Copy the philo as a base for the bonus

It may be easier to repurpose the code for the bonus.

18 months agoRemove whitespace from end of lines in Makefile
Lukas Jiriste [Fri, 10 May 2024 06:47:59 +0000 (08:47 +0200)]
Remove whitespace from end of lines in Makefile

18 months agoFurther change cleanup order
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.

18 months agoAdd the check for well fed philosophers
Lukas Jiriste [Thu, 9 May 2024 10:38:31 +0000 (12:38 +0200)]
Add the check for well fed philosophers

18 months agoFormat to comply with the 42 Norm
Lukas Jiriste [Thu, 9 May 2024 09:59:59 +0000 (11:59 +0200)]
Format to comply with the 42 Norm

18 months agoAdd missing index increment, change cleanup order
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.

18 months agoJoin the threads for valgrind not to complain
Lukas Jiriste [Thu, 9 May 2024 09:06:09 +0000 (11:06 +0200)]
Join the threads for valgrind not to complain

18 months agoFix discrepancy caused by confusing ms and us
Lukas Jiriste [Thu, 9 May 2024 09:00:54 +0000 (11:00 +0200)]
Fix discrepancy caused by confusing ms and us

18 months agoImplement checking for the death of philosopher
Lukas Jiriste [Thu, 9 May 2024 08:27:02 +0000 (10:27 +0200)]
Implement checking for the death of philosopher

Also minor refactoring

18 months agoImplement the philo actions
Lukas Jiriste [Thu, 9 May 2024 07:41:44 +0000 (09:41 +0200)]
Implement the philo actions

20 months agoImplement seat_philosophers
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)

20 months agoAdd initialization of some members of philos
Lukas Jiriste [Thu, 28 Mar 2024 13:49:02 +0000 (14:49 +0100)]
Add initialization of some members of philos

20 months agoFix allocation of wrong size
Lukas Jiriste [Thu, 28 Mar 2024 11:01:32 +0000 (12:01 +0100)]
Fix allocation of wrong size

The way the allocation was written, it allocated space for pointer
instead of the structure pointed to. This has been corrected.

20 months agoFix logic bug
Lukas Jiriste [Thu, 28 Mar 2024 10:54:47 +0000 (11:54 +0100)]
Fix logic bug

20 months agoImplement conditional compilation
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.

20 months agoImplement initialization, cleanup and mutex struct
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.

20 months agoRemove unnecessary include
Lukas Jiriste [Thu, 28 Mar 2024 08:38:54 +0000 (09:38 +0100)]
Remove unnecessary include

20 months agoImplement parsing of input
Lukas Jiriste [Thu, 28 Mar 2024 08:33:12 +0000 (09:33 +0100)]
Implement parsing of input

20 months agoAdd Makefile (doesn't compile yet)
Lukas Jiriste [Tue, 26 Mar 2024 10:10:42 +0000 (11:10 +0100)]
Add Makefile (doesn't compile yet)

20 months agoChange the context of the time variables
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)?

20 months agoCreate outline of the solution
Lukas Jiriste [Tue, 26 Mar 2024 08:13:48 +0000 (09:13 +0100)]
Create outline of the solution

20 months agoAdd .gitignore as an initial commit
Lukas Jiriste [Fri, 22 Mar 2024 09:49:13 +0000 (10:49 +0100)]
Add .gitignore as an initial commit