create copys philo_one and mv philo_two
This commit is contained in:
parent
a51b881289
commit
8dff2b4a80
3
.vscode/dryrun.log
vendored
3
.vscode/dryrun.log
vendored
@ -1,6 +1,5 @@
|
||||
make --dry-run --always-make --keep-going --print-directory
|
||||
make --dry-run --keep-going --print-directory
|
||||
make: Entering directory `/Users/mdenys/Desktop/projects/philosophers'
|
||||
|
||||
make: Leaving directory `/Users/mdenys/Desktop/projects/philosophers'
|
||||
|
||||
make: *** No targets specified and no makefile found. Stop.
|
||||
|
||||
25
.vscode/targets.log
vendored
25
.vscode/targets.log
vendored
@ -7,8 +7,10 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question
|
||||
|
||||
# This program built for i386-apple-darwin11.3.0
|
||||
|
||||
make: *** No rule to make target `all'. Stop.
|
||||
|
||||
# Make data base, printed on Thu May 13 12:04:43 2021
|
||||
|
||||
# Make data base, printed on Sun May 16 14:47:41 2021
|
||||
|
||||
# Variables
|
||||
|
||||
@ -16,9 +18,6 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question
|
||||
<D = $(patsubst %/,%,$(dir $<))
|
||||
# automatic
|
||||
?F = $(notdir $?)
|
||||
|
||||
make: *** No rule to make target `all'. Stop.
|
||||
|
||||
# environment
|
||||
VSCODE_LOG_NATIVE = false
|
||||
# automatic
|
||||
@ -38,17 +37,17 @@ _ = /usr/bin/make
|
||||
# makefile
|
||||
MAKEFILE_LIST :=
|
||||
# environment
|
||||
VSCODE_NODE_CACHED_DATA_DIR = /Users/mdenys/Library/Application Support/Code/CachedData/cfa2e218100323074ac1948c885448fdf4de2a7f
|
||||
VSCODE_NODE_CACHED_DATA_DIR = /Users/mdenys/Library/Application Support/Code/CachedData/054a9295330880ed74ceaedda236253b4f39a335
|
||||
# environment
|
||||
VSCODE_VERBOSE_LOGGING = true
|
||||
# environment
|
||||
VSCODE_IPC_HOOK_EXTHOST = /var/folders/zz/zyxvpxvq6csfxvn_n003vhs000yw68/T/vscode-ipc-c1d2049e-7cbe-449e-8843-c482df06e729.sock
|
||||
VSCODE_IPC_HOOK_EXTHOST = /var/folders/zz/zyxvpxvq6csfxvn_n003vhs000yw68/T/vscode-ipc-9805dd75-8740-4f27-af98-b26ffd7c1ffb.sock
|
||||
# environment
|
||||
VSCODE_CWD = /
|
||||
# environment
|
||||
Apple_PubSub_Socket_Render = /private/tmp/com.apple.launchd.EPpFRNecPx/Render
|
||||
Apple_PubSub_Socket_Render = /private/tmp/com.apple.launchd.mReJxtxopP/Render
|
||||
# environment
|
||||
PATH = /Users/mdenys/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Frameworks/Mono.framework/Versions/Current/Commands
|
||||
PATH = /Users/mdenys/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki
|
||||
# environment
|
||||
VSCODE_LOG_STACK = false
|
||||
# environment
|
||||
@ -56,7 +55,7 @@ ELECTRON_RUN_AS_NODE = 1
|
||||
# default
|
||||
.FEATURES := target-specific order-only second-expansion else-if archives jobserver check-symlink
|
||||
# environment
|
||||
SSH_AUTH_SOCK = /private/tmp/com.apple.launchd.HmOvHs1Y9f/Listeners
|
||||
SSH_AUTH_SOCK = /private/tmp/com.apple.launchd.XfddAWpCNO/Listeners
|
||||
# automatic
|
||||
%F = $(notdir $%)
|
||||
# environment
|
||||
@ -108,7 +107,7 @@ TMPDIR = /var/folders/zz/zyxvpxvq6csfxvn_n003vhs000yw68/T/
|
||||
# automatic
|
||||
*F = $(notdir $*)
|
||||
# environment
|
||||
VSCODE_IPC_HOOK = /Users/mdenys/Library/Application Support/Code/1.56.0-main.sock
|
||||
VSCODE_IPC_HOOK = /Users/mdenys/Library/Application Support/Code/1.56.2-main.sock
|
||||
# makefile
|
||||
MAKEFLAGS = Rrqp
|
||||
# environment
|
||||
@ -116,7 +115,7 @@ MFLAGS = -Rrqp
|
||||
# automatic
|
||||
*D = $(patsubst %/,%,$(dir $*))
|
||||
# environment
|
||||
XPC_SERVICE_NAME = com.microsoft.VSCode.1224
|
||||
XPC_SERVICE_NAME = com.microsoft.VSCode.6236
|
||||
# automatic
|
||||
+D = $(patsubst %/,%,$(dir $+))
|
||||
# automatic
|
||||
@ -140,7 +139,7 @@ MAKELEVEL := 0
|
||||
# environment
|
||||
LANG = C
|
||||
# environment
|
||||
VSCODE_PID = 91790
|
||||
VSCODE_PID = 27414
|
||||
# variable set hash-table stats:
|
||||
# Load=63/1024=6%, Rehash=0, Collisions=1/84=1%
|
||||
|
||||
@ -224,6 +223,6 @@ GNUmakefile:
|
||||
# strcache size: total = 0 / max = 0 / min = 4096 / avg = 0
|
||||
# strcache free: total = 0 / max = 0 / min = 4096 / avg = 0
|
||||
|
||||
# Finished Make data base on Thu May 13 12:04:43 2021
|
||||
# Finished Make data base on Sun May 16 14:47:41 2021
|
||||
|
||||
|
||||
|
||||
45
philo_two/Makefile
Normal file
45
philo_two/Makefile
Normal file
@ -0,0 +1,45 @@
|
||||
.PHONY: all clean fclean re
|
||||
|
||||
SRCS = main.c utilc.c parser.c logic.c messages.c prepare_simulation.c old_utilc.c time.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)
|
||||
82
philo_two/includes/philo_header.h
Normal file
82
philo_two/includes/philo_header.h
Normal file
@ -0,0 +1,82 @@
|
||||
#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 <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;
|
||||
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;
|
||||
pthread_mutex_t write;
|
||||
int smert;
|
||||
int opt;
|
||||
} t_all;
|
||||
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
unsigned worked:1;
|
||||
unsigned can_run_simulation:1;
|
||||
int more_fork;
|
||||
int less_fork;
|
||||
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;
|
||||
pthread_mutex_t eat_mutex;
|
||||
} t_philo;
|
||||
|
||||
typedef struct s_philo_data
|
||||
{
|
||||
pthread_mutex_t *mutex;
|
||||
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);
|
||||
#endif
|
||||
99
philo_two/srcs/logic.c
Normal file
99
philo_two/srcs/logic.c
Normal file
@ -0,0 +1,99 @@
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
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)
|
||||
{
|
||||
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);
|
||||
philo->count_eat++;
|
||||
}
|
||||
void sleeping(t_philo *philo)
|
||||
{
|
||||
t_all *all;
|
||||
all = philo->all_struct;
|
||||
messages(philo->all_struct, philo, "sleeping");
|
||||
pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]);
|
||||
pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]);
|
||||
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);
|
||||
return(2);
|
||||
}
|
||||
}
|
||||
else
|
||||
return(2);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
12
philo_two/srcs/messages.c
Normal file
12
philo_two/srcs/messages.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include "../includes/philo_header.h"
|
||||
|
||||
void messages(t_all *all, t_philo *philo, char *str)
|
||||
{
|
||||
time_t 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);
|
||||
pthread_mutex_unlock(&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]) - 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]) - 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);
|
||||
}
|
||||
}
|
||||
98
philo_two/srcs/prepare_simulation.c
Normal file
98
philo_two/srcs/prepare_simulation.c
Normal file
@ -0,0 +1,98 @@
|
||||
#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++;
|
||||
}
|
||||
}
|
||||
printf("все запустились !\n");
|
||||
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_fork_for_philo(t_all *all)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
|
||||
while(i <= all->number_of_philosophers)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
all->data_philo->philors[i].more_fork = all->number_of_philosophers;
|
||||
all->data_philo->philors[i].less_fork = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
all->data_philo->philors[i].more_fork = return_big(i, i + 1);
|
||||
all->data_philo->philors[i].less_fork = return_less(i, i + 1);
|
||||
}
|
||||
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;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
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(sizeof(pthread_mutex_t), all->number_of_philosophers);
|
||||
while (i <= all->number_of_philosophers)
|
||||
{
|
||||
pthread_mutex_init(&all->data_philo->mutex[i], NULL);
|
||||
i++;
|
||||
}
|
||||
all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers);
|
||||
get_fork_for_philo(all);
|
||||
return (0);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
printf("I can't create a thread t%d\n", i);
|
||||
}
|
||||
else
|
||||
{
|
||||
pthread_detach(all->data_philo->philors[i].thread);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
37
philo_two/srcs/time.c
Normal file
37
philo_two/srcs/time.c
Normal file
@ -0,0 +1,37 @@
|
||||
#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 = ft_get_time() + interval_ms;
|
||||
time_t 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);
|
||||
}
|
||||
53
philo_two/srcs/utilc.c
Normal file
53
philo_two/srcs/utilc.c
Normal file
@ -0,0 +1,53 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
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 i;
|
||||
|
||||
i = 0;
|
||||
all->smert = 42;
|
||||
ft_msleep(200);
|
||||
while (i <= all->number_of_philosophers)
|
||||
{
|
||||
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]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user