Browse Source

norminage

master
narnaud 3 years ago
parent
commit
c9484b90ef
  1. 2
      philo/philo.c
  2. 4
      philo/philo.h
  3. 2
      philo/philo_init.c
  4. 2
      philo/philo_life.c
  5. 8
      philo_bonus/philo.c
  6. 6
      philo_bonus/philo.h
  7. 6
      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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

4
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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -61,7 +61,7 @@ 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

2
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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

8
philo_bonus/philo.c

@ -6,7 +6,7 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -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)
{ {
@ -85,14 +84,15 @@ 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);

6
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>

6
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,7 +40,6 @@ 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);
} }
@ -53,6 +51,6 @@ int gettime(void)
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