Add all untracked files

This commit is contained in:
whaffman 2025-01-28 15:02:01 +01:00
parent a197fdc79c
commit 1ca5e59f8d
12 changed files with 296 additions and 43 deletions

44
.gitignore vendored
View File

@ -4,52 +4,10 @@
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.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/

43
philo/src/check_philos.c Normal file
View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

18
philo/src/ft_min.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

23
philo/src/get_death.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

23
philo/src/get_finished.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

24
philo/src/philo_die.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

39
philo/src/philo_eat.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

26
philo/src/philo_sleep.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

21
philo/src/set_death.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

20
philo/src/set_finished.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: 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);
}
}