「✨」 feat: added timing calc and parsing
This commit is contained in:
@ -6,26 +6,44 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/07 16:18:23 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/07/09 12:04:14 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
|
||||
# include <stdio.h>
|
||||
# include <pthread.h>
|
||||
# include <stdint.h>
|
||||
# include <stdbool.h>
|
||||
# include <sys/time.h>
|
||||
|
||||
typedef enum s_philo_state
|
||||
typedef enum s_pstate
|
||||
{
|
||||
DIED = -1,
|
||||
EAT,
|
||||
THINK,
|
||||
SLEEP,
|
||||
FORK_TAKEN
|
||||
} t_philo_state;
|
||||
} t_pstate;
|
||||
|
||||
void log_philo(uint32_t timestamp, uint16_t philo, t_philo_state type);
|
||||
typedef struct s_pdata
|
||||
{
|
||||
uint32_t die_time;
|
||||
uint32_t sleep_time;
|
||||
uint32_t eat_time;
|
||||
uint16_t philo_nbr;
|
||||
uint8_t meal_nbr;
|
||||
bool error;
|
||||
} t_pdata;
|
||||
|
||||
void log_philo(uint32_t timestamp, uint16_t philo, t_pstate type);
|
||||
t_pdata philo_parse(char **argv, int ac);
|
||||
uint16_t get_time_in_ms(struct timeval t0, struct timeval t1);
|
||||
|
||||
/**
|
||||
* For debug purpose to be REMOVED
|
||||
*/
|
||||
void print_philo_data(t_pdata data);
|
||||
|
||||
#endif
|
||||
|
18
philo/include/utils.h
Normal file
18
philo/include/utils.h
Normal file
@ -0,0 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/08 17:46:19 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/08 17:46:50 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef UTILS_H
|
||||
# define UTILS_H
|
||||
|
||||
long long ft_atoll(const char *nptr);
|
||||
|
||||
#endif
|
BIN
philo/philo
BIN
philo/philo
Binary file not shown.
35
philo/src/ft_atoll.c
Normal file
35
philo/src/ft_atoll.c
Normal file
@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoll.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/08 17:29:13 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/08 17:29:15 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
long long ft_atoll(const char *nptr)
|
||||
{
|
||||
char sign;
|
||||
long long nbr;
|
||||
|
||||
sign = 1;
|
||||
nbr = 0;
|
||||
while ((*nptr >= 7 && *nptr <= 13) || *nptr == 32)
|
||||
nptr++;
|
||||
if (*nptr == '-')
|
||||
{
|
||||
sign *= -1;
|
||||
nptr++;
|
||||
}
|
||||
else if (*nptr == '+')
|
||||
nptr++;
|
||||
while (*nptr >= '0' && *nptr <= '9')
|
||||
{
|
||||
nbr = nbr * 10 + (*nptr - '0');
|
||||
nptr++;
|
||||
}
|
||||
return (nbr * sign);
|
||||
}
|
19
philo/src/get_time_in_ms.c
Normal file
19
philo/src/get_time_in_ms.c
Normal file
@ -0,0 +1,19 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_time_in_ms.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/09 13:14:50 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <stdint.h>
|
||||
|
||||
uint16_t get_time_in_ms(struct timeval t0, struct timeval t1)
|
||||
{
|
||||
return (((t1.tv_sec-t0.tv_sec) * 1000000 + t1.tv_usec-t0.tv_usec)/1000);
|
||||
}
|
@ -6,14 +6,14 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/07 17:56:28 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/07/08 16:22:28 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
#include "philo_msg.h"
|
||||
|
||||
void log_philo(uint32_t timestamp, uint16_t philo, t_philo_state type)
|
||||
void log_philo(uint32_t timestamp, uint16_t philo, t_pstate type)
|
||||
{
|
||||
if (type == EAT)
|
||||
printf("%u %hu %s", timestamp, philo, EATING_MSG);
|
||||
|
40
philo/src/parse_argv.c
Normal file
40
philo/src/parse_argv.c
Normal file
@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parse_argv.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/08 15:30:46 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/09 10:46:47 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
#include "utils.h"
|
||||
|
||||
t_pdata fill_pdata(char **av)
|
||||
{
|
||||
t_pdata data;
|
||||
|
||||
if (!av)
|
||||
{
|
||||
data.error = true;
|
||||
return (data);
|
||||
}
|
||||
data.philo_nbr = ft_atoll(av[1]);
|
||||
data.die_time = ft_atoll(av[2]);
|
||||
data.eat_time = ft_atoll(av[3]);
|
||||
data.sleep_time = ft_atoll(av[4]);
|
||||
if (av[5])
|
||||
data.meal_nbr = ft_atoll(av[5]);
|
||||
data.error = false;
|
||||
return (data);
|
||||
}
|
||||
|
||||
t_pdata philo_parse(char **av, int ac)
|
||||
{
|
||||
if (!(ac > 4 && ac <= 6))
|
||||
return (fill_pdata(NULL));
|
||||
return (fill_pdata(av));
|
||||
}
|
@ -6,15 +6,28 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/07 17:44:08 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/07/09 13:17:55 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
#include <limits.h>
|
||||
#include "utils.h"
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
(void)ac;
|
||||
(void)av;
|
||||
log_philo(10, 5, DIED);
|
||||
t_pdata data;
|
||||
struct timeval t0;
|
||||
struct timeval t1;
|
||||
|
||||
gettimeofday(&t0, NULL);
|
||||
data = philo_parse(av, ac);
|
||||
gettimeofday(&t1, NULL);
|
||||
if (data.error == true)
|
||||
return (EXIT_FAILURE);
|
||||
print_philo_data(data);
|
||||
log_philo(get_time_in_ms(t0, t1), 0, DIED);
|
||||
}
|
||||
|
18
philo/src/print_philo_data.c
Normal file
18
philo/src/print_philo_data.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include "philo.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void print_philo_data(t_pdata data)
|
||||
{
|
||||
if (data.error == true)
|
||||
{
|
||||
printf("error = true\n");
|
||||
return ;
|
||||
}
|
||||
else
|
||||
printf("error = false\n");
|
||||
printf("die_time = %u\n", data.die_time);
|
||||
printf("sleep_time = %u\n", data.sleep_time);
|
||||
printf("eat_time = %u\n", data.eat_time);
|
||||
printf("philo_nbr = %u\n", data.philo_nbr);
|
||||
printf("meal_nbr = %u\n", data.meal_nbr);
|
||||
}
|
Reference in New Issue
Block a user