refactor files
This commit is contained in:
parent
e102f3c553
commit
a51b881289
@ -1,6 +1,6 @@
|
||||
.PHONY: all clean fclean re
|
||||
|
||||
SRCS = main.c utilc.c parser.c logic.c messages.c prepare_simulation.c
|
||||
SRCS = main.c utilc.c parser.c logic.c messages.c prepare_simulation.c old_utilc.c time.c
|
||||
|
||||
|
||||
SRC_DIR = ./srcs/
|
||||
|
||||
@ -15,9 +15,7 @@ void *run_trhead(t_philo *philo)
|
||||
if (philo->all_struct->opt)
|
||||
{
|
||||
if (philo->count_eat == philo->all_struct->number_of_times_each_philosopher_must_eat)
|
||||
{
|
||||
philo->all_struct->smert = 42; // TODO реализовать завершение программы
|
||||
}
|
||||
philo->all_struct->smert = 42;
|
||||
}
|
||||
cycle(philo);
|
||||
}
|
||||
@ -72,23 +70,20 @@ int run_observer(t_all *all)
|
||||
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) // TODO пофиксить смерть
|
||||
if (time_eat > all->time_to_die)
|
||||
{
|
||||
printf("%ld---------------симуляция окончена фил %d умер ---------------\n", ft_get_time() - all->start_time, i + 1);
|
||||
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);
|
||||
@ -99,8 +94,6 @@ int runner(t_all *all)
|
||||
run_simulation(all);
|
||||
}
|
||||
if (!run_observer(all))
|
||||
{
|
||||
return(0);
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
57
philo_one/srcs/old_utilc.c
Normal file
57
philo_one/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);
|
||||
}
|
||||
@ -57,29 +57,18 @@ int parse_param(t_all *all, int optional, char **argv)
|
||||
int parse_and_check_values(char **argv, int quantity)
|
||||
{
|
||||
t_all *all;
|
||||
int bool;
|
||||
|
||||
all = structalloc_and_bzero();
|
||||
if (quantity == 5)
|
||||
bool = 0;
|
||||
if (quantity == 6)
|
||||
bool = 1;
|
||||
all = structalloc_and_bzero();
|
||||
if (quantity == 5 || quantity == 6)
|
||||
{
|
||||
if (!parse_param(all, 0, argv))
|
||||
if (!parse_param(all, bool, argv))
|
||||
{
|
||||
printf("ok standart run\n");
|
||||
if (runner(all) == 2) // TODO это завершение симуляции
|
||||
return (2);
|
||||
return(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("bad arg\n");
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
else if (quantity == 6)
|
||||
{
|
||||
if (!parse_param(all, 1, argv))
|
||||
{
|
||||
printf("ok run with optional\n");
|
||||
if (runner(all) == 2) // TODO тоже пофиксить;
|
||||
if (runner(all) == 2)
|
||||
return (2);
|
||||
return(1);
|
||||
}
|
||||
@ -91,7 +80,7 @@ int parse_and_check_values(char **argv, int quantity)
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("error please use argument 5 (optional) or 4 (standart) \n");
|
||||
printf("error please use argument 5 (optional) or 4 (standart)\n");
|
||||
free(all);
|
||||
return(0);
|
||||
}
|
||||
|
||||
37
philo_one/srcs/time.c
Normal file
37
philo_one/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);
|
||||
}
|
||||
@ -1,61 +1,5 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
t_all *structalloc_and_bzero(void)
|
||||
{
|
||||
t_all *all;
|
||||
@ -87,45 +31,6 @@ int return_less(int a, int b)
|
||||
return (b);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void ft_exit(t_all *all)
|
||||
{
|
||||
int i;
|
||||
@ -145,5 +50,4 @@ void ft_exit(t_all *all)
|
||||
free(&all->data_philo->philors[i]);
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user