Compare commits
No commits in common. "1ca5e59f8d3e76eb8409ed0ac15795360ae84c44" and "dd686a4f85d82e67766b1914e3611287ece86d33" have entirely different histories.
1ca5e59f8d
...
dd686a4f85
44
.gitignore
vendored
44
.gitignore
vendored
@ -4,10 +4,52 @@
|
|||||||
|
|
||||||
# Object files
|
# Object files
|
||||||
*.o
|
*.o
|
||||||
|
*.ko
|
||||||
|
*.obj
|
||||||
|
*.elf
|
||||||
|
|
||||||
|
# Linker output
|
||||||
|
*.ilk
|
||||||
|
*.map
|
||||||
|
*.exp
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
# Libraries
|
# Libraries
|
||||||
*.lib
|
*.lib
|
||||||
*.a
|
*.a
|
||||||
|
*.la
|
||||||
|
*.lo
|
||||||
|
|
||||||
|
# Shared objects (inc. Windows DLLs)
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.so.*
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
*.i*86
|
||||||
|
*.x86_64
|
||||||
|
*.hex
|
||||||
|
|
||||||
|
# Debug files
|
||||||
|
*.dSYM/
|
||||||
|
*.su
|
||||||
|
*.idb
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# Kernel Module Compile Results
|
||||||
|
*.mod*
|
||||||
|
*.cmd
|
||||||
|
.tmp_versions/
|
||||||
|
modules.order
|
||||||
|
Module.symvers
|
||||||
|
Mkfile.old
|
||||||
|
dkms.conf
|
||||||
philo
|
philo
|
||||||
!philo/
|
|
||||||
|
|||||||
@ -57,10 +57,8 @@ typedef struct s_philo
|
|||||||
int death;
|
int death;
|
||||||
pthread_mutex_t *death_lock;
|
pthread_mutex_t *death_lock;
|
||||||
int last_meal;
|
int last_meal;
|
||||||
pthread_mutex_t *last_meal_lock;
|
|
||||||
t_rules *rules;
|
t_rules *rules;
|
||||||
int n_eat;
|
int n_eat;
|
||||||
pthread_mutex_t *n_eat_lock;
|
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|
||||||
// memset, printf, malloc, free, write,
|
// memset, printf, malloc, free, write,
|
||||||
|
|||||||
@ -1,43 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* check_philos.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:13:50 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:13:51 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
int check_philos(t_rules *rules)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int min_n_eat;
|
|
||||||
|
|
||||||
min_n_eat = INT_MAX;
|
|
||||||
i = 0;
|
|
||||||
while (i < rules->n_philos)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(rules->philos[i].n_eat_lock);
|
|
||||||
min_n_eat = ft_min(rules->philos[i].n_eat, min_n_eat);
|
|
||||||
pthread_mutex_unlock(rules->philos[i].n_eat_lock);
|
|
||||||
pthread_mutex_lock(rules->philos[i].last_meal_lock);
|
|
||||||
if (get_time() - rules->philos[i].last_meal >= rules->time_to_die)
|
|
||||||
{
|
|
||||||
philo_die(&rules->philos[i]);
|
|
||||||
set_finished(rules);
|
|
||||||
return (FAILURE);
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(rules->philos[i].last_meal_lock);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (min_n_eat >= rules->n_must_eat)
|
|
||||||
{
|
|
||||||
set_finished(rules);
|
|
||||||
return (FAILURE);
|
|
||||||
}
|
|
||||||
return (SUCCESS);
|
|
||||||
}
|
|
||||||
@ -12,22 +12,6 @@
|
|||||||
|
|
||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
int create_philo_mutexes(t_philo *philo)
|
|
||||||
{
|
|
||||||
philo->death_lock = malloc(sizeof(pthread_mutex_t));
|
|
||||||
philo->last_meal_lock = malloc(sizeof(pthread_mutex_t));
|
|
||||||
philo->n_eat_lock = malloc(sizeof(pthread_mutex_t));
|
|
||||||
if (!philo->death_lock || !philo->last_meal_lock || !philo->n_eat_lock)
|
|
||||||
return (FAILURE);
|
|
||||||
if (pthread_mutex_init(philo->death_lock, NULL))
|
|
||||||
return (FAILURE);
|
|
||||||
if (pthread_mutex_init(philo->last_meal_lock, NULL))
|
|
||||||
return (FAILURE);
|
|
||||||
if (pthread_mutex_init(philo->n_eat_lock, NULL))
|
|
||||||
return (FAILURE);
|
|
||||||
return (SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
int create_mutexes(t_rules *rules)
|
int create_mutexes(t_rules *rules)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@ -41,8 +25,6 @@ int create_mutexes(t_rules *rules)
|
|||||||
{
|
{
|
||||||
if (pthread_mutex_init(&rules->forks[i], NULL))
|
if (pthread_mutex_init(&rules->forks[i], NULL))
|
||||||
return (FAILURE);
|
return (FAILURE);
|
||||||
if (create_philo_mutexes(&rules->philos[i]) == FAILURE)
|
|
||||||
return (FAILURE);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (SUCCESS);
|
return (SUCCESS);
|
||||||
|
|||||||
@ -24,14 +24,7 @@ int destroy_mutexes(t_rules *rules)
|
|||||||
while (i < rules->n_philos)
|
while (i < rules->n_philos)
|
||||||
{
|
{
|
||||||
if (pthread_mutex_destroy(&rules->forks[i]))
|
if (pthread_mutex_destroy(&rules->forks[i]))
|
||||||
return (printf("1"), FAILURE);
|
return (FAILURE);
|
||||||
if (pthread_mutex_destroy(rules->philos[i].death_lock))
|
|
||||||
return (printf("2"), FAILURE);
|
|
||||||
// pthread_mutex_lock(rules->philos[i].last_meal_lock);
|
|
||||||
if (pthread_mutex_destroy(rules->philos[i].last_meal_lock))
|
|
||||||
return (printf("3: %d\n", i), perror("Error"), FAILURE);
|
|
||||||
if (pthread_mutex_destroy(rules->philos[i].n_eat_lock))
|
|
||||||
return (printf("4"), FAILURE);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (SUCCESS);
|
return (SUCCESS);
|
||||||
|
|||||||
@ -19,9 +19,6 @@ void free_philos(t_rules *rules)
|
|||||||
while (i < rules->n_philos)
|
while (i < rules->n_philos)
|
||||||
{
|
{
|
||||||
free(rules->philos[i].pid);
|
free(rules->philos[i].pid);
|
||||||
free(rules->philos[i].death_lock);
|
|
||||||
free(rules->philos[i].last_meal_lock);
|
|
||||||
free(rules->philos[i].n_eat_lock);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
free(rules->monitor);
|
free(rules->monitor);
|
||||||
|
|||||||
@ -1,18 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* ft_min.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:13:55 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:13:55 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
int ft_min(int a, int b)
|
|
||||||
{
|
|
||||||
if (a < b)
|
|
||||||
return (a);
|
|
||||||
return (b);
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* get_death.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:13:56 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:13:56 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
int get_death(t_philo *philo)
|
|
||||||
{
|
|
||||||
int death;
|
|
||||||
|
|
||||||
pthread_mutex_lock(philo->death_lock);
|
|
||||||
death = philo->death;
|
|
||||||
pthread_mutex_unlock(philo->death_lock);
|
|
||||||
return (death);
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* get_finished.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:13:56 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:13:56 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
int get_finished(t_rules *rules)
|
|
||||||
{
|
|
||||||
int finished;
|
|
||||||
|
|
||||||
pthread_mutex_lock(rules->finished_lock);
|
|
||||||
finished = rules->finished;
|
|
||||||
pthread_mutex_unlock(rules->finished_lock);
|
|
||||||
return (finished);
|
|
||||||
}
|
|
||||||
@ -18,10 +18,10 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (FAILURE == parse_arguments(argc, argv, &rules))
|
if (FAILURE == parse_arguments(argc, argv, &rules))
|
||||||
return (printf(ERROR_USAGE), EXIT_FAILURE);
|
return (printf(ERROR_USAGE), EXIT_FAILURE);
|
||||||
if (FAILURE == create_philos(&rules))
|
|
||||||
return (printf(ERROR_MALLOC), EXIT_FAILURE);
|
|
||||||
if (FAILURE == create_mutexes(&rules))
|
if (FAILURE == create_mutexes(&rules))
|
||||||
return (printf(ERROR_MUTEX), EXIT_FAILURE);
|
return (printf(ERROR_MUTEX), EXIT_FAILURE);
|
||||||
|
if (FAILURE == create_philos(&rules))
|
||||||
|
return (printf(ERROR_MALLOC), EXIT_FAILURE);
|
||||||
rules.start_time = get_time() + START_DELAY;
|
rules.start_time = get_time() + START_DELAY;
|
||||||
pthread_mutex_lock(rules.finished_lock);
|
pthread_mutex_lock(rules.finished_lock);
|
||||||
if (FAILURE == create_threads(&rules))
|
if (FAILURE == create_threads(&rules))
|
||||||
|
|||||||
@ -1,31 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* monitor_routine.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:13:59 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:13:59 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
|
|
||||||
void *monitor_routine(void *arg)
|
|
||||||
{
|
|
||||||
t_rules *rules;
|
|
||||||
|
|
||||||
rules = (t_rules *)arg;
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (!check_philos(rules))
|
|
||||||
{
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
usleep(500);
|
|
||||||
}
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* philo_die.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:14:00 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:14:00 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
void philo_die(t_philo *philo)
|
|
||||||
{
|
|
||||||
print_status(philo, "died");
|
|
||||||
pthread_mutex_lock(philo->death_lock);
|
|
||||||
philo->death = 1;
|
|
||||||
pthread_mutex_unlock(philo->death_lock);
|
|
||||||
set_finished(philo->rules);
|
|
||||||
pthread_mutex_unlock(philo->l_fork);
|
|
||||||
pthread_mutex_unlock(philo->r_fork);
|
|
||||||
}
|
|
||||||
@ -1,39 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* philo_eat.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:14:01 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:14:01 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
int philo_eat(t_philo *philo)
|
|
||||||
{
|
|
||||||
if (philo->id % 2 == 1)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(philo->r_fork);
|
|
||||||
print_status(philo, "has taken a fork");
|
|
||||||
}
|
|
||||||
pthread_mutex_lock(philo->l_fork);
|
|
||||||
print_status(philo, "has taken a fork");
|
|
||||||
if (philo->id % 2 != 1)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(philo->r_fork);
|
|
||||||
print_status(philo, "has taken a fork");
|
|
||||||
}
|
|
||||||
if (get_finished(philo->rules))
|
|
||||||
return (FAILURE);
|
|
||||||
print_status(philo, "is eating");
|
|
||||||
pthread_mutex_lock(philo->last_meal_lock);
|
|
||||||
philo->last_meal = get_time();
|
|
||||||
pthread_mutex_unlock(philo->last_meal_lock);
|
|
||||||
pthread_mutex_lock(philo->n_eat_lock);
|
|
||||||
philo->n_eat++;
|
|
||||||
pthread_mutex_unlock(philo->n_eat_lock);
|
|
||||||
return (SUCCESS);
|
|
||||||
}
|
|
||||||
@ -1,26 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* philo_sleep.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:14:02 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:14:02 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
int philo_sleep(t_philo *philo, int time)
|
|
||||||
{
|
|
||||||
const int start = get_time();
|
|
||||||
|
|
||||||
while (get_time() - start < time)
|
|
||||||
{
|
|
||||||
if (philo->death)
|
|
||||||
return (FAILURE);
|
|
||||||
usleep(50);
|
|
||||||
}
|
|
||||||
return (SUCCESS);
|
|
||||||
}
|
|
||||||
@ -1,21 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* set_death.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:14:03 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:14:03 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
|
|
||||||
void set_death(t_philo *philo)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(philo->death_lock);
|
|
||||||
philo->death = 1;
|
|
||||||
pthread_mutex_unlock(philo->death_lock);
|
|
||||||
}
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* set_finished.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:14:04 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:14:04 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
void set_finished(t_rules *rules)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(rules->finished_lock);
|
|
||||||
rules->finished = 1;
|
|
||||||
pthread_mutex_unlock(rules->finished_lock);
|
|
||||||
}
|
|
||||||
@ -1,27 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: o_ :::::: ::: */
|
|
||||||
/* synchronize_philos.c :+: / :+::+: :+: */
|
|
||||||
/* +:+ > +:++:+ +:+ */
|
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
||||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
||||||
/* Created: 2025/01/27 14:14:04 by whaffman #+#+# #+#+# #+# #+# | */
|
|
||||||
/* Updated: 2025/01/27 14:14:04 by whaffman ### ### ### ### / \ */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "philo.h"
|
|
||||||
|
|
||||||
void synchronize_philos(t_philo *philo)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(philo->rules->finished_lock);
|
|
||||||
pthread_mutex_unlock(philo->rules->finished_lock);
|
|
||||||
pthread_mutex_lock(philo->last_meal_lock);
|
|
||||||
philo->last_meal = get_time();
|
|
||||||
pthread_mutex_unlock(philo->last_meal_lock);
|
|
||||||
if (philo->id % 2 == 0)
|
|
||||||
{
|
|
||||||
print_status(philo, "is thinking");
|
|
||||||
usleep(philo->rules->time_to_eat * 10);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user