aboutsummaryrefslogtreecommitdiff
path: root/philo_three/src/child.c
blob: 661e0a62a8ae718762c81d023424279c12c8df20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   child.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <me@cacharle.xyz>                 +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/09/30 14:36:16 by cacharle          #+#    #+#             */
/*   Updated: 2021/01/10 14:24:05 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "philo_three.h"

static void	*st_routine_death(t_philo *philo)
{
	while (true)
	{
		sem_wait(philo->sem_eat);
		if (h_time_now() - philo->time_last_eat > philo->conf->timeout_death)
			break ;
		sem_post(philo->sem_eat);
		usleep(2000);
	}
	event_die(philo);
	return (NULL);
}

static void	st_child_loop(t_philo *philo)
{
	long int	eat_counter;

	eat_counter = 0;
	philo->time_last_eat = h_time_now();
	while (true)
	{
		sem_wait(philo->sem_grab);
		event_take_fork(philo);
		event_take_fork(philo);
		sem_post(philo->sem_grab);
		event_eat(philo);
		eat_counter++;
		if (philo->conf->meal_num != -1 && eat_counter == philo->conf->meal_num)
		{
			sem_wait(philo->sem_stdout);
			sem_post(philo->sem_meal_num);
			sem_post(philo->sem_stdout);
		}
		event_sleep(philo);
		event_think(philo);
	}
}

#define PHILO_SEM_EAT_PREFIX "semaphore_philo_three_eat_"

static bool	st_child_setup(t_philo *philo)
{
	if (!h_sem_create(PHILO_SEM_FINISH_NAME, 0, &philo->sem_finish) ||
		!h_sem_create(philo_sem_eat_name(PHILO_SEM_EAT_PREFIX, philo->id),
			1, &philo->sem_eat) ||
		!h_sem_create(PHILO_SEM_NAME, 0, &philo->forks) ||
		!h_sem_create(PHILO_SEM_STDOUT_NAME, 0, &philo->sem_stdout) ||
		!h_sem_create(PHILO_SEM_MEAL_NUM_NAME, 0, &philo->sem_meal_num) ||
		!h_sem_create(PHILO_SEM_START_NAME, 0, &philo->sem_start) ||
		!h_sem_create(PHILO_SEM_GRAB_NAME, 0, &philo->sem_grab))
		return (false);
	return (true);
}

pid_t		child_start(t_philo *philo)
{
	pthread_t	thread_death;
	pid_t		pid;

	if ((pid = fork()) == -1)
		return (-1);
	if (pid == 0)
	{
		philo->sem_finish = SEM_FAILED;
		if (!st_child_setup(philo))
		{
			if (philo->sem_finish != SEM_FAILED)
				sem_post(philo->sem_finish);
			exit(1);
		}
		sem_wait(philo->sem_start);
		philo->time_last_eat = h_time_now();
		if (pthread_create(&thread_death, NULL,
				(t_routine)st_routine_death, philo) != 0)
			exit(sem_post(philo->sem_finish));
		pthread_detach(thread_death);
		st_child_loop(philo);
		exit(0);
	}
	return (pid);
}