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