fix: I create now 1 thread by philo

This commit is contained in:
Camille Chauvet 2023-04-19 11:53:50 +00:00
parent a37cbf88f3
commit 4e27d103a8
6 changed files with 42 additions and 24 deletions

6
data.c
View File

@ -6,12 +6,14 @@
#include "philo.h"
#include "philos.h"
#include "struct.h"
#include <string.h>
bool data_init(t_data *data)
{
data->forks = malloc(sizeof(bool) * data->nb_philos);
if (data->forks == NULL)
return (1);
memset(data->forks, 1, data->nb_philos);
data->philos = malloc(sizeof(t_philo) * data->nb_philos);
if (data->philos == NULL)
{
@ -25,8 +27,9 @@ bool data_init(t_data *data)
free(data->forks);
return (1);
}
pthread_mutex_init(&data->forks_mutex, NULL);
pthread_mutex_init(&data->stop_mutex, NULL);
data->stop = 0;
data->nb_meals = 0;
return (0);
}
@ -34,7 +37,6 @@ void data_destroyer(t_data *data)
{
free(data->threads);
free(data->forks);
pthread_mutex_destroy(&data->nb_meal_mutex);
pthread_mutex_destroy(&data->forks_mutex);
pthread_mutex_destroy(&data->stop_mutex);
philos_destroyer(data);

6
main.c
View File

@ -1,6 +1,7 @@
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "./philos.h"
#include "./data.h"
@ -49,8 +50,11 @@ void check_routine(t_data *data)
while (true)
{
if ((ssize_t) get_min_meal(data) == data->nb_meals)
{
printf("g pu faim\n");
stop(data);
return ;
}
i = 0;
while (i < data->nb_philos)
{

26
philo.c
View File

@ -4,6 +4,8 @@
#include "data.h"
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include "./print.h"
@ -36,7 +38,7 @@ void eat(t_philo *philo, t_data *data)
pthread_mutex_unlock(&philo->nb_meal_mutex);
pthread_mutex_lock(&data->forks_mutex);
data->forks[philo->id] = 1;
data->forks[(philo->id + 1) % (data->nb_philos) + 1] = 1;
data->forks[(philo->id + 1) % data->nb_philos] = 1;
pthread_mutex_unlock(&data->forks_mutex);
}
@ -53,13 +55,14 @@ bool philo_eat(t_philo *philo, t_data *data)
return (1);
pthread_mutex_lock(&data->forks_mutex);
left_fork = data->forks[philo->id];
right_fork = data->forks[(philo->id + 1) % (data->nb_philos) + 1];
right_fork = data->forks[(philo->id + 1) % data->nb_philos];
if (right_fork && left_fork)
{
data->forks[philo->id] = !left_fork;
data->forks[(philo->id + 1) % (data->nb_philos) + 1] = !right_fork;
data->forks[philo->id] = 0;
data->forks[(philo->id + 1) % data->nb_philos] = 0;
}
pthread_mutex_unlock(&data->forks_mutex);
usleep(10);
}
eat(philo, data);
pthread_mutex_lock(&philo->last_eat_mutex);
@ -82,13 +85,15 @@ void *philo_routine(void *arg)
philo = arg;
data = philo->data;
if (philo->id % 2)
usleep(data->sleep_time * 1000);
while (true)
{
print_thinking(philo);
check(data);
philo_eat(philo, data);
check(data);
print_thinking(philo);
if (check(data))
return (NULL);
if (philo_eat(philo, data))
return (NULL);
if (check(data))
return (NULL);
philo_sleep(data, philo);
@ -107,8 +112,9 @@ t_philo *philo_init(t_data *data)
philo->id = id++;
philo->data = data;
philo->nb_meal = 0;
philo->last_eat = get_time();
pthread_mutex_init(&philo->nb_meal_mutex, NULL);
pthread_mutex_init(&philo->nb_meal_mutex, NULL);
pthread_mutex_init(&philo->nb_meal_mutex, NULL);
pthread_mutex_init(&philo->last_eat_mutex, NULL);
pthread_mutex_init(&philo->last_sleep_mutex, NULL);
return (philo);
}

22
print.c
View File

@ -3,39 +3,47 @@
#include <pthread.h>
#include <stddef.h>
#include "./time.h"
#include "struct.h"
static void print(size_t id, char *str)
static void print(t_data *data, size_t id, char *str)
{
static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
size_t time;
pthread_mutex_lock(&data->stop_mutex);
if (data->stop)
{
pthread_mutex_unlock(&data->stop_mutex);
return ;
}
pthread_mutex_unlock(&data->stop_mutex);
time = get_time();
pthread_mutex_lock(&print_mutex);
printf("%zu %zu %s\n", time, id, str);
printf("%6zu %3zu %s\n", time, id, str);
pthread_mutex_unlock(&print_mutex);
}
void print_take_a_fork(t_philo *philo)
{
print(philo->id, "has taken a fork");
print(philo->data, philo->id, "has taken a fork");
}
void print_eating(t_philo *philo)
{
print(philo->id, "is eating");
print(philo->data, philo->id, "is eating");
}
void print_sleeping(t_philo *philo)
{
print(philo->id, "is sleeping");
print(philo->data, philo->id, "is sleeping");
}
void print_thinking(t_philo *philo)
{
print(philo->id, "is thinking");
print(philo->data, philo->id, "is thinking");
}
void print_died(t_philo *philo)
{
print(philo->id, "is died");
print(philo->data, philo->id, "is died");
}

View File

@ -2,6 +2,7 @@
#include "struct.h"
#include <pthread.h>
#include <stdbool.h>
#include <unistd.h>
#include <stddef.h>
bool threads_init(t_data *data)

5
time.c
View File

@ -5,13 +5,10 @@
size_t get_time(void)
{
size_t timedefault = 0;
size_t time;
struct timeval tv;
gettimeofday(&tv, NULL);
time = tv.tv_sec * 1000000 + tv.tv_usec;
if (timedefault == 0)
timedefault = time;
return ((time - timedefault) / 1000);
return (time / 1000);
}