max working

This commit is contained in:
Matthos Denys 2021-05-20 13:37:08 +03:00
parent 14ef5e36c1
commit 62373dc04b
7 changed files with 27 additions and 59 deletions

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2021/05/20 12:58:27 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:16:23 by mdenys #+# #+# */
/* Updated: 2021/05/20 12:23:12 by mdenys ### ########.fr */
/* Updated: 2021/05/20 13:21:37 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,7 @@ int ft_exit(t_all *all, int ret)
{
sem_close(all->data_philo->philors[i].eat_sem);
sem_close(&all->data_philo->sems[i]);
free(all->data_philo->philors[i].name);
i++;
}
free(all->data_philo->philors);

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */
/* Updated: 2021/05/20 12:12:44 by mdenys ### ########.fr */
/* Updated: 2021/05/20 13:36:21 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,12 +14,15 @@
void *run_trhead(t_philo *philo)
{
while (philo->all_struct->smert != 42)
while (1)
{
if (philo->all_struct->opt)
{
if (philo->count_eat == philo->all_struct->etarate_meal)
{
philo->all_struct->smert = 42;
break ;
}
}
cycle(philo);
}
@ -43,12 +46,10 @@ void eating(t_philo *philo)
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++;
philo->last_eat = ft_get_time() - philo->all_struct->start_time;
messages(philo->all_struct, philo, "eating");
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);
}
@ -58,6 +59,8 @@ void sleeping(t_philo *philo)
all = philo->all_struct;
messages(philo->all_struct, philo, "sleeping");
sem_post(all->data_philo->sems);
sem_post(all->data_philo->sems);
ft_msleep(philo->all_struct->time_to_sleep);
}

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:46 by mdenys #+# #+# */
/* Updated: 2021/05/20 12:12:46 by mdenys ### ########.fr */
/* Updated: 2021/05/20 13:26:16 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,8 +17,6 @@ int main(int argc, char **argv)
if (parse_and_check_values(argv, argc) == 2)
return (0);
else
{
return (1);
}
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:48 by mdenys #+# #+# */
/* Updated: 2021/05/20 12:12:49 by mdenys ### ########.fr */
/* Updated: 2021/05/20 13:08:58 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,10 +17,7 @@ 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);
}
sem_wait(all->write);
printf("%ld %d %s\n", timestemp, philo->nbr_philo + 1, str);
sem_post(all->write);
}

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */
/* Updated: 2021/05/20 12:22:45 by mdenys ### ########.fr */
/* Updated: 2021/05/20 13:27:11 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
int run_observer_return(t_all *all, int i)
{
sem_wait(all->write);
printf("%ld %d died\n", \
ft_get_time() - all->start_time, i + 1);
all->smert = 42;
@ -31,18 +32,16 @@ int run_observer(t_all *all)
i = 0;
while (i < all->nbr_phils)
{
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)
return (run_observer_return(all, i));
}
else
sem_wait(all->data_philo->philors[i].eat_sem);
time_eat = (ft_get_time() - \
all->start_time) - all->data_philo->philors[i].last_eat;
if (time_eat > all->time_to_die)
return (run_observer_return(all, i));
sem_post(all->data_philo->philors[i].eat_sem);
if (all->smert == 42)
return (ft_exit(all, 2));
i++;
}
ft_msleep(1);
}
return (0);
}

View File

@ -6,42 +6,12 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */
/* Updated: 2021/05/20 12:12:59 by mdenys ### ########.fr */
/* Updated: 2021/05/20 13:21:04 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo_header.h"
int check_all_run(t_all *all)
{
int i;
i = 0;
while (i < all->nbr_phils)
{
if (all->data_philo->philors[i].worked)
{
i++;
}
}
return (1);
}
void run_simulation(t_all *all)
{
int i;
i = 0;
while (i < all->nbr_phils)
{
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;