Browse Source

norminage

master
narnaud 3 years ago
parent
commit
c9484b90ef
  1. 2
      philo/philo.c
  2. 10
      philo/philo.h
  3. 2
      philo/philo_init.c
  4. 4
      philo/philo_life.c
  5. 16
      philo_bonus/philo.c
  6. 12
      philo_bonus/philo.h
  7. 10
      philo_bonus/philo_utils.c

2
philo/philo.c

@ -6,7 +6,7 @@
/* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */ /* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/19 00:36:57 by narnaud@stude #+# #+# */ /* Created: 2022/05/19 00:36:57 by narnaud@stude #+# #+# */
/* Updated: 2022/05/20 08:39:34 by narnaud ### ########.fr */ /* Updated: 2022/05/20 10:30:18 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

10
philo/philo.h

@ -6,7 +6,7 @@
/* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */ /* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/18 22:23:52 by narnaud@stude #+# #+# */ /* Created: 2022/05/18 22:23:52 by narnaud@stude #+# #+# */
/* Updated: 2022/05/19 00:42:59 by narnaud@stude ### ########.fr */ /* Updated: 2022/05/20 10:29:02 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -59,9 +59,9 @@ void invit_philos(t_philo *philos);
void *philos_life(void *r); void *philos_life(void *r);
// ---------------------------------- philo_utils // ---------------------------------- philo_utils
int safe_print(t_philo *philo, char *str); int safe_print(t_philo *philo, char *str);
int safe_wait(t_room *room, int start, int duration); int safe_wait(t_room *room, int start, int duration);
int room_clock(); int room_clock(void);
int mini_atoi(char *nbr); int mini_atoi(char *nbr);
#endif #endif

2
philo/philo_init.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */ /* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */
/* Updated: 2022/05/20 08:38:02 by narnaud ### ########.fr */ /* Updated: 2022/05/20 10:27:07 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

4
philo/philo_life.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */ /* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */
/* Updated: 2022/05/20 08:35:19 by narnaud ### ########.fr */ /* Updated: 2022/05/20 10:27:47 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,7 @@ static int eat(t_room *room, t_philo *philo)
{ {
pthread_mutex_lock(philo->fork); pthread_mutex_lock(philo->fork);
if (!safe_print(philo, "%d %d has taken a fork\n") if (!safe_print(philo, "%d %d has taken a fork\n")
|| !philo->fork_r) || !philo->fork_r)
return (1); return (1);
pthread_mutex_lock(philo->fork_r); pthread_mutex_lock(philo->fork_r);
safe_print(philo, "%d %d has taken a fork\n"); safe_print(philo, "%d %d has taken a fork\n");

16
philo_bonus/philo.c

@ -6,13 +6,13 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:03:42 by narnaud #+# #+# */ /* Created: 2022/05/16 14:03:42 by narnaud #+# #+# */
/* Updated: 2022/05/20 09:57:34 by narnaud ### ########.fr */ /* Updated: 2022/05/20 10:34:10 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
void *check_halt(void * p) void *check_halt(void *p)
{ {
int time; int time;
t_philo *philo; t_philo *philo;
@ -63,7 +63,6 @@ void init_philos(t_table *table)
amount = table->param[PHILO_AMOUNT]; amount = table->param[PHILO_AMOUNT];
table->pid = malloc(sizeof(pid_t) * amount); table->pid = malloc(sizeof(pid_t) * amount);
//memset(table->pid, 0, sizeof(pid_t) * amount);
i = 0; i = 0;
while (i < amount) while (i < amount)
{ {
@ -81,18 +80,19 @@ void init_philos(t_table *table)
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
static t_table *table; static t_table *table;
static int i = 0; static int i = 0;
int status; static int status;
sem_unlink("/chopsticks"); sem_unlink("/chopsticks");
sem_unlink("/death"); sem_unlink("/death");
if (argc < 5 || argc > 6) if (argc < 5 || argc > 6)
return (1); return (1);
table = parsing(argv + 1); table = parsing(argv + 1);
table->sptr = sem_open("/chopsticks", O_CREAT, 0664, table->param[PHILO_AMOUNT]); table->sptr = sem_open("/chopsticks", O_CREAT, 0664,
table->param[PHILO_AMOUNT]);
table->death = sem_open("/death", O_CREAT, 0664, 1); table->death = sem_open("/death", O_CREAT, 0664, 1);
init_philos(table); init_philos(table);
waitpid(-1, &status, 0); waitpid(-1, &status, 0);
@ -103,5 +103,5 @@ int main(int argc, char **argv)
} }
sem_unlink("/chopsticks"); sem_unlink("/chopsticks");
sem_unlink("/deaths"); sem_unlink("/deaths");
return(0); return (0);
} }

12
philo_bonus/philo.h

@ -6,12 +6,12 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:03:01 by narnaud #+# #+# */ /* Created: 2022/05/16 14:03:01 by narnaud #+# #+# */
/* Updated: 2022/05/20 09:54:31 by narnaud ### ########.fr */ /* Updated: 2022/05/20 10:38:02 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PHILOSOPHERS_H #ifndef PHILO_H
# define PHILOSOPHERS_H # define PHILO_H
# include <unistd.h> # include <unistd.h>
# include <stdio.h> # include <stdio.h>
@ -49,12 +49,12 @@ typedef struct s_table
{ {
int param[5]; int param[5];
int i; int i;
sem_t *sptr; sem_t *sptr;
sem_t *death; sem_t *death;
t_philo *philos; t_philo *philos;
int time; int time;
int *pid; int *pid;
} t_table; } t_table;
/***************************/ /***************************/

10
philo_bonus/philo_utils.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */ /* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */
/* Updated: 2022/05/20 09:57:42 by narnaud ### ########.fr */ /* Updated: 2022/05/20 10:36:06 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,7 +30,6 @@ t_table *parsing(char **argv)
t_table *table; t_table *table;
table = malloc(sizeof(t_table)); table = malloc(sizeof(t_table));
//ft_bzero(table, 0, sizeof(t_table));
table->param[PHILO_AMOUNT] = mini_atoi(argv[0]); table->param[PHILO_AMOUNT] = mini_atoi(argv[0]);
table->param[DIE_TIME] = mini_atoi(argv[1]); table->param[DIE_TIME] = mini_atoi(argv[1]);
table->param[EAT_DURATION] = mini_atoi(argv[2]); table->param[EAT_DURATION] = mini_atoi(argv[2]);
@ -41,18 +40,17 @@ t_table *parsing(char **argv)
table->param[MEALS_AMOUNT] = 2147483647; table->param[MEALS_AMOUNT] = 2147483647;
table->time = gettime(); table->time = gettime();
table->philos = malloc(sizeof(t_philo) * table->param[PHILO_AMOUNT]); table->philos = malloc(sizeof(t_philo) * table->param[PHILO_AMOUNT]);
//memset(table->philos, 0, sizeof(t_philo) * table->nb_of_philos);
return (table); return (table);
} }
int gettime(void) int gettime(void)
{ {
static struct timeval starttime = {0,0}; static struct timeval starttime = {0, 0};
struct timeval actualtime; struct timeval actualtime;
if(starttime.tv_sec == 0 && starttime.tv_usec == 0) if (starttime.tv_sec == 0 && starttime.tv_usec == 0)
gettimeofday(&starttime, NULL); gettimeofday(&starttime, NULL);
gettimeofday(&actualtime, NULL); gettimeofday(&actualtime, NULL);
return ((int)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000)\ return ((int)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000)
- (starttime.tv_sec * 1000 + starttime.tv_usec / 1000))); - (starttime.tv_sec * 1000 + starttime.tv_usec / 1000)));
} }

Loading…
Cancel
Save