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,24 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: 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 " \
#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_MUTEX "Can not create mutex"
#define ERROR_MALLOC "Malloc Failure"
#define ERROR_THREADS "Can not create threads"
typedef struct s_rules
{
@ -33,7 +44,6 @@ typedef struct s_rules
pthread_mutex_t *finished_lock;
pthread_mutex_t *forks;
struct s_philo *philos;
} t_rules;
typedef struct s_philo
@ -59,7 +69,7 @@ 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 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);

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"
int check_death(t_philo *philo)
{
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"
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);
return (FAILURE);
while (i < rules->n_philos)
{
if (pthread_mutex_init(&rules->forks[i], NULL))
return(FAILURE);
return (FAILURE);
i++;
}
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"
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]);

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

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"
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"
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"
int main(int argc, char *argv[])
@ -6,24 +18,20 @@ int main(int argc, char *argv[])
if (FAILURE == parse_arguments(argc, argv, &rules))
return (printf(ERROR_USAGE), EXIT_FAILURE);
printf("arguments parsed\n");
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);
}

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"
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))
@ -18,6 +29,7 @@ int parse_arguments(int argc,char *argv[], t_rules *rules)
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));

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"
void set_finished(t_rules *rules)
{
@ -15,6 +27,7 @@ int get_finished(t_rules *rules)
pthread_mutex_unlock(rules->finished_lock);
return (finished);
}
void philo_die(t_philo *philo)
{
print_status(philo, "died");
@ -27,7 +40,8 @@ void philo_die(t_philo *philo)
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);
@ -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))

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"
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"
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"
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++;
}
}

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