aboutsummaryrefslogtreecommitdiff
path: root/philo_two/src/main.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-04 12:20:25 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-04 12:20:25 +0100
commit907debbb7d1e7ccc4914805cfe4acbed92b82bcc (patch)
tree199d60e60d9d9b689062c5f08d6381c090127619 /philo_two/src/main.c
parent7e971b3fa805c40d02ac3e996ab1d181f9584866 (diff)
downloadphilosophers-907debbb7d1e7ccc4914805cfe4acbed92b82bcc.tar.gz
philosophers-907debbb7d1e7ccc4914805cfe4acbed92b82bcc.tar.bz2
philosophers-907debbb7d1e7ccc4914805cfe4acbed92b82bcc.zip
Added buffered output, Added waiting for all to be started
Diffstat (limited to 'philo_two/src/main.c')
-rw-r--r--philo_two/src/main.c17
1 files changed, 12 insertions, 5 deletions
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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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]);