Skip to content

Commit

Permalink
philo
Browse files Browse the repository at this point in the history
  • Loading branch information
m3zh committed Nov 8, 2021
1 parent 2b1c419 commit a4061c9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions philo_bonus/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
# By: mlazzare <mlazzare@student.s19.be> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/09/15 12:21:19 by mdesalle #+# #+# #
# Updated: 2021/11/04 20:19:16 by mlazzare ### ########.fr #
# Updated: 2021/11/07 16:15:45 by mlazzare ### ########.fr #
# #
# **************************************************************************** #

NAME = philo

CC = gcc -g3
CFLAGS = -Wall -Wextra -Werror
CFLAGS += -fsanitize=address
#CFLAGS += -fsanitize=address

LIB = -lpthread

Expand Down
3 changes: 2 additions & 1 deletion philo_bonus/inc/philo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mlazzare <mlazzare@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/07 14:32:05 by mlazzare #+# #+# */
/* Updated: 2021/11/04 16:41:33 by mlazzare ### ########.fr */
/* Updated: 2021/11/08 09:27:42 by mlazzare ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,6 +45,7 @@ typedef struct s_params
long int start;
sem_t *death;
sem_t *fork;
void *philo;
} t_params;

typedef struct s_philo
Expand Down
5 changes: 3 additions & 2 deletions philo_bonus/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mlazzare <mlazzare@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/07 14:31:20 by mlazzare #+# #+# */
/* Updated: 2021/11/04 18:57:21 by mlazzare ### ########.fr */
/* Updated: 2021/11/07 15:59:17 by mlazzare ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -61,7 +61,8 @@ static int init_params(t_params *p, char **ag)
p->max_iter = ft_atoi(ag[5]);
}
p->over = 0;
sem = init_params_semaphore(p);
if (p->num > 0)
sem = init_params_semaphore(p);
return (sem || p->num < 0 || p->time2die < 0 || p->time2eat < 0
|| p->time2sleep < 0 || p->max_iter < 0);
}
Expand Down
41 changes: 20 additions & 21 deletions philo_bonus/src/philosphers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mlazzare <mlazzare@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/07 14:31:20 by mlazzare #+# #+# */
/* Updated: 2021/11/04 20:24:43 by mlazzare ### ########.fr */
/* Updated: 2021/11/08 09:28:50 by mlazzare ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,25 +22,25 @@ static int check_meals(t_philo *p, int last)
static void *check_thread(void *job)
{
int i;
int num;
t_philo *philo;
t_params *p;
t_philo *philo;

philo = (t_philo *)job;
num = philo[0].params->num;
p = (t_params *)job;
philo = (t_philo *)p->philo;
ft_usleep(5);
while (!philo[0].params->over)
while (!p->over)
{
i = -1;
while (++i < num)
while (++i < p->num)
if (check_death(&philo[i]) || check_meals(&philo[i], i))
philo[0].params->over = 1;
p->over = 1;
printf("loop\n");
}
if (philo[0].params->check_meal && philo[num - 1].iter_num == philo[0].params->max_iter)
if (p->check_meal && philo[p->num - 1].iter_num == p->max_iter)
{
ft_usleep(5 * num);
ft_usleep(5 * p->num);
printf(" \n");
printf(" All philosophers have eaten %d times\n", philo[0].params->max_iter);
printf(" All philosophers have eaten %d times\n", p->max_iter);
final_print(1);
return (NULL);
}
Expand All @@ -62,7 +62,7 @@ static int init_thread(t_params *p, t_philo *philo)
&thread_routine, &philo[i]) == -1)
return (error_msg("Error\nFailed to create thread\n", p, philo, 2));
}
if (pthread_create(&death_tid, NULL, &check_thread, philo) == -1)
if (pthread_create(&death_tid, NULL, &check_thread, p) == -1)
return (error_msg("Error\nFailed to create death thread\n", p, philo, 2));
sem_post(p->death);
return (0);
Expand All @@ -74,23 +74,22 @@ static void end_thread(t_params *p, t_philo *philo)

i = -1;
while (++i < p->num)
pthread_join(philo[i].life_tid, (void *)&philo[i]);
pthread_detach(philo[i].life_tid);
ft_usleep(2 * p->num);
sem_close(p->death);
sem_unlink("death");
sem_unlink("/death");
sem_close(p->fork);
sem_unlink("fork");
sem_unlink("/fork");
free(philo);
}

int philosophers(t_params *params)
{
t_philo *philo;

philo = malloc(sizeof(t_philo) * params->num);
if (!philo || init_philo(params, philo))
params->philo = malloc(sizeof(t_philo) * params->num);
if (!params->philo || init_philo(params, params->philo))
return (EXIT_FAILURE);
if (init_thread(params, philo))
if (init_thread(params, params->philo))
return (EXIT_FAILURE);
end_thread(params, philo);
end_thread(params, params->philo);
return (0);
}
7 changes: 3 additions & 4 deletions philo_bonus/src/thread_routine.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mlazzare <mlazzare@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 13:16:12 by mlazzare #+# #+# */
/* Updated: 2021/11/04 20:24:10 by mlazzare ### ########.fr */
/* Updated: 2021/11/08 08:53:03 by mlazzare ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,7 +27,6 @@ int check_death(t_philo *p)
long int now;

now = time_now(p) - p->last_meal;
printf("%lu now\n", now);
sem_wait(p->params->death);
if (now > p->params->time2die)
{
Expand Down Expand Up @@ -93,8 +92,8 @@ void *thread_routine(void *job)

starved = 0;
philo = (t_philo *)job;
// if (philo->id & 1)
// ft_usleep(2.5);
if (philo->id & 1)
ft_usleep(2.5);
philo->thread_start = philo->params->start;
philo->last_meal = time_now(philo);
while (!starved && !philo->dead && !philo->params->over)
Expand Down

0 comments on commit a4061c9

Please sign in to comment.