Compare commits
4 Commits
531d3146e4
...
d6aee63600
Author | SHA1 | Date | |
---|---|---|---|
|
d6aee63600 | ||
|
07d4cd05c1 | ||
|
4f138b78e3 | ||
|
6759520714 |
3
Makefile
3
Makefile
@ -1,5 +1,8 @@
|
||||
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,13 +6,14 @@
|
||||
#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(bool) * data->nb_philos);
|
||||
data->forks = malloc(sizeof(pthread_mutex_t) * data->nb_philos);
|
||||
if (data->forks == NULL)
|
||||
return (1);
|
||||
memset(data->forks, 1, data->nb_philos);
|
||||
@ -29,7 +30,12 @@ bool data_init(t_data *data)
|
||||
free(data->forks);
|
||||
return (1);
|
||||
}
|
||||
pthread_mutex_init(&data->forks_mutex, NULL);
|
||||
i = 0;
|
||||
while (i < data->nb_philos)
|
||||
{
|
||||
pthread_mutex_init(&data->forks[i], NULL);
|
||||
i++;
|
||||
}
|
||||
pthread_mutex_init(&data->stop_mutex, NULL);
|
||||
pthread_mutex_init(&data->print_mutex, NULL);
|
||||
return (0);
|
||||
@ -52,7 +58,12 @@ void data_destroyer(t_data *data)
|
||||
i++;
|
||||
usleep(1000);
|
||||
}
|
||||
pthread_mutex_destroy(&data->forks_mutex);
|
||||
i = 0;
|
||||
while (i < data->nb_philos)
|
||||
{
|
||||
pthread_mutex_destroy(&data->forks[i]);
|
||||
i++;
|
||||
}
|
||||
pthread_mutex_destroy(&data->stop_mutex);
|
||||
pthread_mutex_destroy(&data->print_mutex);
|
||||
free(data->threads);
|
||||
|
22
data.h
22
data.h
@ -1,5 +1,23 @@
|
||||
# include "./struct.h"
|
||||
#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;
|
||||
|
||||
bool data_init(t_data *data);
|
||||
void data_destroyer(t_data *data);
|
||||
|
||||
#endif
|
||||
|
15
main.c
15
main.c
@ -10,7 +10,7 @@
|
||||
#include "./threads.h"
|
||||
#include "./time.h"
|
||||
#include "./print.h"
|
||||
#include "./struct.h"
|
||||
#include "./utils/utils.h"
|
||||
|
||||
size_t get_min_meal(t_data *data)
|
||||
{
|
||||
@ -61,25 +61,14 @@ void *check_routine(t_data *data)
|
||||
pthread_mutex_unlock(&philo->last_eat_mutex);
|
||||
if (ok == 1)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
109
philo.c
109
philo.c
@ -1,18 +1,16 @@
|
||||
#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);
|
||||
}
|
||||
@ -33,55 +31,35 @@ bool check(t_philo *philo, t_data *data)
|
||||
return (stop);
|
||||
}
|
||||
|
||||
void eat(t_philo *philo, t_data *data)
|
||||
{
|
||||
print_take_a_fork(philo);
|
||||
print_take_a_fork(philo);
|
||||
print_eating(philo);
|
||||
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(&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;
|
||||
size_t time;
|
||||
|
||||
left_fork = 0;
|
||||
right_fork = 0;
|
||||
while (left_fork == 0 || right_fork == 0)
|
||||
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);
|
||||
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(&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);
|
||||
@ -90,23 +68,14 @@ bool philo_eat(t_philo *philo, t_data *data)
|
||||
|
||||
void philo_sleep(t_data *data, t_philo *philo)
|
||||
{
|
||||
print_sleeping(philo);
|
||||
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->sleep_time * 1000);
|
||||
philo->last_sleep = get_time();
|
||||
pthread_mutex_unlock(&philo->last_sleep_mutex);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -118,18 +87,26 @@ void *philo_routine(void *arg)
|
||||
philo = arg;
|
||||
data = philo->data;
|
||||
print_thinking(philo);
|
||||
usleep(philo->id * (data->life_expectency / data->nb_philos));
|
||||
usleep((philo->id * (data->life_expectency / data->nb_philos)) * 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);
|
||||
@ -148,10 +125,8 @@ 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);
|
||||
}
|
||||
|
20
philo.h
20
philo.h
@ -6,17 +6,31 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:38:14 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/13 12:58:09 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/04/25 14:05:15 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <pthread.h>
|
||||
#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,5 +1,4 @@
|
||||
#include "./philo.h"
|
||||
#include "struct.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
38
print.c
38
print.c
@ -1,24 +1,30 @@
|
||||
#include "philo.h"
|
||||
#include <bits/pthreadtypes.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "./time.h"
|
||||
#include "struct.h"
|
||||
#include "./utils/utils.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);
|
||||
printf("%07zu %03zu %s\n", time, id + 1, str);
|
||||
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');
|
||||
}
|
||||
pthread_mutex_unlock(&data->print_mutex);
|
||||
}
|
||||
|
||||
@ -44,5 +50,17 @@ void print_thinking(t_philo *philo)
|
||||
|
||||
void print_died(t_philo *philo)
|
||||
{
|
||||
print(philo->data, philo->id, "died");
|
||||
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);
|
||||
}
|
||||
|
41
struct.h
41
struct.h
@ -1,41 +0,0 @@
|
||||
#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,5 +1,4 @@
|
||||
#include "philo.h"
|
||||
#include "struct.h"
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
@ -1,5 +1,4 @@
|
||||
# include "./data.h"
|
||||
#include "struct.h"
|
||||
# include <stdbool.h>
|
||||
|
||||
bool threads_init(t_data *data);
|
||||
|
6
utils/putchar.c
Normal file
6
utils/putchar.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_putchar(char c)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
}
|
47
utils/putnum.c
Normal file
47
utils/putnum.c
Normal file
@ -0,0 +1,47 @@
|
||||
#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);
|
||||
}
|
14
utils/putstr.c
Normal file
14
utils/putstr.c
Normal file
@ -0,0 +1,14 @@
|
||||
#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/11 14:46:12 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/04/25 14:09:53 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,5 +17,8 @@
|
||||
|
||||
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