-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
copied mandatory to bonus folder for project setup
- Loading branch information
1 parent
048080d
commit 2981f24
Showing
9 changed files
with
599 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# **************************************************************************** # | ||
# # | ||
# ::: :::::::: # | ||
# Makefile :+: :+: :+: # | ||
# +:+ +:+ +:+ # | ||
# By: slathouw <[email protected]> +#+ +:+ +#+ # | ||
# +#+#+#+#+#+ +#+ # | ||
# Created: 2022/01/08 13:26:56 by slathouw #+# #+# # | ||
# Updated: 2022/01/10 18:33:27 by slathouw ### ########.fr # | ||
# # | ||
# **************************************************************************** # | ||
|
||
############# | ||
# VARIABLES # | ||
############# | ||
|
||
NAME = philo_bonus | ||
INCLUDES= includes | ||
CC = gcc | ||
CFLAGS = -Wall -Wextra -Werror -g | ||
LIBFLAGS = -lpthread | ||
OBJDIR = obj | ||
|
||
# ADD "SRCS" FILES HERE | ||
SOURCES = death.c free_destroy.c philo.c utils.c utils2.c init.c life.c | ||
SRCDIR = srcs | ||
SRCS = ${addprefix $(SRCDIR)/, $(SOURCES)} | ||
OBJS = ${addprefix $(OBJDIR)/srcs_, $(SOURCES:.c=.o)} | ||
|
||
all : ${NAME} | ||
|
||
############### | ||
# COMPILATION # | ||
############### | ||
|
||
# PHILO linking compilation | ||
$(NAME) : $(OBJS) includes/philo.h | ||
@${CC} ${CFLAGS} -I ${INCLUDES} ${OBJS} $(LIBFLAGS) -o ${NAME} | ||
@echo "philo_bonus binary created!" | ||
|
||
# SRCS object compilation | ||
$(OBJDIR)/srcs_%.o: $(SRCDIR)/%.c | ||
@mkdir -p obj | ||
@${CC} ${CFLAGS} -I ${INCLUDES} -c $< -o $@ | ||
|
||
clean: | ||
@rm -f $(OBJS) | ||
@rm -rf $(OBJDIR) | ||
@echo "philo_bonus objects removed..." | ||
|
||
fclean: clean | ||
@rm -f $(NAME) | ||
@echo "philo_bonus binary removed..." | ||
|
||
|
||
re : fclean all | ||
|
||
.PHONY: all clean fclean re |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* philo_bonus.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: slathouw <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/01/08 13:31:43 by slathouw #+# #+# */ | ||
/* Updated: 2022/01/10 18:25:59 by slathouw ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef PHILO_BONUS_H | ||
# define PHILO_BONUS_H | ||
|
||
/* | ||
Memleak on detached threads: | ||
https://stackoverflow.com/questions/20893358/ | ||
a-detached-pthread-causes-memory-leaks | ||
*/ | ||
# include <string.h> | ||
# include <stdio.h> | ||
# include <stdlib.h> | ||
# include <unistd.h> | ||
# include <sys/time.h> | ||
# include <pthread.h> | ||
|
||
typedef struct s_philo t_philo; | ||
typedef struct s_dinner t_dinner; | ||
|
||
struct s_philo | ||
{ | ||
int id; | ||
time_t tstamp_last_meal; | ||
int n_meals; | ||
int dead; | ||
pthread_mutex_t *l_fork; | ||
pthread_mutex_t *r_fork; | ||
pthread_mutex_t mealtime_lock; | ||
t_dinner *d; | ||
}; | ||
|
||
struct s_dinner | ||
{ | ||
int n_philos; | ||
int t_to_die; | ||
int t_to_eat; | ||
int t_to_sleep; | ||
time_t tstamp_start; | ||
int finished; | ||
int min_n_meals; | ||
pthread_mutex_t *fork_arr; | ||
pthread_mutex_t printer; | ||
t_philo *philo_arr; | ||
pthread_t *life_threads; | ||
}; | ||
|
||
/*UTILS.c*/ | ||
time_t get_tstamp(void); | ||
int ft_atoi(const char *str); | ||
int ft_is_nbr(char *str); | ||
int err(const char *s); | ||
void ft_bzero(void *s, size_t n); | ||
|
||
/*UTILS2.c*/ | ||
void print_action(t_philo *p, time_t ts, const char *s); | ||
void carefully_oversleep(int ms); | ||
|
||
/*INIT.c*/ | ||
int init_dinner(t_dinner *d, int ac, char **av); | ||
int init_mutex(t_dinner *d); | ||
int init_philos(t_dinner *dinner); | ||
int init_threads(t_dinner *d); | ||
|
||
/*LIFE.c*/ | ||
void take_forks(t_philo *p); | ||
void eat(t_philo *p); | ||
void go_to_sleep(t_philo *p); | ||
void think(t_philo *p); | ||
void *life(void *philosopher); | ||
|
||
/*DEATH.c*/ | ||
void kill_philo(t_philo *p); | ||
void *reap_death(void *phil_arr); | ||
int all_finished_eating(t_philo *phil_arr); | ||
|
||
/*FREE_DESTROY.c*/ | ||
void destroy_mutex(t_dinner *d); | ||
void free_all(t_dinner *d); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* death.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: slathouw <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/01/09 10:31:04 by slathouw #+# #+# */ | ||
/* Updated: 2022/01/10 18:31:48 by slathouw ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "philo_bonus.h" | ||
|
||
void kill_philo(t_philo *p); | ||
void *reap_death(void *phil_arr); | ||
int all_finished_eating(t_philo *phil_arr); | ||
|
||
void kill_philo(t_philo *p) | ||
{ | ||
p->dead = 1; | ||
p->d->finished = 1; | ||
print_action(p, get_tstamp(), "has died"); | ||
} | ||
|
||
int all_finished_eating(t_philo *phil_arr) | ||
{ | ||
int flag_finished_eating; | ||
int i; | ||
t_dinner *d; | ||
|
||
d = phil_arr[0].d; | ||
if (d->min_n_meals < 0) | ||
return (0); | ||
flag_finished_eating = 1; | ||
i = -1; | ||
while (++i < d->n_philos) | ||
if (phil_arr[i].n_meals < d->min_n_meals) | ||
flag_finished_eating = 0; | ||
if (flag_finished_eating) | ||
d->finished = 1; | ||
return (flag_finished_eating); | ||
} | ||
|
||
void *reap_death(void *phil_arr) | ||
{ | ||
t_philo *philos; | ||
time_t now; | ||
int i; | ||
t_dinner *d; | ||
|
||
philos = (t_philo *)phil_arr; | ||
d = philos[0].d; | ||
while (!d->finished) | ||
{ | ||
carefully_oversleep(3); | ||
i = -1; | ||
while (++i < d->n_philos) | ||
{ | ||
now = get_tstamp(); | ||
pthread_mutex_lock(&philos[i].mealtime_lock); | ||
if (now - philos[i].tstamp_last_meal > d->t_to_die) | ||
{ | ||
kill_philo(&philos[i]); | ||
return (NULL); | ||
} | ||
pthread_mutex_unlock(&philos[i].mealtime_lock); | ||
} | ||
if (all_finished_eating(philos)) | ||
return (NULL); | ||
} | ||
return (NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* free_destroy.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: slathouw <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/01/09 10:27:51 by slathouw #+# #+# */ | ||
/* Updated: 2022/01/10 18:34:01 by slathouw ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "philo_bonus.h" | ||
|
||
void destroy_mutex(t_dinner *d); | ||
void free_all(t_dinner *d); | ||
|
||
void destroy_mutex(t_dinner *d) | ||
{ | ||
int i; | ||
|
||
i = -1; | ||
while (++i < d->n_philos) | ||
{ | ||
pthread_mutex_unlock(&d->philo_arr[i].mealtime_lock); | ||
pthread_mutex_destroy(&d->philo_arr[i].mealtime_lock); | ||
pthread_mutex_unlock(&d->fork_arr[i]); | ||
pthread_mutex_destroy(&d->fork_arr[i]); | ||
} | ||
pthread_mutex_unlock(&d->printer); | ||
pthread_mutex_destroy(&d->printer); | ||
} | ||
|
||
void free_all(t_dinner *d) | ||
{ | ||
if (d->fork_arr) | ||
free(d->fork_arr); | ||
if (d->philo_arr) | ||
free(d->philo_arr); | ||
if (d->life_threads) | ||
free(d->life_threads); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* init.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: slathouw <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/01/09 10:20:57 by slathouw #+# #+# */ | ||
/* Updated: 2022/01/10 18:32:01 by slathouw ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "philo_bonus.h" | ||
|
||
int init_dinner(t_dinner *d, int ac, char **av); | ||
int init_mutex(t_dinner *d); | ||
int init_philos(t_dinner *dinner); | ||
int init_threads(t_dinner *d); | ||
|
||
int init_dinner(t_dinner *d, int ac, char **av) | ||
{ | ||
ft_bzero(d, sizeof(t_dinner)); | ||
d->n_philos = ft_atoi(av[1]); | ||
d->t_to_die = ft_atoi(av[2]); | ||
d->t_to_eat = ft_atoi(av[3]); | ||
d->t_to_sleep = ft_atoi(av[4]); | ||
d->finished = 0; | ||
if (ac == 5) | ||
d->min_n_meals = -1; | ||
else | ||
d->min_n_meals = ft_atoi(av[5]); | ||
return (1); | ||
} | ||
|
||
int init_mutex(t_dinner *d) | ||
{ | ||
int n_philos; | ||
pthread_mutex_t *forks; | ||
|
||
n_philos = d->n_philos; | ||
forks = malloc(sizeof(pthread_mutex_t) * n_philos); | ||
if (!forks) | ||
return (0); | ||
while (n_philos--) | ||
pthread_mutex_init(&forks[n_philos], NULL); | ||
pthread_mutex_init(&d->printer, NULL); | ||
d->fork_arr = forks; | ||
return (1); | ||
} | ||
|
||
int init_philos(t_dinner *dinner) | ||
{ | ||
int i; | ||
t_philo *phil_arr; | ||
|
||
i = -1; | ||
phil_arr = (t_philo *) malloc(sizeof(t_philo) * dinner->n_philos); | ||
if (!phil_arr) | ||
{ | ||
if (dinner->fork_arr) | ||
free(dinner->fork_arr); | ||
return (0); | ||
} | ||
while (++i < dinner->n_philos) | ||
{ | ||
ft_bzero(&phil_arr[i], sizeof(t_philo)); | ||
phil_arr[i].id = i; | ||
phil_arr[i].l_fork = &dinner->fork_arr[i]; | ||
phil_arr[i].r_fork = &dinner->fork_arr[(i + 1) % dinner->n_philos]; | ||
phil_arr[i].d = dinner; | ||
pthread_mutex_init(&phil_arr[i].mealtime_lock, NULL); | ||
} | ||
dinner->philo_arr = phil_arr; | ||
return (1); | ||
} | ||
|
||
int init_threads(t_dinner *d) | ||
{ | ||
int n_philos; | ||
pthread_t *life_threads; | ||
pthread_t grim_reaper; | ||
|
||
n_philos = d->n_philos; | ||
d->tstamp_start = get_tstamp(); | ||
life_threads = (pthread_t *) malloc(sizeof(pthread_t) * n_philos); | ||
if (!life_threads) | ||
{ | ||
free_all(d); | ||
return (0); | ||
} | ||
while (n_philos--) | ||
{ | ||
d->philo_arr[n_philos].tstamp_last_meal = get_tstamp(); | ||
pthread_create(&life_threads[n_philos], NULL, | ||
&life, (void *)&d->philo_arr[n_philos]); | ||
pthread_detach(life_threads[n_philos]); | ||
} | ||
pthread_create(&grim_reaper, NULL, &reap_death, (void *)d->philo_arr); | ||
pthread_join(grim_reaper, NULL); | ||
//reap_death(d->philo_arr); | ||
d->life_threads = life_threads; | ||
return (1); | ||
} |
Oops, something went wrong.