aboutsummaryrefslogtreecommitdiff
path: root/philo_three/src/child.c
blob: f0e7ebd409b664b6a5333513fcdd92c3e43be919 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   child.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <me@cacharle.xyz>                 +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/09/30 14:36:16 by cacharle          #+#    #+#             */
/*   Updated: 2020/09/30 14:39:58 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "philo_three.h"

void	*routine_death(t_philo_args *arg)
{
	t_time	current;

	current = h_time_now();
	while (current - arg->time_last_eat < arg->conf->timeout_death)
		current = h_time_now();
	if (!arg->conf->all_alive)
		return (NULL);
	event_die(arg);
	return (NULL);
}

pid_t	child_start(t_philo_args *arg)
{
	pid_t	pid;
	sem_t			*forks;
	sem_t			*sem_stdout;
	sem_t			*sem_alive;

	pid = fork();
	if (pid == -1)
		return (-1);
	if (pid == 0)
	{
		forks = sem_open(PHILO_SEM_NAME, 0);
		sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0);
		pthread_create(&death_thread, NULL, (t_routine)routine_death, &args);
		event_think();
		while (true)
		{
			event_eat();
			event_sleep();
			event_think();
		}
		exit(0);
	}
	return pid;
}