start rewriteing philo routine

This commit is contained in:
whaffman 2025-01-24 15:07:02 +01:00
parent 4e45d4bace
commit dee0a058e4
5 changed files with 49 additions and 25 deletions

View File

@ -11,25 +11,26 @@
/* ************************************************************************** */
#ifndef PHILO_H
#define PHILO_H
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <sys/time.h>
# define PHILO_H
# include <pthread.h>
# include <stdio.h>
# include <unistd.h>
# include <stdlib.h>
# include <string.h>
# include <stddef.h>
# include <sys/time.h>
# include <limits.h>
#define SUCCESS 1
#define FAILURE 0
# define SUCCESS 1
# define FAILURE 0
#define START_DELAY 0
# define START_DELAY 0
#define ERROR_USAGE "Usage: ./philo number_of_philosophers time_to_die " \
# define ERROR_USAGE "Usage: ./philo number_of_philosophers time_to_die " \
"time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]\n"
#define ERROR_MUTEX "Can not create mutex"
#define ERROR_MALLOC "Malloc Failure"
#define ERROR_THREADS "Can not create threads"
# define ERROR_MUTEX "Can not create mutex"
# define ERROR_MALLOC "Malloc Failure"
# define ERROR_THREADS "Can not create threads"
typedef struct s_rules
{
@ -43,6 +44,7 @@ typedef struct s_rules
pthread_mutex_t *print_lock;
pthread_mutex_t *finished_lock;
pthread_mutex_t *forks;
pthread_t *monitor;
struct s_philo *philos;
} t_rules;
@ -80,5 +82,8 @@ int check_death(t_philo *philo);
void philo_die(t_philo *philo);
int philo_died_while_sleeping(t_philo *philo, int time);
int philo_eat(t_philo *philo);
void *monitor_routine(void *arg);
int check_philos(t_rules *rules);
int ft_min(int a, int b);
#endif

View File

@ -31,6 +31,7 @@ int create_philos(t_rules *rules)
philo->pid = 0;
philo->rules = rules;
philo->last_meal = get_time() + START_DELAY;
philo->n_eat = 0;
i++;
}
return (SUCCESS);

View File

@ -28,5 +28,10 @@ int create_threads(t_rules *rules)
return (FAILURE);
i++;
}
rules->monitor = malloc(sizeof(pthread_t));
if (!rules->monitor)
return (FAILURE);
if (pthread_create(rules->monitor, NULL, &monitor_routine, rules))
return (FAILURE);
return (SUCCESS);
}

View File

@ -25,5 +25,7 @@ int join_threads(t_rules *rules)
return (FAILURE);
i++;
}
if (pthread_join(*rules->monitor, NULL))
return (FAILURE);
return (SUCCESS);
}

View File

@ -11,6 +11,9 @@
/* ************************************************************************** */
#include "philo.h"
void set_finished(t_rules *rules)
{
pthread_mutex_lock(rules->finished_lock);
@ -70,19 +73,25 @@ int philo_eat(t_philo *philo)
philo->n_eat++;
return (SUCCESS);
}
void synchronize_philos(t_philo *philo)
{
pthread_mutex_lock(philo->rules->finished_lock);
pthread_mutex_unlock(philo->rules->finished_lock);
philo->last_meal = get_time();
if (philo->id % 2 == 1)
{
print_status(philo, "is thinking");
usleep(philo->rules->time_to_eat * 10);
}
}
void *philo_routine(void *arg)
{
t_philo *philo;
philo = (t_philo *)arg;
pthread_mutex_lock(philo->rules->finished_lock);
pthread_mutex_unlock(philo->rules->finished_lock);
if (philo->id % 2 == 1)
usleep(philo->rules->time_to_eat * 100);
while (!philo->death && !philo->rules->finished
&& (philo->n_eat < philo->rules->n_must_eat
|| philo->rules->n_must_eat == -1))
synchronize_philos(philo);
while (!philo->death && !get_finished(philo->rules))
{
if (check_death(philo))
return (philo_die(philo), NULL);
@ -92,9 +101,11 @@ void *philo_routine(void *arg)
return (philo_die(philo), NULL);
pthread_mutex_unlock(philo->l_fork);
pthread_mutex_unlock(philo->r_fork);
if (!philo->rules->finished)
print_status(philo, "is sleeping");
if (philo_died_while_sleeping(philo, philo->rules->time_to_sleep))
return (philo_die(philo), NULL);
if(!philo->rules->finished)
print_status(philo, "is thinking");
}
return (NULL);