diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-24 13:06:05 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-24 13:06:05 +0200 |
| commit | 2baa67fd7ad31fe53ab21d257a104478e787fb62 (patch) | |
| tree | ac38bdf7aecd7056e1b7dfdd51f5403547966380 /philo_two/src/main.c | |
| parent | 47e1a7b4af69e1998182126310e42af83cf214ed (diff) | |
| download | philosophers-2baa67fd7ad31fe53ab21d257a104478e787fb62.tar.gz philosophers-2baa67fd7ad31fe53ab21d257a104478e787fb62.tar.bz2 philosophers-2baa67fd7ad31fe53ab21d257a104478e787fb62.zip | |
Replacing mutex stdout by semaphore in philo two, Compilation on Linux
Diffstat (limited to 'philo_two/src/main.c')
| -rw-r--r-- | philo_two/src/main.c | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/philo_two/src/main.c b/philo_two/src/main.c index b86dc51..788122b 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,27 +6,30 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2020/10/05 16:23:19 by cacharle ### ########.fr */ +/* Updated: 2020/10/24 13:02:12 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" -#define PHILO_SEM_NAME "semaphore_philo_two" +#define PHILO_SEM_NAME "semaphore_philo_two" +#define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout" static int st_destroy( sem_t *forks, t_philo *philos, pthread_t *threads, - int philo_num) + t_philo_conf *conf) { int i; i = -1; - while (++i < philo_num) + while (++i < conf->philo_num) sem_post(forks); sem_close(forks); sem_unlink(PHILO_SEM_NAME); + sem_close(conf->sem_stdout); + sem_unlink(PHILO_SEM_STDOUT_NAME); free(philos); free(threads); return (1); @@ -44,11 +47,14 @@ static int st_setup( *forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0700, conf->philo_num); if (*forks == SEM_FAILED) return (1); + sem_unlink(PHILO_SEM_STDOUT_NAME); + conf->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, O_CREAT | O_EXCL, 0700, 1); + if (conf->sem_stdout == SEM_FAILED) + return (1); *threads = NULL; if ((*philos = routine_create_philos(conf, *forks)) == NULL || - (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL || - pthread_mutex_init(&conf->mutex_stdout, NULL) != 0) - return (st_destroy(*forks, *philos, *threads, conf->philo_num)); + (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL) + return (st_destroy(*forks, *philos, *threads, conf)); conf->all_alive = true; i = -1; while (++i < conf->philo_num) @@ -57,7 +63,7 @@ static int st_setup( { while (--i >= 0) pthread_detach((*threads)[i]); - return (st_destroy(*forks, *philos, *threads, conf->philo_num)); + return (st_destroy(*forks, *philos, *threads, conf)); } return (0); } @@ -81,6 +87,6 @@ int main(int argc, char **argv) i = -1; while (++i < conf.philo_num) pthread_detach(threads[i]); - st_destroy(forks, philos, threads, conf.philo_num); + st_destroy(forks, philos, threads, &conf); return (0); } |
