fixed stdheaders, odd philo delay, synchro start

This commit is contained in:
whaffman 2025-01-20 15:56:59 +01:00
parent a01bfff67e
commit 4e45d4bace
15 changed files with 298 additions and 142 deletions

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* philo.h :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:34 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:34 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#ifndef PHILO_H #ifndef PHILO_H
#define PHILO_H #define PHILO_H
#include <pthread.h> #include <pthread.h>
@ -8,11 +20,10 @@
#include <stddef.h> #include <stddef.h>
#include <sys/time.h> #include <sys/time.h>
#define SUCCESS 1 #define SUCCESS 1
#define FAILURE 0 #define FAILURE 0
# define START_DELAY 500 #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" "time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]\n"
@ -33,7 +44,6 @@ typedef struct s_rules
pthread_mutex_t *finished_lock; pthread_mutex_t *finished_lock;
pthread_mutex_t *forks; pthread_mutex_t *forks;
struct s_philo *philos; struct s_philo *philos;
} t_rules; } t_rules;
typedef struct s_philo typedef struct s_philo

View File

@ -1,7 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* check_alive.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:43 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:35 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int check_death(t_philo *philo) int check_death(t_philo *philo)
{ {
return (get_time() - philo->last_meal >= philo->rules->time_to_die); return (get_time() - philo->last_meal >= philo->rules->time_to_die);
} }

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* create_mutexes.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:44 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:35 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int create_mutexes(t_rules *rules) int create_mutexes(t_rules *rules)
@ -5,12 +17,10 @@ int create_mutexes(t_rules *rules)
int i; int i;
i = 0; i = 0;
printf("creating mutexes\n");
if (pthread_mutex_init(rules->finished_lock, NULL)) if (pthread_mutex_init(rules->finished_lock, NULL))
return(printf("death mutex created"), FAILURE); return (FAILURE);
if (pthread_mutex_init(rules->print_lock, NULL)) if (pthread_mutex_init(rules->print_lock, NULL))
return(printf("print mutex created"), FAILURE); return (FAILURE);
printf("creating forks\n");
rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t)); rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t));
if (!rules->forks) if (!rules->forks)
return (FAILURE); return (FAILURE);

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* create_philos.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:45 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:36 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int create_philos(t_rules *rules) int create_philos(t_rules *rules)

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* create_threads.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:45 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:37 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int create_threads(t_rules *rules) int create_threads(t_rules *rules)

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* destroy_mutexes.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:46 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:38 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int destroy_mutexes(t_rules *rules) int destroy_mutexes(t_rules *rules)

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* free_philos.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:46 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:39 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
void free_philos(t_rules *rules) void free_philos(t_rules *rules)

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* join_threads.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:47 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:39 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int join_threads(t_rules *rules) int join_threads(t_rules *rules)

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* main.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:48 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:40 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -6,24 +18,20 @@ 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);
printf("arguments parsed\n");
if (FAILURE == create_mutexes(&rules)) if (FAILURE == create_mutexes(&rules))
return (printf(ERROR_MUTEX), EXIT_FAILURE); return (printf(ERROR_MUTEX), EXIT_FAILURE);
printf("mutexes created\n");
if (FAILURE == create_philos(&rules)) if (FAILURE == create_philos(&rules))
return (printf(ERROR_MALLOC), EXIT_FAILURE); return (printf(ERROR_MALLOC), EXIT_FAILURE);
printf("philos created\n");
print_rules(&rules);
rules.start_time = get_time() + START_DELAY; rules.start_time = get_time() + START_DELAY;
pthread_mutex_lock(rules.finished_lock);
if (FAILURE == create_threads(&rules)) if (FAILURE == create_threads(&rules))
return (printf(ERROR_THREADS), EXIT_FAILURE); return (printf(ERROR_THREADS), EXIT_FAILURE);
printf("threads created\n"); pthread_mutex_unlock(rules.finished_lock);
if (FAILURE == join_threads(&rules)) if (FAILURE == join_threads(&rules))
return (printf(ERROR_THREADS), EXIT_FAILURE); return (printf(ERROR_THREADS), EXIT_FAILURE);
printf("threads joined\n");
if (FAILURE == destroy_mutexes(&rules)) if (FAILURE == destroy_mutexes(&rules))
return (printf(ERROR_MUTEX), EXIT_FAILURE); return (printf(ERROR_MUTEX), EXIT_FAILURE);
printf("mutexes destroyed\n");
free_philos(&rules); free_philos(&rules);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View File

@ -1,9 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* parse_arguments.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:48 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:40 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int parse_arguments(int argc, char *argv[], t_rules *rules) int parse_arguments(int argc, char *argv[], t_rules *rules)
{ {
rules->n_must_eat = -1; rules->n_must_eat = -1;
printf("argc: %d\n", argc);
if (argc != 5 && argc != 6) if (argc != 5 && argc != 6)
return (FAILURE); return (FAILURE);
if (!ph_atoi(argv[1], &rules->n_philos)) if (!ph_atoi(argv[1], &rules->n_philos))
@ -18,6 +29,7 @@ int parse_arguments(int argc,char *argv[], t_rules *rules)
return (FAILURE); return (FAILURE);
rules->print_lock = malloc(sizeof(pthread_mutex_t)); rules->print_lock = malloc(sizeof(pthread_mutex_t));
rules->finished_lock = malloc(sizeof(pthread_mutex_t)); rules->finished_lock = malloc(sizeof(pthread_mutex_t));
rules->finished = 0;
if (!rules->print_lock || !rules->finished_lock) if (!rules->print_lock || !rules->finished_lock)
return (FAILURE); return (FAILURE);
rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t)); rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t));

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* philo_routine.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:49 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:41 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
void set_finished(t_rules *rules) void set_finished(t_rules *rules)
{ {
@ -15,6 +27,7 @@ int get_finished(t_rules *rules)
pthread_mutex_unlock(rules->finished_lock); pthread_mutex_unlock(rules->finished_lock);
return (finished); return (finished);
} }
void philo_die(t_philo *philo) void philo_die(t_philo *philo)
{ {
print_status(philo, "died"); print_status(philo, "died");
@ -27,6 +40,7 @@ void philo_die(t_philo *philo)
int philo_died_while_sleeping(t_philo *philo, int time) int philo_died_while_sleeping(t_philo *philo, int time)
{ {
const int start = get_time(); const int start = get_time();
while (get_time() - start < time) while (get_time() - start < time)
{ {
if (philo->last_meal - get_time() >= philo->rules->time_to_die) if (philo->last_meal - get_time() >= philo->rules->time_to_die)
@ -44,7 +58,7 @@ int philo_eat(t_philo *philo)
} }
pthread_mutex_lock(philo->l_fork); pthread_mutex_lock(philo->l_fork);
print_status(philo, "has taken a fork"); print_status(philo, "has taken a fork");
if (philo->id % 2 == 0) if (philo->id % 2 != 1)
{ {
pthread_mutex_lock(philo->r_fork); pthread_mutex_lock(philo->r_fork);
print_status(philo, "has taken a fork"); print_status(philo, "has taken a fork");
@ -62,13 +76,14 @@ void *philo_routine(void *arg)
t_philo *philo; t_philo *philo;
philo = (t_philo *)arg; philo = (t_philo *)arg;
pthread_mutex_lock(philo->rules->finished_lock);
while (get_time() < philo->rules->start_time) pthread_mutex_unlock(philo->rules->finished_lock);
usleep(50); if (philo->id % 2 == 1)
usleep(philo->rules->time_to_eat * 100);
while (!philo->death && !philo->rules->finished while (!philo->death && !philo->rules->finished
&& (philo->n_eat < philo->rules->n_must_eat || philo->rules->n_must_eat == -1)) && (philo->n_eat < philo->rules->n_must_eat
|| philo->rules->n_must_eat == -1))
{ {
if (check_death(philo)) if (check_death(philo))
return (philo_die(philo), NULL); return (philo_die(philo), NULL);
if (!philo_eat(philo)) if (!philo_eat(philo))

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* get_time.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:08 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:42 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int get_time(void) int get_time(void)

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ph_atoi.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:09 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:42 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
static int ph_isspace(int c) static int ph_isspace(int c)

View File

@ -1,31 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_rules.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:10 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:43 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
void print_rules(t_rules *rules) void print_rules(t_rules *rules)
{ {
int i;
printf("n_philos: %d\n", rules->n_philos); printf("n_philos: %d\n", rules->n_philos);
printf("time_to_die: %d\n", rules->time_to_die); printf("time_to_die: %d\n", rules->time_to_die);
printf("time_to_eat: %d\n", rules->time_to_eat); printf("time_to_eat: %d\n", rules->time_to_eat);
printf("time_to_sleep: %d\n", rules->time_to_sleep); printf("time_to_sleep: %d\n", rules->time_to_sleep);
printf("n_must_eat: %d\n", rules->n_must_eat); printf("n_must_eat: %d\n", rules->n_must_eat);
i = 0;
while (i < rules->n_philos)
{
printf("philo %d has id: %d\n", i, rules->philos[i].id);
i++;
}
i = 0;
while (i < rules->n_philos)
{
printf("fork %d has pointer:%p\n", i, &rules->forks[i]);
i++;
}
i = 0;
while (i < rules->n_philos)
{
printf("philo %d\n has l_fork %p and r_fork: %p\n", rules->philos[i].id, rules->philos[i].l_fork, rules->philos[i].r_fork);
i++;
}
} }

View File

@ -1,8 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_status.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:10 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:44 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
void print_status(t_philo *philo, char *status) void print_status(t_philo *philo, char *status)
{ {
pthread_mutex_lock(philo->rules->print_lock); pthread_mutex_lock(philo->rules->print_lock);
printf("%d %d %s\n", get_time() - philo->rules->start_time, philo->id, status); printf("%d %d %s\n",
get_time() - philo->rules->start_time,
philo->id,
status);
pthread_mutex_unlock(philo->rules->print_lock); pthread_mutex_unlock(philo->rules->print_lock);
} }