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

#ifndef PHILO_H
# define PHILO_H

# include <unistd.h>
# include <stdlib.h>
# include <stdio.h>
# include <pthread.h>
# include "ft_utils.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 available;
    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);

#endif