Compare commits
No commits in common. "d6aee6360029934cab447d3fb5a2133e8789ead1" and "531d3146e4da105ce1de39628a8160991b97ec78" have entirely different histories.
d6aee63600
...
531d3146e4
3
Makefile
3
Makefile
@ -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 \
|
||||
|
19
data.c
19
data.c
@ -6,14 +6,13 @@
|
||||
#include "./data.h"
|
||||
#include "philo.h"
|
||||
#include "philos.h"
|
||||
#include "struct.h"
|
||||
#include <string.h>
|
||||
|
||||
bool data_init(t_data *data)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
data->stop = 0;
|
||||
data->forks = malloc(sizeof(pthread_mutex_t) * data->nb_philos);
|
||||
data->forks = malloc(sizeof(bool) * data->nb_philos);
|
||||
if (data->forks == NULL)
|
||||
return (1);
|
||||
memset(data->forks, 1, data->nb_philos);
|
||||
@ -30,12 +29,7 @@ bool data_init(t_data *data)
|
||||
free(data->forks);
|
||||
return (1);
|
||||
}
|
||||
i = 0;
|
||||
while (i < data->nb_philos)
|
||||
{
|
||||
pthread_mutex_init(&data->forks[i], NULL);
|
||||
i++;
|
||||
}
|
||||
pthread_mutex_init(&data->forks_mutex, NULL);
|
||||
pthread_mutex_init(&data->stop_mutex, NULL);
|
||||
pthread_mutex_init(&data->print_mutex, NULL);
|
||||
return (0);
|
||||
@ -58,12 +52,7 @@ void data_destroyer(t_data *data)
|
||||
i++;
|
||||
usleep(1000);
|
||||
}
|
||||
i = 0;
|
||||
while (i < data->nb_philos)
|
||||
{
|
||||
pthread_mutex_destroy(&data->forks[i]);
|
||||
i++;
|
||||
}
|
||||
pthread_mutex_destroy(&data->forks_mutex);
|
||||
pthread_mutex_destroy(&data->stop_mutex);
|
||||
pthread_mutex_destroy(&data->print_mutex);
|
||||
free(data->threads);
|
||||
|
22
data.h
22
data.h
@ -1,23 +1,5 @@
|
||||
#ifndef DATA_H
|
||||
# define DATA_H
|
||||
# include <pthread.h>
|
||||
# include <sys/types.h>
|
||||
|
||||
typedef struct s_data
|
||||
{
|
||||
size_t eat_time;
|
||||
size_t sleep_time;
|
||||
size_t life_expectency;
|
||||
size_t nb_philos;
|
||||
ssize_t nb_meals;
|
||||
void **philos;
|
||||
pthread_t *threads;
|
||||
pthread_mutex_t *forks;
|
||||
pthread_mutex_t stop_mutex;
|
||||
bool stop;
|
||||
pthread_mutex_t print_mutex;
|
||||
} t_data;
|
||||
# include "./struct.h"
|
||||
|
||||
bool data_init(t_data *data);
|
||||
void data_destroyer(t_data *data);
|
||||
#endif
|
||||
|
||||
|
17
main.c
17
main.c
@ -10,7 +10,7 @@
|
||||
#include "./threads.h"
|
||||
#include "./time.h"
|
||||
#include "./print.h"
|
||||
#include "./utils/utils.h"
|
||||
#include "./struct.h"
|
||||
|
||||
size_t get_min_meal(t_data *data)
|
||||
{
|
||||
@ -61,14 +61,25 @@ void *check_routine(t_data *data)
|
||||
pthread_mutex_unlock(&philo->last_eat_mutex);
|
||||
if (ok == 1)
|
||||
{
|
||||
stop(data);
|
||||
print_died(philo);
|
||||
stop(data);
|
||||
return (NULL);
|
||||
}
|
||||
pthread_mutex_lock(&philo->last_sleep_mutex);
|
||||
// printf("time=%zu, last_sleep=%zu, life_expectency=%zu\n", get_time(), philo->last_sleep, data->life_expectency);
|
||||
ok = !(philo->last_sleep + data->life_expectency > get_time());
|
||||
pthread_mutex_unlock(&philo->last_sleep_mutex);
|
||||
if (ok == 1)
|
||||
{
|
||||
print_died(philo);
|
||||
stop(data);
|
||||
return (NULL);
|
||||
}
|
||||
i++;
|
||||
if ((ssize_t) get_min_meal(data) == data->nb_meals)
|
||||
if ((ssize_t) get_min_meal(data) >= data->nb_meals)
|
||||
{
|
||||
stop(data);
|
||||
printf("g pu faim\n");
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
103
philo.c
103
philo.c
@ -1,16 +1,18 @@
|
||||
#include "philo.h"
|
||||
#include "struct.h"
|
||||
#include "time.h"
|
||||
#include "data.h"
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include "./print.h"
|
||||
#include "philo.h"
|
||||
#include "time.h"
|
||||
#include "data.h"
|
||||
|
||||
void philo_destroyer(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_destroy(&philo->nb_meal_mutex);
|
||||
pthread_mutex_destroy(&philo->last_sleep_mutex);
|
||||
pthread_mutex_destroy(&philo->last_eat_mutex);
|
||||
free(philo);
|
||||
}
|
||||
@ -31,35 +33,55 @@ bool check(t_philo *philo, t_data *data)
|
||||
return (stop);
|
||||
}
|
||||
|
||||
bool philo_eat(t_philo *philo, t_data *data)
|
||||
void 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);
|
||||
}
|
||||
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);
|
||||
pthread_mutex_lock(&philo->last_eat_mutex);
|
||||
if (get_time() - philo->last_eat + data->eat_time > data->life_expectency)
|
||||
{
|
||||
pthread_mutex_unlock(&philo->last_eat_mutex);
|
||||
usleep((data->life_expectency -
|
||||
(get_time() - philo->last_eat)) * 1000);
|
||||
return ;
|
||||
}
|
||||
else
|
||||
usleep(data->eat_time * 1000);
|
||||
pthread_mutex_unlock(&data->forks[philo->id]);
|
||||
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
|
||||
pthread_mutex_unlock(&philo->last_eat_mutex);
|
||||
pthread_mutex_lock(&philo->nb_meal_mutex);
|
||||
philo->nb_meal++;
|
||||
pthread_mutex_unlock(&philo->nb_meal_mutex);
|
||||
pthread_mutex_lock(&data->forks_mutex);
|
||||
data->forks[philo->id] = 1;
|
||||
data->forks[(philo->id + 1) % data->nb_philos] = 1;
|
||||
pthread_mutex_unlock(&data->forks_mutex);
|
||||
}
|
||||
|
||||
bool philo_eat(t_philo *philo, t_data *data)
|
||||
{
|
||||
bool left_fork;
|
||||
bool right_fork;
|
||||
|
||||
left_fork = 0;
|
||||
right_fork = 0;
|
||||
while (left_fork == 0 || right_fork == 0)
|
||||
{
|
||||
if (check(philo, data))
|
||||
return (1);
|
||||
pthread_mutex_lock(&data->forks_mutex);
|
||||
left_fork = data->forks[philo->id];
|
||||
if (((philo->id + 1) % data->nb_philos) != philo->id)
|
||||
right_fork = data->forks[(philo->id + 1) % data->nb_philos];
|
||||
if (right_fork && left_fork)
|
||||
{
|
||||
data->forks[philo->id] = 0;
|
||||
data->forks[(philo->id + 1) % data->nb_philos] = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&data->forks_mutex);
|
||||
usleep(10);
|
||||
}
|
||||
eat(philo, data);
|
||||
pthread_mutex_lock(&philo->last_eat_mutex);
|
||||
philo->last_eat = get_time();
|
||||
pthread_mutex_unlock(&philo->last_eat_mutex);
|
||||
@ -68,14 +90,23 @@ bool philo_eat(t_philo *philo, t_data *data)
|
||||
|
||||
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);
|
||||
pthread_mutex_lock(&philo->last_sleep_mutex);
|
||||
// printf("time=%zu, last_sleep=%zu, sleep_time=%zu, life_expectency=%zu\n", get_time(), philo->last_sleep, data->sleep_time, data->life_expectency);
|
||||
if (get_time() - philo->last_sleep + data->sleep_time > data->life_expectency)
|
||||
{
|
||||
pthread_mutex_unlock(&philo->last_sleep_mutex);
|
||||
// printf("rompiche=%zu\n", (data->life_expectency -
|
||||
// (get_time() - philo->last_sleep)));
|
||||
usleep((data->life_expectency -
|
||||
(get_time() - philo->last_sleep)) * 1000);
|
||||
return ;
|
||||
}
|
||||
else
|
||||
usleep(data->eat_time * 1000);
|
||||
usleep(data->sleep_time * 1000);
|
||||
philo->last_sleep = get_time();
|
||||
pthread_mutex_unlock(&philo->last_sleep_mutex);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -87,26 +118,18 @@ void *philo_routine(void *arg)
|
||||
philo = arg;
|
||||
data = philo->data;
|
||||
print_thinking(philo);
|
||||
usleep((philo->id * (data->life_expectency / data->nb_philos)) * 1000);
|
||||
usleep(philo->id * (data->life_expectency / data->nb_philos));
|
||||
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);
|
||||
@ -125,8 +148,10 @@ t_philo *philo_init(t_data *data)
|
||||
philo->nb_meal = 0;
|
||||
philo->stop = 0;
|
||||
philo->last_eat = get_time();
|
||||
philo->last_sleep = get_time();
|
||||
pthread_mutex_init(&philo->nb_meal_mutex, NULL);
|
||||
pthread_mutex_init(&philo->last_eat_mutex, NULL);
|
||||
pthread_mutex_init(&philo->last_sleep_mutex, NULL);
|
||||
pthread_mutex_init(&philo->stop_mutex, NULL);
|
||||
return (philo);
|
||||
}
|
||||
|
18
philo.h
18
philo.h
@ -6,31 +6,17 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:38:14 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/25 14:05:15 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/04/13 12:58:09 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
#include <pthread.h>
|
||||
# include <stdio.h>
|
||||
# include <stdbool.h>
|
||||
# include <stdlib.h>
|
||||
# include "./data.h"
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
size_t id;
|
||||
pthread_mutex_t nb_meal_mutex;
|
||||
size_t nb_meal;
|
||||
pthread_mutex_t last_eat_mutex;
|
||||
size_t last_eat;
|
||||
pthread_mutex_t stop_mutex;
|
||||
bool stop;
|
||||
t_data *data;
|
||||
} t_philo;
|
||||
|
||||
t_philo *philo_init(t_data *data);
|
||||
void philo_destroyer(t_philo *philo);
|
||||
void *philo_routine(void *arg);
|
||||
#endif
|
||||
|
||||
|
1
philos.c
1
philos.c
@ -1,4 +1,5 @@
|
||||
#include "./philo.h"
|
||||
#include "struct.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
38
print.c
38
print.c
@ -1,30 +1,24 @@
|
||||
#include "philo.h"
|
||||
#include <bits/pthreadtypes.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "./time.h"
|
||||
#include "./utils/utils.h"
|
||||
#include "struct.h"
|
||||
|
||||
static void print(t_data *data, size_t id, char *str)
|
||||
{
|
||||
size_t time;
|
||||
bool stop;
|
||||
|
||||
pthread_mutex_lock(&data->stop_mutex);
|
||||
if (data->stop)
|
||||
{
|
||||
pthread_mutex_unlock(&data->stop_mutex);
|
||||
return ;
|
||||
}
|
||||
pthread_mutex_unlock(&data->stop_mutex);
|
||||
time = get_time();
|
||||
pthread_mutex_lock(&data->print_mutex);
|
||||
pthread_mutex_lock(&data->stop_mutex);
|
||||
stop = data->stop;
|
||||
pthread_mutex_unlock(&data->stop_mutex);
|
||||
if (stop == false)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
@ -50,17 +44,5 @@ void print_thinking(t_philo *philo)
|
||||
|
||||
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);
|
||||
print(philo->data, philo->id, "died");
|
||||
}
|
||||
|
41
struct.h
Normal file
41
struct.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef STRUCT_H
|
||||
# define STRUCT_H
|
||||
# include <pthread.h>
|
||||
# include <stddef.h>
|
||||
# include <sys/types.h>
|
||||
|
||||
typedef struct s_data
|
||||
{
|
||||
size_t eat_time;
|
||||
size_t sleep_time;
|
||||
size_t life_expectency;
|
||||
size_t nb_philos;
|
||||
ssize_t nb_meals;
|
||||
void **philos;
|
||||
pthread_t *threads;
|
||||
pthread_mutex_t forks_mutex;
|
||||
bool *forks;
|
||||
pthread_mutex_t stop_mutex;
|
||||
bool stop;
|
||||
pthread_mutex_t print_mutex;
|
||||
} t_data;
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
size_t id;
|
||||
pthread_mutex_t nb_meal_mutex;
|
||||
size_t nb_meal;
|
||||
pthread_mutex_t last_eat_mutex;
|
||||
size_t last_eat;
|
||||
pthread_mutex_t last_sleep_mutex;
|
||||
size_t last_sleep;
|
||||
pthread_mutex_t stop_mutex;
|
||||
bool stop;
|
||||
t_data *data;
|
||||
} t_philo;
|
||||
|
||||
t_philo *philo_create(t_data *data);
|
||||
t_philo *philo_destoyer(t_philo *philo);
|
||||
void *philo_routine(void *arg);
|
||||
|
||||
#endif
|
@ -1,4 +1,5 @@
|
||||
#include "philo.h"
|
||||
#include "struct.h"
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
@ -1,4 +1,5 @@
|
||||
# include "./data.h"
|
||||
#include "struct.h"
|
||||
# include <stdbool.h>
|
||||
|
||||
bool threads_init(t_data *data);
|
||||
|
@ -1,6 +0,0 @@
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_putchar(char c)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
#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);
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
#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++;
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/11 14:38:07 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/25 14:09:53 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/04/11 14:46:12 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,8 +17,5 @@
|
||||
|
||||
bool ft_isnum(char str[]);
|
||||
int ft_atoi(const char str[]);
|
||||
void ft_putchar(char c);
|
||||
void ft_putstr(char str[]);
|
||||
void ft_putnum(size_t num, size_t padding);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user