I thought that the program should be a tool to demonstrate the dangers
in parallel computing. Hence I let the user wholly define what
philosophers can do.
This point of view has however failed me multiple times during
evaluation... This (and a couple of conversations) led me to believe the
only way to pass is to add another element - the time_to_think.
When odd number of philosophers is entered and the time_to_sleep is too
low, the time_to_think is set to such a value that philosophers can
share the forks fairly.
#include <stddef.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdlib.h>
int is_sane(t_settings *settings)
{
return (settings->philo_count > 0);
int is_sane(t_settings *settings)
{
return (settings->philo_count > 0);
+ if (settings->philo_count % 2
+ && 3 * settings->time_to_eat < settings->time_to_die
+ && 2 * settings->time_to_eat > settings->time_to_sleep)
+ settings->time_to_think = 2 * settings->time_to_eat
+ - settings->time_to_sleep + RESERVE;
+ else
+ settings->time_to_think = 0;
if (argc == 6)
{
if (parse_arg_uint(&settings->min_eat_num, argv[5]))
if (argc == 6)
{
if (parse_arg_uint(&settings->min_eat_num, argv[5]))
static void philo_think(t_philo *philo)
{
static void philo_think(t_philo *philo)
{
- report(philo, "is thinking");
+ if (!report(philo, "is thinking"))
+ usleep(philo->settings->time_to_think);
{
t_philo *const philo = void_philo;
{
t_philo *const philo = void_philo;
+ report(philo, "is thinking");
if (philo->id % 2)
usleep(1000);
while (!end(philo->settings))
{
if (philo->id % 2)
usleep(1000);
while (!end(philo->settings))
{
philo_eat(philo);
philo_sleep(philo);
philo_eat(philo);
philo_sleep(philo);
useconds_t time_to_die;
useconds_t time_to_eat;
useconds_t time_to_sleep;
useconds_t time_to_die;
useconds_t time_to_eat;
useconds_t time_to_sleep;
+ useconds_t time_to_think;
size_t philo_count;
struct timeval start;
t_mutex terminal_lock;
size_t philo_count;
struct timeval start;
t_mutex terminal_lock;