From 907debbb7d1e7ccc4914805cfe4acbed92b82bcc Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 4 Jan 2021 12:20:25 +0100 Subject: Added buffered output, Added waiting for all to be started --- common/common.h | 3 ++- common/io.c | 44 +++++++++++++++++++++++++------------------ philo_one/Makefile | 2 +- philo_one/src/event.c | 17 ++++++++++------- philo_one/src/main.c | 3 ++- philo_one/src/philo.c | 8 ++++++-- philo_one/src/philo_one.h | 5 +++-- philo_one/src/routine.c | 11 ++++++----- philo_three/src/child.c | 9 ++++++--- philo_three/src/event.c | 4 +++- philo_three/src/main.c | 14 ++++++++++++-- philo_three/src/philo_three.h | 5 ++++- philo_two/src/event.c | 4 +++- philo_two/src/main.c | 17 ++++++++++++----- philo_two/src/philo_two.h | 3 ++- philo_two/src/routine.c | 11 ++++++----- 16 files changed, 104 insertions(+), 56 deletions(-) diff --git a/common/common.h b/common/common.h index d224264..09fdf79 100644 --- a/common/common.h +++ b/common/common.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:34:25 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 11:52:30 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +62,7 @@ void h_sleep(t_time sleep_time); void philo_put( size_t id, t_philo_event event, t_time initial_time); +void philo_put_flush(void); int h_err(int ret, const char *format, char *str); #endif diff --git a/common/io.c b/common/io.c index 5d50ccc..c01accd 100644 --- a/common/io.c +++ b/common/io.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ -/* Updated: 2021/01/03 14:02:21 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:10:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ static size_t st_strlen(char *s) { - int counter; + size_t counter; counter = 0; while (s[counter]) @@ -27,38 +27,46 @@ static char *st_nbrcpy(char *dst, long long int num) if (num > 9) dst = st_nbrcpy(dst, num / 10); dst[0] = num % 10 + '0'; - dst[1] = '\0'; return (dst + 1); } -static void st_strcat(char *dst, char *str) +static char *st_strcpy_end(char *dst, char *str) { - while (*dst != '\0') - dst++; while (*str != '\0') *dst++ = *str++; - *dst = '\0'; + return (dst); } +#define PHILO_PUT_BUF_SIZE 4048 + +static char g_buf[PHILO_PUT_BUF_SIZE + 256] = {'\0'}; +static char *g_curr = g_buf; + void philo_put(size_t id, t_philo_event event, t_time initial_time) { - static char buf[2048]; - buf[0] = '\0'; - st_nbrcpy(buf, h_time_now() - initial_time); - st_strcat(buf, " "); - st_nbrcpy(buf + st_strlen(buf), id); + g_curr = st_nbrcpy(g_curr, h_time_now() - initial_time); + g_curr = st_strcpy_end(g_curr, " "); + g_curr = st_nbrcpy(g_curr, id); if (event == EVENT_FORK) - st_strcat(buf, " has taken fork\n"); + g_curr = st_strcpy_end(g_curr, " has taken fork\n"); else if (event == EVENT_EAT) - st_strcat(buf, " is eating\n"); + g_curr = st_strcpy_end(g_curr, " is eating\n"); else if (event == EVENT_SLEEP) - st_strcat(buf, " is sleeping\n"); + g_curr = st_strcpy_end(g_curr, " is sleeping\n"); else if (event == EVENT_THINK) - st_strcat(buf, " is thinking\n"); + g_curr = st_strcpy_end(g_curr, " is thinking\n"); else if (event == EVENT_DIE) - st_strcat(buf, " died\n"); - write(STDOUT_FILENO, buf, st_strlen(buf)); + g_curr = st_strcpy_end(g_curr, " died\n"); + if (g_curr - g_buf > PHILO_PUT_BUF_SIZE) + philo_put_flush(); +} + +void philo_put_flush(void) +{ + write(STDOUT_FILENO, g_buf, g_curr - g_buf); + g_curr = g_buf; + g_buf[0] = '\0'; } int h_err(int ret, const char *format, char *str) diff --git a/philo_one/Makefile b/philo_one/Makefile index 0026516..6c6a97a 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2021/01/01 15:18:11 by charles ### ########.fr # +# Updated: 2021/01/04 10:23:35 by cacharle ### ########.fr # # # # **************************************************************************** # diff --git a/philo_one/src/event.c b/philo_one/src/event.c index 8ceab65..2e24772 100644 --- a/philo_one/src/event.c +++ b/philo_one/src/event.c @@ -6,21 +6,25 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:48:00 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:08:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -void event_take_fork(t_philo *arg, pthread_mutex_t *fork) +void event_take_fork(t_philo *arg) { if (philo_finished(arg->conf)) return ; - pthread_mutex_lock(fork); + pthread_mutex_lock(arg->fork_left); + pthread_mutex_lock(arg->fork_right); pthread_mutex_lock(&arg->conf->mutex_stdout); if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); + if (philo_finished(arg->conf)) + return ; + philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); } @@ -32,6 +36,8 @@ void event_eat(t_philo *arg) if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_EAT, arg->conf->initial_time); + if (arg->conf->philo_num < 30) + philo_put_flush(); pthread_mutex_unlock(&arg->conf->mutex_stdout); h_sleep(arg->conf->timeout_eat); } @@ -69,9 +75,6 @@ void event_die(t_philo *arg) if (philo_finished(arg->conf)) return ; pthread_mutex_lock(&arg->conf->mutex_stdout); - if (philo_finished(arg->conf)) - return ; - arg->conf->all_alive = false; philo_put(arg->id, EVENT_DIE, arg->conf->initial_time); - pthread_mutex_unlock(&arg->conf->mutex_stdout); + arg->conf->all_alive = false; } diff --git a/philo_one/src/main.c b/philo_one/src/main.c index 55e70d3..c1bbe1c 100644 --- a/philo_one/src/main.c +++ b/philo_one/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:57:04 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:04:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,6 +63,7 @@ int main(int argc, char **argv) } while (!philo_finished(&conf)) ; + philo_put_flush(); philos_detach(philos, conf.philo_num); forks_destroy(forks, conf.philo_num); pthread_mutex_destroy(&conf.mutex_stdout); diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 870857b..f86dfb6 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:49:45 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 10:42:53 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,8 @@ t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks) philos[i].conf = conf; philos[i].fork_left = forks + i % conf->philo_num; philos[i].fork_right = forks + (i + 1) % conf->philo_num; + pthread_mutex_init(&philos[i].mutex_start, NULL); + pthread_mutex_lock(&philos[i].mutex_start); } return (philos); } @@ -49,8 +51,10 @@ bool philos_start(t_philo *philos, long int num) pthread_detach(philos[i].thread); return (false); } - usleep(200); } + i = -1; + while (++i < num) + pthread_mutex_unlock(&philos[i].mutex_start); return (true); } diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index de5f2c4..2ebe708 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:56:16 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 10:38:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ typedef struct s_philo pthread_mutex_t *fork_left; pthread_mutex_t *fork_right; pthread_mutex_t *mutex_stdout; + pthread_mutex_t mutex_start; } t_philo; /* @@ -81,7 +82,7 @@ void *routine_death(t_philo *arg); ** io.c */ -void event_take_fork(t_philo *arg, pthread_mutex_t *fork); +void event_take_fork(t_philo *arg); void event_eat(t_philo *arg); void event_think(t_philo *arg); void event_sleep( diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index d8291ef..c43d9c3 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:42:36 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 10:42:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,19 +35,20 @@ void *routine_philo(t_philo *arg) pthread_t thread_death; long int eat_counter; + pthread_mutex_lock(&arg->mutex_start); if (philo_finished(arg->conf)) return (NULL); + if (arg->id % 2 == 0) + usleep(1000); arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); eat_counter = 0; - event_think(arg); while (!philo_finished(arg->conf)) { - event_take_fork(arg, arg->fork_left); - event_take_fork(arg, arg->fork_right); - event_eat(arg); + event_take_fork(arg); arg->time_last_eat = h_time_now(); + event_eat(arg); eat_counter++; st_check_meal_num_finished(arg, eat_counter); event_sleep(arg, arg->fork_right, arg->fork_left); diff --git a/philo_three/src/child.c b/philo_three/src/child.c index e9c743b..6060cf7 100644 --- a/philo_three/src/child.c +++ b/philo_three/src/child.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:47:27 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 11:03:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,9 +34,8 @@ void st_child_loop(t_philo *philo) while (true) { event_take_fork(philo); - event_take_fork(philo); - event_eat(philo); philo->time_last_eat = h_time_now(); + event_eat(philo); eat_counter++; if (philo->conf->meal_num != -1 && eat_counter == philo->conf->meal_num) { @@ -61,9 +60,13 @@ pid_t child_start(t_philo *philo) philo->forks = sem_open(PHILO_SEM_NAME, 0); philo->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0); philo->sem_finish = sem_open(PHILO_SEM_FINISH_NAME, 0); + philo->sem_start = sem_open(PHILO_SEM_START_NAME, 0); philo->time_last_eat = h_time_now(); pthread_create(&thread_death, NULL, (t_routine)routine_death, philo); event_think(philo); + sem_wait(philo->sem_start); + if (philo->id % 2 == 0) + usleep(500); st_child_loop(philo); exit(0); } diff --git a/philo_three/src/event.c b/philo_three/src/event.c index 185147f..665f260 100644 --- a/philo_three/src/event.c +++ b/philo_three/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:56:51 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 10:57:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,11 @@ void event_take_fork(t_philo *philo) { + sem_wait(philo->forks); sem_wait(philo->forks); sem_wait(philo->sem_stdout); philo_put(philo->id, EVENT_FORK, philo->initial_time); + philo_put(philo->id, EVENT_FORK, philo->initial_time); sem_post(philo->sem_stdout); } diff --git a/philo_three/src/main.c b/philo_three/src/main.c index 8a5dec4..105903d 100644 --- a/philo_three/src/main.c +++ b/philo_three/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ -/* Updated: 2021/01/03 14:01:58 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 11:06:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,8 @@ static int st_destroy(t_sems *sems, pid_t *pids, int philo_num) sem_unlink(PHILO_SEM_STDOUT_NAME); sem_close(sems->sem_finish); sem_unlink(PHILO_SEM_FINISH_NAME); + sem_close(sems->sem_start); + sem_unlink(PHILO_SEM_START_NAME); return (1); } @@ -46,6 +48,7 @@ static int st_setup( sems->sem_stdout = SEM_FAILED; sems->sem_finish = SEM_FAILED; + sems->sem_start = SEM_FAILED; if ((sems->forks = st_sem_create(PHILO_SEM_NAME, args->philo_num)) == SEM_FAILED || (sems->sem_stdout = @@ -53,9 +56,14 @@ static int st_setup( || (sems->sem_finish = st_sem_create(PHILO_SEM_FINISH_NAME, args->meal_num == -1 ? 1 : args->philo_num)) == SEM_FAILED + || (sems->sem_start = + st_sem_create(PHILO_SEM_START_NAME, args->philo_num)) == SEM_FAILED || (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL) return (st_destroy(sems, *pids, 0)); i = -1; + while (++i < args->philo_num) + sem_wait(sems->sem_start); + i = -1; while (++i < args->philo_num) { philo.conf = args; @@ -63,8 +71,10 @@ static int st_setup( philo.initial_time = initial_time; if (((*pids)[i] = child_start(&philo)) == -1) return (st_destroy(sems, *pids, i)); - usleep(200); } + i = -1; + while (++i < args->philo_num) + sem_post(sems->sem_start); return (0); } diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h index 3d6b138..fe3441b 100644 --- a/philo_three/src/philo_three.h +++ b/philo_three/src/philo_three.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:51:54 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 11:01:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ # define PHILO_SEM_NAME "semaphore_philo_three" # define PHILO_SEM_STDOUT_NAME "semaphore_philo_three_stdout" # define PHILO_SEM_FINISH_NAME "semaphore_philo_three_finish" +# define PHILO_SEM_START_NAME "semaphore_philo_three_start" typedef struct s_philo { @@ -36,6 +37,7 @@ typedef struct s_philo sem_t *forks; sem_t *sem_stdout; sem_t *sem_finish; + sem_t *sem_start; } t_philo; typedef struct s_sems @@ -43,6 +45,7 @@ typedef struct s_sems sem_t *forks; sem_t *sem_stdout; sem_t *sem_finish; + sem_t *sem_start; } t_sems; pid_t child_start(t_philo *arg); diff --git a/philo_two/src/event.c b/philo_two/src/event.c index 25fcb0e..4eafd90 100644 --- a/philo_two/src/event.c +++ b/philo_two/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:53:35 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:15:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,11 @@ void event_take_fork(t_philo *arg) { + sem_wait(arg->forks); sem_wait(arg->forks); sem_wait(arg->conf->sem_stdout); philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); + philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); sem_post(arg->conf->sem_stdout); } diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 0f823b5..61f0410 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:54:45 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:14:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #define PHILO_SEM_NAME "semaphore_philo_two" #define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout" #define PHILO_SEM_FINISH_NAME "semaphore_philo_two_finish" +#define PHILO_SEM_START_NAME "semaphore_philo_two_start" static int st_destroy( t_philo *philos, @@ -23,6 +24,7 @@ static int st_destroy( sem_unlink(PHILO_SEM_NAME); sem_unlink(PHILO_SEM_STDOUT_NAME); sem_unlink(PHILO_SEM_FINISH_NAME); + sem_unlink(PHILO_SEM_START_NAME); free(philos); free(threads); return (1); @@ -46,16 +48,19 @@ static int st_setup( if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, forks) || !st_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) || !st_sem_create(PHILO_SEM_FINISH_NAME, - conf->meal_num == -1 ? 1 : conf->philo_num, &conf->sem_finish)) + conf->meal_num == -1 ? 1 : conf->philo_num, &conf->sem_finish) || + !st_sem_create(PHILO_SEM_START_NAME, conf->philo_num, &conf->sem_start)) return (1); *threads = NULL; if ((*philos = routine_create_philos(conf, *forks)) == NULL || (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL) return (st_destroy(*philos, *threads)); + i = -1; + while (++i < conf->philo_num) + sem_wait(conf->sem_start); conf->initial_time = h_time_now(); i = -1; while (++i < conf->philo_num) - { if (pthread_create(*threads + i, NULL, (t_routine)routine_philo, *philos + i) != 0) { @@ -63,8 +68,6 @@ static int st_setup( pthread_detach((*threads)[i]); return (st_destroy(*philos, *threads)); } - usleep(200); - } return (0); } @@ -102,7 +105,11 @@ int main(int argc, char **argv) return (0); if (st_setup(&conf, &philos, &forks, &threads) != 0) return (1); + i = -1; + while (++i < conf.philo_num) + sem_post(conf.sem_start); st_wait(&conf); + philo_put_flush(); i = -1; while (++i < conf.philo_num) pthread_detach(threads[i]); diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index 8261b0c..bde41c1 100644 --- a/philo_two/src/philo_two.h +++ b/philo_two/src/philo_two.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:55:04 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 09:35:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ typedef struct t_time initial_time; sem_t *sem_stdout; sem_t *sem_finish; + sem_t *sem_start; } t_philo_conf; typedef struct diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index 3c04817..d6e1df3 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2021/01/03 12:57:27 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:17:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,18 @@ void *routine_philo(t_philo *arg) long int eat_counter; pthread_t thread_death; + eat_counter = 0; + sem_wait(arg->conf->sem_start); + if (arg->id % 2 == 0) + usleep(500); arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); - eat_counter = 0; - event_think(arg); while (true) { event_take_fork(arg); - event_take_fork(arg); - event_eat(arg); arg->time_last_eat = h_time_now(); + event_eat(arg); if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num) { sem_wait(arg->conf->sem_stdout); -- cgit