aboutsummaryrefslogtreecommitdiff
path: root/philo/src/main.c
blob: 1a07271972002df91c9c610c01722111fa8a526d (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/01/14 17:13:30 by dkaiser           #+#    #+#             */
/*   Updated: 2025/01/18 11:36:33 by dkaiser          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "philo.h"


int load_data(t_phdata *data, int argc, char *argv[])
{
    data->nbr_of_philos = ft_atoi(argv[1]);
    data->time_to_die = ft_atoi(argv[2]);
    data->time_to_eat = ft_atoi(argv[3]);
    data->time_to_sleep = ft_atoi(argv[4]);
    if (argc == 6)
        data->times_must_eat = ft_atoi(argv[2]);
    else
        data->times_must_eat = -1;
    if (data->nbr_of_philos <= 0)
        return (ft_err("Must have at least one philosopher"));
    if (data->time_to_die < 0)
        return (ft_err("ttd can't be negative"));
    if (data->time_to_eat < 0)
        return (ft_err("tte can't be negative"));
    if (data->time_to_sleep < 0)
        return (ft_err("tts can't be negative"));
    return (EXIT_SUCCESS);
}

int init(t_philo **philos, t_phdata *data)
{
    int i;
    int result;

    *philos = (t_philo *)malloc(sizeof(t_philo) * data->nbr_of_philos);
    if (*philos == NULL)
        return (ft_err(ERR_MALLOC));
    data->forks = (t_fork *)malloc(sizeof(t_fork) * data->nbr_of_philos);
    if (data->forks == NULL)
    {
        free(*philos);
        return (ft_err(ERR_MALLOC));
    }
    i = 0;
    while (i < data->nbr_of_philos)
    {
        (*philos)[i].id = i + 1;
        (*philos)[i].is_alive = 1;
        (*philos)[i].times_eaten = 0;
        (*philos)[i].data = data;
        data->forks[i].available = 1;
        result = pthread_mutex_init(&(data->forks[i].mutex), NULL);
        if (result != 0)
            return (result);
        i++;
    }
    return (EXIT_SUCCESS);
}

int main(int argc, char *argv[])
{
    t_phdata data;
    t_philo *philos;
    int result;

    if (argc != 5 && argc != 6)
        return(ft_err(ERR_USAGE));
    result = load_data(&data, argc, argv);
    if (result != EXIT_SUCCESS)
        return (result);
    result = init(&philos, &data);
    if (result != EXIT_SUCCESS)
        return (result);
    result = run_simulation(data.nbr_of_philos, philos);
    free(philos);
    free(data.forks);
    return (result);
}