заготовка
This commit is contained in:
parent
ee083cf6cc
commit
4c5f6ad3e3
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@ -8,8 +8,8 @@
|
|||||||
"type": "lldb",
|
"type": "lldb",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "Debug",
|
"name": "Debug",
|
||||||
"program": "${workspaceFolder}/philo_two/philo_two",
|
"program": "${workspaceFolder}/philo_three/philo_three",
|
||||||
"args": ["5", "800", "200", "200", "7"],
|
"args": ["4", "310", "200", "200"],
|
||||||
"cwd": "${workspaceFolder}"
|
"cwd": "${workspaceFolder}"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
59
philo_three/Makefile
Normal file
59
philo_three/Makefile
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2021/05/20 12:13:10 by mdenys #+# #+# #
|
||||||
|
# Updated: 2021/05/20 20:45:52 by mdenys ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re
|
||||||
|
|
||||||
|
SRCS = main.c utilc.c parser.c logic.c messages.c\
|
||||||
|
prepare_simulation.c old_utilc.c time.c ft_itoa.c\
|
||||||
|
ft_strjoin.c utilc2.c observer.c ft_exit.c
|
||||||
|
|
||||||
|
|
||||||
|
SRC_DIR = ./srcs/
|
||||||
|
OBJ_DIR = ./obj/
|
||||||
|
|
||||||
|
OBJS = $(SRCS:%.c=%.o)
|
||||||
|
|
||||||
|
OBJS_FILES = $(addprefix $(OBJ_DIR), $(OBJS))
|
||||||
|
|
||||||
|
HEADER = philo_header.h
|
||||||
|
|
||||||
|
vpath %.h includes
|
||||||
|
vpath %.o $(OBJ_DIR)
|
||||||
|
vpath %.c $(SRC_DIR)
|
||||||
|
|
||||||
|
CC = gcc
|
||||||
|
RM = rm -f
|
||||||
|
CFLAGS = -g -Wall -Wextra -Werror -I ./includes/
|
||||||
|
|
||||||
|
NAME = philo_three
|
||||||
|
|
||||||
|
all: $(OBJ_DIR) $(NAME)
|
||||||
|
|
||||||
|
norme :
|
||||||
|
norminette $(addprefix $(SRC_DIR), $(SRCS))
|
||||||
|
|
||||||
|
$(OBJ_DIR):
|
||||||
|
mkdir -p obj
|
||||||
|
|
||||||
|
$(NAME): $(OBJS_FILES)
|
||||||
|
$(CC) $(CFLAGS) -o $(NAME) $(OBJS_FILES) -lpthread
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) $(OBJS_FILES)
|
||||||
|
|
||||||
|
$(OBJ_DIR)%.o: %.c $(HEADER)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
$(RM) $(NAME)
|
||||||
|
|
||||||
|
re: fclean $(NAME)
|
||||||
95
philo_three/includes/philo_header.h
Normal file
95
philo_three/includes/philo_header.h
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* philo_header.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:33 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 14:01:48 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PHILO_HEADER_H
|
||||||
|
# define PHILO_HEADER_H
|
||||||
|
|
||||||
|
# define _SLEEP_STEP 50
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <pthread.h>
|
||||||
|
# include <sys/time.h>
|
||||||
|
# include <semaphore.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <string.h>
|
||||||
|
# define _SEM_NAME_FORKS "pSemForks"
|
||||||
|
# define _SEM_WRITE_LOCK "write"
|
||||||
|
|
||||||
|
typedef struct s_philo_data t_philo_data;
|
||||||
|
|
||||||
|
typedef struct s_all
|
||||||
|
{
|
||||||
|
int nbr_phils;
|
||||||
|
time_t time_to_die;
|
||||||
|
time_t time_to_eat;
|
||||||
|
time_t time_to_sleep;
|
||||||
|
int etarate_meal;
|
||||||
|
time_t start_time;
|
||||||
|
t_philo_data *data_philo;
|
||||||
|
sem_t *write;
|
||||||
|
int smert;
|
||||||
|
int opt;
|
||||||
|
sem_t *bucket;
|
||||||
|
} t_all;
|
||||||
|
|
||||||
|
typedef struct s_philo
|
||||||
|
{
|
||||||
|
unsigned worked :1;
|
||||||
|
unsigned can_run_simulation :1;
|
||||||
|
pthread_t thread;
|
||||||
|
int count_eat;
|
||||||
|
time_t time_to_die;
|
||||||
|
int died;
|
||||||
|
int nbr_philo;
|
||||||
|
time_t last_eat;
|
||||||
|
time_t limit;
|
||||||
|
t_all *all_struct;
|
||||||
|
int is_eat;
|
||||||
|
sem_t *eat_sem;
|
||||||
|
char *name;
|
||||||
|
} t_philo;
|
||||||
|
|
||||||
|
typedef struct s_philo_data
|
||||||
|
{
|
||||||
|
sem_t *sems;
|
||||||
|
t_philo *philors;
|
||||||
|
} t_philo_data;
|
||||||
|
|
||||||
|
void *ft_calloc(size_t num, size_t size);
|
||||||
|
t_all *structalloc_and_bzero(void);
|
||||||
|
int parse_and_check_values(char **argv, int quantity);
|
||||||
|
int ft_atoi(const char *str);
|
||||||
|
int check_f(char ch);
|
||||||
|
int runner(t_all *all);
|
||||||
|
int return_big(int a, int b);
|
||||||
|
int return_less(int a, int b);
|
||||||
|
time_t ft_get_time(void);
|
||||||
|
void *run_trhead(t_philo *philo);
|
||||||
|
int prepare_resources(t_all *all);
|
||||||
|
int check_all_run(t_all *all);
|
||||||
|
void run_simulation(t_all *all);
|
||||||
|
void run_all_phils(t_all *all);
|
||||||
|
void messages(t_all *all, t_philo *philo, char *str);
|
||||||
|
void cycle(t_philo *philo);
|
||||||
|
void ft_msleep(time_t interval_ms);
|
||||||
|
void my_usleep(long time);
|
||||||
|
int ft_exit(t_all *all, int ret);
|
||||||
|
char *ft_itoa(int n);
|
||||||
|
char *ft_strjoin(char const *s1, char const *s2);
|
||||||
|
sem_t *reopen_sem(const char *name, int value);
|
||||||
|
char *sem_set_name(char *str, int i);
|
||||||
|
size_t ft_strlcpy(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);
|
||||||
|
void get_data_for_philo(t_all *all);
|
||||||
|
int run_observer(t_all *all);
|
||||||
|
#endif
|
||||||
31
philo_three/srcs/ft_exit.c
Normal file
31
philo_three/srcs/ft_exit.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_exit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:16:23 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 13:21:37 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]);
|
||||||
|
free(all->data_philo->philors[i].name);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(all->data_philo->philors);
|
||||||
|
free(all->data_philo);
|
||||||
|
free(all);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
68
philo_three/srcs/ft_itoa.c
Normal file
68
philo_three/srcs/ft_itoa.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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"
|
||||||
|
|
||||||
|
static int nbleng(unsigned int n)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (n >= 10)
|
||||||
|
{
|
||||||
|
n /= 10;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
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 *dst;
|
||||||
|
unsigned int leng;
|
||||||
|
unsigned int nbr;
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
nbr = ft_itoa_tern(n);
|
||||||
|
leng = nbleng(nbr);
|
||||||
|
i = 0;
|
||||||
|
dst = (char *)malloc(sizeof(char) * leng + 1 + ft_itoa_tern2(n));
|
||||||
|
if (n < 0)
|
||||||
|
{
|
||||||
|
dst[i] = '-';
|
||||||
|
leng++;
|
||||||
|
}
|
||||||
|
i = leng - 1;
|
||||||
|
while (nbr >= 10)
|
||||||
|
{
|
||||||
|
dst[i] = nbr % 10 + '0';
|
||||||
|
nbr /= 10;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
dst[i] = nbr % 10 + '0';
|
||||||
|
dst[leng] = '\0';
|
||||||
|
return (dst);
|
||||||
|
}
|
||||||
34
philo_three/srcs/ft_strjoin.c
Normal file
34
philo_three/srcs/ft_strjoin.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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"
|
||||||
|
|
||||||
|
char *ft_strjoin(char const *s1, char const *s2)
|
||||||
|
{
|
||||||
|
int len1;
|
||||||
|
int len2;
|
||||||
|
char *new;
|
||||||
|
|
||||||
|
if (s1 && s2)
|
||||||
|
{
|
||||||
|
len1 = ft_strlen((char *)s1);
|
||||||
|
len2 = ft_strlen((char *)s2);
|
||||||
|
new = malloc(sizeof(char *) * len1 + len2 + 1);
|
||||||
|
if (new)
|
||||||
|
{
|
||||||
|
ft_strlcpy(new, s1, len1 + 1);
|
||||||
|
ft_strlcat(new, s2, len1 + len2 + 1);
|
||||||
|
return ((void *)(new));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
74
philo_three/srcs/logic.c
Normal file
74
philo_three/srcs/logic.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* logic.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 16:18:47 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
void *run_trhead(t_philo *philo)
|
||||||
|
{
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if (philo->all_struct->opt)
|
||||||
|
{
|
||||||
|
if (philo->count_eat == philo->all_struct->etarate_meal)
|
||||||
|
{
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cycle(philo);
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void take_forks(t_philo *philo)
|
||||||
|
{
|
||||||
|
t_all *all;
|
||||||
|
|
||||||
|
all = philo->all_struct;
|
||||||
|
sem_wait(all->bucket);
|
||||||
|
sem_wait(all->data_philo->sems);
|
||||||
|
messages(philo->all_struct, philo, "has taken a fork");
|
||||||
|
sem_wait(all->data_philo->sems);
|
||||||
|
messages(philo->all_struct, philo, "has taken a fork");
|
||||||
|
sem_post(all->bucket);
|
||||||
|
}
|
||||||
|
|
||||||
|
void eating(t_philo *philo)
|
||||||
|
{
|
||||||
|
t_all *all;
|
||||||
|
|
||||||
|
all = philo->all_struct;
|
||||||
|
sem_wait(philo->eat_sem);
|
||||||
|
messages(philo->all_struct, philo, "eating");
|
||||||
|
philo->last_eat = ft_get_time() - philo->all_struct->start_time;
|
||||||
|
philo->count_eat++;
|
||||||
|
ft_msleep(philo->all_struct->time_to_eat);
|
||||||
|
sem_post(philo->eat_sem);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sleeping(t_philo *philo)
|
||||||
|
{
|
||||||
|
t_all *all;
|
||||||
|
|
||||||
|
all = philo->all_struct;
|
||||||
|
messages(philo->all_struct, philo, "sleeping");
|
||||||
|
sem_post(all->data_philo->sems);
|
||||||
|
sem_post(all->data_philo->sems);
|
||||||
|
ft_msleep(philo->all_struct->time_to_sleep);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cycle(t_philo *philo)
|
||||||
|
{
|
||||||
|
messages(philo->all_struct, philo, "thinking");
|
||||||
|
take_forks(philo);
|
||||||
|
eating(philo);
|
||||||
|
sleeping(philo);
|
||||||
|
}
|
||||||
22
philo_three/srcs/main.c
Normal file
22
philo_three/srcs/main.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:46 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 13:26:16 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (parse_and_check_values(argv, argc) == 2)
|
||||||
|
return (0);
|
||||||
|
else
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
23
philo_three/srcs/messages.c
Normal file
23
philo_three/srcs/messages.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* messages.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:48 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 16:10:56 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
void messages(t_all *all, t_philo *philo, char *str)
|
||||||
|
{
|
||||||
|
time_t timestemp;
|
||||||
|
|
||||||
|
timestemp = ft_get_time() - all->start_time;
|
||||||
|
sem_wait(all->write);
|
||||||
|
printf("%ld %d %s\n", timestemp, philo->nbr_philo + 1, str);
|
||||||
|
sem_post(all->write);
|
||||||
|
}
|
||||||
57
philo_three/srcs/observer.c
Normal file
57
philo_three/srcs/observer.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* observer.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 16:10:26 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
int run_observer_return(t_all *all, int i)
|
||||||
|
{
|
||||||
|
sem_wait(all->write);
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
sem_wait(all->data_philo->philors[i].eat_sem);
|
||||||
|
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));
|
||||||
|
sem_post(all->data_philo->philors[i].eat_sem);
|
||||||
|
if (all->smert == 42)
|
||||||
|
return (ft_exit(all, 2));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
69
philo_three/srcs/old_utilc.c
Normal file
69
philo_three/srcs/old_utilc.c
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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"
|
||||||
|
|
||||||
|
int check_f(char ch)
|
||||||
|
{
|
||||||
|
if (ch == '\t' || ch == '\n' || ch == ' ' || \
|
||||||
|
ch == '\v' || ch == '\f' || ch == '\r')
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_atoi(const char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
unsigned long int result;
|
||||||
|
int operator;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
operator = 1;
|
||||||
|
result = 0;
|
||||||
|
while (check_f(str[i]))
|
||||||
|
i++;
|
||||||
|
if (str[i] == '-' || str[i] == '+')
|
||||||
|
{
|
||||||
|
if (str[i] == '-')
|
||||||
|
operator *= -1;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (str[i] >= '0' && str[i] <= '9')
|
||||||
|
{
|
||||||
|
if (result > 922337203685477580 && operator == -1)
|
||||||
|
return (0);
|
||||||
|
else if (result > 922337203685477580 && operator == 1)
|
||||||
|
return (-1);
|
||||||
|
result = result * 10 + str[i] - '0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (result * operator);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *ft_calloc(size_t num, size_t size)
|
||||||
|
{
|
||||||
|
void *addr;
|
||||||
|
unsigned int nbytes;
|
||||||
|
|
||||||
|
nbytes = num * size;
|
||||||
|
addr = malloc(nbytes);
|
||||||
|
if (!size || !num || (size == 0 && num == 0))
|
||||||
|
return ((void *)addr);
|
||||||
|
if (!addr)
|
||||||
|
return (NULL);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memset(addr, '\0', nbytes);
|
||||||
|
}
|
||||||
|
return ((void *)addr);
|
||||||
|
}
|
||||||
104
philo_three/srcs/parser.c
Normal file
104
philo_three/srcs/parser.c
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parser.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:56 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 14:11:43 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
int check_param(t_all *all, int optional)
|
||||||
|
{
|
||||||
|
if (optional)
|
||||||
|
{
|
||||||
|
if (all->nbr_phils <= 0)
|
||||||
|
return (1);
|
||||||
|
if (all->time_to_die <= 0)
|
||||||
|
return (1);
|
||||||
|
if (all->time_to_eat <= 0)
|
||||||
|
return (1);
|
||||||
|
if (all->time_to_sleep <= 0)
|
||||||
|
return (1);
|
||||||
|
if (all->etarate_meal <= 0)
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (all->nbr_phils <= 0)
|
||||||
|
return (1);
|
||||||
|
if (all->time_to_die <= 0)
|
||||||
|
return (1);
|
||||||
|
if (all->time_to_eat <= 0)
|
||||||
|
return (1);
|
||||||
|
if (all->time_to_sleep <= 0)
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_param(t_all *all, int optional, char **argv)
|
||||||
|
{
|
||||||
|
all->smert = 0;
|
||||||
|
if (optional)
|
||||||
|
{
|
||||||
|
all->nbr_phils = ft_atoi(argv[1]);
|
||||||
|
all->time_to_die = ft_atoi(argv[2]);
|
||||||
|
all->time_to_eat = ft_atoi(argv[3]);
|
||||||
|
all->time_to_sleep = ft_atoi(argv[4]);
|
||||||
|
all->etarate_meal = ft_atoi(argv[5]);
|
||||||
|
all->opt = 1;
|
||||||
|
return (check_param(all, 1));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
all->nbr_phils = ft_atoi(argv[1]);
|
||||||
|
all->time_to_die = ft_atoi(argv[2]);
|
||||||
|
all->time_to_eat = ft_atoi(argv[3]);
|
||||||
|
all->time_to_sleep = ft_atoi(argv[4]);
|
||||||
|
all->etarate_meal = -1;
|
||||||
|
all->opt = 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)
|
||||||
|
{
|
||||||
|
t_all *all;
|
||||||
|
int bool;
|
||||||
|
|
||||||
|
parse_and_check_part2(quantity, &bool);
|
||||||
|
all = structalloc_and_bzero();
|
||||||
|
if (quantity == 5 || quantity == 6)
|
||||||
|
{
|
||||||
|
if (!parse_param(all, bool, argv))
|
||||||
|
{
|
||||||
|
if (runner(all) == 2)
|
||||||
|
return (2);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("bad arg\n");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("error please use argument 5 (optional) or 4 (standart)\n");
|
||||||
|
free(all);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
}
|
||||||
66
philo_three/srcs/prepare_simulation.c
Normal file
66
philo_three/srcs/prepare_simulation.c
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* prepare_simulation.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */
|
||||||
|
/* Updated: 2021/05/20 14:01:31 by mdenys ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
void get_data_f_philo(t_all *all)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < all->nbr_phils)
|
||||||
|
{
|
||||||
|
all->data_philo->philors[i].died = 0;
|
||||||
|
all->data_philo->philors[i].nbr_philo = i;
|
||||||
|
all->data_philo->philors[i].worked = 0;
|
||||||
|
all->data_philo->philors[i].can_run_simulation = 0;
|
||||||
|
all->data_philo->philors[i].all_struct = all;
|
||||||
|
all->data_philo->philors[i].is_eat = 0;
|
||||||
|
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);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int prepare_resources(t_all *all)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
all->data_philo = ft_calloc(sizeof(t_philo_data), 1);
|
||||||
|
all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->nbr_phils);
|
||||||
|
all->write = reopen_sem(_SEM_WRITE_LOCK, 1);
|
||||||
|
all->bucket = reopen_sem("bucket", 1);
|
||||||
|
all->data_philo->philors = (t_philo *)ft_calloc(all->nbr_phils \
|
||||||
|
+ 1, sizeof(t_philo));
|
||||||
|
get_data_f_philo(all);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void run_all_phils(t_all *all)
|
||||||
|
{
|
||||||
|
void *philo;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < all->nbr_phils)
|
||||||
|
{
|
||||||
|
philo = (void *)(&all->data_philo->philors[i]);
|
||||||
|
if (pthread_create(&all->data_philo->philors[i].thread, NULL, \
|
||||||
|
(void *)run_trhead, philo) == -1)
|
||||||
|
printf("I can't create a thread t%d\n", i);
|
||||||
|
else
|
||||||
|
pthread_detach(all->data_philo->philors[i].thread);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
54
philo_three/srcs/time.c
Normal file
54
philo_three/srcs/time.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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"
|
||||||
|
|
||||||
|
time_t ft_get_time(void)
|
||||||
|
{
|
||||||
|
time_t return_value;
|
||||||
|
struct timeval current_time;
|
||||||
|
|
||||||
|
gettimeofday(¤t_time, NULL);
|
||||||
|
return_value = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
|
||||||
|
return (return_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_msleep(time_t interval_ms)
|
||||||
|
{
|
||||||
|
time_t end_ms;
|
||||||
|
time_t now_ms;
|
||||||
|
|
||||||
|
end_ms = ft_get_time() + interval_ms;
|
||||||
|
now_ms = ft_get_time();
|
||||||
|
while (now_ms < end_ms)
|
||||||
|
{
|
||||||
|
usleep(_SLEEP_STEP);
|
||||||
|
now_ms = ft_get_time();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long get_time(void)
|
||||||
|
{
|
||||||
|
struct timeval time;
|
||||||
|
|
||||||
|
gettimeofday(&time, NULL);
|
||||||
|
return ((long)(time.tv_sec * 1000 + time.tv_usec / 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
void my_usleep(long time)
|
||||||
|
{
|
||||||
|
long t;
|
||||||
|
|
||||||
|
t = get_time();
|
||||||
|
while (get_time() - t < time)
|
||||||
|
usleep(1);
|
||||||
|
}
|
||||||
98
philo_three/srcs/utilc.c
Normal file
98
philo_three/srcs/utilc.c
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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"
|
||||||
|
|
||||||
|
t_all *structalloc_and_bzero(void)
|
||||||
|
{
|
||||||
|
t_all *all;
|
||||||
|
|
||||||
|
all = ft_calloc(sizeof(t_all), 1);
|
||||||
|
all->nbr_phils = -1;
|
||||||
|
all->time_to_die = -1;
|
||||||
|
all->time_to_eat = -1;
|
||||||
|
all->time_to_sleep = -1;
|
||||||
|
all->etarate_meal = -1;
|
||||||
|
return (all);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ft_strlcpy(char *dst, const char *src, size_t size)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
unsigned int count;
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
if (!dst && !src)
|
||||||
|
return (0);
|
||||||
|
while (src[count] != '\0')
|
||||||
|
{
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
if (size > 0)
|
||||||
|
{
|
||||||
|
while (src[i] != '\0' && i < (size - 1) && src[i])
|
||||||
|
{
|
||||||
|
dst[i] = src[i];
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
dst[i] = '\0';
|
||||||
|
}
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ft_strlcat(char *dst, const char *src, size_t size)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
unsigned int len;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
if (!dst && !src)
|
||||||
|
return (0);
|
||||||
|
len = ft_strlen(dst);
|
||||||
|
i = 0;
|
||||||
|
if (size < len)
|
||||||
|
return (ft_strlen((char *)src) + size);
|
||||||
|
if (size)
|
||||||
|
{
|
||||||
|
while (src[i] && len + 1 < size)
|
||||||
|
dst[len++] = src[i++];
|
||||||
|
dst[len] = '\0';
|
||||||
|
}
|
||||||
|
while (src[i])
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_strlen(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *sem_set_name(char *str, int i)
|
||||||
|
{
|
||||||
|
char *temp_nbr;
|
||||||
|
char *returnstr;
|
||||||
|
|
||||||
|
temp_nbr = ft_itoa(i);
|
||||||
|
returnstr = ft_strjoin(str, temp_nbr);
|
||||||
|
free(temp_nbr);
|
||||||
|
return (returnstr);
|
||||||
|
}
|
||||||
19
philo_three/srcs/utilc2.c
Normal file
19
philo_three/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));
|
||||||
|
}
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */
|
/* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */
|
||||||
/* Updated: 2021/05/20 16:17:35 by mdenys ### ########.fr */
|
/* Updated: 2021/05/20 16:18:47 by mdenys ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -18,7 +18,6 @@ void *run_trhead(t_philo *philo)
|
|||||||
{
|
{
|
||||||
if (philo->all_struct->opt)
|
if (philo->all_struct->opt)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (philo->count_eat == philo->all_struct->etarate_meal)
|
if (philo->count_eat == philo->all_struct->etarate_meal)
|
||||||
{
|
{
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user