corrigé mixed up
This commit is contained in:
parent
9f5f416a77
commit
3f0ec62059
|
@ -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 = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c
|
||||||
SRCS_BONUS =
|
|
||||||
OBJS = ${SRCS:.c=.o}
|
OBJS = ${SRCS:.c=.o}
|
||||||
OBJS_BONUS = ${SRCS_BONUS:.c=.o}
|
|
||||||
UTILS = utils/utils.a
|
UTILS = utils/utils.a
|
||||||
CC = clang
|
CC = clang
|
||||||
CFLAGS = -g -Wall -Wextra -Werror -pthread
|
CFLAGS = -g -Wall -Wextra -Werror -pthread
|
||||||
|
@ -13,10 +11,6 @@ ${NAME}: ${OBJS}
|
||||||
make -C utils
|
make -C utils
|
||||||
${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${UTILS}
|
${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${UTILS}
|
||||||
|
|
||||||
bonus: ${OBJS_BONUS}
|
|
||||||
make -C utils
|
|
||||||
${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${UTILS}
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f ${OBJS}
|
rm -f ${OBJS}
|
||||||
rm -f ${OBJS_BONUS}
|
rm -f ${OBJS_BONUS}
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/24 14:21:54 by erey-bet #+# #+# */
|
/* 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;
|
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);
|
return (1);
|
||||||
}
|
}
|
||||||
if (parsing(argv, &config))
|
if (parsing(argv, &config))
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/29 20:44:30 by erey-bet #+# #+# */
|
/* 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)
|
if (!same->all_eat)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
*same->all_eat = 0;
|
*same->all_eat = 0;
|
||||||
|
same->time = get_utime();
|
||||||
return (same);
|
return (same);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,14 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */
|
/* 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
|
#ifndef PHILO_H
|
||||||
# define PHILO_H
|
# define PHILO_H
|
||||||
|
|
||||||
# include "../utils/utils.h"
|
# include "utils/utils.h"
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <pthread.h>
|
# include <pthread.h>
|
||||||
|
@ -37,14 +37,13 @@ typedef struct s_same
|
||||||
pthread_mutex_t *mutex;
|
pthread_mutex_t *mutex;
|
||||||
int *all_eat;
|
int *all_eat;
|
||||||
int *death;
|
int *death;
|
||||||
|
long long time;
|
||||||
} t_same;
|
} t_same;
|
||||||
|
|
||||||
typedef struct s_only
|
typedef struct s_only
|
||||||
{
|
{
|
||||||
int id;
|
int id;
|
||||||
int next_id;
|
int next_id;
|
||||||
long long time;
|
|
||||||
long long last_eat_time;
|
long long last_eat_time;
|
||||||
int eat;
|
int eat;
|
||||||
} t_only;
|
} t_only;
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */
|
/* 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;
|
*nbr_philo = philo->same->config->nbr_philo;
|
||||||
only->next_id = (only->id + 1) % *nbr_philo;
|
only->next_id = (only->id + 1) % *nbr_philo;
|
||||||
only->time = get_utime();
|
only->last_eat_time = get_utime();
|
||||||
only->last_eat_time = only->time;
|
|
||||||
if (only->id % 2 == 1 || (*nbr_philo % 2 == 1
|
if (only->id % 2 == 1 || (*nbr_philo % 2 == 1
|
||||||
&& only->id == *nbr_philo - 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,
|
int in_loop_eat_sleep(t_philo *philo, t_only *only,
|
||||||
t_config *config, int nbr_philo)
|
t_config *config, int nbr_philo)
|
||||||
{
|
{
|
||||||
|
only->last_eat_time = get_utime();
|
||||||
if (!verif_die_eating(philo, only, config))
|
if (!verif_die_eating(philo, only, config))
|
||||||
return (0);
|
return (0);
|
||||||
usleep(config->time_eat);
|
usleep(config->time_eat);
|
||||||
only->last_eat_time = get_utime();
|
|
||||||
if (verif_finish(philo, only))
|
if (verif_finish(philo, only))
|
||||||
return (0);
|
return (0);
|
||||||
if (config->must_eat > -1 && only->eat < config->must_eat)
|
if (config->must_eat > -1 && only->eat < config->must_eat)
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */
|
/* 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;
|
int i;
|
||||||
|
|
||||||
destroy_mutex(same->mutex, 3);
|
destroy_mutex(same->mutex, same->config->nbr_philo + 2);
|
||||||
free(threads);
|
if (threads)
|
||||||
free(same->mutex);
|
free(threads);
|
||||||
free(same->death);
|
if (same && same->mutex)
|
||||||
free(same->all_eat);
|
free(same->mutex);
|
||||||
free(same);
|
if (same && same->mutex)
|
||||||
i = -1;
|
free(same->death);
|
||||||
while (philo[++i])
|
if (same && same->mutex)
|
||||||
|
free(same->all_eat);
|
||||||
|
if (same)
|
||||||
|
free(same);
|
||||||
|
if (philo)
|
||||||
{
|
{
|
||||||
free(philo[i]->only);
|
i = -1;
|
||||||
free(philo[i]);
|
while (philo[++i])
|
||||||
|
{
|
||||||
|
if (philo[i]->only)
|
||||||
|
free(philo[i]->only);
|
||||||
|
if (philo[i])
|
||||||
|
free(philo[i]);
|
||||||
|
}
|
||||||
|
free(philo);
|
||||||
}
|
}
|
||||||
free(philo);
|
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */
|
/* 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)
|
void message_die(t_philo *philo, char *msg)
|
||||||
{
|
{
|
||||||
char send[1024];
|
t_config *config;
|
||||||
int i;
|
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++] = ' ';
|
send[i++] = ' ';
|
||||||
i += ft_lltos(send + i, philo->only->id + 1);
|
i += ft_lltos(send + i, philo->only->id + 1);
|
||||||
send[i++] = ' ';
|
send[i++] = ' ';
|
||||||
|
@ -36,10 +38,12 @@ void message_die(t_philo *philo, char *msg)
|
||||||
|
|
||||||
void message(t_philo *philo, char *msg)
|
void message(t_philo *philo, char *msg)
|
||||||
{
|
{
|
||||||
char send[1024];
|
t_config *config;
|
||||||
int i;
|
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++] = ' ';
|
send[i++] = ' ';
|
||||||
i += ft_lltos(send + i, philo->only->id + 1);
|
i += ft_lltos(send + i, philo->only->id + 1);
|
||||||
send[i++] = ' ';
|
send[i++] = ' ';
|
Loading…
Reference in a new issue