start rewriteing philo routine
This commit is contained in:
parent
4e45d4bace
commit
dee0a058e4
@ -19,6 +19,7 @@
|
||||
# include <string.h>
|
||||
# include <stddef.h>
|
||||
# include <sys/time.h>
|
||||
# include <limits.h>
|
||||
|
||||
# define SUCCESS 1
|
||||
# define FAILURE 0
|
||||
@ -43,6 +44,7 @@ typedef struct s_rules
|
||||
pthread_mutex_t *print_lock;
|
||||
pthread_mutex_t *finished_lock;
|
||||
pthread_mutex_t *forks;
|
||||
pthread_t *monitor;
|
||||
struct s_philo *philos;
|
||||
} t_rules;
|
||||
|
||||
@ -80,5 +82,8 @@ int check_death(t_philo *philo);
|
||||
void philo_die(t_philo *philo);
|
||||
int philo_died_while_sleeping(t_philo *philo, int time);
|
||||
int philo_eat(t_philo *philo);
|
||||
void *monitor_routine(void *arg);
|
||||
int check_philos(t_rules *rules);
|
||||
int ft_min(int a, int b);
|
||||
|
||||
#endif
|
||||
|
||||
@ -31,6 +31,7 @@ int create_philos(t_rules *rules)
|
||||
philo->pid = 0;
|
||||
philo->rules = rules;
|
||||
philo->last_meal = get_time() + START_DELAY;
|
||||
philo->n_eat = 0;
|
||||
i++;
|
||||
}
|
||||
return (SUCCESS);
|
||||
|
||||
@ -28,5 +28,10 @@ int create_threads(t_rules *rules)
|
||||
return (FAILURE);
|
||||
i++;
|
||||
}
|
||||
rules->monitor = malloc(sizeof(pthread_t));
|
||||
if (!rules->monitor)
|
||||
return (FAILURE);
|
||||
if (pthread_create(rules->monitor, NULL, &monitor_routine, rules))
|
||||
return (FAILURE);
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
@ -25,5 +25,7 @@ int join_threads(t_rules *rules)
|
||||
return (FAILURE);
|
||||
i++;
|
||||
}
|
||||
if (pthread_join(*rules->monitor, NULL))
|
||||
return (FAILURE);
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
@ -11,6 +11,9 @@
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
|
||||
|
||||
void set_finished(t_rules *rules)
|
||||
{
|
||||
pthread_mutex_lock(rules->finished_lock);
|
||||
@ -70,19 +73,25 @@ int philo_eat(t_philo *philo)
|
||||
philo->n_eat++;
|
||||
return (SUCCESS);
|
||||
}
|
||||
void synchronize_philos(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(philo->rules->finished_lock);
|
||||
pthread_mutex_unlock(philo->rules->finished_lock);
|
||||
philo->last_meal = get_time();
|
||||
if (philo->id % 2 == 1)
|
||||
{
|
||||
print_status(philo, "is thinking");
|
||||
usleep(philo->rules->time_to_eat * 10);
|
||||
}
|
||||
}
|
||||
|
||||
void *philo_routine(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = (t_philo *)arg;
|
||||
pthread_mutex_lock(philo->rules->finished_lock);
|
||||
pthread_mutex_unlock(philo->rules->finished_lock);
|
||||
if (philo->id % 2 == 1)
|
||||
usleep(philo->rules->time_to_eat * 100);
|
||||
while (!philo->death && !philo->rules->finished
|
||||
&& (philo->n_eat < philo->rules->n_must_eat
|
||||
|| philo->rules->n_must_eat == -1))
|
||||
synchronize_philos(philo);
|
||||
while (!philo->death && !get_finished(philo->rules))
|
||||
{
|
||||
if (check_death(philo))
|
||||
return (philo_die(philo), NULL);
|
||||
@ -92,9 +101,11 @@ void *philo_routine(void *arg)
|
||||
return (philo_die(philo), NULL);
|
||||
pthread_mutex_unlock(philo->l_fork);
|
||||
pthread_mutex_unlock(philo->r_fork);
|
||||
if (!philo->rules->finished)
|
||||
print_status(philo, "is sleeping");
|
||||
if (philo_died_while_sleeping(philo, philo->rules->time_to_sleep))
|
||||
return (philo_die(philo), NULL);
|
||||
if(!philo->rules->finished)
|
||||
print_status(philo, "is thinking");
|
||||
}
|
||||
return (NULL);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user