normed
This commit is contained in:
parent
5f1937beec
commit
02ffa0e76a
@ -1,8 +1,20 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2021/05/20 12:13:10 by mdenys #+# #+# #
|
||||||
|
# Updated: 2021/05/20 12:16:38 by mdenys ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
.PHONY: all clean fclean re
|
.PHONY: all clean fclean re
|
||||||
|
|
||||||
SRCS = main.c utilc.c parser.c logic.c messages.c\
|
SRCS = main.c utilc.c parser.c logic.c messages.c\
|
||||||
prepare_simulation.c old_utilc.c time.c ft_itoa.c\
|
prepare_simulation.c old_utilc.c time.c ft_itoa.c\
|
||||||
ft_strjoin.c
|
ft_strjoin.c utilc2.c observer.c ft_exit.c
|
||||||
|
|
||||||
|
|
||||||
SRC_DIR = ./srcs/
|
SRC_DIR = ./srcs/
|
||||||
|
|||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* philo_header.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:33 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:21:47 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef PHILO_HEADER_H
|
#ifndef PHILO_HEADER_H
|
||||||
# define PHILO_HEADER_H
|
# define PHILO_HEADER_H
|
||||||
|
|
||||||
@ -9,7 +21,6 @@
|
|||||||
# include <semaphore.h>
|
# include <semaphore.h>
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
# include <string.h>
|
# include <string.h>
|
||||||
|
|
||||||
# define _SEM_NAME_FORKS "pSemForks"
|
# define _SEM_NAME_FORKS "pSemForks"
|
||||||
# define _SEM_WRITE_LOCK "write"
|
# define _SEM_WRITE_LOCK "write"
|
||||||
|
|
||||||
@ -17,11 +28,11 @@ typedef struct s_philo_data t_philo_data;
|
|||||||
|
|
||||||
typedef struct s_all
|
typedef struct s_all
|
||||||
{
|
{
|
||||||
int number_of_philosophers;
|
int nbr_phils;
|
||||||
time_t time_to_die;
|
time_t time_to_die;
|
||||||
time_t time_to_eat;
|
time_t time_to_eat;
|
||||||
time_t time_to_sleep;
|
time_t time_to_sleep;
|
||||||
int number_of_times_each_philosopher_must_eat;
|
int etarate_meal;
|
||||||
time_t start_time;
|
time_t start_time;
|
||||||
t_philo_data *data_philo;
|
t_philo_data *data_philo;
|
||||||
sem_t *write;
|
sem_t *write;
|
||||||
@ -29,7 +40,6 @@ typedef struct s_all
|
|||||||
int opt;
|
int opt;
|
||||||
} t_all;
|
} t_all;
|
||||||
|
|
||||||
|
|
||||||
typedef struct s_philo
|
typedef struct s_philo
|
||||||
{
|
{
|
||||||
unsigned worked :1;
|
unsigned worked :1;
|
||||||
@ -71,7 +81,7 @@ void messages(t_all *all, t_philo *philo, char *str);
|
|||||||
void cycle(t_philo *philo);
|
void cycle(t_philo *philo);
|
||||||
void ft_msleep(time_t interval_ms);
|
void ft_msleep(time_t interval_ms);
|
||||||
void my_usleep(long time);
|
void my_usleep(long time);
|
||||||
void ft_exit(t_all *all);
|
int ft_exit(t_all *all, int ret);
|
||||||
char *ft_itoa(int n);
|
char *ft_itoa(int n);
|
||||||
char *ft_strjoin(char const *s1, char const *s2);
|
char *ft_strjoin(char const *s1, char const *s2);
|
||||||
sem_t *reopen_sem(const char *name, int value);
|
sem_t *reopen_sem(const char *name, int value);
|
||||||
@ -80,4 +90,5 @@ size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
|||||||
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
||||||
int ft_strlen(char *str);
|
int ft_strlen(char *str);
|
||||||
void get_data_for_philo(t_all *all);
|
void get_data_for_philo(t_all *all);
|
||||||
|
int run_observer(t_all *all);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
BIN
philo_two/obj/ft_exit.o
Normal file
BIN
philo_two/obj/ft_exit.o
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
philo_two/obj/observer.o
Normal file
BIN
philo_two/obj/observer.o
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
philo_two/obj/utilc2.o
Normal file
BIN
philo_two/obj/utilc2.o
Normal file
Binary file not shown.
Binary file not shown.
30
philo_two/srcs/ft_exit.c
Normal file
30
philo_two/srcs/ft_exit.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_exit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:16:23 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:23:12 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
int ft_exit(t_all *all, int ret)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < all->nbr_phils)
|
||||||
|
{
|
||||||
|
sem_close(all->data_philo->philors[i].eat_sem);
|
||||||
|
sem_close(&all->data_philo->sems[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(all->data_philo->philors);
|
||||||
|
free(all->data_philo);
|
||||||
|
free(all);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_itoa.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:37 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:12:38 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
static int nbleng(unsigned int n)
|
static int nbleng(unsigned int n)
|
||||||
@ -13,6 +25,20 @@ static int nbleng(unsigned int n)
|
|||||||
return (i + 1);
|
return (i + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ft_itoa_tern(int n)
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
return (n * -1);
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_itoa_tern2(int n)
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
char *ft_itoa(int n)
|
char *ft_itoa(int n)
|
||||||
{
|
{
|
||||||
char *dst;
|
char *dst;
|
||||||
@ -20,11 +46,10 @@ char *ft_itoa(int n)
|
|||||||
unsigned int nbr;
|
unsigned int nbr;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
nbr = (n < 0 ? -n : n);
|
nbr = ft_itoa_tern(n);
|
||||||
leng = nbleng(nbr);
|
leng = nbleng(nbr);
|
||||||
i = 0;
|
i = 0;
|
||||||
if (!(dst = (char *)malloc(sizeof(char) * leng + 1 + (n < 0 ? 1 : 0))))
|
dst = (char *)malloc(sizeof(char) * leng + 1 + ft_itoa_tern2(n));
|
||||||
return (NULL);
|
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
{
|
{
|
||||||
dst[i] = '-';
|
dst[i] = '-';
|
||||||
|
|||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strjoin.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:40 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:12:41 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
char *ft_strjoin(char const *s1, char const *s2)
|
char *ft_strjoin(char const *s1, char const *s2)
|
||||||
@ -15,7 +27,7 @@ char *ft_strjoin(char const *s1, char const *s2)
|
|||||||
{
|
{
|
||||||
ft_strlcpy(new, s1, len1 + 1);
|
ft_strlcpy(new, s1, len1 + 1);
|
||||||
ft_strlcat(new, s2, len1 + len2 + 1);
|
ft_strlcat(new, s2, len1 + len2 + 1);
|
||||||
return (void *)(new);
|
return ((void *)(new));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* logic.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:12:44 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
void *run_trhead(t_philo *philo)
|
void *run_trhead(t_philo *philo)
|
||||||
@ -6,7 +18,7 @@ void *run_trhead(t_philo *philo)
|
|||||||
{
|
{
|
||||||
if (philo->all_struct->opt)
|
if (philo->all_struct->opt)
|
||||||
{
|
{
|
||||||
if (philo->count_eat == philo->all_struct->number_of_times_each_philosopher_must_eat)
|
if (philo->count_eat == philo->all_struct->etarate_meal)
|
||||||
philo->all_struct->smert = 42;
|
philo->all_struct->smert = 42;
|
||||||
}
|
}
|
||||||
cycle(philo);
|
cycle(philo);
|
||||||
@ -17,15 +29,18 @@ void *run_trhead(t_philo *philo)
|
|||||||
void take_forks(t_philo *philo)
|
void take_forks(t_philo *philo)
|
||||||
{
|
{
|
||||||
t_all *all;
|
t_all *all;
|
||||||
|
|
||||||
all = philo->all_struct;
|
all = philo->all_struct;
|
||||||
sem_wait(all->data_philo->sems);
|
sem_wait(all->data_philo->sems);
|
||||||
messages(philo->all_struct, philo, "has taken a fork");
|
messages(philo->all_struct, philo, "has taken a fork");
|
||||||
sem_wait(all->data_philo->sems);
|
sem_wait(all->data_philo->sems);
|
||||||
messages(philo->all_struct, philo, "has taken a fork");
|
messages(philo->all_struct, philo, "has taken a fork");
|
||||||
}
|
}
|
||||||
|
|
||||||
void eating(t_philo *philo)
|
void eating(t_philo *philo)
|
||||||
{
|
{
|
||||||
t_all *all;
|
t_all *all;
|
||||||
|
|
||||||
all = philo->all_struct;
|
all = philo->all_struct;
|
||||||
sem_wait(philo->eat_sem);
|
sem_wait(philo->eat_sem);
|
||||||
messages(philo->all_struct, philo, "eating");
|
messages(philo->all_struct, philo, "eating");
|
||||||
@ -36,9 +51,11 @@ void eating(t_philo *philo)
|
|||||||
sem_post(all->data_philo->sems);
|
sem_post(all->data_philo->sems);
|
||||||
sem_post(philo->eat_sem);
|
sem_post(philo->eat_sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sleeping(t_philo *philo)
|
void sleeping(t_philo *philo)
|
||||||
{
|
{
|
||||||
t_all *all;
|
t_all *all;
|
||||||
|
|
||||||
all = philo->all_struct;
|
all = philo->all_struct;
|
||||||
messages(philo->all_struct, philo, "sleeping");
|
messages(philo->all_struct, philo, "sleeping");
|
||||||
ft_msleep(philo->all_struct->time_to_sleep);
|
ft_msleep(philo->all_struct->time_to_sleep);
|
||||||
@ -51,44 +68,3 @@ void cycle(t_philo *philo)
|
|||||||
eating(philo);
|
eating(philo);
|
||||||
sleeping(philo);
|
sleeping(philo);
|
||||||
}
|
}
|
||||||
|
|
||||||
int run_observer(t_all *all)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
i = 0;
|
|
||||||
time_t time_eat;
|
|
||||||
while(1)
|
|
||||||
{
|
|
||||||
i = 0;
|
|
||||||
|
|
||||||
while(i < all->number_of_philosophers)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (all->smert != 42)
|
|
||||||
{
|
|
||||||
time_eat = (ft_get_time() - all->start_time) - all->data_philo->philors[i].last_eat;
|
|
||||||
if (time_eat > all->time_to_die)
|
|
||||||
{
|
|
||||||
printf("%ld %d died\n", ft_get_time() - all->start_time, i + 1);
|
|
||||||
all->smert = 42;
|
|
||||||
return(2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return(2);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
ft_msleep(1);
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int runner(t_all *all)
|
|
||||||
{
|
|
||||||
prepare_resources(all);
|
|
||||||
all->start_time = ft_get_time();
|
|
||||||
run_all_phils(all);
|
|
||||||
if (!run_observer(all))
|
|
||||||
return(0);
|
|
||||||
return(1);
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:46 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:12:46 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
|
|||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* messages.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:48 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:12:49 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
void messages(t_all *all, t_philo *philo, char *str)
|
void messages(t_all *all, t_philo *philo, char *str)
|
||||||
|
|||||||
58
philo_two/srcs/observer.c
Normal file
58
philo_two/srcs/observer.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* observer.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:22:45 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
int run_observer_return(t_all *all, int i)
|
||||||
|
{
|
||||||
|
printf("%ld %d died\n", \
|
||||||
|
ft_get_time() - all->start_time, i + 1);
|
||||||
|
all->smert = 42;
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int run_observer(t_all *all)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
time_t time_eat;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while (i < all->nbr_phils)
|
||||||
|
{
|
||||||
|
if (all->smert != 42)
|
||||||
|
{
|
||||||
|
time_eat = (ft_get_time() - \
|
||||||
|
all->start_time) - all->data_philo->philors[i].last_eat;
|
||||||
|
if (time_eat > all->time_to_die)
|
||||||
|
return (run_observer_return(all, i));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return (ft_exit(all, 2));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
ft_msleep(1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int runner(t_all *all)
|
||||||
|
{
|
||||||
|
prepare_resources(all);
|
||||||
|
all->start_time = ft_get_time();
|
||||||
|
run_all_phils(all);
|
||||||
|
if (!run_observer(all))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* old_utilc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:53 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:12:54 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
int check_f(char ch)
|
int check_f(char ch)
|
||||||
|
|||||||
@ -1,10 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parser.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:56 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:12:56 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
int check_param(t_all *all, int optional)
|
int check_param(t_all *all, int optional)
|
||||||
{
|
{
|
||||||
if (optional)
|
if (optional)
|
||||||
{
|
{
|
||||||
if (all->number_of_philosophers <= 0)
|
if (all->nbr_phils <= 0)
|
||||||
return (1);
|
return (1);
|
||||||
if (all->time_to_die <= 0)
|
if (all->time_to_die <= 0)
|
||||||
return (1);
|
return (1);
|
||||||
@ -12,12 +24,12 @@ int check_param(t_all *all, int optional)
|
|||||||
return (1);
|
return (1);
|
||||||
if (all->time_to_sleep <= 0)
|
if (all->time_to_sleep <= 0)
|
||||||
return (1);
|
return (1);
|
||||||
if (all->number_of_times_each_philosopher_must_eat <= 0)
|
if (all->etarate_meal <= 0)
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (all->number_of_philosophers <= 0)
|
if (all->nbr_phils <= 0)
|
||||||
return (1);
|
return (1);
|
||||||
if (all->time_to_die <= 0)
|
if (all->time_to_die <= 0)
|
||||||
return (1);
|
return (1);
|
||||||
@ -34,35 +46,40 @@ int parse_param(t_all *all, int optional, char **argv)
|
|||||||
all->smert = 0;
|
all->smert = 0;
|
||||||
if (optional)
|
if (optional)
|
||||||
{
|
{
|
||||||
all->number_of_philosophers = ft_atoi(argv[1]);
|
all->nbr_phils = ft_atoi(argv[1]);
|
||||||
all->time_to_die = ft_atoi(argv[2]);
|
all->time_to_die = ft_atoi(argv[2]);
|
||||||
all->time_to_eat = ft_atoi(argv[3]);
|
all->time_to_eat = ft_atoi(argv[3]);
|
||||||
all->time_to_sleep = ft_atoi(argv[4]);
|
all->time_to_sleep = ft_atoi(argv[4]);
|
||||||
all->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]);
|
all->etarate_meal = ft_atoi(argv[5]);
|
||||||
all->opt = 1;
|
all->opt = 1;
|
||||||
return (check_param(all, 1));
|
return (check_param(all, 1));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
all->number_of_philosophers = ft_atoi(argv[1]);
|
all->nbr_phils = ft_atoi(argv[1]);
|
||||||
all->time_to_die = ft_atoi(argv[2]);
|
all->time_to_die = ft_atoi(argv[2]);
|
||||||
all->time_to_eat = ft_atoi(argv[3]);
|
all->time_to_eat = ft_atoi(argv[3]);
|
||||||
all->time_to_sleep = ft_atoi(argv[4]);
|
all->time_to_sleep = ft_atoi(argv[4]);
|
||||||
all->number_of_times_each_philosopher_must_eat = 0;
|
all->etarate_meal = 0;
|
||||||
all->opt = 0;
|
all->opt = 0;
|
||||||
return (check_param(all, 0));
|
return (check_param(all, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void parse_and_check_part2(int quantity, int *bool)
|
||||||
|
{
|
||||||
|
if (quantity == 5)
|
||||||
|
*bool = 0;
|
||||||
|
if (quantity == 6)
|
||||||
|
*bool = 1;
|
||||||
|
}
|
||||||
|
|
||||||
int parse_and_check_values(char **argv, int quantity)
|
int parse_and_check_values(char **argv, int quantity)
|
||||||
{
|
{
|
||||||
t_all *all;
|
t_all *all;
|
||||||
int bool;
|
int bool;
|
||||||
|
|
||||||
if (quantity == 5)
|
parse_and_check_part2(quantity, &bool);
|
||||||
bool = 0;
|
|
||||||
if (quantity == 6)
|
|
||||||
bool = 1;
|
|
||||||
all = structalloc_and_bzero();
|
all = structalloc_and_bzero();
|
||||||
if (quantity == 5 || quantity == 6)
|
if (quantity == 5 || quantity == 6)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* prepare_simulation.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:12:59 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
int check_all_run(t_all *all)
|
int check_all_run(t_all *all)
|
||||||
@ -5,7 +17,7 @@ int check_all_run(t_all *all)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < all->number_of_philosophers)
|
while (i < all->nbr_phils)
|
||||||
{
|
{
|
||||||
if (all->data_philo->philors[i].worked)
|
if (all->data_philo->philors[i].worked)
|
||||||
{
|
{
|
||||||
@ -20,7 +32,7 @@ void run_simulation(t_all *all)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < all->number_of_philosophers)
|
while (i < all->nbr_phils)
|
||||||
{
|
{
|
||||||
if (!all->data_philo->philors[i].can_run_simulation)
|
if (!all->data_philo->philors[i].can_run_simulation)
|
||||||
{
|
{
|
||||||
@ -35,7 +47,7 @@ void get_data_f_philo(t_all *all)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < all->number_of_philosophers)
|
while (i < all->nbr_phils)
|
||||||
{
|
{
|
||||||
all->data_philo->philors[i].died = 0;
|
all->data_philo->philors[i].died = 0;
|
||||||
all->data_philo->philors[i].nbr_philo = i;
|
all->data_philo->philors[i].nbr_philo = i;
|
||||||
@ -44,7 +56,8 @@ void get_data_f_philo(t_all *all)
|
|||||||
all->data_philo->philors[i].all_struct = all;
|
all->data_philo->philors[i].all_struct = all;
|
||||||
all->data_philo->philors[i].is_eat = 0;
|
all->data_philo->philors[i].is_eat = 0;
|
||||||
all->data_philo->philors[i].name = sem_set_name("philo", i);
|
all->data_philo->philors[i].name = sem_set_name("philo", i);
|
||||||
all->data_philo->philors[i].eat_sem = reopen_sem(all->data_philo->philors[i].name, 1);
|
all->data_philo->philors[i].eat_sem = \
|
||||||
|
reopen_sem(all->data_philo->philors[i].name, 1);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,9 +68,9 @@ int prepare_resources(t_all *all)
|
|||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
all->data_philo = ft_calloc(sizeof(t_philo_data), 1);
|
all->data_philo = ft_calloc(sizeof(t_philo_data), 1);
|
||||||
all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->number_of_philosophers);
|
all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->nbr_phils);
|
||||||
all->write = reopen_sem(_SEM_WRITE_LOCK, 1);
|
all->write = reopen_sem(_SEM_WRITE_LOCK, 1);
|
||||||
all->data_philo->philors = (t_philo *)ft_calloc(all->number_of_philosophers \
|
all->data_philo->philors = (t_philo *)ft_calloc(all->nbr_phils \
|
||||||
+ 1, sizeof(t_philo));
|
+ 1, sizeof(t_philo));
|
||||||
get_data_f_philo(all);
|
get_data_f_philo(all);
|
||||||
return (0);
|
return (0);
|
||||||
@ -69,7 +82,7 @@ void run_all_phils(t_all *all)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < all->number_of_philosophers)
|
while (i < all->nbr_phils)
|
||||||
{
|
{
|
||||||
philo = (void *)(&all->data_philo->philors[i]);
|
philo = (void *)(&all->data_philo->philors[i]);
|
||||||
if (pthread_create(&all->data_philo->philors[i].thread, NULL, \
|
if (pthread_create(&all->data_philo->philors[i].thread, NULL, \
|
||||||
|
|||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* time.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:13:01 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:13:01 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
time_t ft_get_time(void)
|
time_t ft_get_time(void)
|
||||||
|
|||||||
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utilc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:13:03 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:13:04 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
t_all *structalloc_and_bzero(void)
|
t_all *structalloc_and_bzero(void)
|
||||||
@ -5,11 +17,11 @@ t_all *structalloc_and_bzero(void)
|
|||||||
t_all *all;
|
t_all *all;
|
||||||
|
|
||||||
all = ft_calloc(sizeof(t_all), 1);
|
all = ft_calloc(sizeof(t_all), 1);
|
||||||
all->number_of_philosophers = -1;
|
all->nbr_phils = -1;
|
||||||
all->time_to_die = -1;
|
all->time_to_die = -1;
|
||||||
all->time_to_eat = -1;
|
all->time_to_eat = -1;
|
||||||
all->time_to_sleep = -1;
|
all->time_to_sleep = -1;
|
||||||
all->number_of_times_each_philosopher_must_eat = -1;
|
all->etarate_meal = -1;
|
||||||
return (all);
|
return (all);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +76,6 @@ size_t ft_strlcat(char *dst, const char *src, size_t size)
|
|||||||
return (len);
|
return (len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int ft_strlen(char *str)
|
int ft_strlen(char *str)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@ -85,9 +96,3 @@ char *sem_set_name(char *str, int i)
|
|||||||
free(temp_nbr);
|
free(temp_nbr);
|
||||||
return (returnstr);
|
return (returnstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
sem_t *reopen_sem(const char *name, int value)
|
|
||||||
{
|
|
||||||
sem_unlink(name);
|
|
||||||
return (sem_open(name, O_RDWR | O_CREAT, S_IRWXU, value));
|
|
||||||
}
|
|
||||||
|
|||||||
19
philo_two/srcs/utilc2.c
Normal file
19
philo_two/srcs/utilc2.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utilc2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/19 17:34:52 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 12:13:07 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
sem_t *reopen_sem(const char *name, int value)
|
||||||
|
{
|
||||||
|
sem_unlink(name);
|
||||||
|
return (sem_open(name, O_RDWR | O_CREAT, S_IRWXU, value));
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user