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