Browse Source

add: suport meals amount

master
narnaud 3 years ago
parent
commit
bcdbeda7ff
  1. 6
      philo_bonus/philo.c
  2. 4
      philo_bonus/philo.h
  3. 2
      philo_bonus/philo_utils.c
  4. 71
      philo_bonus/philosophers.h

6
philo_bonus/philo.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:03:42 by narnaud #+# #+# */
/* Updated: 2022/05/20 09:39:41 by narnaud ### ########.fr */
/* Updated: 2022/05/20 09:57:34 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,6 +27,8 @@ void *check_halt(void * p)
printf("%i %d died\n", time, philo->id);
exit(EXIT_SUCCESS);
}
if (philo->meals_done == philo->table->param[MEALS_AMOUNT])
exit(EXIT_SUCCESS);
}
}
@ -44,6 +46,7 @@ void philo_life(t_philo *philo)
philo->last_meal_time = gettime();
printf("%i %d is eating\n", gettime(), philo->id);
usleep(table->param[EAT_DURATION] * 1000);
philo->meals_done++;
sem_post(table->sptr);
sem_post(table->sptr);
printf("%i %d is sleeping\n", gettime(), philo->id);
@ -67,6 +70,7 @@ void init_philos(t_table *table)
table->pid[i] = fork();
table->philos[i].table = table;
table->philos[i].id = i;
table->philos[i].meals_done = 0;
if (!table->pid[i])
{
pthread_create(&checks, NULL, check_halt, table->philos + i);

4
philo_bonus/philo.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:03:01 by narnaud #+# #+# */
/* Updated: 2022/05/20 09:26:20 by narnaud ### ########.fr */
/* Updated: 2022/05/20 09:54:31 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,7 +40,7 @@ typedef struct s_table t_table;
typedef struct s_philo
{
int id;
size_t meals_done;
int meals_done;
int last_meal_time;
t_table *table;
} t_philo;

2
philo_bonus/philo_utils.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */
/* Updated: 2022/05/20 09:40:52 by narnaud ### ########.fr */
/* Updated: 2022/05/20 09:57:42 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */

71
philo_bonus/philosophers.h

@ -1,71 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philosophers.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:03:01 by narnaud #+# #+# */
/* Updated: 2022/05/16 14:03:20 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILOSOPHERS_H
# define PHILOSOPHERS_H
/*
Chaque philosophe doit être représenté par un thread.
Toutes les fourchettes sont au centre de la table.
Elles nont pas détat spécifique en mémoire, mais le nombre de fourchettes
disponibles est représenté par un sémaphore. Chaque philosophe est représenté
par un processus différent. Cependant, le processus principal ne doit pas être
un philosophe.
*/
# include <unistd.h>
# include <stdio.h>
# include <stdlib.h>
# include <pthread.h>
# include <string.h>
# include <errno.h>
# include <fcntl.h>
# include <signal.h>
# include <sys/wait.h>
# include <sys/time.h>
# include <sys/stat.h>
# include <semaphore.h>
typedef struct s_table t_table;
typedef struct s_philo
{
int id;
size_t meals_done;
size_t last_meal_time;
t_table *table;
} t_philo;
typedef struct s_table
{
size_t nb_of_philos;
size_t time_to_die;
size_t eating_time;
size_t sleeping_time;
size_t nb_of_meal;
int i;
sem_t *sptr;
sem_t *death;
t_philo *philos;
size_t time;
size_t start_time;
int *pid;
} t_table;
/***************************/
t_table *parsing(char **argv);
size_t gettime(void);
#endif
Loading…
Cancel
Save