aboutsummaryrefslogtreecommitdiff
path: root/philo/include/philo.h
blob: bbc1bc28b11df280b77969a146a771b6889883ac (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   philo.h                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/01/17 10:23:19 by dkaiser           #+#    #+#             */
/*   Updated: 2025/01/26 12:05:33 by dkaiser          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef PHILO_H
# define PHILO_H

# include "ft_utils.h"
# include <pthread.h>
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>

# define ERR_USAGE "Usage: <nbr_of_philos> <ttd> <tte> <tts> [times_must_eat]"
# define ERR_MALLOC "Memory allocation failed"

typedef struct s_fork
{
	int				owner;
	pthread_mutex_t	mutex;
}					t_fork;

typedef struct s_phdata
{
	int				nbr_of_philos;
	int				time_to_die;
	int				time_to_eat;
	int				time_to_sleep;
	int				times_must_eat;
	int				philos_must_eat;
	pthread_mutex_t	pme_mutex;
	int				simulation_running;
	t_fork			*forks;
}					t_phdata;

typedef struct s_philo
{
	int				id;
	int				last_time_eaten;
	int				times_must_eat;
	int				is_alive;
	pthread_t		thread;
	t_phdata		*data;
}					t_philo;

int					run_simulation(int nbr_of_philos, t_philo *philos,
						t_phdata *data);
int					philo_die(t_philo *philo);
void				philo_eat(t_philo *philo);

#endif