Compare commits
No commits in common. "1461285dbf9285f9471c89ce94ef816680651ebd" and "622c2bf226fd720d6ed004d33ebaa7f61c0a9f18" have entirely different histories.
1461285dbf
...
622c2bf226
4
Makefile
4
Makefile
@ -15,13 +15,13 @@ NAME = philo
|
||||
|
||||
CC = clang
|
||||
|
||||
FLAGS = -Wall -Wextra -Werror -g -pthread
|
||||
FLAGS = -Wall -Wextra -Werror -g
|
||||
|
||||
%.o: %.c
|
||||
${CC} ${FLAGS} -c -o $@ $<
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
${CC} ${FLAGS} ${OBJS} -o ${NAME}
|
||||
${CC} ${OBJS} -o ${NAME}
|
||||
|
||||
all: ${NAME}
|
||||
|
||||
|
19
data.c
19
data.c
@ -2,7 +2,6 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "./data.h"
|
||||
#include "philo.h"
|
||||
#include "philos.h"
|
||||
@ -36,23 +35,9 @@ bool data_init(t_data *data)
|
||||
|
||||
void data_destroyer(t_data *data)
|
||||
{
|
||||
size_t i;
|
||||
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->stop_mutex);
|
||||
free(data->threads);
|
||||
free(data->forks);
|
||||
pthread_mutex_destroy(&data->forks_mutex);
|
||||
pthread_mutex_destroy(&data->stop_mutex);
|
||||
philos_destroyer(data);
|
||||
}
|
||||
|
4
main.c
4
main.c
@ -53,8 +53,8 @@ void check_routine(t_data *data)
|
||||
{
|
||||
if ((ssize_t) get_min_meal(data) == data->nb_meals)
|
||||
{
|
||||
stop(data);
|
||||
printf("g pu faim\n");
|
||||
stop(data);
|
||||
return ;
|
||||
}
|
||||
i = 0;
|
||||
@ -67,7 +67,6 @@ void check_routine(t_data *data)
|
||||
if (ok == 1)
|
||||
{
|
||||
print_died(philo);
|
||||
stop(data);
|
||||
return ;
|
||||
}
|
||||
i++;
|
||||
@ -79,7 +78,6 @@ int main(int ac, char **av)
|
||||
{
|
||||
t_data data;
|
||||
|
||||
get_time();
|
||||
if (parsing(&data, av + 1, ac - 1))
|
||||
return (1);
|
||||
if (data_init(&data))
|
||||
|
@ -44,7 +44,5 @@ bool parsing(t_data *data, char **args, size_t n)
|
||||
return (1);
|
||||
}
|
||||
set_value(data, args, n);
|
||||
if (data->nb_philos == 0)
|
||||
write(2, "Argument error !\n", 16);
|
||||
return (data->nb_philos == 0);
|
||||
return (0);
|
||||
}
|
||||
|
26
philo.c
26
philo.c
@ -17,19 +17,13 @@ void philo_destroyer(t_philo *philo)
|
||||
free(philo);
|
||||
}
|
||||
|
||||
bool check(t_philo *philo, t_data *data)
|
||||
bool check(t_data *data)
|
||||
{
|
||||
bool stop;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -57,12 +51,11 @@ bool philo_eat(t_philo *philo, t_data *data)
|
||||
right_fork = 0;
|
||||
while (left_fork == 0 || right_fork == 0)
|
||||
{
|
||||
if (check(philo, data))
|
||||
if (check(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];
|
||||
right_fork = data->forks[(philo->id + 1) % data->nb_philos];
|
||||
if (right_fork && left_fork)
|
||||
{
|
||||
data->forks[philo->id] = 0;
|
||||
@ -92,20 +85,18 @@ void *philo_routine(void *arg)
|
||||
|
||||
philo = arg;
|
||||
data = philo->data;
|
||||
print_thinking(philo);
|
||||
usleep(philo->id * (data->life_expectency / data->nb_philos));
|
||||
if (philo->id % 2)
|
||||
usleep(data->sleep_time * 1000);
|
||||
while (true)
|
||||
{
|
||||
if (check(philo, data))
|
||||
print_thinking(philo);
|
||||
if (check(data))
|
||||
return (NULL);
|
||||
if (philo_eat(philo, data))
|
||||
return (NULL);
|
||||
if (check(philo, data))
|
||||
if (check(data))
|
||||
return (NULL);
|
||||
philo_sleep(data, philo);
|
||||
if (check(philo, data))
|
||||
return (NULL);
|
||||
print_thinking(philo);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
@ -125,6 +116,5 @@ t_philo *philo_init(t_data *data)
|
||||
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);
|
||||
}
|
||||
|
4
print.c
4
print.c
@ -19,7 +19,7 @@ static void print(t_data *data, size_t id, char *str)
|
||||
pthread_mutex_unlock(&data->stop_mutex);
|
||||
time = get_time();
|
||||
pthread_mutex_lock(&print_mutex);
|
||||
printf("%07zu %3zu %s\n", time, id + 1, str);
|
||||
printf("%6zu %3zu %s\n", time, id, str);
|
||||
pthread_mutex_unlock(&print_mutex);
|
||||
}
|
||||
|
||||
@ -45,5 +45,5 @@ void print_thinking(t_philo *philo)
|
||||
|
||||
void print_died(t_philo *philo)
|
||||
{
|
||||
print(philo->data, philo->id, "died");
|
||||
print(philo->data, philo->id, "is died");
|
||||
}
|
||||
|
2
struct.h
2
struct.h
@ -28,8 +28,6 @@ typedef struct s_philo
|
||||
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;
|
||||
|
||||
|
5
time.c
5
time.c
@ -6,12 +6,9 @@
|
||||
size_t get_time(void)
|
||||
{
|
||||
size_t time;
|
||||
static size_t start_time = 0;
|
||||
struct timeval tv;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
time = tv.tv_sec * 1000000 + tv.tv_usec;
|
||||
if (start_time == 0)
|
||||
start_time = time;
|
||||
return ((time - start_time) / 1000);
|
||||
return (time / 1000);
|
||||
}
|
||||
|
@ -6,28 +6,21 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:43:33 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/19 13:49:58 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/04/11 14:47:30 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../philo.h"
|
||||
#include <stddef.h>
|
||||
#include <limits.h>
|
||||
|
||||
bool ft_isnum(char str[])
|
||||
{
|
||||
size_t i;
|
||||
size_t num;
|
||||
|
||||
num = 0;
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
if (str[i] > '9' || str[i] < '0')
|
||||
return (0);
|
||||
num = num * 10 + str[i] - 48;
|
||||
if (num > INT_MAX)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
|
Loading…
Reference in New Issue
Block a user