Compare commits

..

18 Commits

Author SHA1 Message Date
6e383ed9d6 ok j'arrette de troll 2023-06-01 14:24:36 +02:00
b0f5095569 ok g troll 2023-06-01 14:23:15 +02:00
f88abe9631 dsfsd 2023-05-25 17:31:02 +02:00
984f4c59bf norm 2023-05-25 15:24:35 +02:00
61b599b9f2 opti 2023-05-25 15:24:17 +02:00
f1927d349b fix: add stdbool remove useless memset 2023-05-25 14:57:43 +02:00
1b3c96b38d fix: data race 2023-05-24 16:19:21 +02:00
550851a5c8 Merge branch 'master' of git.chauvet.pro:starnakin/42_Philosopher 2023-05-24 13:55:43 +00:00
0956af87ca fix: data race 2023-05-24 15:55:19 +02:00
7e5d4af478 fix: support only > 0 number 2023-05-17 13:44:38 +02:00
008b30e12c fix: add 0 to the print 2023-05-17 13:37:20 +02:00
aefb6ceec0 bozosujet 2023-05-17 13:28:42 +02:00
8371b01390 clean: remove compile file 2023-05-17 13:26:16 +02:00
3e9f5e829f fix: replace write by printf 2023-05-17 13:25:34 +02:00
958448ca4e finish2 2023-05-16 11:26:03 +00:00
291c7b40b1 finish 2023-05-16 11:25:20 +00:00
b940b46924 ?fix 2023-05-05 16:59:36 +00:00
5c309f2f02 fix: sleeptime and eat time if time to die < time eat|sleep 2023-05-02 11:44:16 +00:00
24 changed files with 64 additions and 180 deletions

View File

@ -1,8 +1,5 @@
SRCS = parsing.c \
utils/ft_isnum.c \
utils/putchar.c \
utils/putnum.c \
utils/putstr.c \
utils/ft_atoi.c \
main.c \
time.c \

View File

@ -6,20 +6,15 @@
/* 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 */
/* Updated: 2023/05/25 14:48:52 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "./data.h"
#include "philo.h"
#include "philos.h"
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
bool data_init(t_data *data)
{
@ -29,7 +24,6 @@ bool data_init(t_data *data)
data->forks = malloc(sizeof(pthread_mutex_t) * data->nb_philos);
if (data->forks == NULL)
return (1);
memset(data->forks, 1, data->nb_philos);
data->philos = malloc(sizeof(t_philo) * data->nb_philos);
if (data->philos == NULL)
free(data->forks);
@ -67,6 +61,7 @@ void data_destroyer(t_data *data)
i++;
usleep(1000);
}
usleep(1000);
i = -1;
while (++i < (ssize_t) data->nb_philos)
pthread_mutex_destroy(&data->forks[i]);

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2023/05/25 15:26:38 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
# define DATA_H
# include <pthread.h>
# include <sys/types.h>
# include <stdbool.h>
typedef struct s_data
{
@ -21,7 +22,7 @@ typedef struct s_data
size_t sleep_time;
size_t life_expectency;
size_t nb_philos;
size_t nb_meals;
long nb_meals;
void **philos;
pthread_t *threads;
pthread_mutex_t *forks;

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2023/05/25 16:34:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -75,11 +75,6 @@ int routine(t_data *data)
print_died(philo);
return (1);
}
if (get_min_meal(data) >= data->nb_meals)
{
stop(data);
return (1);
}
i++;
}
return (0);
@ -87,13 +82,14 @@ int routine(t_data *data)
void *check_routine(t_data *data)
{
while (true)
while (data->stop == 0)
{
if (routine(data))
{
return (NULL);
}
}
return (NULL);
}
int main(int ac, char **av)

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2023/05/17 13:43:29 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,6 +30,8 @@ static bool check_value(char **av, size_t n)
{
if (ft_isnum(av[i]) == 0)
return (1);
if (!(ft_atoi(av[i]) > 0))
return (1);
i++;
}
return (0);

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2023/06/01 14:23:53 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,26 +16,19 @@
#include "./print.h"
#include "data.h"
bool check(t_philo *philo, t_data *data)
bool check(t_data *data)
{
bool stop;
if (get_min_meal(data) == data->nb_meals)
if (data->nb_meals != -1 && get_min_meal(data) == (size_t) 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);
data->stop = 1;
pthread_mutex_unlock(&data->stop_mutex);
}
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);
}
@ -43,14 +36,14 @@ 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))
if (check(data))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
while ((philo->id + 1) % data->nb_philos == philo->id)
{
if (check(philo, data))
if (check(data))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
@ -58,7 +51,7 @@ int philo_take_forks(t_philo *philo, t_data *data)
}
pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]);
print_take_a_fork(philo);
if (check(philo, data))
if (check(data))
{
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
pthread_mutex_unlock(&data->forks[philo->id]);
@ -72,24 +65,25 @@ 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);
usleep(get_time_eat(philo, data) * 1000);
pthread_mutex_unlock(&data->forks[philo->id]);
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
if (check(data))
return (1);
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)
int philo_sleep(t_data *data, t_philo *philo)
{
print_sleeping(philo);
usleep(data->eat_time * 1000);
usleep(get_time_sleep(philo, data) * 1000);
return (0);
}
void *philo_routine(void *arg)
@ -100,18 +94,20 @@ void *philo_routine(void *arg)
philo = arg;
data = philo->data;
print_thinking(philo);
usleep((philo->id % 2) * (data->eat_time) * 1000);
usleep((philo->id % 2) * (get_time_eat(philo, data)) * 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))
if (check(data)
|| philo_eat(philo, data)
|| check(data)
|| philo_sleep(data, philo)
|| check(data))
{
pthread_mutex_lock(&philo->stop_mutex);
philo->stop = 1;
pthread_mutex_unlock(&philo->stop_mutex);
return (NULL);
}
print_thinking(philo);
}
return (NULL);

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2023/05/25 16:47:54 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,12 +30,7 @@ static void print(t_data *data, size_t id, char *str)
if (stop == false)
{
time = get_time();
ft_putnum(time, 7);
ft_putchar(' ');
ft_putnum(id + 1, 3);
ft_putchar(' ');
ft_putstr(str);
ft_putchar('\n');
printf("%07zu %03zu %s\n", time, id + 1, str);
}
pthread_mutex_unlock(&data->print_mutex);
}

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2023/05/17 13:33:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,11 +22,6 @@ void print_died(t_philo *philo)
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');
printf("%07zu %03zu %s\n", time, philo->id + 1, "died");
pthread_mutex_unlock(&data->print_mutex);
}

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2023/05/25 17:28:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,13 +30,22 @@ size_t get_time(void)
return ((time - start_time) / 1000);
}
size_t get_time_perfect(t_philo *philo, t_data *data)
size_t get_time_eat(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
if (time + data->eat_time > data->life_expectency + philo->last_eat)
return (data->life_expectency + philo->last_eat - time);
return (data->eat_time);
}
size_t get_time_sleep(t_philo *philo, t_data *data)
{
size_t time;
time = get_time();
if (time + data->sleep_time > data->life_expectency + philo->last_eat)
return (data->life_expectency + philo->last_eat - time);
return (data->sleep_time);
}

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2023/05/02 11:27:53 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,7 @@
# include <stddef.h>
size_t get_time(void);
size_t get_time_perfect(t_philo *philo, t_data *data);
size_t get_time_eat(t_philo *philo, t_data *data);
size_t get_time_sleep(t_philo *philo, t_data *data);
#endif

View File

@ -1,18 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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>
void ft_putchar(char c)
{
write(1, &c, 1);
}

View File

@ -1,59 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <unistd.h>
#include <stdlib.h>
#include "utils.h"
static size_t get_size(size_t num)
{
size_t nb_digit;
size_t rest;
nb_digit = 0;
if (num == 0)
nb_digit++;
rest = num;
while (rest != 0)
{
nb_digit++;
rest = rest / 10;
}
return (nb_digit);
}
static void ft_putdigit(size_t num)
{
if (num > 9)
{
ft_putdigit(num / 10);
ft_putchar(num % 10 + 48);
}
else
ft_putchar(num + 48);
}
void ft_putnum(size_t num, size_t padding)
{
size_t size;
size_t i;
i = 0;
size = get_size(num);
while (i < padding - size)
{
ft_putchar('0');
i++;
}
ft_putdigit(num);
}

View File

@ -1,26 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <stddef.h>
void ft_putstr(char *str)
{
size_t i;
i = 0;
while (str[i] != 0)
{
ft_putchar(str[i]);
i++;
}
}