aboutsummaryrefslogtreecommitdiff
path: root/philo_three
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-10-05 15:06:03 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-10-05 15:06:03 +0200
commit473cf5d7576744b679b7a504232b5ebd4b5d689f (patch)
tree6518e4fd26bf4584e5a2d72b9e1289473bce76da /philo_three
parenta237321ee53f44793ebc8b9db26b743f092b5e40 (diff)
downloadphilosophers-473cf5d7576744b679b7a504232b5ebd4b5d689f.tar.gz
philosophers-473cf5d7576744b679b7a504232b5ebd4b5d689f.tar.bz2
philosophers-473cf5d7576744b679b7a504232b5ebd4b5d689f.zip
Norming
Diffstat (limited to 'philo_three')
-rw-r--r--philo_three/Makefile2
-rw-r--r--philo_three/src/child.c9
-rw-r--r--philo_three/src/main.c26
-rw-r--r--philo_three/src/philo_three.h16
4 files changed, 23 insertions, 30 deletions
diff --git a/philo_three/Makefile b/philo_three/Makefile
index a9f4640..3687670 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: 2020/10/01 09:10:44 by cacharle ### ########.fr #
+# Updated: 2020/10/05 10:31:22 by cacharle ### ########.fr #
# #
# **************************************************************************** #
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index 5bc0129..2e9b566 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: 2020/10/01 09:11:29 by cacharle ### ########.fr */
+/* Updated: 2020/10/05 15:02:10 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -25,11 +25,10 @@ void *routine_death(t_philo *philo)
pid_t child_start(t_philo *philo)
{
- pid_t pid;
pthread_t thread_death;
+ pid_t pid;
- pid = fork();
- if (pid == -1)
+ if ((pid = fork()) == -1)
return (-1);
if (pid == 0)
{
@@ -50,5 +49,5 @@ pid_t child_start(t_philo *philo)
}
exit(0);
}
- return pid;
+ return (pid);
}
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index a6c6370..04b1069 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: 2020/10/01 09:34:55 by cacharle ### ########.fr */
+/* Updated: 2020/10/05 14:31:39 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,27 +23,25 @@ sem_t *philo_sem_create(char *name, int value)
return (sem);
}
-int main(int argc, char **argv)
+int main(int argc, char **argv)
{
t_philo_args args;
t_philo philo;
sem_t *forks;
sem_t *sem_stdout;
sem_t *sem_dead;
+ pid_t *pids;
+ int i;
if (!parse_args(&args, argc, argv))
return (1);
-
-
- if ((forks = philo_sem_create(PHILO_SEM_NAME, args.philo_num)) == NULL ||
- (sem_stdout = philo_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == NULL ||
- (sem_dead = philo_sem_create(PHILO_SEM_DEAD_NAME, 1)) == NULL)
+ if ((forks = philo_sem_create(PHILO_SEM_NAME, args.philo_num)) == NULL
+ || (sem_stdout = philo_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == NULL
+ || (sem_dead = philo_sem_create(PHILO_SEM_DEAD_NAME, 0)) == NULL)
return (1);
-
- pid_t *pids;
- pids = malloc(sizeof(pid_t) * args.philo_num);
-
- int i = -1;
+ if ((pids = malloc(sizeof(pid_t) * args.philo_num)) == NULL)
+ return (1);
+ i = -1;
while (++i < args.philo_num)
{
philo.conf = &args;
@@ -54,15 +52,11 @@ int main(int argc, char **argv)
return (1);
}
}
-
- sem_wait(sem_dead);
sem_wait(sem_dead);
-
i = -1;
while (++i < args.philo_num)
kill(pids[i], SIGKILL);
free(pids);
-
sem_close(forks);
sem_close(sem_stdout);
sem_close(sem_dead);
diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h
index c0afe78..e30a857 100644
--- a/philo_three/src/philo_three.h
+++ b/philo_three/src/philo_three.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */
-/* Updated: 2020/10/01 09:10:30 by cacharle ### ########.fr */
+/* Updated: 2020/10/05 15:05:26 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -25,7 +25,7 @@
# define PHILO_SEM_STDOUT_NAME "semaphore_philo_three_stdout"
# define PHILO_SEM_DEAD_NAME "semaphore_philo_three_dead"
-typedef struct
+typedef struct s_philo
{
t_philo_args *conf;
int id;
@@ -35,12 +35,12 @@ typedef struct
sem_t *sem_dead;
} t_philo;
-pid_t child_start(t_philo *arg);
+pid_t child_start(t_philo *arg);
-void event_take_fork(t_philo *arg);
-void event_eat(t_philo *arg);
-void event_think(t_philo *arg);
-void event_sleep(t_philo *arg);
-void event_die(t_philo *arg);
+void event_take_fork(t_philo *arg);
+void event_eat(t_philo *arg);
+void event_think(t_philo *arg);
+void event_sleep(t_philo *arg);
+void event_die(t_philo *arg);
#endif