after refactor this code , create run all thread at the same time
This commit is contained in:
parent
66c8025548
commit
cf9c97f3ee
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
typedef struct s_philo
|
typedef struct s_philo
|
||||||
{
|
{
|
||||||
|
unsigned worked:1;
|
||||||
|
unsigned can_run_simulation:1;
|
||||||
int more_fork;
|
int more_fork;
|
||||||
int less_fork;
|
int less_fork;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
@ -18,6 +20,7 @@ typedef struct s_philo
|
|||||||
time_t time_to_die;
|
time_t time_to_die;
|
||||||
int died;
|
int died;
|
||||||
int nbr_philo;
|
int nbr_philo;
|
||||||
|
|
||||||
pthread_mutex_t *eat_mutex;
|
pthread_mutex_t *eat_mutex;
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|
||||||
@ -48,4 +51,5 @@ int runner(t_all *all, int optional);
|
|||||||
int return_big(int a, int b);
|
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);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -6,6 +6,55 @@ void *test(char *str)
|
|||||||
return(NULL);
|
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)
|
||||||
|
{
|
||||||
|
philo->worked = 1;
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
if (philo->can_run_simulation)
|
||||||
|
{
|
||||||
|
printf("я могу что то делать ура я %d\n", philo->nbr_philo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int func_test(t_all *all)
|
int func_test(t_all *all)
|
||||||
{
|
{
|
||||||
@ -40,7 +89,7 @@ void get_fork_for_philo(t_all *all)
|
|||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
while(i <= all->number_of_philosophers)
|
while(i < all->number_of_philosophers)
|
||||||
{
|
{
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
{
|
{
|
||||||
@ -54,6 +103,8 @@ void get_fork_for_philo(t_all *all)
|
|||||||
}
|
}
|
||||||
all->data_philo->philors[i].died = 0;
|
all->data_philo->philors[i].died = 0;
|
||||||
all->data_philo->philors[i].nbr_philo = i;
|
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++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -80,7 +131,8 @@ void run_all_phils(t_all *all)
|
|||||||
int i;
|
int i;
|
||||||
i = 0;
|
i = 0;
|
||||||
char *str;
|
char *str;
|
||||||
while(i <= all->number_of_philosophers)
|
void *philo;
|
||||||
|
while(i < all->number_of_philosophers)
|
||||||
{
|
{
|
||||||
if (i != 0)
|
if (i != 0)
|
||||||
{
|
{
|
||||||
@ -88,10 +140,11 @@ void run_all_phils(t_all *all)
|
|||||||
}
|
}
|
||||||
str = malloc(sizeof(char) * 80);
|
str = malloc(sizeof(char) * 80);
|
||||||
str[80] = 0;
|
str[80] = 0;
|
||||||
sprintf(str, "t%d", i);
|
sprintf(str, "t%d", i + 1);
|
||||||
if (pthread_create(&all->data_philo->philors[i].thread, NULL, (void*)test, str) == -1)
|
philo = (void*)(&all->data_philo->philors[i]);
|
||||||
|
if (pthread_create(&all->data_philo->philors[i].thread, NULL, (void*)run_trhead, philo) == -1)
|
||||||
{
|
{
|
||||||
printf("не могу создать поток t%d\n", i);
|
printf("I can't create a thread t%d\n", i);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -116,6 +169,7 @@ void philo_think(t_all *all, int i)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void messages(t_all *all, time_t *time, int phil, char *str)
|
void messages(t_all *all, time_t *time, int phil, char *str)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&all->write);
|
pthread_mutex_lock(&all->write);
|
||||||
@ -128,9 +182,11 @@ int run_observer(t_all *all)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int died;
|
int died;
|
||||||
|
time_t time;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
died = 0;
|
died = 0;
|
||||||
|
time = ft_get_time();
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
i = 0;
|
i = 0;
|
||||||
@ -138,6 +194,8 @@ int run_observer(t_all *all)
|
|||||||
{
|
{
|
||||||
if (all->data_philo->philors[i].died == 0)
|
if (all->data_philo->philors[i].died == 0)
|
||||||
{
|
{
|
||||||
|
time = ft_get_time();
|
||||||
|
// printf("%ld\n", time - all->start_time);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -163,7 +221,12 @@ int runner(t_all *all, int optional)
|
|||||||
if (optional)
|
if (optional)
|
||||||
{
|
{
|
||||||
prepare_resources(all);
|
prepare_resources(all);
|
||||||
|
all->start_time = ft_get_time();
|
||||||
run_all_phils(all);
|
run_all_phils(all);
|
||||||
|
if (check_all_run(all))
|
||||||
|
{
|
||||||
|
run_simulation(all);
|
||||||
|
}
|
||||||
if (!run_observer(all))
|
if (!run_observer(all))
|
||||||
{
|
{
|
||||||
return(0);
|
return(0);
|
||||||
|
|||||||
@ -93,5 +93,17 @@ time_t ft_get_time(void)
|
|||||||
time_t return_value;
|
time_t return_value;
|
||||||
struct timeval current_time;
|
struct timeval current_time;
|
||||||
gettimeofday(¤t_time, NULL);
|
gettimeofday(¤t_time, NULL);
|
||||||
return (return_value = current_time.tv_sec * 1000 + current_time.tv_usec / 1000);
|
return_value = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
|
||||||
|
return (return_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
unsigned long ft_time_is(void)
|
||||||
|
{
|
||||||
|
static struct timeval tv;
|
||||||
|
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
return ((tv.tv_sec * (unsigned long)1000) + (tv.tv_usec / 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user