42 school project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

60 lines
1.9 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */
/* Updated: 2022/05/16 14:04:11 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
static int mini_atoi(char *nbr)
{
int ret;
ret = 0;
while (*nbr >= '0' && *nbr <= '9')
{
ret = (*nbr - '0') + (10 * ret);
nbr++;
}
return (ret);
}
t_table *parsing(char **argv)
{
t_table *table;
table = malloc(sizeof(t_table));
memset(table, 0, sizeof(t_table));
table->nb_of_philos = mini_atoi(argv[0]);
table->philos = malloc(sizeof(t_philo) * table->nb_of_philos);
memset(table->philos, 0, sizeof(t_philo) * table->nb_of_philos);
table->time_to_die = mini_atoi(argv[1]);
table->eating_time = mini_atoi(argv[2]);
table->sleeping_time = mini_atoi(argv[3]);
table->time = gettime();
if (argv[4])
table->nb_of_meal = mini_atoi(argv[4]);
else
table->nb_of_meal = 2147483647;
return (table);
}
size_t gettime(void)
{
static struct timeval time = {0,0};
struct timeval actualtime;
size_t ret;
if(time.tv_sec == 0 && time.tv_usec == 0)
gettimeofday(&time, NULL);
gettimeofday(&actualtime, NULL);
ret = (size_t)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000)\
- (time.tv_sec * 1000 + time.tv_usec / 1000));
return (ret);
}