aboutsummaryrefslogtreecommitdiff
path: root/philo_one
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-27 10:43:43 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-27 10:43:43 +0200
commitf0a36076950bf0c3356ad73382ce3d341cdb0463 (patch)
tree5678bcfa57c94abb918b3d53b8e3ccd28a9c770a /philo_one
parent3c2c3a07396c001f51eac8d0fd0fc84e83eb127b (diff)
downloadphilosophers-f0a36076950bf0c3356ad73382ce3d341cdb0463.tar.gz
philosophers-f0a36076950bf0c3356ad73382ce3d341cdb0463.tar.bz2
philosophers-f0a36076950bf0c3356ad73382ce3d341cdb0463.zip
Added multiple meal, Removing bloat
Diffstat (limited to 'philo_one')
-rw-r--r--philo_one/Makefile17
-rw-r--r--philo_one/common.c20
-rw-r--r--philo_one/forks.c14
-rw-r--r--philo_one/helper.c23
-rw-r--r--philo_one/io.c21
-rw-r--r--philo_one/main.c2
-rw-r--r--philo_one/philo.c32
-rw-r--r--philo_one/philo_one.h13
-rw-r--r--philo_one/routine.c16
9 files changed, 66 insertions, 92 deletions
diff --git a/philo_one/Makefile b/philo_one/Makefile
index 8b1213b..5fe57d5 100644
--- a/philo_one/Makefile
+++ b/philo_one/Makefile
@@ -6,14 +6,14 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2020/04/22 13:02:38 by charles ### ########.fr #
+# Updated: 2020/09/27 10:35:32 by charles ### ########.fr #
# #
# **************************************************************************** #
RM = rm -f
CC = gcc
-CCFLAGS = -Wall -Wextra -Werror
+CCFLAGS = -Wall -Wextra #-Werror
LDFLAGS = -lpthread
NAME = philo_one
@@ -26,14 +26,19 @@ SRC = main.c \
common.c \
helper.c
-OBJ = $(SRC:.c=.o)
+OBJDIR = obj
-all: $(NAME)
+OBJ = $(SRC:%.c=$(OBJDIR)/%.o)
+
+all: prebuild $(NAME)
+
+prebuild:
+ @mkdir -pv $(OBJDIR)
$(NAME): $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)
-%.o: %.c
+$(OBJDIR)/%.o: %.c
$(CC) $(CCFLAGS) -c -o $@ $<
clean:
@@ -43,3 +48,5 @@ fclean: clean
$(RM) $(NAME)
re: fclean all
+
+.PHONY: all prebuild clean fclean re
diff --git a/philo_one/common.c b/philo_one/common.c
index b9d6e9a..f67cd48 100644
--- a/philo_one/common.c
+++ b/philo_one/common.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
-/* Updated: 2020/04/22 13:22:51 by charles ### ########.fr */
+/* Updated: 2020/09/27 10:36:31 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,15 +16,17 @@ bool parse_args(t_philo_conf *philo_args, int argc, char **argv)
{
if (argc != 5 && argc != 6)
return (false);
- if ((philo_args->philo_num = h_atoi_strict_unsigned(argv[1])) == -1
- || (philo_args->timeout_death = h_atoi_strict_unsigned(argv[2])) == -1
- || (philo_args->timeout_eat = h_atoi_strict_unsigned(argv[3])) == -1
- || (philo_args->timeout_sleep = h_atoi_strict_unsigned(argv[4])) == -1)
- return (false);
- if (argc == 6
- && (philo_args->meal_num = h_atoi_strict_unsigned(argv[5])) == -1)
+ if ((philo_args->philo_num = h_atou_strict(argv[1])) == -1
+ || (philo_args->timeout_death = h_atou_strict(argv[2])) == -1
+ || (philo_args->timeout_eat = h_atou_strict(argv[3])) == -1
+ || (philo_args->timeout_sleep = h_atou_strict(argv[4])) == -1)
return (false);
+ if (argc == 6)
+ {
+ if ((philo_args->meal_num = h_atou_strict(argv[5])) == -1)
+ return (false);
+ }
else
- philo_args->meal_num = -1;
+ philo_args->meal_num = 1;
return (true);
}
diff --git a/philo_one/forks.c b/philo_one/forks.c
index 065c7c6..ebe5261 100644
--- a/philo_one/forks.c
+++ b/philo_one/forks.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:46:40 by cacharle #+# #+# */
-/* Updated: 2020/04/22 13:26:58 by charles ### ########.fr */
+/* Updated: 2020/09/27 09:26:12 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,8 +17,7 @@ pthread_mutex_t *forks_new(int num)
int i;
pthread_mutex_t *forks;
- if ((forks = (pthread_mutex_t*)malloc(
- sizeof(pthread_mutex_t) * num)) == NULL)
+ if ((forks = malloc(num * sizeof(pthread_mutex_t))) == NULL)
return (NULL);
i = -1;
while (++i < num)
@@ -40,15 +39,14 @@ void forks_destroy(pthread_mutex_t *forks, int num)
}
t_routine_arg *forks_dispatch(
- t_philo *philos,
- pthread_mutex_t *forks,
- t_philo_conf *conf)
+ t_philo *philos,
+ pthread_mutex_t *forks,
+ t_philo_conf *conf)
{
int i;
t_routine_arg *routine_args;
- if ((routine_args = (t_routine_arg*)malloc(
- sizeof(t_routine_arg) * conf->philo_num)) == NULL)
+ if ((routine_args = malloc(conf->philo_num * sizeof(t_routine_arg))) == NULL)
return (NULL);
i = -1;
while (++i < conf->philo_num)
diff --git a/philo_one/helper.c b/philo_one/helper.c
index cd4b8a0..2c3db3d 100644
--- a/philo_one/helper.c
+++ b/philo_one/helper.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
-/* Updated: 2020/04/22 11:57:08 by charles ### ########.fr */
+/* Updated: 2020/09/27 10:43:04 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,7 +22,7 @@ static int h_strlen(char *s)
return (counter);
}
-long int h_atoi_strict_unsigned(char *s)
+long int h_atou_strict(char *s)
{
long int num;
@@ -32,12 +32,16 @@ long int h_atoi_strict_unsigned(char *s)
while (*s >= '0' && *s <= '9')
{
num *= 10;
+ if (num > UINT_MAX)
+ return (-1);
num += *s - '0';
+ if (num > UINT_MAX)
+ return (-1);
s++;
}
if (*s != '\0')
return (-1);
- return num;
+ return (num);
}
void h_putnbr(unsigned long int num)
@@ -57,19 +61,6 @@ void h_putstr(char *s)
write(STDOUT_FILENO, s, h_strlen(s));
}
-void *h_calloc(int count, int size)
-{
- int i;
- void *ptr;
-
- if ((ptr = malloc(count * size)) == NULL)
- return (NULL);
- i = count * size;
- while (i-- > 0)
- ((unsigned char*)ptr)[i] = 0x0;
- return (ptr);
-}
-
t_time h_time_now(void)
{
struct timeval tv;
diff --git a/philo_one/io.c b/philo_one/io.c
index b4d9949..c91e9ab 100644
--- a/philo_one/io.c
+++ b/philo_one/io.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2020/04/22 13:37:40 by charles ### ########.fr */
+/* Updated: 2020/09/27 10:33:29 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -31,12 +31,19 @@ static void philo_put(int id, t_philo_event event)
void io_eat(t_routine_arg *arg)
{
- if (!arg->conf->all_alive)
- return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
- philo_put(arg->philo->id, EVENT_EAT);
- pthread_mutex_unlock(&arg->conf->mutex_stdout);
- usleep(arg->conf->timeout_eat * 1000);
+ int eat_counter;
+
+ eat_counter = 0;
+ while (eat_counter < arg->conf->meal_num)
+ {
+ if (!arg->conf->all_alive)
+ return ;
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
+ philo_put(arg->philo->id, EVENT_EAT);
+ pthread_mutex_unlock(&arg->conf->mutex_stdout);
+ usleep(arg->conf->timeout_eat * 1000);
+ eat_counter++;
+ }
}
void io_think(t_routine_arg *arg)
diff --git a/philo_one/main.c b/philo_one/main.c
index 4739c0b..b43c0fe 100644
--- a/philo_one/main.c
+++ b/philo_one/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */
-/* Updated: 2020/04/22 13:07:15 by charles ### ########.fr */
+/* Updated: 2020/09/27 10:36:40 by charles ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/philo_one/philo.c b/philo_one/philo.c
index 97a0616..c32dc76 100644
--- a/philo_one/philo.c
+++ b/philo_one/philo.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */
-/* Updated: 2020/02/15 01:01:51 by cacharle ### ########.fr */
+/* Updated: 2020/09/27 10:42:25 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,7 +19,7 @@ t_philo *philos_new(int num)
if (num < 0)
return (NULL);
- if ((philos = (t_philo*)h_calloc(num + 1, sizeof(t_philo))) == NULL)
+ if ((philos = malloc(num * sizeof(t_philo))) == NULL)
return (NULL);
i = -1;
while (++i < num)
@@ -30,14 +30,6 @@ t_philo *philos_new(int num)
return (philos);
}
-void philos_destroy(t_philo *philos, int num)
-{
- (void)num;
- if (philos == NULL)
- return ;
- free(philos);
-}
-
bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num)
{
int i;
@@ -47,7 +39,7 @@ bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num)
{
philos[i].alive = true;
if (pthread_create(&philos[i].thread, NULL,
- &routine_philo, (void*)(routine_args + i)) == -1)
+ (void *(*)(void*))routine_philo, (void*)(routine_args + i)) == -1)
return (false);
}
return (true);
@@ -67,21 +59,3 @@ void philos_join(t_philo *philos, int num)
}
}
}
-
-bool philos_starved(t_philo *philos, int num)
-{
- int i;
-
- i = -1;
- while (++i < num)
- {
- if (!philos[i].alive)
- {
- i = -1;
- while (++i < num)
- philos[i].alive = false;
- return (true);
- }
- }
- return (false);
-}
diff --git a/philo_one/philo_one.h b/philo_one/philo_one.h
index 7badd71..331db2f 100644
--- a/philo_one/philo_one.h
+++ b/philo_one/philo_one.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */
-/* Updated: 2020/04/22 13:26:06 by charles ### ########.fr */
+/* Updated: 2020/09/27 10:42:58 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,6 +18,7 @@
# include <stdlib.h>
# include <pthread.h>
# include <stdbool.h>
+# include <limits.h>
typedef long int t_time;
@@ -59,8 +60,6 @@ typedef struct s_routine_arg
pthread_mutex_t *mutex_stdout;
} t_routine_arg;
-typedef void (*t_philo_routine)(void *arg);
-
/*
** forks.c
*/
@@ -83,14 +82,13 @@ bool philos_start(
t_routine_arg *routine_args,
int num);
void philos_join(t_philo *philos, int num);
-bool philos_starved(t_philo *philos, int num);
/*
** routine.c
*/
-void *routine_philo(void *void_arg);
-void *routine_death(void *void_arg);
+void *routine_philo(t_routine_arg *arg);
+void *routine_death(t_routine_arg *arg);
/*
** io.c
@@ -114,11 +112,10 @@ bool parse_args(
** helper.c
*/
-long int h_atoi_strict_unsigned(char *s);
+long int h_atou_strict(char *s);
void h_putnbr(unsigned long num);
void h_putchar(char c);
void h_putstr(char *s);
-void *h_calloc(int count, int size);
t_time h_time_now(void);
#endif
diff --git a/philo_one/routine.c b/philo_one/routine.c
index 69952b1..d52fe2e 100644
--- a/philo_one/routine.c
+++ b/philo_one/routine.c
@@ -6,22 +6,20 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */
-/* Updated: 2020/04/22 13:39:48 by charles ### ########.fr */
+/* Updated: 2020/09/27 10:25:56 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-void *routine_philo(void *void_arg)
+void *routine_philo(t_routine_arg *arg)
{
- t_routine_arg *arg;
pthread_t thread_death;
- arg = (t_routine_arg*)void_arg;
if (!arg->conf->all_alive)
return (NULL);
arg->philo->time_last_eat = h_time_now();
- if (pthread_create(&thread_death, NULL, routine_death, arg) != 0)
+ if (pthread_create(&thread_death, NULL, (void *(*)(void*))routine_death, arg) != 0)
return (NULL);
io_think(arg);
while (arg->conf->all_alive)
@@ -39,18 +37,18 @@ void *routine_philo(void *void_arg)
return (NULL);
}
-void *routine_death(void *void_arg)
+void *routine_death(t_routine_arg *arg)
{
- t_routine_arg *arg;
t_time current;
- arg = (t_routine_arg*)void_arg;
current = h_time_now();
while (arg->conf->all_alive &&
current - arg->philo->time_last_eat < arg->conf->timeout_death)
current = h_time_now();
- io_die(arg);
+ if (!arg->conf->all_alive)
+ return (NULL);
pthread_mutex_lock(&arg->conf->mutex_all_alive);
+ io_die(arg);
arg->conf->all_alive = false;
pthread_mutex_unlock(&arg->conf->mutex_all_alive);
return (NULL);