Finito philo
This commit is contained in:
parent
bf18eed965
commit
ffb68409b1
6
Makefile
6
Makefile
|
@ -1,4 +1,4 @@
|
|||
SRCS = mandatory/main.c mandatory/parsing.c mandatory/philo.c mandatory/threads.c
|
||||
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 =
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
OBJS_BONUS = ${SRCS_BONUS:.c=.o}
|
||||
|
@ -139,9 +139,5 @@ bozo :
|
|||
@wget -q -O bozo.gif https://i.kym-cdn.com/photos/images/newsfeed/002/322/200/e51.gif
|
||||
@xdg-open bozo.gif
|
||||
@@sleep 4.26
|
||||
@#@printf '\e[?1049h'
|
||||
@#tput smcup
|
||||
@#@xdotool key F11
|
||||
@#@sleep 4.1
|
||||
@pkill eog
|
||||
@rm bozo.gif
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/24 14:21:54 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/12 16:53:38 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/30 00:07:31 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
t_config config;
|
||||
t_config config;
|
||||
|
||||
if (argc < 5)
|
||||
{
|
||||
|
|
93
mandatory/manage_threads.c
Normal file
93
mandatory/manage_threads.c
Normal file
|
@ -0,0 +1,93 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* manage_threads.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
static t_same *create_struct_same(t_config *config)
|
||||
{
|
||||
t_same *same;
|
||||
|
||||
same = ft_calloc(sizeof(t_same), 1);
|
||||
if (!same)
|
||||
return (NULL);
|
||||
same->mutex = ft_calloc(sizeof(pthread_mutex_t), config->nbr_philo + 2);
|
||||
if (!same->mutex)
|
||||
return (NULL);
|
||||
init_mutex(same->mutex, config->nbr_philo + 2);
|
||||
same->config = config;
|
||||
same->death = ft_calloc(sizeof(int), 1);
|
||||
if (!same->death)
|
||||
return (NULL);
|
||||
*same->death = 0;
|
||||
same->all_eat = ft_calloc(sizeof(int), 1);
|
||||
if (!same->all_eat)
|
||||
return (NULL);
|
||||
*same->all_eat = 0;
|
||||
return (same);
|
||||
}
|
||||
|
||||
static t_only *create_struct_only(int id)
|
||||
{
|
||||
t_only *only;
|
||||
|
||||
only = ft_calloc(sizeof(t_only), 1);
|
||||
if (!only)
|
||||
return (NULL);
|
||||
only->id = id;
|
||||
only->eat = 0;
|
||||
return (only);
|
||||
}
|
||||
|
||||
static t_philo *init_philo(t_same *same, t_only *only)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = ft_calloc(sizeof(t_philo), 1);
|
||||
if (!philo)
|
||||
return (NULL);
|
||||
philo->same = same;
|
||||
if (!philo->same)
|
||||
return (NULL);
|
||||
philo->only = only;
|
||||
if (!philo->only)
|
||||
return (NULL);
|
||||
return (philo);
|
||||
}
|
||||
|
||||
int manage_threads(t_config *config)
|
||||
{
|
||||
pthread_t *thds;
|
||||
t_philo **philo;
|
||||
t_same *same;
|
||||
int i;
|
||||
|
||||
thds = ft_calloc(config->nbr_philo, sizeof(pthread_t));
|
||||
if (thds == NULL)
|
||||
return (1);
|
||||
philo = ft_calloc(config->nbr_philo + 1, sizeof(t_philo *));
|
||||
if (philo == NULL)
|
||||
return (1);
|
||||
same = create_struct_same(config);
|
||||
i = -1;
|
||||
while (++i < config->nbr_philo)
|
||||
{
|
||||
philo[i] = init_philo(same, create_struct_only(i));
|
||||
if (!philo[i] || pthread_create(&thds[i], NULL,
|
||||
(void *)&philosopher, philo[i]) != 0)
|
||||
return (1);
|
||||
}
|
||||
philo[i] = NULL;
|
||||
while (--i > -1)
|
||||
pthread_join(thds[i], NULL);
|
||||
free_all(thds, same, philo);
|
||||
return (0);
|
||||
}
|
|
@ -6,32 +6,45 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:59:29 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/27 16:59:25 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/31 15:13:30 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
int parsing(char *argv[], t_config *conf)
|
||||
int error(char *s1, char *s2)
|
||||
{
|
||||
if (ft_atoi_check(argv[1]))
|
||||
ft_putstr_fd(s1, 2);
|
||||
ft_putstr_fd(s2, 2);
|
||||
write(2, "\n", 1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int get_argv(char **argv, long *conf, char *message, int multiplier)
|
||||
{
|
||||
if (ft_atoi_check(*argv))
|
||||
{
|
||||
conf->nbr_philo = ft_atoi(argv[1]);
|
||||
if (ft_atoi_check(argv[2]))
|
||||
{
|
||||
conf->time_die = ft_atoi(argv[2]) * 1000;
|
||||
if (ft_atoi_check(argv[3]))
|
||||
{
|
||||
conf->time_eat = ft_atoi(argv[3]) * 1000;
|
||||
if (ft_atoi_check(argv[4]))
|
||||
{
|
||||
conf->time_sleep = ft_atoi(argv[4]) * 1000;
|
||||
if (ft_atoi_check(argv[5]))
|
||||
conf->must_eat = ft_atoi(argv[5]);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
*conf = (long)ft_atoi(*argv) * multiplier;
|
||||
if (*conf < 1)
|
||||
return (error("Wrong number of ", message));
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int parsing(char *argv[], t_config *conf)
|
||||
{
|
||||
if (!get_argv(&argv[1], (long *)&conf->nbr_philo, "philosopher", 1)
|
||||
&& !get_argv(&argv[2], &conf->time_die, "time to die", 1000)
|
||||
&& !get_argv(&argv[3], &conf->time_eat, "time to eat", 1000)
|
||||
&& !get_argv(&argv[4], &conf->time_sleep, "time to sleep", 1000))
|
||||
{
|
||||
conf->must_eat = -1;
|
||||
if (!argv[5])
|
||||
return (0);
|
||||
if (!get_argv(&argv[5], &conf->must_eat, "must eat time", 1))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/27 16:55:06 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/31 17:47:02 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
|||
|
||||
typedef struct s_config
|
||||
{
|
||||
int nbr_philo;
|
||||
int nbr_philo;
|
||||
long time_die;
|
||||
long time_eat;
|
||||
long time_sleep;
|
||||
|
@ -33,17 +33,20 @@ typedef struct s_config
|
|||
|
||||
typedef struct s_same
|
||||
{
|
||||
t_config *config;
|
||||
t_config *config;
|
||||
pthread_mutex_t *mutex;
|
||||
long long *time;
|
||||
int *death;
|
||||
int *all_eat;
|
||||
int *death;
|
||||
|
||||
} t_same;
|
||||
|
||||
typedef struct s_only
|
||||
{
|
||||
int id;
|
||||
int eat;
|
||||
int id;
|
||||
int next_id;
|
||||
long long time;
|
||||
long long last_eat_time;
|
||||
int eat;
|
||||
} t_only;
|
||||
|
||||
typedef struct s_philo
|
||||
|
@ -52,8 +55,23 @@ typedef struct s_philo
|
|||
t_only *only;
|
||||
} t_philo;
|
||||
|
||||
int parsing(char *argv[], t_config *conf);
|
||||
int manage_threads(t_config *conf);
|
||||
void *philosopher(t_philo *philo);
|
||||
/* PARSING */
|
||||
int parsing(char *argv[], t_config *conf);
|
||||
|
||||
/* MANAGE THREADS */
|
||||
int manage_threads(t_config *config);
|
||||
|
||||
/* UTILS */
|
||||
long long get_utime(void);
|
||||
int verif_die_eating(t_philo *philo, t_only *only, t_config *config);
|
||||
int verif_finish(t_philo *philo, t_only *only);
|
||||
int finish(t_philo *philo, long long last_eat_time);
|
||||
void message(long long time, int id, char *msg);
|
||||
void init_mutex(pthread_mutex_t *mutex, int nbr);
|
||||
void destroy_mutex(pthread_mutex_t *mutex, int nbr);
|
||||
void free_all(pthread_t *threads, t_same *same, t_philo **philo);
|
||||
|
||||
/* THREADS */
|
||||
void *philosopher(t_philo *philo);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,204 +6,133 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/28 00:42:38 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/31 18:21:03 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
long get_usec(long long time)
|
||||
int finish(t_philo *philo, long long last_eat_time)
|
||||
{
|
||||
return (time % 1000000);
|
||||
int dead;
|
||||
int all_eat;
|
||||
int nbr_philo;
|
||||
|
||||
nbr_philo = philo->same->config->nbr_philo;
|
||||
if (philo->same->config->must_eat > -1)
|
||||
{
|
||||
pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]);
|
||||
all_eat = *philo->same->all_eat;
|
||||
pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]);
|
||||
}
|
||||
pthread_mutex_lock(&philo->same->mutex[nbr_philo]);
|
||||
dead = *philo->same->death;
|
||||
pthread_mutex_unlock(&philo->same->mutex[nbr_philo]);
|
||||
return (get_utime() - last_eat_time >= philo->same->config->time_die
|
||||
|| dead
|
||||
|| (philo->same->config->must_eat != -1
|
||||
&& all_eat >= philo->same->config->must_eat * nbr_philo)
|
||||
);
|
||||
}
|
||||
|
||||
long long get_utime()
|
||||
void init_philo(t_philo *philo, t_only *only,
|
||||
t_config *config, int *nbr_philo)
|
||||
{
|
||||
struct timeval time;
|
||||
|
||||
gettimeofday(&time, NULL);
|
||||
return (time.tv_sec * 1000000 + time.tv_usec);
|
||||
*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;
|
||||
if (only->id % 2 == 1 || (*nbr_philo % 2 == 1
|
||||
&& only->id == *nbr_philo - 1))
|
||||
{
|
||||
message(only->time, only->id, "is thinking\n");
|
||||
if (get_utime() - only->last_eat_time + config->time_eat
|
||||
>= config->time_die)
|
||||
usleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
else
|
||||
usleep(config->time_eat / (only->id % 2 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
long long get_mstime()
|
||||
int in_loop_eat_sleep(t_philo *philo, t_only *only,
|
||||
t_config *config, int nbr_philo)
|
||||
{
|
||||
struct timeval time;
|
||||
|
||||
gettimeofday(&time, NULL);
|
||||
return (time.tv_usec / 1000);
|
||||
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)
|
||||
{
|
||||
only->eat++;
|
||||
pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]);
|
||||
*philo->same->all_eat += 1;
|
||||
pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]);
|
||||
}
|
||||
message(only->time, only->id, "is sleeping\n");
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
if (get_utime() - only->last_eat_time + config->time_sleep
|
||||
>= config->time_die)
|
||||
{
|
||||
usleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
usleep(config->time_sleep);
|
||||
return (1);
|
||||
}
|
||||
|
||||
long long get_time()
|
||||
void loop_philosopher(t_philo *philo, t_only *only,
|
||||
t_config *config, int nbr_philo)
|
||||
{
|
||||
struct timeval time;
|
||||
|
||||
gettimeofday(&time, NULL);
|
||||
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
||||
}
|
||||
|
||||
void message(long long time, int id, char *msg)
|
||||
{
|
||||
char send[1024];
|
||||
int i;
|
||||
|
||||
i = ft_itoa_bozo(send, (get_utime() - time) / 1000 );
|
||||
send[i++] = ' ';
|
||||
i += ft_itoa_bozo(send + i, id + 1);
|
||||
send[i++] = ' ';
|
||||
while (*msg)
|
||||
send[i++] = *msg++;
|
||||
write(1, send, i);
|
||||
while (!finish(philo, only->last_eat_time))
|
||||
{
|
||||
if (!finish(philo, only->last_eat_time)
|
||||
&& !pthread_mutex_lock(&philo->same->mutex[only->id]))
|
||||
{
|
||||
message(only->time, only->id, "has taken a fork\n");
|
||||
while (only->id == only->next_id
|
||||
&& !finish(philo, only->last_eat_time))
|
||||
;
|
||||
if (!finish(philo, only->last_eat_time)
|
||||
&& !pthread_mutex_lock(&philo->same->mutex[only->next_id]))
|
||||
{
|
||||
if (verif_finish(philo, only))
|
||||
break ;
|
||||
message(only->time, only->id, "has taken a fork\n");
|
||||
message(only->time, only->id, "is eating\n");
|
||||
if (!in_loop_eat_sleep(philo, only, config, nbr_philo))
|
||||
break ;
|
||||
}
|
||||
else
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
}
|
||||
if (!finish(philo, only->last_eat_time))
|
||||
message(only->time, only->id, "is thinking\n");
|
||||
}
|
||||
}
|
||||
|
||||
void *philosopher(t_philo *philo)
|
||||
{
|
||||
long long time;
|
||||
long long last_eat_time;
|
||||
int id;
|
||||
int next_id;
|
||||
t_only *only;
|
||||
t_config *config;
|
||||
int nbr_philo;
|
||||
|
||||
id = *philo->only->id;
|
||||
next_id = (*philo->only->id + 1) % philo->same->config->nbr_philo;
|
||||
time = get_utime();
|
||||
last_eat_time = time;
|
||||
if (id % 2 == 1 || (philo->same->config->nbr_philo % 2 == 1 && id == philo->same->config->nbr_philo - 1))
|
||||
{
|
||||
message(time, id, "is thinking\n");
|
||||
usleep(philo->same->config->time_eat);
|
||||
}
|
||||
while (get_utime() - last_eat_time < philo->same->config->time_die && !*philo->same->death)
|
||||
{
|
||||
if (!*philo->same->death && !pthread_mutex_lock(&philo->same->mutex[id]))
|
||||
{
|
||||
// message(time, id, "is taking %d fork\n");
|
||||
if (!*philo->same->death && !pthread_mutex_lock(&philo->same->mutex[next_id]))
|
||||
{
|
||||
//message(time, id, "is taking %d fork\n");
|
||||
message(time, id, "is eating\n");
|
||||
if (get_utime() - last_eat_time + philo->same->config->time_eat >= philo->same->config->time_die || *philo->same->death)
|
||||
{
|
||||
message(time, id, "dieing 1\n");
|
||||
break ;
|
||||
}
|
||||
else
|
||||
usleep(philo->same->config->time_eat);
|
||||
last_eat_time = get_utime();
|
||||
pthread_mutex_unlock(&philo->same->mutex[next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[id]);
|
||||
message(time, id, "is sleeping\n");
|
||||
if (get_utime() - last_eat_time + philo->same->config->time_sleep >= philo->same->config->time_die || *philo->same->death)
|
||||
{
|
||||
message(time, id, "dieing 2\n");
|
||||
break ;
|
||||
}
|
||||
else
|
||||
usleep(philo->same->config->time_sleep);
|
||||
}
|
||||
else
|
||||
pthread_mutex_unlock(&philo->same->mutex[id]);
|
||||
}
|
||||
message(time, id, "is thinking\n");
|
||||
}
|
||||
if (!*philo->same->death)
|
||||
only = philo->only;
|
||||
config = philo->same->config;
|
||||
init_philo(philo, only, config, &nbr_philo);
|
||||
loop_philosopher(philo, only, config, nbr_philo);
|
||||
pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]);
|
||||
pthread_mutex_lock(&philo->same->mutex[nbr_philo]);
|
||||
if (!*philo->same->death && (config->must_eat == -1
|
||||
|| (config->must_eat > -1 && only->eat < config->must_eat)))
|
||||
{
|
||||
*philo->same->death = 1;
|
||||
message(time, id, "died\n");
|
||||
message(only->time, only->id, "died\n");
|
||||
}
|
||||
//else
|
||||
//message(time, id, " stop\n");
|
||||
pthread_mutex_unlock(&philo->same->mutex[nbr_philo]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void init_mutex(pthread_mutex_t *mutex, int nbr)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (++i < nbr)
|
||||
pthread_mutex_init(&mutex[i], NULL);
|
||||
}
|
||||
|
||||
void destroy_mutex(pthread_mutex_t *mutex, int nbr)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (++i < nbr)
|
||||
pthread_mutex_destroy(&mutex[i]);
|
||||
}
|
||||
|
||||
t_same *create_struct_same(t_config *config)
|
||||
{
|
||||
t_same *same;
|
||||
pthread_mutex_t *mutex;
|
||||
long long *time;
|
||||
int *death;
|
||||
|
||||
same = ft_calloc(sizeof(t_same), 1);
|
||||
time = ft_calloc(sizeof(long long), 1);
|
||||
mutex = ft_calloc(sizeof(pthread_mutex_t), config->nbr_philo + 1);
|
||||
init_mutex(mutex, config->nbr_philo + 1);
|
||||
same->config = config;
|
||||
same->mutex = mutex;
|
||||
same->time = time;
|
||||
death = ft_calloc(sizeof(int), 1);
|
||||
*death = 0;
|
||||
same->death = death;
|
||||
return (same);
|
||||
}
|
||||
|
||||
t_only *create_struct_only(int id)
|
||||
{
|
||||
t_only *only;
|
||||
|
||||
only = ft_calloc(sizeof(t_only), 1);
|
||||
only->id = id;
|
||||
only->eat = 0;
|
||||
return (only);
|
||||
}
|
||||
|
||||
t_philo *init_philo(t_same *same, t_only *only)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = ft_calloc(sizeof(t_philo), 1);
|
||||
philo->same = same;
|
||||
philo->only = only;
|
||||
return (philo);
|
||||
}
|
||||
|
||||
int manage_threads(t_config *config)
|
||||
{
|
||||
/*(void)config;
|
||||
long long time = get_utime();
|
||||
message(time, 0, "frghjuikfgvbjhnkiugtfgv\n");
|
||||
ft_putnbr_fd(get_utime() - time, 1);
|
||||
write(1, "\n", 1);*/
|
||||
pthread_t *threads;
|
||||
t_same *same;
|
||||
int i;
|
||||
|
||||
threads = ft_calloc(config->nbr_philo + 1, sizeof(pthread_t));
|
||||
if (threads == NULL)
|
||||
return (1);
|
||||
same = create_struct_same(config);
|
||||
i = -1;
|
||||
while (++i < config->nbr_philo)
|
||||
{
|
||||
if (pthread_create(&threads[i + 1], NULL, (void *)&philosopher, init_philo(same, create_struct_only(i))) != 0)
|
||||
{
|
||||
write(2, "Error thread creation\n", 21);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
while (--i > -1)
|
||||
{
|
||||
if (pthread_join(threads[i], NULL) != 0)
|
||||
{
|
||||
write(2, "Error thread finish\n", 20);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
destroy_mutex(same->mutex, 3);
|
||||
return (0);
|
||||
}
|
||||
|
|
50
mandatory/utils_manage_threads.c
Normal file
50
mandatory/utils_manage_threads.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils_manage_threads.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
void init_mutex(pthread_mutex_t *mutex, int nbr)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (++i < nbr)
|
||||
pthread_mutex_init(&mutex[i], NULL);
|
||||
}
|
||||
|
||||
void destroy_mutex(pthread_mutex_t *mutex, int nbr)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (++i < nbr)
|
||||
pthread_mutex_destroy(&mutex[i]);
|
||||
}
|
||||
|
||||
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])
|
||||
{
|
||||
free(philo[i]->only);
|
||||
free(philo[i]);
|
||||
}
|
||||
free(philo);
|
||||
}
|
59
mandatory/utils_threads.c
Normal file
59
mandatory/utils_threads.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils_threads.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/30 18:10:31 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
long long get_utime(void)
|
||||
{
|
||||
struct timeval time;
|
||||
|
||||
gettimeofday(&time, NULL);
|
||||
return (time.tv_sec * 1000000 + time.tv_usec);
|
||||
}
|
||||
|
||||
void message(long long time, int id, char *msg)
|
||||
{
|
||||
char send[1024];
|
||||
int i;
|
||||
|
||||
i = ft_lltos(send, (get_utime() - time) / 1000);
|
||||
send[i++] = ' ';
|
||||
i += ft_lltos(send + i, id + 1);
|
||||
send[i++] = ' ';
|
||||
while (*msg)
|
||||
send[i++] = *msg++;
|
||||
write(1, send, i);
|
||||
}
|
||||
|
||||
int verif_finish(t_philo *philo, t_only *only)
|
||||
{
|
||||
if (finish(philo, only->last_eat_time))
|
||||
{
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int verif_die_eating(t_philo *philo, t_only *only, t_config *config)
|
||||
{
|
||||
if (get_utime() - only->last_eat_time + config->time_eat
|
||||
>= config->time_die)
|
||||
{
|
||||
usleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
227
start.sh
227
start.sh
|
@ -1,227 +0,0 @@
|
|||
#!/bin/zsh
|
||||
|
||||
echo ' _____ _ _ _ _ _____ _
|
||||
| _ | |_|_| |___ ___ ___ ___| |_ ___ ___ ___ |_ _|__ ___| |_ ___ ___
|
||||
| __| | | | . |_ -| . | . | | -_| _|_ -| | || -_|_ -| _| -_| _|
|
||||
|__| |_|_|_|_|___|___|___| _|_|_|___|_| |___| |_||___|___|_| |___|_|
|
||||
|_| '
|
||||
|
||||
if [ "$#" -ne 2 ]; then
|
||||
echo "Usage: start.sh <Project Folder> <Test Type>"
|
||||
echo "\tType 0: test philo, and philo_bonus"
|
||||
echo "\tType 1: test philo only"
|
||||
echo "\tType 2: test philo_bonus only"
|
||||
|
||||
exit
|
||||
fi
|
||||
|
||||
if [ "$2" -gt 2 -o "$2" -lt 0 ]; then
|
||||
echo "[Error]: Wrong Arguments"
|
||||
exit
|
||||
fi
|
||||
|
||||
echo "\e[92m[+] Given Folder: $1"
|
||||
echo "[+] Test Type: $2\e[0m\n"
|
||||
echo "\e[94m[+] In Case of a failed test, please check ./errors_log file for more information\e[0m\n"
|
||||
|
||||
error_log ()
|
||||
{
|
||||
echo "[$1-$2]: $3" >> ./errors_log
|
||||
}
|
||||
|
||||
test_one ()
|
||||
{
|
||||
("$2/$1" 4 310 200 100 > "./log_$1")&
|
||||
sleep 5
|
||||
pkill $1
|
||||
output=$(grep died -m 1 "./log_$1" | awk '{print $NF}')
|
||||
if [ "$output" = "died" ];then
|
||||
echo "\e[92m[+] Test #1 Succeeded !\e[0m"
|
||||
else
|
||||
echo "\e[91m[+] Test #1 Failed !\e[0m"
|
||||
error_log $1 "Test #1" "Given 4 310 200 100 arguments to $1, a philosopher should die !"
|
||||
fi
|
||||
rm -rf "./log_$1"
|
||||
}
|
||||
|
||||
test_two ()
|
||||
{
|
||||
echo "\e[94m[+] Test #2: Executing your program for 180 second, please wait...\e[0m"
|
||||
("$2/$1" 4 410 200 200 > /dev/null)&
|
||||
i=1
|
||||
error=0
|
||||
while [ $i -lt 180 ];do
|
||||
printf "\r[%d...]" $i
|
||||
pgrep $1 > /dev/null
|
||||
if [ "$?" -ne 0 ];then
|
||||
echo "\r\e[91m[+] Test #2 Failed\e[0m"
|
||||
error_log $1 "Test #2" "Given 4 410 200 200 arguments to $1, no philosopher should die !"
|
||||
error=1
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
i=$(( $i + 1 ))
|
||||
done
|
||||
sleep 1
|
||||
if [ $error -eq 0 ];then
|
||||
pkill $1
|
||||
echo "\r\e[92m[+] Test #2 Succeeded\e[0m"
|
||||
fi
|
||||
}
|
||||
|
||||
test_three ()
|
||||
{
|
||||
echo "\e[94m[+] Test #3: Executing your program for 180 second, please wait...\e[0m"
|
||||
("$2/$1" 5 800 200 200 > /dev/null)&
|
||||
i=1
|
||||
error=0
|
||||
while [ $i -lt 180 ];do
|
||||
printf "\r[%d...]" $i
|
||||
pgrep $1 > /dev/null
|
||||
if [ "$?" -ne 0 ];then
|
||||
echo "\r\e[91m[+] Test #3 Failed\e[0m"
|
||||
error_log $1 "Test #3" "Given 4 800 200 200 arguments to $1, no philosopher should die !"
|
||||
error=1
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
i=$(( $i + 1 ))
|
||||
done
|
||||
sleep 1
|
||||
if [ $error -eq 0 ];then
|
||||
pkill $1
|
||||
echo "\r\e[92m[+] Test #3 Succeeded\e[0m"
|
||||
fi
|
||||
}
|
||||
|
||||
test_four ()
|
||||
{
|
||||
("$2/$1" 4 410 200 200 $3 > "./log_$1")&
|
||||
sleep 10
|
||||
pgrep $1 > /dev/null
|
||||
if [ "$?" -eq 1 ];then
|
||||
lines=$(grep eating "./log_$1" | wc -l)
|
||||
if [ $lines -ge $4 ];then
|
||||
echo "\t\e[92m[+] Test #4-$5 Succeeded\e[0m"
|
||||
else
|
||||
echo "\t\e[91m[+] Test #4-$5 Failed\e[0m"
|
||||
error_log $1 "Test #4" "Given 4 410 200 200 $3 arguments to $1, $1 should only be stopped if each philosopher ate at least $3 times !"
|
||||
fi
|
||||
else
|
||||
echo "\t\e[91m[+] Test #4-$5 Failed\e[0m"
|
||||
error_log $1 "Test #4" "Given 4 410 200 200 $3 arguments to $1, $1 should stop !"
|
||||
pkill $1
|
||||
fi
|
||||
rm -rf "./log_$1"
|
||||
}
|
||||
|
||||
test_five ()
|
||||
{
|
||||
echo "\e[94m[+] Test #5 on progress, please wait...\e[0m"
|
||||
i=1
|
||||
t=0
|
||||
error=0
|
||||
while [ $i -le 10 ];do
|
||||
("$2/$1" 2 60 60 60 > "./log_$1")&
|
||||
sleep 2
|
||||
pgrep $1 > /dev/null
|
||||
if [ "$?" -eq 1 ];then
|
||||
printf "\r[%d/10]" $i
|
||||
tmp=$(grep died -m 1 "./log_$1" | awk '{print $1}' | sed 's/[^0-9]*//g')
|
||||
if [ $i -gt 1 ];then
|
||||
x=$(expr $tmp - $t)
|
||||
x=${x#-}
|
||||
if [ $x -gt 10 ];then
|
||||
printf "\r\e[91m[+] Test #5 Failed\e[0m\n"
|
||||
error_log $1 "Test #5" "Given 2 60 60 60 arguments to $1, the time difference of each death shouldn't be bigger than 10ms !"
|
||||
error=1
|
||||
break
|
||||
fi
|
||||
else
|
||||
t=$tmp
|
||||
fi
|
||||
else
|
||||
printf "\r\e[91m[+] Test #5 Failed\e[0m\n"
|
||||
error_log $1 "Test #5" "Given 2 60 60 60 arguments to $1, a philosopher should die !"
|
||||
pkill $1
|
||||
break
|
||||
fi
|
||||
i=$(( $i + 1 ))
|
||||
done
|
||||
|
||||
if [ $error -eq 0 ];then
|
||||
echo "\r\e[92m[+] Test #5 Succeeded\e[0m"
|
||||
fi
|
||||
}
|
||||
|
||||
test_six ()
|
||||
{
|
||||
("$2/$1" 10 410 200 200 > /dev/null)&
|
||||
sleep 2
|
||||
forks=$(pgrep $1 | wc -l)
|
||||
if [ "$forks" -eq 11 ];then
|
||||
printf "\r\e[92m[+] Test #6 Succeeded\e[0m\n"
|
||||
else
|
||||
printf "\r\e[91m[+] Test #6 Failed\e[0m\n"
|
||||
error_log $1 "Test #6" "Given 10 410 200 200 arguments to $1, 10 processes should be forked, each process for a philosopher !"
|
||||
fi
|
||||
pkill $1
|
||||
}
|
||||
|
||||
if [ "$2" -eq 1 -o "$2" -eq 0 ];then
|
||||
|
||||
echo "[============[Testing philo]==============]\n"
|
||||
|
||||
target="philo"
|
||||
make -C $1 > /dev/null
|
||||
|
||||
if [ "$?" -ne 0 ];then
|
||||
echo "\n[+] There's a problem while compiling $target, please recheck your inputs"
|
||||
exit
|
||||
fi
|
||||
|
||||
test_one $target $1
|
||||
|
||||
test_two $target $1
|
||||
|
||||
test_three $target $1
|
||||
|
||||
echo "\e[94m[+] Test #4 on progress, please wait...\e[0m"
|
||||
test_four $target $1 7 28 1
|
||||
test_four $target $1 10 40 2
|
||||
test_four $target $1 12 48 3
|
||||
test_four $target $1 15 60 4
|
||||
|
||||
test_five $target $1
|
||||
rm -rf "./log_$target"
|
||||
fi
|
||||
|
||||
if [ "$2" -eq 2 -o "$2" -eq 0 ];then
|
||||
|
||||
echo "\n[============[Testing philo_bonus]==============]\n"
|
||||
|
||||
target="philo_bonus"
|
||||
make -C "$1/$target" > /dev/null
|
||||
|
||||
if [ "$?" -ne 0 ];then
|
||||
echo "\n[+] There's a problem while compiling $target, please recheck your inputs"
|
||||
exit
|
||||
fi
|
||||
|
||||
test_one $target $1
|
||||
|
||||
test_two $target $1
|
||||
|
||||
test_three $target $1
|
||||
|
||||
echo "\e[94m[+] Test #4 on progress, please wait...\e[0m"
|
||||
test_four $target $1 7 28 1
|
||||
test_four $target $1 10 40 2
|
||||
test_four $target $1 12 48 3
|
||||
test_four $target $1 15 60 4
|
||||
|
||||
test_five $target $1
|
||||
|
||||
test_six $target $1
|
||||
rm -rf "./log_$target"
|
||||
fi
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
SRCS = ft_atoi.c ft_calloc.c ft_putstr_fd.c ft_putnbr_fd.c ft_atoi_check.c \
|
||||
ft_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c ft_bzero.c ft_itoa.c \
|
||||
ft_strcat.c ft_strcpy.c ft_itoa_bozo.c
|
||||
ft_strcat.c ft_strcpy.c ft_lltos.c
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
CC = clang
|
||||
CFLAGS = -g -Wall -Wextra -Werror
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/21 08:21:05 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/08 21:09:45 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/28 22:36:46 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -37,10 +37,8 @@ int ft_atoi(const char *nptr)
|
|||
result = 0;
|
||||
while (*nptr >= '0' && *nptr <= '9')
|
||||
{
|
||||
if (result >= 2147483647 && sign == 1)
|
||||
if (result >= 2147483647)
|
||||
return (-1);
|
||||
else if (result >= 2147483647)
|
||||
return (0);
|
||||
result = result * 10 + *nptr - '0';
|
||||
nptr++;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
#include "utils.h"
|
||||
|
||||
int ft_itoa_bozo(char *str, long long n)
|
||||
int ft_lltos(char *str, long long n)
|
||||
{
|
||||
long long v;
|
||||
long long q;
|
|
@ -1,14 +1,38 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo.c :+: :+: :+: */
|
||||
/* ft_itoa_bozo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 20:43:51 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/13 13:43:32 by erey-bet ### ########.fr */
|
||||
/* Created: 2022/10/04 17:12:52 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/27 15:48:37 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
#include "utils.h"
|
||||
|
||||
int ft_lltos(char *str, long long n)
|
||||
{
|
||||
long long v;
|
||||
long long q;
|
||||
long long i;
|
||||
|
||||
i = 0;
|
||||
v = n;
|
||||
if (n < 0)
|
||||
{
|
||||
str[i++] = '-';
|
||||
v *= -1;
|
||||
}
|
||||
q = 1;
|
||||
while (n / q > 9)
|
||||
q *= 10;
|
||||
while (q > 0)
|
||||
{
|
||||
str[i++] = (n / q % 10) + 48;
|
||||
q /= 10;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (i);
|
||||
}
|
|
@ -5,13 +5,13 @@
|
|||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 16:31:10 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/27 04:06:39 by erey-bet ### ########.fr */
|
||||
/* Created: 2023/03/30 00:04:31 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/30 01:47:49 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBFT_H
|
||||
# define LIBFT_H
|
||||
#ifndef UTILS_H
|
||||
# define UTILS_H
|
||||
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
|
@ -28,6 +28,6 @@ void ft_putchar_fd(char c, int fd);
|
|||
char *ft_itoa(long long n);
|
||||
void ft_strcpy(char *dest, const char *src);
|
||||
void ft_strcat(char *dest, const char *src);
|
||||
int ft_itoa_bozo(char *str, long long n);
|
||||
int ft_lltos(char *str, long long n);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue