working and norminette
This commit is contained in:
parent
1ca5e59f8d
commit
95ebe2b00b
@ -26,7 +26,6 @@ RM = rm -rf
|
||||
|
||||
INCLUDES = -I./$(INC_PATH)
|
||||
CFLAGS = -Wall -Wextra -Werror -MMD -g
|
||||
|
||||
UNAME_S := $(shell uname -s)
|
||||
ifeq ($(UNAME_S),Linux)
|
||||
LDLIBS := -pthread
|
||||
|
||||
@ -26,8 +26,6 @@
|
||||
|
||||
# define START_DELAY 0
|
||||
|
||||
# define ERROR_USAGE "Usage: ./philo number_of_philosophers time_to_die " \
|
||||
"time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]\n"
|
||||
# define ERROR_MUTEX "Can not create mutex"
|
||||
# define ERROR_MALLOC "Malloc Failure"
|
||||
# define ERROR_THREADS "Can not create threads"
|
||||
@ -91,6 +89,7 @@ void set_finished(t_rules *rules);
|
||||
int get_finished(t_rules *rules);
|
||||
void synchronize_philos(t_philo *philo);
|
||||
int philo_sleep(t_philo *philo, int time);
|
||||
|
||||
void unlock_forks(t_philo *philo);
|
||||
void take_forks(t_philo *philo);
|
||||
|
||||
#endif
|
||||
|
||||
@ -29,15 +29,13 @@ int check_philos(t_rules *rules)
|
||||
{
|
||||
philo_die(&rules->philos[i]);
|
||||
set_finished(rules);
|
||||
pthread_mutex_unlock(rules->philos[i].last_meal_lock);
|
||||
return (FAILURE);
|
||||
}
|
||||
pthread_mutex_unlock(rules->philos[i].last_meal_lock);
|
||||
i++;
|
||||
}
|
||||
if (min_n_eat >= rules->n_must_eat)
|
||||
{
|
||||
set_finished(rules);
|
||||
return (FAILURE);
|
||||
}
|
||||
return (set_finished(rules), FAILURE);
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
@ -17,21 +17,14 @@ int destroy_mutexes(t_rules *rules)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (pthread_mutex_destroy(rules->finished_lock))
|
||||
return (FAILURE);
|
||||
if (pthread_mutex_destroy(rules->print_lock))
|
||||
return (FAILURE);
|
||||
pthread_mutex_destroy(rules->finished_lock);
|
||||
pthread_mutex_destroy(rules->print_lock);
|
||||
while (i < rules->n_philos)
|
||||
{
|
||||
if (pthread_mutex_destroy(&rules->forks[i]))
|
||||
return (printf("1"), FAILURE);
|
||||
if (pthread_mutex_destroy(rules->philos[i].death_lock))
|
||||
return (printf("2"), FAILURE);
|
||||
// pthread_mutex_lock(rules->philos[i].last_meal_lock);
|
||||
if (pthread_mutex_destroy(rules->philos[i].last_meal_lock))
|
||||
return (printf("3: %d\n", i), perror("Error"), FAILURE);
|
||||
if (pthread_mutex_destroy(rules->philos[i].n_eat_lock))
|
||||
return (printf("4"), FAILURE);
|
||||
pthread_mutex_destroy(&rules->forks[i]);
|
||||
pthread_mutex_destroy(rules->philos[i].death_lock);
|
||||
pthread_mutex_destroy(rules->philos[i].last_meal_lock);
|
||||
pthread_mutex_destroy(rules->philos[i].n_eat_lock);
|
||||
i++;
|
||||
}
|
||||
return (SUCCESS);
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
void free_philos(t_rules *rules)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < rules->n_philos)
|
||||
{
|
||||
|
||||
@ -27,5 +27,6 @@ int join_threads(t_rules *rules)
|
||||
}
|
||||
if (pthread_join(*rules->monitor, NULL))
|
||||
return (FAILURE);
|
||||
usleep(14000);
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
@ -12,12 +12,18 @@
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
void print_usage(void)
|
||||
{
|
||||
printf("Usage: ./philo n_philos time_to_die"
|
||||
"time_to_eat time_to_sleep [n_must_eat]\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
t_rules rules;
|
||||
|
||||
if (FAILURE == parse_arguments(argc, argv, &rules))
|
||||
return (printf(ERROR_USAGE), EXIT_FAILURE);
|
||||
return (print_usage(), EXIT_FAILURE);
|
||||
if (FAILURE == create_philos(&rules))
|
||||
return (printf(ERROR_MALLOC), EXIT_FAILURE);
|
||||
if (FAILURE == create_mutexes(&rules))
|
||||
@ -29,7 +35,6 @@ int main(int argc, char *argv[])
|
||||
pthread_mutex_unlock(rules.finished_lock);
|
||||
if (FAILURE == join_threads(&rules))
|
||||
return (printf(ERROR_THREADS), EXIT_FAILURE);
|
||||
|
||||
if (FAILURE == destroy_mutexes(&rules))
|
||||
return (printf(ERROR_MUTEX), EXIT_FAILURE);
|
||||
free_philos(&rules);
|
||||
|
||||
@ -12,7 +12,6 @@
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
|
||||
void *monitor_routine(void *arg)
|
||||
{
|
||||
t_rules *rules;
|
||||
@ -20,7 +19,6 @@ void *monitor_routine(void *arg)
|
||||
rules = (t_rules *)arg;
|
||||
while (1)
|
||||
{
|
||||
|
||||
if (!check_philos(rules))
|
||||
{
|
||||
break ;
|
||||
|
||||
@ -19,6 +19,4 @@ void philo_die(t_philo *philo)
|
||||
philo->death = 1;
|
||||
pthread_mutex_unlock(philo->death_lock);
|
||||
set_finished(philo->rules);
|
||||
pthread_mutex_unlock(philo->l_fork);
|
||||
pthread_mutex_unlock(philo->r_fork);
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
int philo_eat(t_philo *philo)
|
||||
void take_forks(t_philo *philo)
|
||||
{
|
||||
if (philo->id % 2 == 1)
|
||||
{
|
||||
@ -26,14 +26,21 @@ int philo_eat(t_philo *philo)
|
||||
pthread_mutex_lock(philo->r_fork);
|
||||
print_status(philo, "has taken a fork");
|
||||
}
|
||||
if (get_finished(philo->rules))
|
||||
return (FAILURE);
|
||||
print_status(philo, "is eating");
|
||||
pthread_mutex_lock(philo->last_meal_lock);
|
||||
philo->last_meal = get_time();
|
||||
pthread_mutex_unlock(philo->last_meal_lock);
|
||||
pthread_mutex_lock(philo->n_eat_lock);
|
||||
philo->n_eat++;
|
||||
pthread_mutex_unlock(philo->n_eat_lock);
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
int philo_eat(t_philo *philo)
|
||||
{
|
||||
take_forks(philo);
|
||||
if (get_finished(philo->rules))
|
||||
return (unlock_forks(philo), FAILURE);
|
||||
pthread_mutex_lock(philo->last_meal_lock);
|
||||
pthread_mutex_lock(philo->n_eat_lock);
|
||||
print_status(philo, "is eating");
|
||||
philo->last_meal = get_time();
|
||||
philo->n_eat++;
|
||||
pthread_mutex_unlock(philo->last_meal_lock);
|
||||
pthread_mutex_unlock(philo->n_eat_lock);
|
||||
if (!philo_sleep(philo, philo->rules->time_to_eat))
|
||||
return (unlock_forks(philo), FAILURE);
|
||||
return (unlock_forks(philo), SUCCESS);
|
||||
}
|
||||
|
||||
@ -12,27 +12,50 @@
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
void unlock_forks(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_unlock(philo->l_fork);
|
||||
pthread_mutex_unlock(philo->r_fork);
|
||||
}
|
||||
|
||||
int think_time(t_rules *rules)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = (rules->time_to_die - rules->time_to_eat - rules->time_to_sleep) / 5;
|
||||
return (res);
|
||||
}
|
||||
|
||||
void one_philo(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(philo->l_fork);
|
||||
print_status(philo, "has taken a fork");
|
||||
pthread_mutex_unlock(philo->l_fork);
|
||||
philo_sleep(philo, philo->rules->time_to_die);
|
||||
}
|
||||
|
||||
void *philo_routine(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = (t_philo *)arg;
|
||||
synchronize_philos(philo);
|
||||
if (philo->rules->n_philos == 1)
|
||||
{
|
||||
one_philo(philo);
|
||||
return (NULL);
|
||||
}
|
||||
while (!philo->death && !get_finished(philo->rules))
|
||||
{
|
||||
if (!philo_eat(philo))
|
||||
break ;
|
||||
if (!philo_sleep(philo, philo->rules->time_to_eat))
|
||||
break ;
|
||||
pthread_mutex_unlock(philo->l_fork);
|
||||
pthread_mutex_unlock(philo->r_fork);
|
||||
if (!philo->death && !get_finished(philo->rules))
|
||||
print_status(philo, "is sleeping");
|
||||
if (!philo_sleep(philo, philo->rules->time_to_sleep))
|
||||
break ;
|
||||
if (!philo->death && !get_finished(philo->rules))
|
||||
print_status(philo, "is thinking");
|
||||
if (!philo_sleep(philo, (philo->rules->time_to_die - philo->rules->time_to_eat - philo->rules->time_to_sleep) / 3))
|
||||
if (!philo_sleep(philo, think_time(philo->rules)))
|
||||
break ;
|
||||
}
|
||||
return (NULL);
|
||||
|
||||
@ -12,7 +12,6 @@
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
|
||||
void set_death(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(philo->death_lock);
|
||||
|
||||
@ -24,4 +24,9 @@ void synchronize_philos(t_philo *philo)
|
||||
print_status(philo, "is thinking");
|
||||
usleep(philo->rules->time_to_eat * 10);
|
||||
}
|
||||
else if (philo->id == philo->rules->n_philos && philo->rules->n_philos > 1)
|
||||
{
|
||||
print_status(philo, "is thinking");
|
||||
usleep(philo->rules->time_to_eat * 10);
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,8 +14,9 @@
|
||||
|
||||
void print_status(t_philo *philo, char *status)
|
||||
{
|
||||
if (philo->death)
|
||||
return ;
|
||||
pthread_mutex_lock(philo->death_lock);
|
||||
if (!philo->death)
|
||||
{
|
||||
pthread_mutex_lock(philo->rules->print_lock);
|
||||
printf("%d %d %s\n",
|
||||
get_time() - philo->rules->start_time,
|
||||
@ -23,3 +24,5 @@ void print_status(t_philo *philo, char *status)
|
||||
status);
|
||||
pthread_mutex_unlock(philo->rules->print_lock);
|
||||
}
|
||||
pthread_mutex_unlock(philo->death_lock);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user