clean: norm
This commit is contained in:
parent
7e7538aa40
commit
796e186c36
2
Makefile
2
Makefile
@ -7,8 +7,10 @@ SRCS = parsing.c \
|
|||||||
main.c \
|
main.c \
|
||||||
time.c \
|
time.c \
|
||||||
print.c \
|
print.c \
|
||||||
|
print2.c \
|
||||||
data.c \
|
data.c \
|
||||||
philo.c \
|
philo.c \
|
||||||
|
philo_routine.c \
|
||||||
philos.c \
|
philos.c \
|
||||||
threads.c
|
threads.c
|
||||||
|
|
||||||
|
36
data.c
36
data.c
@ -1,7 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* data.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:28:36 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 12:04:31 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "./data.h"
|
#include "./data.h"
|
||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
@ -10,7 +23,7 @@
|
|||||||
|
|
||||||
bool data_init(t_data *data)
|
bool data_init(t_data *data)
|
||||||
{
|
{
|
||||||
size_t i;
|
ssize_t i;
|
||||||
|
|
||||||
data->stop = 0;
|
data->stop = 0;
|
||||||
data->forks = malloc(sizeof(pthread_mutex_t) * data->nb_philos);
|
data->forks = malloc(sizeof(pthread_mutex_t) * data->nb_philos);
|
||||||
@ -19,10 +32,9 @@ bool data_init(t_data *data)
|
|||||||
memset(data->forks, 1, data->nb_philos);
|
memset(data->forks, 1, data->nb_philos);
|
||||||
data->philos = malloc(sizeof(t_philo) * data->nb_philos);
|
data->philos = malloc(sizeof(t_philo) * data->nb_philos);
|
||||||
if (data->philos == NULL)
|
if (data->philos == NULL)
|
||||||
{
|
|
||||||
free(data->forks);
|
free(data->forks);
|
||||||
|
if (data->philos == NULL)
|
||||||
return (1);
|
return (1);
|
||||||
}
|
|
||||||
data->threads = malloc(sizeof(pthread_t) * data->nb_philos);
|
data->threads = malloc(sizeof(pthread_t) * data->nb_philos);
|
||||||
if (data->forks == NULL)
|
if (data->forks == NULL)
|
||||||
{
|
{
|
||||||
@ -30,12 +42,9 @@ bool data_init(t_data *data)
|
|||||||
free(data->forks);
|
free(data->forks);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
i = 0;
|
i = -1;
|
||||||
while (i < data->nb_philos)
|
while (++i < (ssize_t) data->nb_philos)
|
||||||
{
|
|
||||||
pthread_mutex_init(&data->forks[i], NULL);
|
pthread_mutex_init(&data->forks[i], NULL);
|
||||||
i++;
|
|
||||||
}
|
|
||||||
pthread_mutex_init(&data->stop_mutex, NULL);
|
pthread_mutex_init(&data->stop_mutex, NULL);
|
||||||
pthread_mutex_init(&data->print_mutex, NULL);
|
pthread_mutex_init(&data->print_mutex, NULL);
|
||||||
return (0);
|
return (0);
|
||||||
@ -43,12 +52,12 @@ bool data_init(t_data *data)
|
|||||||
|
|
||||||
void data_destroyer(t_data *data)
|
void data_destroyer(t_data *data)
|
||||||
{
|
{
|
||||||
size_t i;
|
ssize_t i;
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
bool stop;
|
bool stop;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < data->nb_philos)
|
while (i < (ssize_t) data->nb_philos)
|
||||||
{
|
{
|
||||||
philo = data->philos[i];
|
philo = data->philos[i];
|
||||||
pthread_mutex_lock(&philo->stop_mutex);
|
pthread_mutex_lock(&philo->stop_mutex);
|
||||||
@ -58,12 +67,9 @@ void data_destroyer(t_data *data)
|
|||||||
i++;
|
i++;
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
}
|
}
|
||||||
i = 0;
|
i = -1;
|
||||||
while (i < data->nb_philos)
|
while (++i < (ssize_t) data->nb_philos)
|
||||||
{
|
|
||||||
pthread_mutex_destroy(&data->forks[i]);
|
pthread_mutex_destroy(&data->forks[i]);
|
||||||
i++;
|
|
||||||
}
|
|
||||||
pthread_mutex_destroy(&data->stop_mutex);
|
pthread_mutex_destroy(&data->stop_mutex);
|
||||||
pthread_mutex_destroy(&data->print_mutex);
|
pthread_mutex_destroy(&data->print_mutex);
|
||||||
free(data->threads);
|
free(data->threads);
|
||||||
|
13
data.h
13
data.h
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* data.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:33:06 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:33:06 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef DATA_H
|
#ifndef DATA_H
|
||||||
# define DATA_H
|
# define DATA_H
|
||||||
# include <pthread.h>
|
# include <pthread.h>
|
||||||
@ -20,4 +32,5 @@ typedef struct s_data
|
|||||||
|
|
||||||
bool data_init(t_data *data);
|
bool data_init(t_data *data);
|
||||||
void data_destroyer(t_data *data);
|
void data_destroyer(t_data *data);
|
||||||
|
size_t get_min_meal(t_data *data);
|
||||||
#endif
|
#endif
|
||||||
|
59
main.c
59
main.c
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:33:25 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:49:33 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@ -44,33 +56,42 @@ void stop(t_data *data)
|
|||||||
pthread_mutex_unlock(&data->stop_mutex);
|
pthread_mutex_unlock(&data->stop_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *check_routine(t_data *data)
|
int routine(t_data *data)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
bool ok;
|
bool ok;
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < data->nb_philos)
|
||||||
|
{
|
||||||
|
philo = data->philos[i];
|
||||||
|
pthread_mutex_lock(&philo->last_eat_mutex);
|
||||||
|
ok = !(philo->last_eat + data->life_expectency > get_time());
|
||||||
|
pthread_mutex_unlock(&philo->last_eat_mutex);
|
||||||
|
if (ok == 1)
|
||||||
|
{
|
||||||
|
stop(data);
|
||||||
|
print_died(philo);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
if (get_min_meal(data) >= data->nb_meals)
|
||||||
|
{
|
||||||
|
stop(data);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *check_routine(t_data *data)
|
||||||
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
i = 0;
|
if (routine(data))
|
||||||
while (i < data->nb_philos)
|
|
||||||
{
|
{
|
||||||
philo = data->philos[i];
|
return (NULL);
|
||||||
pthread_mutex_lock(&philo->last_eat_mutex);
|
|
||||||
ok = !(philo->last_eat + data->life_expectency > get_time());
|
|
||||||
pthread_mutex_unlock(&philo->last_eat_mutex);
|
|
||||||
if (ok == 1)
|
|
||||||
{
|
|
||||||
stop(data);
|
|
||||||
print_died(philo);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
if (get_min_meal(data) >= data->nb_meals)
|
|
||||||
{
|
|
||||||
stop(data);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
parsing.c
12
parsing.c
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parsing.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:34:57 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:34:58 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "utils/utils.h"
|
#include "utils/utils.h"
|
||||||
#include "./data.h"
|
#include "./data.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
17
parsing.h
17
parsing.h
@ -1,4 +1,19 @@
|
|||||||
#include "./data.h"
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parsing.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:39:55 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:40:42 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PARSING_H
|
||||||
|
# define PARSING_H
|
||||||
|
# include "./data.h"
|
||||||
|
|
||||||
bool parsing(t_data *data, char **args, size_t n);
|
bool parsing(t_data *data, char **args, size_t n);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
125
philo.c
125
philo.c
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* philo.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:38:52 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:56:04 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@ -15,119 +27,6 @@ void philo_destroyer(t_philo *philo)
|
|||||||
free(philo);
|
free(philo);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool check(t_philo *philo, t_data *data)
|
|
||||||
{
|
|
||||||
bool stop;
|
|
||||||
|
|
||||||
if (get_min_meal(data) == data->nb_meals)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(&philo->stop_mutex);
|
|
||||||
philo->stop = 1;
|
|
||||||
pthread_mutex_unlock(&philo->stop_mutex);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
pthread_mutex_lock(&data->stop_mutex);
|
|
||||||
stop = data->stop;
|
|
||||||
pthread_mutex_unlock(&data->stop_mutex);
|
|
||||||
if (stop)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(&philo->stop_mutex);
|
|
||||||
philo->stop = 1;
|
|
||||||
pthread_mutex_unlock(&philo->stop_mutex);
|
|
||||||
}
|
|
||||||
return (stop);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool philo_eat(t_philo *philo, t_data *data)
|
|
||||||
{
|
|
||||||
size_t time;
|
|
||||||
|
|
||||||
pthread_mutex_lock(&data->forks[philo->id]);
|
|
||||||
print_take_a_fork(philo);
|
|
||||||
if (check(philo, data))
|
|
||||||
{
|
|
||||||
pthread_mutex_unlock(&data->forks[philo->id]);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
while ((philo->id + 1) % data->nb_philos == philo->id)
|
|
||||||
if (check(philo, data))
|
|
||||||
{
|
|
||||||
pthread_mutex_unlock(&data->forks[philo->id]);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
|
||||||
print_take_a_fork(philo);
|
|
||||||
if (check(philo, data))
|
|
||||||
{
|
|
||||||
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
|
||||||
pthread_mutex_unlock(&data->forks[philo->id]);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
print_eating(philo);
|
|
||||||
time = get_time();
|
|
||||||
if (time - philo->last_eat + data->eat_time > data->life_expectency)
|
|
||||||
usleep((time - philo->last_eat + data->eat_time) * 1000);
|
|
||||||
else
|
|
||||||
usleep(data->eat_time * 1000);
|
|
||||||
pthread_mutex_unlock(&data->forks[philo->id]);
|
|
||||||
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
|
||||||
if (check(philo, data))
|
|
||||||
return (1);
|
|
||||||
pthread_mutex_lock(&philo->last_eat_mutex);
|
|
||||||
philo->last_eat = get_time();
|
|
||||||
pthread_mutex_unlock(&philo->last_eat_mutex);
|
|
||||||
pthread_mutex_lock(&philo->nb_meal_mutex);
|
|
||||||
philo->nb_meal++;
|
|
||||||
pthread_mutex_unlock(&philo->nb_meal_mutex);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void philo_sleep(t_data *data, t_philo *philo)
|
|
||||||
{
|
|
||||||
size_t time;
|
|
||||||
|
|
||||||
print_sleeping(philo);
|
|
||||||
time = get_time();
|
|
||||||
if (time - philo->last_eat + data->eat_time > data->life_expectency)
|
|
||||||
usleep((time - philo->last_eat + data->eat_time) * 1000);
|
|
||||||
else
|
|
||||||
usleep(data->eat_time * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void *philo_routine(void *arg)
|
|
||||||
{
|
|
||||||
t_philo *philo;
|
|
||||||
t_data *data;
|
|
||||||
|
|
||||||
philo = arg;
|
|
||||||
data = philo->data;
|
|
||||||
print_thinking(philo);
|
|
||||||
usleep((philo->id % 2) * (data->eat_time) * 1000);
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (check(philo, data))
|
|
||||||
{
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
if (philo_eat(philo, data))
|
|
||||||
{
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
if (check(philo, data))
|
|
||||||
{
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
philo_sleep(data, philo);
|
|
||||||
if (check(philo, data))
|
|
||||||
{
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
print_thinking(philo);
|
|
||||||
}
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_philo *philo_init(t_data *data)
|
t_philo *philo_init(t_data *data)
|
||||||
{
|
{
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
|
118
philo_routine.c
Normal file
118
philo_routine.c
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* philo_routine.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:44:14 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 12:04:03 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "./time.h"
|
||||||
|
#include "./philo.h"
|
||||||
|
#include "./print.h"
|
||||||
|
#include "data.h"
|
||||||
|
|
||||||
|
bool check(t_philo *philo, t_data *data)
|
||||||
|
{
|
||||||
|
bool stop;
|
||||||
|
|
||||||
|
if (get_min_meal(data) == data->nb_meals)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&philo->stop_mutex);
|
||||||
|
philo->stop = 1;
|
||||||
|
pthread_mutex_unlock(&philo->stop_mutex);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
pthread_mutex_lock(&data->stop_mutex);
|
||||||
|
stop = data->stop;
|
||||||
|
pthread_mutex_unlock(&data->stop_mutex);
|
||||||
|
if (stop)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&philo->stop_mutex);
|
||||||
|
philo->stop = 1;
|
||||||
|
pthread_mutex_unlock(&philo->stop_mutex);
|
||||||
|
}
|
||||||
|
return (stop);
|
||||||
|
}
|
||||||
|
|
||||||
|
int philo_take_forks(t_philo *philo, t_data *data)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&data->forks[philo->id]);
|
||||||
|
print_take_a_fork(philo);
|
||||||
|
if (check(philo, data))
|
||||||
|
{
|
||||||
|
pthread_mutex_unlock(&data->forks[philo->id]);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
while ((philo->id + 1) % data->nb_philos == philo->id)
|
||||||
|
{
|
||||||
|
if (check(philo, data))
|
||||||
|
{
|
||||||
|
pthread_mutex_unlock(&data->forks[philo->id]);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
||||||
|
print_take_a_fork(philo);
|
||||||
|
if (check(philo, data))
|
||||||
|
{
|
||||||
|
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
||||||
|
pthread_mutex_unlock(&data->forks[philo->id]);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool philo_eat(t_philo *philo, t_data *data)
|
||||||
|
{
|
||||||
|
if (philo_take_forks(philo, data))
|
||||||
|
return (1);
|
||||||
|
print_eating(philo);
|
||||||
|
usleep(get_time_perfect(philo, data) * 1000);
|
||||||
|
pthread_mutex_unlock(&data->forks[philo->id]);
|
||||||
|
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
||||||
|
if (check(philo, data))
|
||||||
|
return (1);
|
||||||
|
pthread_mutex_lock(&philo->last_eat_mutex);
|
||||||
|
philo->last_eat = get_time();
|
||||||
|
pthread_mutex_unlock(&philo->last_eat_mutex);
|
||||||
|
pthread_mutex_lock(&philo->nb_meal_mutex);
|
||||||
|
philo->nb_meal++;
|
||||||
|
pthread_mutex_unlock(&philo->nb_meal_mutex);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void philo_sleep(t_data *data, t_philo *philo)
|
||||||
|
{
|
||||||
|
print_sleeping(philo);
|
||||||
|
usleep(data->eat_time * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *philo_routine(void *arg)
|
||||||
|
{
|
||||||
|
t_philo *philo;
|
||||||
|
t_data *data;
|
||||||
|
|
||||||
|
philo = arg;
|
||||||
|
data = philo->data;
|
||||||
|
print_thinking(philo);
|
||||||
|
usleep((philo->id % 2) * (data->eat_time) * 1000);
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (check(philo, data))
|
||||||
|
return (NULL);
|
||||||
|
if (philo_eat(philo, data))
|
||||||
|
return (NULL);
|
||||||
|
if (check(philo, data))
|
||||||
|
return (NULL);
|
||||||
|
philo_sleep(data, philo);
|
||||||
|
if (check(philo, data))
|
||||||
|
return (NULL);
|
||||||
|
print_thinking(philo);
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
12
philos.c
12
philos.c
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* philos.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:30:32 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:30:33 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "./philo.h"
|
#include "./philo.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
16
philos.h
16
philos.h
@ -1,5 +1,21 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* philos.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:33:17 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:33:17 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PHILOS_H
|
||||||
|
# define PHILOS_H
|
||||||
# include <stdbool.h>
|
# include <stdbool.h>
|
||||||
# include "./data.h"
|
# include "./data.h"
|
||||||
|
|
||||||
bool philos_init(t_data *data);
|
bool philos_init(t_data *data);
|
||||||
void philos_destroyer(t_data *data);
|
void philos_destroyer(t_data *data);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
29
print.c
29
print.c
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* print.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:33:33 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:43:42 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
#include <bits/pthreadtypes.h>
|
#include <bits/pthreadtypes.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
@ -47,20 +59,3 @@ void print_thinking(t_philo *philo)
|
|||||||
{
|
{
|
||||||
print(philo->data, philo->id, "is thinking");
|
print(philo->data, philo->id, "is thinking");
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_died(t_philo *philo)
|
|
||||||
{
|
|
||||||
t_data *data;
|
|
||||||
size_t time;
|
|
||||||
|
|
||||||
data = philo->data;
|
|
||||||
time = get_time();
|
|
||||||
pthread_mutex_lock(&data->print_mutex);
|
|
||||||
ft_putnum(time, 7);
|
|
||||||
ft_putchar(' ');
|
|
||||||
ft_putnum(philo->id + 1, 3);
|
|
||||||
ft_putchar(' ');
|
|
||||||
ft_putstr("died");
|
|
||||||
ft_putchar('\n');
|
|
||||||
pthread_mutex_unlock(&data->print_mutex);
|
|
||||||
}
|
|
||||||
|
16
print.h
16
print.h
@ -1,3 +1,17 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* print.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:35:50 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:36:33 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PRINT_H
|
||||||
|
# define PRINT_H
|
||||||
# include "./philo.h"
|
# include "./philo.h"
|
||||||
|
|
||||||
void print_eating(t_philo *philo);
|
void print_eating(t_philo *philo);
|
||||||
@ -5,3 +19,5 @@ void print_take_a_fork(t_philo *philo);
|
|||||||
void print_sleeping(t_philo *philo);
|
void print_sleeping(t_philo *philo);
|
||||||
void print_thinking(t_philo *philo);
|
void print_thinking(t_philo *philo);
|
||||||
void print_died(t_philo *philo);
|
void print_died(t_philo *philo);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
32
print2.c
Normal file
32
print2.c
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* print2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:41:14 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:41:46 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "philo.h"
|
||||||
|
#include "./time.h"
|
||||||
|
#include "./utils/utils.h"
|
||||||
|
|
||||||
|
void print_died(t_philo *philo)
|
||||||
|
{
|
||||||
|
t_data *data;
|
||||||
|
size_t time;
|
||||||
|
|
||||||
|
data = philo->data;
|
||||||
|
time = get_time();
|
||||||
|
pthread_mutex_lock(&data->print_mutex);
|
||||||
|
ft_putnum(time, 7);
|
||||||
|
ft_putchar(' ');
|
||||||
|
ft_putnum(philo->id + 1, 3);
|
||||||
|
ft_putchar(' ');
|
||||||
|
ft_putstr("died");
|
||||||
|
ft_putchar('\n');
|
||||||
|
pthread_mutex_unlock(&data->print_mutex);
|
||||||
|
}
|
15
threads.c
15
threads.c
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* threads.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:33:45 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:37:40 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -11,7 +23,8 @@ bool threads_init(t_data *data)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (i < data->nb_philos)
|
while (i < data->nb_philos)
|
||||||
{
|
{
|
||||||
if (pthread_create(&data->threads[i], NULL, philo_routine, data->philos[i]))
|
if (pthread_create(&data->threads[i], NULL, philo_routine,
|
||||||
|
data->philos[i]))
|
||||||
return (true);
|
return (true);
|
||||||
pthread_detach(data->threads[i]);
|
pthread_detach(data->threads[i]);
|
||||||
i++;
|
i++;
|
||||||
|
15
threads.h
15
threads.h
@ -1,5 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* threads.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:37:07 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:37:08 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef THREADS_H
|
||||||
|
# define THREADS_H
|
||||||
# include "./data.h"
|
# include "./data.h"
|
||||||
# include <stdbool.h>
|
# include <stdbool.h>
|
||||||
|
|
||||||
bool threads_init(t_data *data);
|
bool threads_init(t_data *data);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
25
time.c
25
time.c
@ -1,7 +1,21 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* time.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:35:26 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:54:04 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <bits/types/struct_timeval.h>
|
#include <bits/types/struct_timeval.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include "./philo.h"
|
||||||
|
#include "data.h"
|
||||||
|
|
||||||
size_t get_time(void)
|
size_t get_time(void)
|
||||||
{
|
{
|
||||||
@ -15,3 +29,14 @@ size_t get_time(void)
|
|||||||
start_time = time;
|
start_time = time;
|
||||||
return ((time - start_time) / 1000);
|
return ((time - start_time) / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t get_time_perfect(t_philo *philo, t_data *data)
|
||||||
|
{
|
||||||
|
size_t time;
|
||||||
|
|
||||||
|
time = get_time();
|
||||||
|
if (time - philo->last_eat + data->eat_time > data->life_expectency)
|
||||||
|
return ((time - philo->last_eat + data->eat_time));
|
||||||
|
else
|
||||||
|
return (data->eat_time);
|
||||||
|
}
|
||||||
|
19
time.h
19
time.h
@ -1,3 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* time.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:32:57 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:54:55 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef TIME_H
|
||||||
|
# define TIME_H
|
||||||
|
# include "philo.h"
|
||||||
|
# include "data.h"
|
||||||
# include <stddef.h>
|
# include <stddef.h>
|
||||||
|
|
||||||
size_t get_time(void);
|
size_t get_time(void);
|
||||||
|
size_t get_time_perfect(t_philo *philo, t_data *data);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/27 16:14:34 by cchauvet #+# #+# */
|
/* Created: 2022/09/27 16:14:34 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/04/12 11:12:49 by cchauvet ### ########.fr */
|
/* Updated: 2023/04/27 11:27:44 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ int ft_atoi(const char *str)
|
|||||||
|
|
||||||
sign = 1;
|
sign = 1;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (str[i] == '-' || str[i]== '+')
|
while (str[i] == '-' || str[i] == '+')
|
||||||
{
|
{
|
||||||
if (str[i] == '-')
|
if (str[i] == '-')
|
||||||
sign = -1;
|
sign = -1;
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* putchar.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:28:09 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:28:10 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
void ft_putchar(char c)
|
void ft_putchar(char c)
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* putnum.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:28:00 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:28:01 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* putstr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/27 11:28:26 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/27 11:28:27 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user