fixed stdheaders, odd philo delay, synchro start
This commit is contained in:
parent
a01bfff67e
commit
4e45d4bace
@ -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
|
||||
# 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>
|
||||
|
||||
#define SUCCESS 1
|
||||
#define FAILURE 0
|
||||
|
||||
# define SUCCESS 1
|
||||
# define FAILURE 0
|
||||
#define START_DELAY 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_MUTEX "Can not create mutex"
|
||||
# define ERROR_MALLOC "Malloc Failure"
|
||||
# define ERROR_THREADS "Can not create threads"
|
||||
#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"
|
||||
|
||||
typedef struct s_rules
|
||||
{
|
||||
int n_philos;
|
||||
int time_to_die;
|
||||
int time_to_eat;
|
||||
int time_to_sleep;
|
||||
int n_must_eat;
|
||||
int finished;
|
||||
int start_time;
|
||||
pthread_mutex_t *print_lock;
|
||||
pthread_mutex_t *finished_lock;
|
||||
pthread_mutex_t *forks;
|
||||
struct s_philo *philos;
|
||||
|
||||
} t_rules;
|
||||
int n_philos;
|
||||
int time_to_die;
|
||||
int time_to_eat;
|
||||
int time_to_sleep;
|
||||
int n_must_eat;
|
||||
int finished;
|
||||
int start_time;
|
||||
pthread_mutex_t *print_lock;
|
||||
pthread_mutex_t *finished_lock;
|
||||
pthread_mutex_t *forks;
|
||||
struct s_philo *philos;
|
||||
} t_rules;
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
int id;
|
||||
pthread_t *pid;
|
||||
pthread_mutex_t *l_fork;
|
||||
pthread_mutex_t *r_fork;
|
||||
int death;
|
||||
int last_meal;
|
||||
t_rules *rules;
|
||||
int n_eat;
|
||||
} t_philo;
|
||||
int id;
|
||||
pthread_t *pid;
|
||||
pthread_mutex_t *l_fork;
|
||||
pthread_mutex_t *r_fork;
|
||||
int death;
|
||||
int last_meal;
|
||||
t_rules *rules;
|
||||
int n_eat;
|
||||
} t_philo;
|
||||
|
||||
// memset, printf, malloc, free, write,
|
||||
// usleep, gettimeofday, pthread_create,
|
||||
@ -54,21 +64,21 @@ typedef struct s_philo
|
||||
// pthread_mutex_destroy, pthread_mutex_lock,
|
||||
// pthread_mutex_unlock
|
||||
|
||||
int ph_atoi(const char *nptr, int *res);
|
||||
int get_time(void);
|
||||
void free_philos(t_rules *rules);
|
||||
int destroy_mutexes(t_rules *rules);
|
||||
int create_philos(t_rules *rules);
|
||||
int parse_arguments(int argc,char *argv[], t_rules *rules);
|
||||
int create_mutexes(t_rules *rules);
|
||||
int create_threads(t_rules *rules);
|
||||
void print_status(t_philo *philo, char *status);
|
||||
void *philo_routine(void *philo);
|
||||
int join_threads(t_rules *rules);
|
||||
void print_rules(t_rules *rules);
|
||||
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);
|
||||
int ph_atoi(const char *nptr, int *res);
|
||||
int get_time(void);
|
||||
void free_philos(t_rules *rules);
|
||||
int destroy_mutexes(t_rules *rules);
|
||||
int create_philos(t_rules *rules);
|
||||
int parse_arguments(int argc, char *argv[], t_rules *rules);
|
||||
int create_mutexes(t_rules *rules);
|
||||
int create_threads(t_rules *rules);
|
||||
void print_status(t_philo *philo, char *status);
|
||||
void *philo_routine(void *philo);
|
||||
int join_threads(t_rules *rules);
|
||||
void print_rules(t_rules *rules);
|
||||
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);
|
||||
|
||||
#endif
|
||||
@ -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"
|
||||
|
||||
int check_death(t_philo *philo)
|
||||
{
|
||||
return (get_time() - philo->last_meal >= philo->rules->time_to_die);
|
||||
}
|
||||
|
||||
|
||||
@ -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"
|
||||
|
||||
int create_mutexes(t_rules *rules)
|
||||
@ -5,19 +17,17 @@ int create_mutexes(t_rules *rules)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
printf("creating mutexes\n");
|
||||
if (pthread_mutex_init(rules->finished_lock, NULL))
|
||||
return(printf("death mutex created"), FAILURE);
|
||||
return (FAILURE);
|
||||
if (pthread_mutex_init(rules->print_lock, NULL))
|
||||
return(printf("print mutex created"), FAILURE);
|
||||
printf("creating forks\n");
|
||||
return (FAILURE);
|
||||
rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t));
|
||||
if (!rules->forks)
|
||||
return(FAILURE);
|
||||
if (!rules->forks)
|
||||
return (FAILURE);
|
||||
while (i < rules->n_philos)
|
||||
{
|
||||
if (pthread_mutex_init(&rules->forks[i], NULL))
|
||||
return(FAILURE);
|
||||
return (FAILURE);
|
||||
i++;
|
||||
}
|
||||
return (SUCCESS);
|
||||
|
||||
@ -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"
|
||||
|
||||
int create_philos(t_rules *rules)
|
||||
@ -8,7 +20,7 @@ int create_philos(t_rules *rules)
|
||||
i = 0;
|
||||
rules->philos = malloc(rules->n_philos * sizeof(t_philo));
|
||||
if (!rules->philos)
|
||||
return(FAILURE);
|
||||
return (FAILURE);
|
||||
while (i < rules->n_philos)
|
||||
{
|
||||
philo = &(rules->philos[i]);
|
||||
|
||||
@ -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"
|
||||
|
||||
int create_threads(t_rules *rules)
|
||||
|
||||
@ -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"
|
||||
|
||||
int destroy_mutexes(t_rules *rules)
|
||||
@ -6,13 +18,13 @@ int destroy_mutexes(t_rules *rules)
|
||||
|
||||
i = 0;
|
||||
if (pthread_mutex_destroy(rules->finished_lock))
|
||||
return(FAILURE);
|
||||
return (FAILURE);
|
||||
if (pthread_mutex_destroy(rules->print_lock))
|
||||
return(FAILURE);
|
||||
return (FAILURE);
|
||||
while (i < rules->n_philos)
|
||||
{
|
||||
if (pthread_mutex_destroy(&rules->forks[i]))
|
||||
return(FAILURE);
|
||||
return (FAILURE);
|
||||
i++;
|
||||
}
|
||||
return (SUCCESS);
|
||||
|
||||
@ -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"
|
||||
|
||||
void free_philos(t_rules *rules)
|
||||
|
||||
@ -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"
|
||||
|
||||
int join_threads(t_rules *rules)
|
||||
{
|
||||
int i;
|
||||
t_philo *philo;
|
||||
int i;
|
||||
t_philo *philo;
|
||||
|
||||
i = 0;
|
||||
while (i < rules->n_philos)
|
||||
|
||||
@ -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"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
@ -5,25 +17,21 @@ int main(int argc, char *argv[])
|
||||
t_rules rules;
|
||||
|
||||
if (FAILURE == parse_arguments(argc, argv, &rules))
|
||||
return (printf(ERROR_USAGE), EXIT_FAILURE);
|
||||
printf("arguments parsed\n");
|
||||
return (printf(ERROR_USAGE), EXIT_FAILURE);
|
||||
if (FAILURE == create_mutexes(&rules))
|
||||
return (printf(ERROR_MUTEX), EXIT_FAILURE);
|
||||
printf("mutexes created\n");
|
||||
if (FAILURE == create_philos(&rules))
|
||||
return (printf(ERROR_MALLOC), EXIT_FAILURE);
|
||||
printf("philos created\n");
|
||||
print_rules(&rules);
|
||||
rules.start_time = get_time() + START_DELAY;
|
||||
pthread_mutex_lock(rules.finished_lock);
|
||||
if (FAILURE == create_threads(&rules))
|
||||
return (printf(ERROR_THREADS), EXIT_FAILURE);
|
||||
printf("threads created\n");
|
||||
pthread_mutex_unlock(rules.finished_lock);
|
||||
if (FAILURE == join_threads(&rules))
|
||||
return (printf(ERROR_THREADS), EXIT_FAILURE);
|
||||
printf("threads joined\n");
|
||||
|
||||
if (FAILURE == destroy_mutexes(&rules))
|
||||
return (printf(ERROR_MUTEX), EXIT_FAILURE);
|
||||
printf("mutexes destroyed\n");
|
||||
free_philos(&rules);
|
||||
return(EXIT_SUCCESS);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@ -1,23 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: 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"
|
||||
|
||||
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;
|
||||
printf("argc: %d\n", argc);
|
||||
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))
|
||||
if (argc != 5 && argc != 6)
|
||||
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);
|
||||
if (!ph_atoi(argv[4], &rules->time_to_sleep))
|
||||
return (FAILURE);
|
||||
if (argc == 6 && !ph_atoi(argv[5], &rules->n_must_eat))
|
||||
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 = 0;
|
||||
if (!rules->print_lock || !rules->finished_lock)
|
||||
return (FAILURE);
|
||||
rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t));
|
||||
|
||||
@ -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"
|
||||
void set_finished(t_rules *rules)
|
||||
void set_finished(t_rules *rules)
|
||||
{
|
||||
pthread_mutex_lock(rules->finished_lock);
|
||||
rules->finished = 1;
|
||||
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);
|
||||
finished = rules->finished;
|
||||
pthread_mutex_unlock(rules->finished_lock);
|
||||
return (finished);
|
||||
}
|
||||
void philo_die(t_philo *philo)
|
||||
|
||||
void philo_die(t_philo *philo)
|
||||
{
|
||||
print_status(philo, "died");
|
||||
philo->death = 1;
|
||||
@ -24,10 +37,11 @@ void philo_die(t_philo *philo)
|
||||
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();
|
||||
while(get_time() - start < time)
|
||||
|
||||
while (get_time() - start < time)
|
||||
{
|
||||
if (philo->last_meal - get_time() >= philo->rules->time_to_die)
|
||||
return (SUCCESS);
|
||||
@ -35,7 +49,7 @@ int philo_died_while_sleeping(t_philo *philo, int time)
|
||||
}
|
||||
return (FAILURE);
|
||||
}
|
||||
int philo_eat(t_philo *philo)
|
||||
int philo_eat(t_philo *philo)
|
||||
{
|
||||
if (philo->id % 2 == 1)
|
||||
{
|
||||
@ -44,7 +58,7 @@ int philo_eat(t_philo *philo)
|
||||
}
|
||||
pthread_mutex_lock(philo->l_fork);
|
||||
print_status(philo, "has taken a fork");
|
||||
if (philo->id % 2 == 0)
|
||||
if (philo->id % 2 != 1)
|
||||
{
|
||||
pthread_mutex_lock(philo->r_fork);
|
||||
print_status(philo, "has taken a fork");
|
||||
@ -62,13 +76,14 @@ void *philo_routine(void *arg)
|
||||
t_philo *philo;
|
||||
|
||||
philo = (t_philo *)arg;
|
||||
|
||||
while (get_time() < philo->rules->start_time)
|
||||
usleep(50);
|
||||
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))
|
||||
&& (philo->n_eat < philo->rules->n_must_eat
|
||||
|| philo->rules->n_must_eat == -1))
|
||||
{
|
||||
|
||||
if (check_death(philo))
|
||||
return (philo_die(philo), NULL);
|
||||
if (!philo_eat(philo))
|
||||
|
||||
@ -1,6 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: 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"
|
||||
|
||||
int get_time(void)
|
||||
int get_time(void)
|
||||
{
|
||||
struct timeval time;
|
||||
|
||||
|
||||
@ -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"
|
||||
|
||||
static int ph_isspace(int c)
|
||||
|
||||
@ -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"
|
||||
|
||||
void print_rules(t_rules *rules)
|
||||
void print_rules(t_rules *rules)
|
||||
{
|
||||
int i;
|
||||
|
||||
printf("n_philos: %d\n", rules->n_philos);
|
||||
printf("time_to_die: %d\n", rules->time_to_die);
|
||||
printf("time_to_eat: %d\n", rules->time_to_eat);
|
||||
printf("time_to_sleep: %d\n", rules->time_to_sleep);
|
||||
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++;
|
||||
}
|
||||
|
||||
}
|
||||
@ -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"
|
||||
|
||||
void print_status(t_philo *philo, char *status)
|
||||
void print_status(t_philo *philo, char *status)
|
||||
{
|
||||
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);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user