update
This commit is contained in:
parent
0eda49b6c6
commit
5f1937beec
16
philo_one/.vscode/launch.json
vendored
Normal file
16
philo_one/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug",
|
||||
"program": "${workspaceFolder}/philo_one",
|
||||
"args": ["5", "800", "200", "200", "2"],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,6 +1,18 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2021/05/18 21:18:31 by mdenys #+# #+# #
|
||||
# Updated: 2021/05/18 21:18:31 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
|
||||
SRCS = main.c utilc.c parser.c logic.c logic_cycle.c messages.c prepare_simulation.c old_utilc.c time.c checker.c
|
||||
|
||||
|
||||
SRC_DIR = ./srcs/
|
||||
|
||||
@ -1,3 +1,15 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo_header.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:18:22 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:22:30 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_HEADER_H
|
||||
# define PHILO_HEADER_H
|
||||
|
||||
@ -9,37 +21,26 @@
|
||||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
|
||||
|
||||
|
||||
#define ANSI_COLOR_RED "\x1b[31m"
|
||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
||||
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
||||
#define ANSI_COLOR_BLUE "\x1b[34m"
|
||||
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
||||
#define ANSI_COLOR_CYAN "\x1b[36m"
|
||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||
|
||||
typedef struct s_philo_data t_philo_data;
|
||||
|
||||
typedef struct s_all
|
||||
{
|
||||
int number_of_philosophers;
|
||||
int nbr_phls;
|
||||
time_t time_to_die;
|
||||
time_t time_to_eat;
|
||||
time_t time_to_sleep;
|
||||
int number_of_times_each_philosopher_must_eat;
|
||||
int iteration_eat;
|
||||
time_t start_time;
|
||||
t_philo_data *data_philo;
|
||||
t_philo_data *data;
|
||||
pthread_mutex_t write;
|
||||
int smert;
|
||||
int opt;
|
||||
} t_all;
|
||||
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
unsigned worked:1;
|
||||
unsigned can_run_simulation:1;
|
||||
unsigned worked :1;
|
||||
unsigned can_run_simulation :1;
|
||||
int more_fork;
|
||||
int less_fork;
|
||||
pthread_t thread;
|
||||
@ -78,5 +79,6 @@ 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);
|
||||
void ft_exit(t_all *all);
|
||||
int ft_exit(t_all *all);
|
||||
int check_param(t_all *all, int optional);
|
||||
#endif
|
||||
|
||||
BIN
philo_one/obj/checker.o
Normal file
BIN
philo_one/obj/checker.o
Normal file
Binary file not shown.
BIN
philo_one/obj/logic.o
Normal file
BIN
philo_one/obj/logic.o
Normal file
Binary file not shown.
BIN
philo_one/obj/logic_cycle.o
Normal file
BIN
philo_one/obj/logic_cycle.o
Normal file
Binary file not shown.
BIN
philo_one/obj/main.o
Normal file
BIN
philo_one/obj/main.o
Normal file
Binary file not shown.
BIN
philo_one/obj/messages.o
Normal file
BIN
philo_one/obj/messages.o
Normal file
Binary file not shown.
BIN
philo_one/obj/old_utilc.o
Normal file
BIN
philo_one/obj/old_utilc.o
Normal file
Binary file not shown.
BIN
philo_one/obj/parser.o
Normal file
BIN
philo_one/obj/parser.o
Normal file
Binary file not shown.
BIN
philo_one/obj/prepare_simulation.o
Normal file
BIN
philo_one/obj/prepare_simulation.o
Normal file
Binary file not shown.
BIN
philo_one/obj/time.o
Normal file
BIN
philo_one/obj/time.o
Normal file
Binary file not shown.
BIN
philo_one/obj/utilc.o
Normal file
BIN
philo_one/obj/utilc.o
Normal file
Binary file not shown.
BIN
philo_one/philo_one
Executable file
BIN
philo_one/philo_one
Executable file
Binary file not shown.
75
philo_one/srcs/checker.c
Normal file
75
philo_one/srcs/checker.c
Normal file
@ -0,0 +1,75 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* checker.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:18:17 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:18:17 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
int parse_param(t_all *all, int optional, char **argv)
|
||||
{
|
||||
all->smert = 0;
|
||||
if (optional)
|
||||
{
|
||||
all->nbr_phls = 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->iteration_eat = ft_atoi(argv[5]);
|
||||
all->opt = 1;
|
||||
return (check_param(all, 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
all->nbr_phls = 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->iteration_eat = 0;
|
||||
all->opt = 0;
|
||||
return (check_param(all, 0));
|
||||
}
|
||||
}
|
||||
|
||||
void booller_pars(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;
|
||||
|
||||
booller_pars(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);
|
||||
}
|
||||
}
|
||||
@ -1,88 +1,85 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* logic.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:18:08 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:18:08 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
int check_live(t_all *all)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < all->nbr_phls)
|
||||
{
|
||||
if (all->data->philors[i].died)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void *run_trhead(t_philo *philo)
|
||||
{
|
||||
philo->worked = 1;
|
||||
while(1)
|
||||
{
|
||||
if (philo->can_run_simulation)
|
||||
break;
|
||||
}
|
||||
if (philo->can_run_simulation)
|
||||
{
|
||||
while(philo->can_run_simulation && philo->all_struct->smert != 42)
|
||||
while (philo->all_struct->smert != 42)
|
||||
{
|
||||
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->iteration_eat)
|
||||
{
|
||||
philo->all_struct->smert = 42;
|
||||
}
|
||||
}
|
||||
cycle(philo);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int run_observer42(t_all *all, int i)
|
||||
{
|
||||
time_t time_eat;
|
||||
|
||||
pthread_mutex_lock(&all->data->philors[i].eat_mutex);
|
||||
time_eat = (ft_get_time() - all->start_time) \
|
||||
- all->data->philors[i].last_eat;
|
||||
if (time_eat > all->time_to_die)
|
||||
{
|
||||
printf("%ld %d died\n", \
|
||||
ft_get_time() - all->start_time, i + 1);
|
||||
return (2);
|
||||
}
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
void take_forks(t_philo *philo)
|
||||
{
|
||||
t_all *all;
|
||||
all = philo->all_struct;
|
||||
//printf("less %d %d\n", philo->less_fork, philo->nbr_philo);
|
||||
//printf("big %d %d\n", philo->more_fork, philo->nbr_philo);
|
||||
// printf("mutex: %p\n", all->data_philo->mutex);
|
||||
pthread_mutex_lock(&all->data_philo->mutex[philo->less_fork]);
|
||||
messages(philo->all_struct, philo, "has taken a fork");
|
||||
pthread_mutex_lock(&all->data_philo->mutex[philo->more_fork]);
|
||||
messages(philo->all_struct, philo, "has taken a fork");
|
||||
}
|
||||
void eating(t_philo *philo)
|
||||
{
|
||||
t_all *all;
|
||||
all = philo->all_struct;
|
||||
messages(philo->all_struct, philo, "eating");
|
||||
philo->last_eat = ft_get_time() - philo->all_struct->start_time;
|
||||
ft_msleep(philo->all_struct->time_to_eat);
|
||||
pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]);
|
||||
pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]);
|
||||
philo->count_eat++;
|
||||
}
|
||||
void sleeping(t_philo *philo)
|
||||
{
|
||||
t_all *all;
|
||||
all = philo->all_struct;
|
||||
messages(philo->all_struct, philo, "sleeping");
|
||||
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);
|
||||
pthread_mutex_unlock(&all->data->philors[i].eat_mutex);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int run_observer(t_all *all)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
time_t time_eat;
|
||||
while(1)
|
||||
while (1)
|
||||
{
|
||||
i = 0;
|
||||
while(i < all->number_of_philosophers)
|
||||
while (i < all->nbr_phls)
|
||||
{
|
||||
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)
|
||||
if (run_observer42(all, i) == 2)
|
||||
{
|
||||
printf("%ld %d died\n", ft_get_time() - all->start_time, i + 1);
|
||||
return(2);
|
||||
return (2);
|
||||
}
|
||||
}
|
||||
else
|
||||
return(2);
|
||||
return (ft_exit(all));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@ -92,13 +89,9 @@ int run_observer(t_all *all)
|
||||
int runner(t_all *all)
|
||||
{
|
||||
prepare_resources(all);
|
||||
run_all_phils(all);
|
||||
if (check_all_run(all))
|
||||
{
|
||||
all->start_time = ft_get_time();
|
||||
run_simulation(all);
|
||||
}
|
||||
run_all_phils(all);
|
||||
if (!run_observer(all))
|
||||
return(0);
|
||||
return(1);
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
56
philo_one/srcs/logic_cycle.c
Normal file
56
philo_one/srcs/logic_cycle.c
Normal file
@ -0,0 +1,56 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* logic_cycle.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:18:12 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:18:13 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
void take_forks(t_philo *philo)
|
||||
{
|
||||
t_all *all;
|
||||
|
||||
all = philo->all_struct;
|
||||
pthread_mutex_lock(&all->data->mutex[philo->less_fork]);
|
||||
messages(philo->all_struct, philo, "has taken a fork");
|
||||
pthread_mutex_lock(&all->data->mutex[philo->more_fork]);
|
||||
messages(philo->all_struct, philo, "has taken a fork");
|
||||
}
|
||||
|
||||
void eating(t_philo *philo)
|
||||
{
|
||||
t_all *all;
|
||||
|
||||
all = philo->all_struct;
|
||||
pthread_mutex_lock(&philo->eat_mutex);
|
||||
messages(philo->all_struct, philo, "eating");
|
||||
philo->last_eat = ft_get_time() - philo->all_struct->start_time;
|
||||
ft_msleep(philo->all_struct->time_to_eat);
|
||||
pthread_mutex_unlock(&all->data->mutex[philo->more_fork]);
|
||||
pthread_mutex_unlock(&all->data->mutex[philo->less_fork]);
|
||||
philo->count_eat++;
|
||||
pthread_mutex_unlock(&philo->eat_mutex);
|
||||
}
|
||||
|
||||
void sleeping(t_philo *philo)
|
||||
{
|
||||
t_all *all;
|
||||
|
||||
all = philo->all_struct;
|
||||
messages(philo->all_struct, philo, "sleeping");
|
||||
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);
|
||||
}
|
||||
@ -1,12 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:18:03 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:18:04 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (parse_and_check_values(argv, argc) == 2)
|
||||
return(0);
|
||||
return (0);
|
||||
else
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -1,12 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* messages.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:18:00 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:32:45 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
void messages(t_all *all, t_philo *philo, char *str)
|
||||
{
|
||||
time_t timestemp = ft_get_time() - all->start_time;
|
||||
time_t timestemp;
|
||||
|
||||
timestemp = ft_get_time() - all->start_time;
|
||||
if (philo->all_struct->smert != 42)
|
||||
{
|
||||
pthread_mutex_lock(&all->write);
|
||||
printf("%ld %*s%d %s\n", timestemp, philo->nbr_philo * 30, "", philo->nbr_philo + 1, str);
|
||||
printf("%ld %d %s\n", timestemp, philo->nbr_philo + 1, str);
|
||||
pthread_mutex_unlock(&all->write);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,15 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* old_utilc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:17:55 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:17:56 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
int check_f(char ch)
|
||||
|
||||
@ -1,87 +1,42 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:17:51 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:17:52 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
int check_param(t_all *all, int optional)
|
||||
{
|
||||
if (optional)
|
||||
{
|
||||
if (all->number_of_philosophers <= 0)
|
||||
return(1);
|
||||
if (all->nbr_phls <= 0)
|
||||
return (1);
|
||||
if (all->time_to_die <= 0)
|
||||
return(1);
|
||||
return (1);
|
||||
if (all->time_to_eat <= 0)
|
||||
return(1);
|
||||
return (1);
|
||||
if (all->time_to_sleep <= 0)
|
||||
return(1);
|
||||
if (all->number_of_times_each_philosopher_must_eat <= 0)
|
||||
return(1);
|
||||
return (1);
|
||||
if (all->iteration_eat <= 0)
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (all->number_of_philosophers <= 0)
|
||||
return(1);
|
||||
if (all->nbr_phls <= 0)
|
||||
return (1);
|
||||
if (all->time_to_die <= 0)
|
||||
return(1);
|
||||
return (1);
|
||||
if (all->time_to_eat <= 0)
|
||||
return(1);
|
||||
return (1);
|
||||
if (all->time_to_sleep <= 0)
|
||||
return(1);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int parse_param(t_all *all, int optional, char **argv)
|
||||
{
|
||||
all->smert = 0;
|
||||
if (optional)
|
||||
{
|
||||
all->number_of_philosophers = 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->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]);
|
||||
all->opt = 1;
|
||||
return (check_param(all, 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
all->number_of_philosophers = 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->number_of_times_each_philosopher_must_eat = 0;
|
||||
all->opt = 0;
|
||||
return (check_param(all, 0));
|
||||
}
|
||||
}
|
||||
|
||||
int parse_and_check_values(char **argv, int quantity)
|
||||
{
|
||||
t_all *all;
|
||||
int bool;
|
||||
|
||||
if (quantity == 5)
|
||||
bool = 0;
|
||||
if (quantity == 6)
|
||||
bool = 1;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,15 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* prepare_simulation.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:17:47 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:17:48 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
int check_all_run(t_all *all)
|
||||
@ -5,28 +17,24 @@ int check_all_run(t_all *all)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < all->number_of_philosophers)
|
||||
{
|
||||
if (all->data_philo->philors[i].worked)
|
||||
while (i < all->nbr_phls)
|
||||
{
|
||||
if (all->data->philors[i].worked)
|
||||
i++;
|
||||
}
|
||||
}
|
||||
printf("все запустились !\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
void run_simulation(t_all *all)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < all->number_of_philosophers)
|
||||
while (i < all->nbr_phls)
|
||||
{
|
||||
if (!all->data_philo->philors[i].can_run_simulation)
|
||||
if (!all->data->philors[i].can_run_simulation)
|
||||
{
|
||||
all->data_philo->philors[i].can_run_simulation = 1;
|
||||
all->data->philors[i].can_run_simulation = 1;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@ -37,25 +45,23 @@ void get_fork_for_philo(t_all *all)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
|
||||
while(i < all->number_of_philosophers)
|
||||
while (i < all->nbr_phls)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
all->data_philo->philors[i].less_fork = 0;
|
||||
all->data_philo->philors[i].more_fork = all->number_of_philosophers - 1;
|
||||
all->data->philors[i].less_fork = 0;
|
||||
all->data->philors[i].more_fork = all->nbr_phls - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
all->data_philo->philors[i].less_fork = i - 1;
|
||||
all->data_philo->philors[i].more_fork = i;
|
||||
all->data->philors[i].less_fork = i - 1;
|
||||
all->data->philors[i].more_fork = i;
|
||||
}
|
||||
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->philors[i].died = 0;
|
||||
all->data->philors[i].nbr_philo = i;
|
||||
all->data->philors[i].can_run_simulation = 0;
|
||||
all->data->philors[i].all_struct = all;
|
||||
all->data->philors[i].is_eat = 0;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@ -65,14 +71,16 @@ int prepare_resources(t_all *all)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
all->data_philo = ft_calloc(sizeof(t_philo_data), 1);
|
||||
all->data_philo->mutex = (pthread_mutex_t *)ft_calloc(all->number_of_philosophers + 1, sizeof(pthread_mutex_t));
|
||||
while (i < all->number_of_philosophers)
|
||||
all->data = ft_calloc(sizeof(t_philo_data), 1);
|
||||
all->data->mutex = (pthread_mutex_t *) \
|
||||
ft_calloc(all->nbr_phls + 1, sizeof(pthread_mutex_t));
|
||||
while (i < all->nbr_phls)
|
||||
{
|
||||
pthread_mutex_init(&all->data_philo->mutex[i], NULL);
|
||||
pthread_mutex_init(&all->data->mutex[i], NULL);
|
||||
i++;
|
||||
}
|
||||
all->data_philo->philors = (t_philo *)ft_calloc(all->number_of_philosophers + 1, sizeof(t_philo));
|
||||
all->data->philors = (t_philo *) \
|
||||
ft_calloc(all->nbr_phls + 1, sizeof(t_philo));
|
||||
get_fork_for_philo(all);
|
||||
return (0);
|
||||
}
|
||||
@ -80,19 +88,17 @@ int prepare_resources(t_all *all)
|
||||
void run_all_phils(t_all *all)
|
||||
{
|
||||
int i;
|
||||
i = 0;
|
||||
void *philo;
|
||||
while(i <= all->number_of_philosophers)
|
||||
{
|
||||
philo = (void*)(&all->data_philo->philors[i]);
|
||||
if (pthread_create(&all->data_philo->philors[i].thread, NULL, (void*)run_trhead, philo) == -1)
|
||||
|
||||
i = 0;
|
||||
while (i < all->nbr_phls)
|
||||
{
|
||||
philo = (void *)(&all->data->philors[i]);
|
||||
if (pthread_create(&all->data->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);
|
||||
}
|
||||
pthread_detach(all->data->philors[i].thread);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* time.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:17:42 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:17:43 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);
|
||||
@ -11,9 +24,11 @@ time_t ft_get_time(void)
|
||||
|
||||
void ft_msleep(time_t interval_ms)
|
||||
{
|
||||
time_t end_ms = ft_get_time() + interval_ms;
|
||||
time_t now_ms = ft_get_time();
|
||||
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);
|
||||
@ -24,6 +39,7 @@ void ft_msleep(time_t interval_ms)
|
||||
long get_time(void)
|
||||
{
|
||||
struct timeval time;
|
||||
|
||||
gettimeofday(&time, NULL);
|
||||
return ((long)(time.tv_sec * 1000 + time.tv_usec / 1000));
|
||||
}
|
||||
@ -31,6 +47,7 @@ long get_time(void)
|
||||
void my_usleep(long time)
|
||||
{
|
||||
long t;
|
||||
|
||||
t = get_time();
|
||||
while (get_time() - t < time)
|
||||
usleep(1);
|
||||
|
||||
@ -1,3 +1,15 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utilc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/05/18 21:17:31 by mdenys #+# #+# */
|
||||
/* Updated: 2021/05/18 21:17:37 by mdenys ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
t_all *structalloc_and_bzero(void)
|
||||
@ -5,49 +17,27 @@ t_all *structalloc_and_bzero(void)
|
||||
t_all *all;
|
||||
|
||||
all = ft_calloc(sizeof(t_all), 1);
|
||||
all->number_of_philosophers = -1;
|
||||
all->nbr_phls = -1;
|
||||
all->time_to_die = -1;
|
||||
all->time_to_eat = -1;
|
||||
all->time_to_sleep = -1;
|
||||
all->number_of_times_each_philosopher_must_eat = -1;
|
||||
return(all);
|
||||
all->iteration_eat = -1;
|
||||
return (all);
|
||||
}
|
||||
|
||||
int return_big(int a, int b)
|
||||
{
|
||||
if (a > b)
|
||||
{
|
||||
return (a);
|
||||
}
|
||||
return (b);
|
||||
}
|
||||
|
||||
int return_less(int a, int b)
|
||||
{
|
||||
if (a < b)
|
||||
{
|
||||
return (a);
|
||||
}
|
||||
return (b);
|
||||
}
|
||||
|
||||
void ft_exit(t_all *all)
|
||||
int ft_exit(t_all *all)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
all->smert = 42;
|
||||
ft_msleep(200);
|
||||
while (i < all->number_of_philosophers)
|
||||
while (i < all->nbr_phls)
|
||||
{
|
||||
pthread_mutex_destroy(&all->data_philo->philors[i].eat_mutex);
|
||||
pthread_mutex_destroy(&all->data_philo->mutex[i]);
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (i < all->number_of_philosophers)
|
||||
{
|
||||
free(&all->data_philo->philors[i]);
|
||||
pthread_mutex_destroy(&all->data->philors[i].eat_mutex);
|
||||
pthread_mutex_destroy(&all->data->mutex[i]);
|
||||
i++;
|
||||
}
|
||||
free(all->data->philors);
|
||||
free(all->data);
|
||||
free(all);
|
||||
return (1);
|
||||
}
|
||||
|
||||
47
philo_two/Makefile
Normal file
47
philo_two/Makefile
Normal file
@ -0,0 +1,47 @@
|
||||
.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
|
||||
|
||||
|
||||
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_two
|
||||
|
||||
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)
|
||||
83
philo_two/includes/philo_header.h
Normal file
83
philo_two/includes/philo_header.h
Normal file
@ -0,0 +1,83 @@
|
||||
#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 number_of_philosophers;
|
||||
time_t time_to_die;
|
||||
time_t time_to_eat;
|
||||
time_t time_to_sleep;
|
||||
int number_of_times_each_philosopher_must_eat;
|
||||
time_t start_time;
|
||||
t_philo_data *data_philo;
|
||||
sem_t *write;
|
||||
int smert;
|
||||
int opt;
|
||||
} 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);
|
||||
void ft_exit(t_all *all);
|
||||
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);
|
||||
#endif
|
||||
BIN
philo_two/obj/ft_itoa.o
Normal file
BIN
philo_two/obj/ft_itoa.o
Normal file
Binary file not shown.
BIN
philo_two/obj/ft_strjoin.o
Normal file
BIN
philo_two/obj/ft_strjoin.o
Normal file
Binary file not shown.
BIN
philo_two/obj/logic.o
Normal file
BIN
philo_two/obj/logic.o
Normal file
Binary file not shown.
BIN
philo_two/obj/main.o
Normal file
BIN
philo_two/obj/main.o
Normal file
Binary file not shown.
BIN
philo_two/obj/messages.o
Normal file
BIN
philo_two/obj/messages.o
Normal file
Binary file not shown.
BIN
philo_two/obj/old_utilc.o
Normal file
BIN
philo_two/obj/old_utilc.o
Normal file
Binary file not shown.
BIN
philo_two/obj/parser.o
Normal file
BIN
philo_two/obj/parser.o
Normal file
Binary file not shown.
BIN
philo_two/obj/prepare_simulation.o
Normal file
BIN
philo_two/obj/prepare_simulation.o
Normal file
Binary file not shown.
BIN
philo_two/obj/time.o
Normal file
BIN
philo_two/obj/time.o
Normal file
Binary file not shown.
BIN
philo_two/obj/utilc.o
Normal file
BIN
philo_two/obj/utilc.o
Normal file
Binary file not shown.
BIN
philo_two/philo_two
Executable file
BIN
philo_two/philo_two
Executable file
Binary file not shown.
43
philo_two/srcs/ft_itoa.c
Normal file
43
philo_two/srcs/ft_itoa.c
Normal file
@ -0,0 +1,43 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
char *dst;
|
||||
unsigned int leng;
|
||||
unsigned int nbr;
|
||||
unsigned int i;
|
||||
|
||||
nbr = (n < 0 ? -n : n);
|
||||
leng = nbleng(nbr);
|
||||
i = 0;
|
||||
if (!(dst = (char *)malloc(sizeof(char) * leng + 1 + (n < 0 ? 1 : 0))))
|
||||
return (NULL);
|
||||
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);
|
||||
}
|
||||
22
philo_two/srcs/ft_strjoin.c
Normal file
22
philo_two/srcs/ft_strjoin.c
Normal file
@ -0,0 +1,22 @@
|
||||
#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);
|
||||
}
|
||||
94
philo_two/srcs/logic.c
Normal file
94
philo_two/srcs/logic.c
Normal file
@ -0,0 +1,94 @@
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
void *run_trhead(t_philo *philo)
|
||||
{
|
||||
while(philo->all_struct->smert != 42)
|
||||
{
|
||||
if (philo->all_struct->opt)
|
||||
{
|
||||
if (philo->count_eat == philo->all_struct->number_of_times_each_philosopher_must_eat)
|
||||
philo->all_struct->smert = 42;
|
||||
}
|
||||
cycle(philo);
|
||||
}
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
void take_forks(t_philo *philo)
|
||||
{
|
||||
t_all *all;
|
||||
all = philo->all_struct;
|
||||
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");
|
||||
}
|
||||
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(all->data_philo->sems);
|
||||
sem_post(all->data_philo->sems);
|
||||
sem_post(philo->eat_sem);
|
||||
}
|
||||
void sleeping(t_philo *philo)
|
||||
{
|
||||
t_all *all;
|
||||
all = philo->all_struct;
|
||||
messages(philo->all_struct, philo, "sleeping");
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
12
philo_two/srcs/main.c
Normal file
12
philo_two/srcs/main.c
Normal file
@ -0,0 +1,12 @@
|
||||
#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);
|
||||
}
|
||||
14
philo_two/srcs/messages.c
Normal file
14
philo_two/srcs/messages.c
Normal file
@ -0,0 +1,14 @@
|
||||
#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;
|
||||
if (philo->all_struct->smert != 42)
|
||||
{
|
||||
sem_wait(all->write);
|
||||
printf("%ld %d %s\n", timestemp, philo->nbr_philo + 1, str);
|
||||
sem_post(all->write);
|
||||
}
|
||||
}
|
||||
57
philo_two/srcs/old_utilc.c
Normal file
57
philo_two/srcs/old_utilc.c
Normal file
@ -0,0 +1,57 @@
|
||||
#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);
|
||||
}
|
||||
87
philo_two/srcs/parser.c
Normal file
87
philo_two/srcs/parser.c
Normal file
@ -0,0 +1,87 @@
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
int check_param(t_all *all, int optional)
|
||||
{
|
||||
if (optional)
|
||||
{
|
||||
if (all->number_of_philosophers <= 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->number_of_times_each_philosopher_must_eat <= 0)
|
||||
return(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (all->number_of_philosophers <= 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->number_of_philosophers = 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->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]);
|
||||
all->opt = 1;
|
||||
return (check_param(all, 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
all->number_of_philosophers = 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->number_of_times_each_philosopher_must_eat = 0;
|
||||
all->opt = 0;
|
||||
return (check_param(all, 0));
|
||||
}
|
||||
}
|
||||
|
||||
int parse_and_check_values(char **argv, int quantity)
|
||||
{
|
||||
t_all *all;
|
||||
int bool;
|
||||
|
||||
if (quantity == 5)
|
||||
bool = 0;
|
||||
if (quantity == 6)
|
||||
bool = 1;
|
||||
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);
|
||||
}
|
||||
}
|
||||
82
philo_two/srcs/prepare_simulation.c
Normal file
82
philo_two/srcs/prepare_simulation.c
Normal file
@ -0,0 +1,82 @@
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
int check_all_run(t_all *all)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < all->number_of_philosophers)
|
||||
{
|
||||
if (all->data_philo->philors[i].worked)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void run_simulation(t_all *all)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < all->number_of_philosophers)
|
||||
{
|
||||
if (!all->data_philo->philors[i].can_run_simulation)
|
||||
{
|
||||
all->data_philo->philors[i].can_run_simulation = 1;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void get_data_f_philo(t_all *all)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < all->number_of_philosophers)
|
||||
{
|
||||
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->number_of_philosophers);
|
||||
all->write = reopen_sem(_SEM_WRITE_LOCK, 1);
|
||||
all->data_philo->philors = (t_philo *)ft_calloc(all->number_of_philosophers \
|
||||
+ 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->number_of_philosophers)
|
||||
{
|
||||
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++;
|
||||
}
|
||||
}
|
||||
42
philo_two/srcs/time.c
Normal file
42
philo_two/srcs/time.c
Normal file
@ -0,0 +1,42 @@
|
||||
#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);
|
||||
}
|
||||
93
philo_two/srcs/utilc.c
Normal file
93
philo_two/srcs/utilc.c
Normal file
@ -0,0 +1,93 @@
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
t_all *structalloc_and_bzero(void)
|
||||
{
|
||||
t_all *all;
|
||||
|
||||
all = ft_calloc(sizeof(t_all), 1);
|
||||
all->number_of_philosophers = -1;
|
||||
all->time_to_die = -1;
|
||||
all->time_to_eat = -1;
|
||||
all->time_to_sleep = -1;
|
||||
all->number_of_times_each_philosopher_must_eat = -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);
|
||||
}
|
||||
|
||||
sem_t *reopen_sem(const char *name, int value)
|
||||
{
|
||||
sem_unlink(name);
|
||||
return (sem_open(name, O_RDWR | O_CREAT, S_IRWXU, value));
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
{
|
||||
"folders": [
|
||||
{
|
||||
"path": "."
|
||||
},
|
||||
{
|
||||
"path": "."
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user