mini refactor
This commit is contained in:
parent
cf9c97f3ee
commit
bbecbe37b8
@ -1,6 +1,6 @@
|
|||||||
.PHONY: all clean fclean re
|
.PHONY: all clean fclean re
|
||||||
|
|
||||||
SRCS = main.c utilc.c parser.c logic.c
|
SRCS = main.c utilc.c parser.c logic.c messages.c prepare_simulation.c
|
||||||
|
|
||||||
|
|
||||||
SRC_DIR = ./srcs/
|
SRC_DIR = ./srcs/
|
||||||
|
|||||||
@ -52,4 +52,9 @@ int return_big(int a, int b);
|
|||||||
int return_less(int a, int b);
|
int return_less(int a, int b);
|
||||||
time_t ft_get_time(void);
|
time_t ft_get_time(void);
|
||||||
unsigned long ft_time_is(void);
|
unsigned long ft_time_is(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);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
BIN
philo_one/philo_one
Executable file
BIN
philo_one/philo_one
Executable file
Binary file not shown.
@ -1,45 +1,5 @@
|
|||||||
#include "../includes/philo_header.h"
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
void *test(char *str)
|
|
||||||
{
|
|
||||||
printf("поток %s отработал\n", str);
|
|
||||||
return(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 *run_trhead(t_philo *philo)
|
void *run_trhead(t_philo *philo)
|
||||||
{
|
{
|
||||||
philo->worked = 1;
|
philo->worked = 1;
|
||||||
@ -55,109 +15,6 @@ void *run_trhead(t_philo *philo)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int func_test(t_all *all)
|
|
||||||
{
|
|
||||||
pthread_t t0;
|
|
||||||
pthread_t t1;
|
|
||||||
void *result;
|
|
||||||
(void)all;
|
|
||||||
|
|
||||||
if (pthread_create(&t0, NULL, (void*)test, "t0") == -1) // предпоследнее это функция и аргумент
|
|
||||||
{
|
|
||||||
printf("не могу создать поток t0\n");
|
|
||||||
}
|
|
||||||
if (pthread_create(&t1, NULL, (void*)test, "t1") == -1) // предпоследнее это функция и аргумент
|
|
||||||
{
|
|
||||||
printf("не могу создать поток t1\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pthread_join(t0, &result) == -1)
|
|
||||||
{
|
|
||||||
printf("не могу присоединиться к потоку t0\n");
|
|
||||||
}
|
|
||||||
if (pthread_join(t1, &result) == -1)
|
|
||||||
{
|
|
||||||
printf("не могу присоединиться к потоку t1\n");
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
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;
|
|
||||||
char *str;
|
|
||||||
void *philo;
|
|
||||||
while(i < all->number_of_philosophers)
|
|
||||||
{
|
|
||||||
if (i != 0)
|
|
||||||
{
|
|
||||||
free(str);
|
|
||||||
}
|
|
||||||
str = malloc(sizeof(char) * 80);
|
|
||||||
str[80] = 0;
|
|
||||||
sprintf(str, "t%d", i + 1);
|
|
||||||
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++;
|
|
||||||
}
|
|
||||||
if (i > 0)
|
|
||||||
{
|
|
||||||
free(str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void philo_think(t_all *all, int i)
|
void philo_think(t_all *all, int i)
|
||||||
{
|
{
|
||||||
time_t time;
|
time_t time;
|
||||||
@ -168,16 +25,6 @@ void philo_think(t_all *all, int i)
|
|||||||
time = ft_get_time();
|
time = ft_get_time();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void messages(t_all *all, time_t *time, int phil, char *str)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(&all->write);
|
|
||||||
phil++;
|
|
||||||
printf("%lu %d %s\n", (unsigned long)time, phil, str);
|
|
||||||
pthread_mutex_unlock(&all->write);
|
|
||||||
}
|
|
||||||
|
|
||||||
int run_observer(t_all *all)
|
int run_observer(t_all *all)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@ -214,8 +61,6 @@ int run_observer(t_all *all)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int runner(t_all *all, int optional)
|
int runner(t_all *all, int optional)
|
||||||
{
|
{
|
||||||
if (optional)
|
if (optional)
|
||||||
|
|||||||
9
philo_one/srcs/messages.c
Normal file
9
philo_one/srcs/messages.c
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include "../includes/philo_header.h"
|
||||||
|
|
||||||
|
void messages(t_all *all, time_t *time, int phil, char *str)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&all->write);
|
||||||
|
phil++;
|
||||||
|
printf("%lu %d %s\n", (unsigned long)time, phil, str);
|
||||||
|
pthread_mutex_unlock(&all->write);
|
||||||
|
}
|
||||||
108
philo_one/srcs/prepare_simulation.c
Normal file
108
philo_one/srcs/prepare_simulation.c
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
#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;
|
||||||
|
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;
|
||||||
|
char *str;
|
||||||
|
void *philo;
|
||||||
|
while(i < all->number_of_philosophers)
|
||||||
|
{
|
||||||
|
if (i != 0)
|
||||||
|
{
|
||||||
|
free(str);
|
||||||
|
}
|
||||||
|
str = malloc(sizeof(char) * 80);
|
||||||
|
str[80] = 0;
|
||||||
|
sprintf(str, "t%d", i + 1);
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
free(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user