Compare commits

..

8 Commits

Author SHA1 Message Date
Camille Chauvet
1461285dbf fix: add pthread flag to compile and link 2023-04-19 14:09:01 +00:00
Camille Chauvet
7e77316d53 add: time is now relatif 2023-04-19 14:08:23 +00:00
Camille Chauvet
6148d6effe change: 'is dead' -> 'dead' and ' 10' -> '000000010' 2023-04-19 14:06:03 +00:00
Camille Chauvet
9281e9f508 fix: stop when philo dead 2023-04-19 14:02:44 +00:00
Camille Chauvet
c674f68b2e fix: check if all threads is stopped before destroye data 2023-04-19 14:01:59 +00:00
Camille Chauvet
07a72c7f89 fix: parsing return error only if nb philo == 0 2023-04-19 13:58:18 +00:00
Camille Chauvet
23e8774c44 fix: check argument overflow 2023-04-19 13:51:57 +00:00
Camille Chauvet
f19a96a233 fix: parsing check nb philos > 0 2023-04-19 13:51:35 +00:00
9 changed files with 59 additions and 18 deletions

View File

@ -15,13 +15,13 @@ NAME = philo
CC = clang CC = clang
FLAGS = -Wall -Wextra -Werror -g FLAGS = -Wall -Wextra -Werror -g -pthread
%.o: %.c %.o: %.c
${CC} ${FLAGS} -c -o $@ $< ${CC} ${FLAGS} -c -o $@ $<
${NAME}: ${OBJS} ${NAME}: ${OBJS}
${CC} ${OBJS} -o ${NAME} ${CC} ${FLAGS} ${OBJS} -o ${NAME}
all: ${NAME} all: ${NAME}

19
data.c
View File

@ -2,6 +2,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "./data.h" #include "./data.h"
#include "philo.h" #include "philo.h"
#include "philos.h" #include "philos.h"
@ -35,9 +36,23 @@ bool data_init(t_data *data)
void data_destroyer(t_data *data) void data_destroyer(t_data *data)
{ {
free(data->threads); size_t i;
free(data->forks); t_philo *philo;
bool stop;
i = 0;
while (i < data->nb_philos)
{
philo = data->philos[i];
pthread_mutex_lock(&philo->stop_mutex);
stop = philo->stop;
pthread_mutex_unlock(&philo->stop_mutex);
if (stop)
i++;
}
pthread_mutex_destroy(&data->forks_mutex); pthread_mutex_destroy(&data->forks_mutex);
pthread_mutex_destroy(&data->stop_mutex); pthread_mutex_destroy(&data->stop_mutex);
free(data->threads);
free(data->forks);
philos_destroyer(data); philos_destroyer(data);
} }

4
main.c
View File

@ -53,8 +53,8 @@ void check_routine(t_data *data)
{ {
if ((ssize_t) get_min_meal(data) == data->nb_meals) if ((ssize_t) get_min_meal(data) == data->nb_meals)
{ {
printf("g pu faim\n");
stop(data); stop(data);
printf("g pu faim\n");
return ; return ;
} }
i = 0; i = 0;
@ -67,6 +67,7 @@ void check_routine(t_data *data)
if (ok == 1) if (ok == 1)
{ {
print_died(philo); print_died(philo);
stop(data);
return ; return ;
} }
i++; i++;
@ -78,6 +79,7 @@ int main(int ac, char **av)
{ {
t_data data; t_data data;
get_time();
if (parsing(&data, av + 1, ac - 1)) if (parsing(&data, av + 1, ac - 1))
return (1); return (1);
if (data_init(&data)) if (data_init(&data))

View File

@ -44,5 +44,7 @@ bool parsing(t_data *data, char **args, size_t n)
return (1); return (1);
} }
set_value(data, args, n); set_value(data, args, n);
return (0); if (data->nb_philos == 0)
write(2, "Argument error !\n", 16);
return (data->nb_philos == 0);
} }

26
philo.c
View File

@ -17,13 +17,19 @@ void philo_destroyer(t_philo *philo)
free(philo); free(philo);
} }
bool check(t_data *data) bool check(t_philo *philo, t_data *data)
{ {
bool stop; bool stop;
pthread_mutex_lock(&data->stop_mutex); pthread_mutex_lock(&data->stop_mutex);
stop = data->stop; stop = data->stop;
pthread_mutex_unlock(&data->stop_mutex); 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); return (stop);
} }
@ -51,11 +57,12 @@ bool philo_eat(t_philo *philo, t_data *data)
right_fork = 0; right_fork = 0;
while (left_fork == 0 || right_fork == 0) while (left_fork == 0 || right_fork == 0)
{ {
if (check(data)) if (check(philo, data))
return (1); return (1);
pthread_mutex_lock(&data->forks_mutex); pthread_mutex_lock(&data->forks_mutex);
left_fork = data->forks[philo->id]; left_fork = data->forks[philo->id];
right_fork = data->forks[(philo->id + 1) % data->nb_philos]; if (((philo->id + 1) % data->nb_philos) != philo->id)
right_fork = data->forks[(philo->id + 1) % data->nb_philos];
if (right_fork && left_fork) if (right_fork && left_fork)
{ {
data->forks[philo->id] = 0; data->forks[philo->id] = 0;
@ -85,18 +92,20 @@ void *philo_routine(void *arg)
philo = arg; philo = arg;
data = philo->data; data = philo->data;
if (philo->id % 2) print_thinking(philo);
usleep(data->sleep_time * 1000); usleep(philo->id * (data->life_expectency / data->nb_philos));
while (true) while (true)
{ {
print_thinking(philo); if (check(philo, data))
if (check(data))
return (NULL); return (NULL);
if (philo_eat(philo, data)) if (philo_eat(philo, data))
return (NULL); return (NULL);
if (check(data)) if (check(philo, data))
return (NULL); return (NULL);
philo_sleep(data, philo); philo_sleep(data, philo);
if (check(philo, data))
return (NULL);
print_thinking(philo);
} }
return (NULL); return (NULL);
} }
@ -116,5 +125,6 @@ t_philo *philo_init(t_data *data)
pthread_mutex_init(&philo->nb_meal_mutex, NULL); pthread_mutex_init(&philo->nb_meal_mutex, NULL);
pthread_mutex_init(&philo->last_eat_mutex, NULL); pthread_mutex_init(&philo->last_eat_mutex, NULL);
pthread_mutex_init(&philo->last_sleep_mutex, NULL); pthread_mutex_init(&philo->last_sleep_mutex, NULL);
pthread_mutex_init(&philo->stop_mutex, NULL);
return (philo); return (philo);
} }

View File

@ -19,7 +19,7 @@ static void print(t_data *data, size_t id, char *str)
pthread_mutex_unlock(&data->stop_mutex); pthread_mutex_unlock(&data->stop_mutex);
time = get_time(); time = get_time();
pthread_mutex_lock(&print_mutex); pthread_mutex_lock(&print_mutex);
printf("%6zu %3zu %s\n", time, id, str); printf("%07zu %3zu %s\n", time, id + 1, str);
pthread_mutex_unlock(&print_mutex); pthread_mutex_unlock(&print_mutex);
} }
@ -45,5 +45,5 @@ void print_thinking(t_philo *philo)
void print_died(t_philo *philo) void print_died(t_philo *philo)
{ {
print(philo->data, philo->id, "is died"); print(philo->data, philo->id, "died");
} }

View File

@ -28,6 +28,8 @@ typedef struct s_philo
size_t last_eat; size_t last_eat;
pthread_mutex_t last_sleep_mutex; pthread_mutex_t last_sleep_mutex;
size_t last_sleep; size_t last_sleep;
pthread_mutex_t stop_mutex;
bool stop;
t_data *data; t_data *data;
} t_philo; } t_philo;

5
time.c
View File

@ -6,9 +6,12 @@
size_t get_time(void) size_t get_time(void)
{ {
size_t time; size_t time;
static size_t start_time = 0;
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
time = tv.tv_sec * 1000000 + tv.tv_usec; time = tv.tv_sec * 1000000 + tv.tv_usec;
return (time / 1000); if (start_time == 0)
start_time = time;
return ((time - start_time) / 1000);
} }

View File

@ -6,21 +6,28 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 14:43:33 by cchauvet #+# #+# */ /* Created: 2023/03/08 14:43:33 by cchauvet #+# #+# */
/* Updated: 2023/04/11 14:47:30 by cchauvet ### ########.fr */ /* Updated: 2023/04/19 13:49:58 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../philo.h" #include "../philo.h"
#include <stddef.h>
#include <limits.h>
bool ft_isnum(char str[]) bool ft_isnum(char str[])
{ {
size_t i; size_t i;
size_t num;
num = 0;
i = 0; i = 0;
while (str[i] != '\0') while (str[i] != '\0')
{ {
if (str[i] > '9' || str[i] < '0') if (str[i] > '9' || str[i] < '0')
return (0); return (0);
num = num * 10 + str[i] - 48;
if (num > INT_MAX)
return (0);
i++; i++;
} }
return (1); return (1);