diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-08 17:57:23 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-08 17:57:23 +0100 |
| commit | 36c5e3a81de8b910bc04a37994b53296c1540353 (patch) | |
| tree | ed8ff37d914606ba3b4d6921956bd9b29cac0f17 | |
| parent | ad7ed73c124c8fcda6629350307f0f4f41b87fe3 (diff) | |
| download | philosophers-36c5e3a81de8b910bc04a37994b53296c1540353.tar.gz philosophers-36c5e3a81de8b910bc04a37994b53296c1540353.tar.bz2 philosophers-36c5e3a81de8b910bc04a37994b53296c1540353.zip | |
Added separate thread to flush buffer in philo_one and philo_two
| -rw-r--r-- | common/Makefile | 4 | ||||
| -rw-r--r-- | common/src/io.c | 5 | ||||
| -rw-r--r-- | philo_one/Makefile | 4 | ||||
| -rw-r--r-- | philo_one/src/event.c | 4 | ||||
| -rw-r--r-- | philo_one/src/main.c | 16 | ||||
| -rw-r--r-- | philo_one/src/philo.c | 6 | ||||
| -rw-r--r-- | philo_one/src/routine.c | 8 | ||||
| -rw-r--r-- | philo_three/Makefile | 4 | ||||
| -rw-r--r-- | philo_three/src/child.c | 5 | ||||
| -rw-r--r-- | philo_three/src/event.c | 7 | ||||
| -rw-r--r-- | philo_three/src/main.c | 2 | ||||
| -rw-r--r-- | philo_two/src/main.c | 16 | ||||
| -rw-r--r-- | philo_two/src/routine.c | 10 |
13 files changed, 66 insertions, 25 deletions
diff --git a/common/Makefile b/common/Makefile index e40fb30..fad09a8 100644 --- a/common/Makefile +++ b/common/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/09 22:39:08 by cacharle #+# #+# # -# Updated: 2021/01/08 14:32:37 by charles ### ########.fr # +# Updated: 2021/01/08 15:05:48 by charles ### ########.fr # # # # **************************************************************************** # @@ -19,7 +19,7 @@ OBJDIR = obj INCDIR = inc CC = gcc -CCFLAGS = -std=c99 -Wall -Wextra -Werror -O2 -I$(INCDIR) +CCFLAGS = -std=c99 -O2 -I$(INCDIR) -Wall -Wextra #-Werror NAME = libphilocommon.a diff --git a/common/src/io.c b/common/src/io.c index c01accd..ae624fb 100644 --- a/common/src/io.c +++ b/common/src/io.c @@ -6,7 +6,7 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ -/* Updated: 2021/01/04 12:10:14 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 15:19:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,14 +37,13 @@ static char *st_strcpy_end(char *dst, char *str) return (dst); } -#define PHILO_PUT_BUF_SIZE 4048 +#define PHILO_PUT_BUF_SIZE 20000 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) { - 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); diff --git a/philo_one/Makefile b/philo_one/Makefile index e976a50..c58aa6e 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2021/01/08 14:31:29 by charles ### ########.fr # +# Updated: 2021/01/08 14:44:00 by charles ### ########.fr # # # # **************************************************************************** # @@ -17,7 +17,7 @@ MAKE = make --no-print-directory COMMONDIR = ../common CC = gcc -CCFLAGS = -std=c99 -Wall -Wextra -Werror -O2 -I$(COMMONDIR)/inc +CCFLAGS = -std=c99 -O2 -I$(COMMONDIR)/inc -Wall -Wextra #-Werror LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon NAME = philo_one diff --git a/philo_one/src/event.c b/philo_one/src/event.c index 2e24772..025435f 100644 --- a/philo_one/src/event.c +++ b/philo_one/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/04 12:08:15 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 16:02:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,8 +36,6 @@ 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); } diff --git a/philo_one/src/main.c b/philo_one/src/main.c index c1bbe1c..b2dc5ed 100644 --- a/philo_one/src/main.c +++ b/philo_one/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2021/01/04 12:04:45 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 15:20:18 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,6 +42,17 @@ static int st_setup( return (0); } +void *routine_flush(t_philo_conf *conf) +{ + while (true) + { + pthread_mutex_lock(&conf->mutex_stdout); + philo_put_flush(); + pthread_mutex_unlock(&conf->mutex_stdout); + usleep(250000); + } +} + int main(int argc, char **argv) { t_philo_conf conf; @@ -54,6 +65,9 @@ int main(int argc, char **argv) return (0); if (st_setup(&conf, &philos, &forks) != 0) return (1); + pthread_t thread_flush; + pthread_create(&thread_flush, NULL, (t_routine)routine_flush, (void*)&conf); + pthread_detach(thread_flush); conf.initial_time = h_time_now(); if (!philos_start(philos, conf.philo_num)) { diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index f86dfb6..589e1c7 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2021/01/04 10:42:53 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 16:15:10 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,7 +54,11 @@ bool philos_start(t_philo *philos, long int num) } i = -1; while (++i < num) + { pthread_mutex_unlock(&philos[i].mutex_start); + /* usleep(1000); */ + } + return (true); } diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index c43d9c3..b3fc71a 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2021/01/04 10:42:48 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 16:14:57 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,8 +38,12 @@ void *routine_philo(t_philo *arg) pthread_mutex_lock(&arg->mutex_start); if (philo_finished(arg->conf)) return (NULL); - if (arg->id % 2 == 0) + if (arg->conf->philo_num % 2 == 0 && arg->id % 2 == 0) usleep(1000); + if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 0) + usleep(1000); + if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 1) + usleep(2000); arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); diff --git a/philo_three/Makefile b/philo_three/Makefile index da20c9d..cac5e51 100644 --- a/philo_three/Makefile +++ b/philo_three/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2021/01/08 14:32:09 by charles ### ########.fr # +# Updated: 2021/01/08 15:03:18 by charles ### ########.fr # # # # **************************************************************************** # @@ -17,7 +17,7 @@ MAKE = make --no-print-directory COMMON_DIR = ../common CC = gcc -CCFLAGS = -std=c99 -Wall -Wextra -Werror -O2 -I$(COMMON_DIR)/inc +CCFLAGS = -std=c99 -O2 -I$(COMMON_DIR)/inc -Wall -Wextra #-Werror LDFLAGS = -lpthread -L$(COMMON_DIR) -lphilocommon NAME = philo_three diff --git a/philo_three/src/child.c b/philo_three/src/child.c index 6060cf7..88a4b7c 100644 --- a/philo_three/src/child.c +++ b/philo_three/src/child.c @@ -6,7 +6,7 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */ -/* Updated: 2021/01/04 11:03:58 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 15:42:02 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,10 +63,9 @@ pid_t child_start(t_philo *philo) 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); + pthread_detach(thread_death); 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 665f260..2a4ccf7 100644 --- a/philo_three/src/event.c +++ b/philo_three/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/04 10:57:35 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 15:13:02 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ void event_take_fork(t_philo *philo) sem_wait(philo->sem_stdout); philo_put(philo->id, EVENT_FORK, philo->initial_time); philo_put(philo->id, EVENT_FORK, philo->initial_time); + philo_put_flush(); sem_post(philo->sem_stdout); } @@ -26,6 +27,7 @@ void event_eat(t_philo *philo) { sem_wait(philo->sem_stdout); philo_put(philo->id, EVENT_EAT, philo->initial_time); + philo_put_flush(); sem_post(philo->sem_stdout); h_sleep(philo->conf->timeout_eat); } @@ -34,6 +36,7 @@ void event_think(t_philo *philo) { sem_wait(philo->sem_stdout); philo_put(philo->id, EVENT_THINK, philo->initial_time); + philo_put_flush(); sem_post(philo->sem_stdout); } @@ -41,6 +44,7 @@ void event_sleep(t_philo *philo) { sem_wait(philo->sem_stdout); philo_put(philo->id, EVENT_SLEEP, philo->initial_time); + philo_put_flush(); sem_post(philo->sem_stdout); sem_post(philo->forks); sem_post(philo->forks); @@ -53,6 +57,7 @@ void event_die(t_philo *philo) sem_wait(philo->sem_stdout); philo_put(philo->id, EVENT_DIE, philo->initial_time); + philo_put_flush(); if (philo->conf->meal_num == -1) sem_post(philo->sem_finish); else diff --git a/philo_three/src/main.c b/philo_three/src/main.c index 105903d..0b3397c 100644 --- a/philo_three/src/main.c +++ b/philo_three/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ -/* Updated: 2021/01/04 11:06:41 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 15:11:05 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 61f0410..2d5c003 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2021/01/04 12:14:57 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 16:32:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,17 @@ static bool st_sem_create(const char *name, unsigned int value, sem_t **sem) != SEM_FAILED); } +void *routine_flush(t_philo_conf *conf) +{ + while (true) + { + sem_wait(conf->sem_stdout); + philo_put_flush(); + sem_post(conf->sem_stdout); + usleep(250000); + } +} + static int st_setup( t_philo_conf *conf, t_philo **philos, @@ -105,6 +116,9 @@ int main(int argc, char **argv) return (0); if (st_setup(&conf, &philos, &forks, &threads) != 0) return (1); + pthread_t thread_flush; + pthread_create(&thread_flush, NULL, (t_routine)routine_flush, (void*)&conf); + pthread_detach(thread_flush); i = -1; while (++i < conf.philo_num) sem_post(conf.sem_start); diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index d6e1df3..d69488d 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2021/01/04 12:17:48 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 16:31:27 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,12 @@ void *routine_philo(t_philo *arg) eat_counter = 0; sem_wait(arg->conf->sem_start); - if (arg->id % 2 == 0) - usleep(500); + /* if (arg->conf->philo_num % 2 == 0 && arg->id % 2 == 0) */ + /* usleep(1000); */ + /* if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 0) */ + /* usleep(1000); */ + /* if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 1) */ + /* usleep(500); */ arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); |
