Browse Source

norminage

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

2
philo/philo.c

@ -6,7 +6,7 @@
/* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
// ---------------------------------- philo_utils
int safe_print(t_philo *philo, char *str);
int safe_wait(t_room *room, int start, int duration);
int room_clock();
int mini_atoi(char *nbr);
int safe_print(t_philo *philo, char *str);
int safe_wait(t_room *room, int start, int duration);
int room_clock(void);
int mini_atoi(char *nbr);
#endif

4
philo/philo_init.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
@ -39,7 +39,7 @@ t_philo *create_philos(t_room *room)
static int i;
int amount;
amount = room->param[PHILO_AMOUNT];
amount = room->param[PHILO_AMOUNT];
ret = (t_philo *)malloc((amount + 1) * sizeof(t_philo));
while (i < amount)
{

4
philo/philo_life.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
if (!safe_print(philo, "%d %d has taken a fork\n")
|| !philo->fork_r)
|| !philo->fork_r)
return (1);
pthread_mutex_lock(philo->fork_r);
safe_print(philo, "%d %d has taken a fork\n");

18
philo_bonus/philo.c

@ -6,18 +6,18 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
void *check_halt(void * p)
void *check_halt(void *p)
{
int time;
t_philo *philo;
philo = p;
philo = p;
while (1)
{
usleep(1000);
@ -63,7 +63,6 @@ void init_philos(t_table *table)
amount = table->param[PHILO_AMOUNT];
table->pid = malloc(sizeof(pid_t) * amount);
//memset(table->pid, 0, sizeof(pid_t) * amount);
i = 0;
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 int i = 0;
int status;
static int i = 0;
static int status;
sem_unlink("/chopsticks");
sem_unlink("/death");
if (argc < 5 || argc > 6)
return (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);
init_philos(table);
waitpid(-1, &status, 0);
@ -103,5 +103,5 @@ int main(int argc, char **argv)
}
sem_unlink("/chopsticks");
sem_unlink("/deaths");
return(0);
return (0);
}

14
philo_bonus/philo.h

@ -6,12 +6,12 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
# define PHILOSOPHERS_H
#ifndef PHILO_H
# define PHILO_H
# include <unistd.h>
# include <stdio.h>
@ -37,7 +37,7 @@ enum e_params
typedef struct s_table t_table;
typedef struct s_philo
typedef struct s_philo
{
int id;
int meals_done;
@ -49,12 +49,12 @@ typedef struct s_table
{
int param[5];
int i;
sem_t *sptr;
sem_t *sptr;
sem_t *death;
t_philo *philos;
t_philo *philos;
int time;
int *pid;
} t_table;
} t_table;
/***************************/

12
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: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;
table = malloc(sizeof(t_table));
//ft_bzero(table, 0, sizeof(t_table));
table->param[PHILO_AMOUNT] = mini_atoi(argv[0]);
table->param[DIE_TIME] = mini_atoi(argv[1]);
table->param[EAT_DURATION] = mini_atoi(argv[2]);
@ -41,18 +40,17 @@ t_table *parsing(char **argv)
table->param[MEALS_AMOUNT] = 2147483647;
table->time = gettime();
table->philos = malloc(sizeof(t_philo) * table->param[PHILO_AMOUNT]);
//memset(table->philos, 0, sizeof(t_philo) * table->nb_of_philos);
return (table);
}
int gettime(void)
{
static struct timeval starttime = {0,0};
static struct timeval starttime = {0, 0};
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(&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)));
}

Loading…
Cancel
Save