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,52 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: 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>
# include <stdio.h> #include <stdio.h>
# include <unistd.h> #include <unistd.h>
# include <stdlib.h> #include <stdlib.h>
# include <string.h> #include <string.h>
# include <stddef.h> #include <stddef.h>
# include <sys/time.h> #include <sys/time.h>
#define SUCCESS 1
#define FAILURE 0
# define SUCCESS 1 #define START_DELAY 0
# define FAILURE 0
# define START_DELAY 500 #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_USAGE "Usage: ./philo number_of_philosophers time_to_die " \ #define ERROR_MUTEX "Can not create mutex"
"time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]\n" #define ERROR_MALLOC "Malloc Failure"
# define ERROR_MUTEX "Can not create mutex" #define ERROR_THREADS "Can not create threads"
# define ERROR_MALLOC "Malloc Failure"
# define ERROR_THREADS "Can not create threads"
typedef struct s_rules typedef struct s_rules
{ {
int n_philos; int n_philos;
int time_to_die; int time_to_die;
int time_to_eat; int time_to_eat;
int time_to_sleep; int time_to_sleep;
int n_must_eat; int n_must_eat;
int finished; int finished;
int start_time; int start_time;
pthread_mutex_t *print_lock; pthread_mutex_t *print_lock;
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
{ {
int id; int id;
pthread_t *pid; pthread_t *pid;
pthread_mutex_t *l_fork; pthread_mutex_t *l_fork;
pthread_mutex_t *r_fork; pthread_mutex_t *r_fork;
int death; int death;
int last_meal; int last_meal;
t_rules *rules; t_rules *rules;
int n_eat; int n_eat;
} t_philo; } t_philo;
// memset, printf, malloc, free, write, // memset, printf, malloc, free, write,
// usleep, gettimeofday, pthread_create, // usleep, gettimeofday, pthread_create,
@ -54,21 +64,21 @@ typedef struct s_philo
// pthread_mutex_destroy, pthread_mutex_lock, // pthread_mutex_destroy, pthread_mutex_lock,
// pthread_mutex_unlock // pthread_mutex_unlock
int ph_atoi(const char *nptr, int *res); int ph_atoi(const char *nptr, int *res);
int get_time(void); int get_time(void);
void free_philos(t_rules *rules); void free_philos(t_rules *rules);
int destroy_mutexes(t_rules *rules); int destroy_mutexes(t_rules *rules);
int create_philos(t_rules *rules); int create_philos(t_rules *rules);
int parse_arguments(int argc,char *argv[], t_rules *rules); int parse_arguments(int argc, char *argv[], t_rules *rules);
int create_mutexes(t_rules *rules); int create_mutexes(t_rules *rules);
int create_threads(t_rules *rules); int create_threads(t_rules *rules);
void print_status(t_philo *philo, char *status); void print_status(t_philo *philo, char *status);
void *philo_routine(void *philo); void *philo_routine(void *philo);
int join_threads(t_rules *rules); int join_threads(t_rules *rules);
void print_rules(t_rules *rules); void print_rules(t_rules *rules);
int check_death(t_philo *philo); int check_death(t_philo *philo);
void philo_die(t_philo *philo); 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);
int philo_eat(t_philo *philo); int philo_eat(t_philo *philo);
#endif #endif

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,20 +17,18 @@ 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);
while (i < rules->n_philos) while (i < rules->n_philos)
{ {
if (pthread_mutex_init(&rules->forks[i], NULL)) if (pthread_mutex_init(&rules->forks[i], NULL))
return(FAILURE); return (FAILURE);
i++; i++;
} }
return (SUCCESS); return (SUCCESS);
} }

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)
@ -8,7 +20,7 @@ int create_philos(t_rules *rules)
i = 0; i = 0;
rules->philos = malloc(rules->n_philos * sizeof(t_philo)); rules->philos = malloc(rules->n_philos * sizeof(t_philo));
if (!rules->philos) if (!rules->philos)
return(FAILURE); return (FAILURE);
while (i < rules->n_philos) while (i < rules->n_philos)
{ {
philo = &(rules->philos[i]); philo = &(rules->philos[i]);
@ -22,4 +34,4 @@ int create_philos(t_rules *rules)
i++; i++;
} }
return (SUCCESS); return (SUCCESS);
} }

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)
@ -17,4 +29,4 @@ int create_threads(t_rules *rules)
i++; i++;
} }
return (SUCCESS); return (SUCCESS);
} }

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)
@ -6,14 +18,14 @@ int destroy_mutexes(t_rules *rules)
i = 0; i = 0;
if (pthread_mutex_destroy(rules->finished_lock)) if (pthread_mutex_destroy(rules->finished_lock))
return(FAILURE); return (FAILURE);
if (pthread_mutex_destroy(rules->print_lock)) if (pthread_mutex_destroy(rules->print_lock))
return(FAILURE); return (FAILURE);
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(FAILURE); return (FAILURE);
i++; i++;
} }
return (SUCCESS); return (SUCCESS);
} }

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)
@ -6,4 +18,4 @@ void free_philos(t_rules *rules)
free(rules->forks); free(rules->forks);
free(rules->print_lock); free(rules->print_lock);
free(rules->finished_lock); free(rules->finished_lock);
} }

View File

@ -1,9 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: 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)
{ {
int i; int i;
t_philo *philo; t_philo *philo;
i = 0; i = 0;
while (i < rules->n_philos) while (i < rules->n_philos)
@ -14,4 +26,4 @@ int join_threads(t_rules *rules)
i++; i++;
} }
return (SUCCESS); return (SUCCESS);
} }

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[])
@ -5,25 +17,21 @@ int main(int argc, char *argv[])
t_rules rules; t_rules rules;
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,27 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: 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);
if (!ph_atoi(argv[1], &rules->n_philos))
return (FAILURE);
if (!ph_atoi(argv[2], &rules->time_to_die))
return (FAILURE); return (FAILURE);
if (!ph_atoi(argv[3], &rules->time_to_eat)) if (!ph_atoi(argv[1], &rules->n_philos))
return (FAILURE);
if (!ph_atoi(argv[2], &rules->time_to_die))
return (FAILURE);
if (!ph_atoi(argv[3], &rules->time_to_eat))
return (FAILURE); return (FAILURE);
if (!ph_atoi(argv[4], &rules->time_to_sleep)) if (!ph_atoi(argv[4], &rules->time_to_sleep))
return (FAILURE);
if (argc == 6 && !ph_atoi(argv[5], &rules->n_must_eat))
return (FAILURE); return (FAILURE);
rules->print_lock = malloc(sizeof(pthread_mutex_t)); if (argc == 6 && !ph_atoi(argv[5], &rules->n_must_eat))
return (FAILURE);
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));
if (!rules->forks) if (!rules->forks)
return (FAILURE); return (FAILURE);
return (SUCCESS); return (SUCCESS);
} }

View File

@ -1,21 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: 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)
{ {
pthread_mutex_lock(rules->finished_lock); pthread_mutex_lock(rules->finished_lock);
rules->finished = 1; rules->finished = 1;
pthread_mutex_unlock(rules->finished_lock); pthread_mutex_unlock(rules->finished_lock);
} }
int get_finished(t_rules *rules) int get_finished(t_rules *rules)
{ {
int finished; int finished;
pthread_mutex_lock(rules->finished_lock); pthread_mutex_lock(rules->finished_lock);
finished = rules->finished; finished = rules->finished;
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");
philo->death = 1; philo->death = 1;
@ -24,10 +37,11 @@ void philo_die(t_philo *philo)
pthread_mutex_unlock(philo->r_fork); pthread_mutex_unlock(philo->r_fork);
} }
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)
return (SUCCESS); return (SUCCESS);
@ -35,7 +49,7 @@ int philo_died_while_sleeping(t_philo *philo, int time)
} }
return (FAILURE); return (FAILURE);
} }
int philo_eat(t_philo *philo) int philo_eat(t_philo *philo)
{ {
if (philo->id % 2 == 1) if (philo->id % 2 == 1)
{ {
@ -44,11 +58,11 @@ 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");
} }
if (check_death(philo)) if (check_death(philo))
return (philo_die(philo), FAILURE); return (philo_die(philo), FAILURE);
print_status(philo, "is eating"); print_status(philo, "is eating");
@ -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)
while (!philo->death && !philo->rules->finished usleep(philo->rules->time_to_eat * 100);
&& (philo->n_eat < philo->rules->n_must_eat || philo->rules->n_must_eat == -1)) while (!philo->death && !philo->rules->finished
&& (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))
@ -83,4 +98,4 @@ void *philo_routine(void *arg)
print_status(philo, "is thinking"); print_status(philo, "is thinking");
} }
return (NULL); return (NULL);
} }

View File

@ -1,9 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: 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)
{ {
struct timeval time; struct timeval time;
gettimeofday(&time, NULL); gettimeofday(&time, NULL);
return (time.tv_sec * 1000 + time.tv_usec / 1000); return (time.tv_sec * 1000 + time.tv_usec / 1000);
} }

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);
} }