corrigé mixed up

This commit is contained in:
Etienne Rey-bethbeder 2023-04-06 14:02:19 +02:00
parent 9f5f416a77
commit 3f0ec62059
24 changed files with 45 additions and 38 deletions

View file

@ -1,7 +1,5 @@
SRCS = mandatory/main.c mandatory/parsing.c mandatory/threads.c mandatory/manage_threads.c mandatory/utils_threads.c mandatory/utils_manage_threads.c
SRCS_BONUS =
SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c
OBJS = ${SRCS:.c=.o}
OBJS_BONUS = ${SRCS_BONUS:.c=.o}
UTILS = utils/utils.a
CC = clang
CFLAGS = -g -Wall -Wextra -Werror -pthread
@ -13,10 +11,6 @@ ${NAME}: ${OBJS}
make -C utils
${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${UTILS}
bonus: ${OBJS_BONUS}
make -C utils
${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${UTILS}
clean:
rm -f ${OBJS}
rm -f ${OBJS_BONUS}

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/24 14:21:54 by erey-bet #+# #+# */
/* Updated: 2023/03/30 00:07:31 by erey-bet ### ########.fr */
/* Updated: 2023/04/06 12:32:25 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,9 +16,9 @@ int main(int argc, char *argv[])
{
t_config config;
if (argc < 5)
if (argc < 5 || argc > 6)
{
write(2, "Not enought argument\n", 21);
write(2, "Wrong number of argument\n", 25);
return (1);
}
if (parsing(argv, &config))

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 20:44:30 by erey-bet #+# #+# */
/* Updated: 2023/03/31 17:52:22 by erey-bet ### ########.fr */
/* Updated: 2023/04/06 13:54:03 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,6 +32,7 @@ static t_same *create_struct_same(t_config *config)
if (!same->all_eat)
return (NULL);
*same->all_eat = 0;
same->time = get_utime();
return (same);
}

View file

@ -6,14 +6,14 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */
/* Updated: 2023/04/01 11:58:47 by erey-bet ### ########.fr */
/* Updated: 2023/04/06 13:59:01 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_H
# define PHILO_H
# include "../utils/utils.h"
# include "utils/utils.h"
# include <stdlib.h>
# include <unistd.h>
# include <pthread.h>
@ -37,14 +37,13 @@ typedef struct s_same
pthread_mutex_t *mutex;
int *all_eat;
int *death;
long long time;
} t_same;
typedef struct s_only
{
int id;
int next_id;
long long time;
long long last_eat_time;
int eat;
} t_only;

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */
/* Updated: 2023/04/01 11:57:59 by erey-bet ### ########.fr */
/* Updated: 2023/04/06 13:50:41 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,8 +40,7 @@ void init_philo(t_philo *philo, t_only *only,
{
*nbr_philo = philo->same->config->nbr_philo;
only->next_id = (only->id + 1) % *nbr_philo;
only->time = get_utime();
only->last_eat_time = only->time;
only->last_eat_time = get_utime();
if (only->id % 2 == 1 || (*nbr_philo % 2 == 1
&& only->id == *nbr_philo - 1))
{
@ -57,10 +56,10 @@ void init_philo(t_philo *philo, t_only *only,
int in_loop_eat_sleep(t_philo *philo, t_only *only,
t_config *config, int nbr_philo)
{
only->last_eat_time = get_utime();
if (!verif_die_eating(philo, only, config))
return (0);
usleep(config->time_eat);
only->last_eat_time = get_utime();
if (verif_finish(philo, only))
return (0);
if (config->must_eat > -1 && only->eat < config->must_eat)

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */
/* Updated: 2023/03/31 17:51:30 by erey-bet ### ########.fr */
/* Updated: 2023/04/06 13:57:40 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -34,17 +34,27 @@ void free_all(pthread_t *threads, t_same *same, t_philo **philo)
{
int i;
destroy_mutex(same->mutex, 3);
free(threads);
free(same->mutex);
free(same->death);
free(same->all_eat);
free(same);
i = -1;
while (philo[++i])
destroy_mutex(same->mutex, same->config->nbr_philo + 2);
if (threads)
free(threads);
if (same && same->mutex)
free(same->mutex);
if (same && same->mutex)
free(same->death);
if (same && same->mutex)
free(same->all_eat);
if (same)
free(same);
if (philo)
{
free(philo[i]->only);
free(philo[i]);
i = -1;
while (philo[++i])
{
if (philo[i]->only)
free(philo[i]->only);
if (philo[i])
free(philo[i]);
}
free(philo);
}
free(philo);
}

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */
/* Updated: 2023/04/01 12:08:51 by erey-bet ### ########.fr */
/* Updated: 2023/04/06 13:59:13 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,10 +22,12 @@ long long get_utime(void)
void message_die(t_philo *philo, char *msg)
{
char send[1024];
int i;
t_config *config;
char send[1024];
int i;
i = ft_lltos(send, (get_utime() - philo->only->time) / 1000);
config = philo->same->config;
i = ft_lltos(send, (get_utime() - philo->same->time) / 1000);
send[i++] = ' ';
i += ft_lltos(send + i, philo->only->id + 1);
send[i++] = ' ';
@ -36,10 +38,12 @@ void message_die(t_philo *philo, char *msg)
void message(t_philo *philo, char *msg)
{
char send[1024];
int i;
t_config *config;
char send[1024];
int i;
i = ft_lltos(send, (get_utime() - philo->only->time) / 1000);
config = philo->same->config;
i = ft_lltos(send, (get_utime() - philo->same->time) / 1000);
send[i++] = ' ';
i += ft_lltos(send + i, philo->only->id + 1);
send[i++] = ' ';