diff options
Diffstat (limited to 'common')
| -rw-r--r-- | common/Makefile | 4 | ||||
| -rw-r--r-- | common/common.c | 4 | ||||
| -rw-r--r-- | common/common.h | 16 | ||||
| -rw-r--r-- | common/helper.c | 26 | ||||
| -rw-r--r-- | common/philo.c | 6 |
5 files changed, 32 insertions, 24 deletions
diff --git a/common/Makefile b/common/Makefile index cf3089c..082d66e 100644 --- a/common/Makefile +++ b/common/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/09 22:39:08 by cacharle #+# #+# # -# Updated: 2020/02/09 23:57:34 by cacharle ### ########.fr # +# Updated: 2020/02/14 00:49:18 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ LIB = ar rcs RM = rm -rf CC = gcc -CCFLAGS = -Wall -Wextra -Werror +CCFLAGS = -Wall -Wextra #-Werror NAME = libphilocommon.a diff --git a/common/common.c b/common/common.c index 03fc32f..0ec7f80 100644 --- a/common/common.c +++ b/common/common.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/02/09 23:57:20 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:32:37 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,6 @@ t_bool parse_args(t_philo_args *philo_args, int argc, char **argv) { - if (philo_args == NULL) - return (FALSE); if (argc != 5 && argc != 6) return (FALSE); if ((philo_args->philo_num = h_strtoposint(argv[1])) == -1 || diff --git a/common/common.h b/common/common.h index def49cf..e314960 100644 --- a/common/common.h +++ b/common/common.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2020/02/09 23:56:48 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 00:35:30 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ # define TRUE 1 typedef int t_bool; +typedef long int t_time; typedef enum { @@ -43,9 +44,9 @@ typedef enum typedef struct { int philo_num; - int timeout_death; - int timeout_eat; - int timeout_sleep; + t_time timeout_death; + t_time timeout_eat; + t_time timeout_sleep; int meal_num; } t_philo_args; @@ -62,8 +63,8 @@ void philo_put_state_change(int id, t_philo_event event); ** philo.c */ -void philo_eat(int id, int timeout); -void philo_sleep(int id, int timeout); +void philo_eat(int id, t_time timeout); +void philo_sleep(int id, t_time timeout); void philo_think(int id); void philo_die(int id); @@ -71,11 +72,12 @@ void philo_die(int id); ** helper.c */ -int h_strtoposint(char *s); +long int h_strtoposint(char *s); int h_strlen(char *s); void h_putnbr(unsigned long num); void h_putchar(char c); void h_putstr(char *s); void *h_calloc(int count, int size); +t_time h_timeval_to_time(struct timeval *tp); #endif diff --git a/common/helper.c b/common/helper.c index 590c415..afdb7f3 100644 --- a/common/helper.c +++ b/common/helper.c @@ -6,20 +6,20 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:15:05 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:37:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -int h_strtoposint(char *s) +long int h_strtoposint(char *s) { - int num; + long int num; if (*s < '0' || *s > '9') return (-1); num = 0; - while (*s != '\0' && *s > '0' && *s < '9') + while (*s >= '0' && *s <= '9') { num *= 10; num += *s - '0'; @@ -40,12 +40,11 @@ int h_strlen(char *s) return (counter); } -void h_putnbr(unsigned long num) +void h_putnbr(unsigned long int num) { - if (num <= 0) - return ; - h_putnbr(num / 10); - h_putchar(num % 10 - '0'); + if (num > 9) + h_putnbr(num / 10); + h_putchar(num % 10 + '0'); } void h_putchar(char c) @@ -70,3 +69,12 @@ void *h_calloc(int count, int size) ((unsigned char*)ptr)[i] = 0x0; return (ptr); } + +t_time h_timeval_to_time(struct timeval *tp) +{ + t_time t; + + t = tp->tv_sec * 1000; + t += tp->tv_usec / 1000; + return (t); +} diff --git a/common/philo.c b/common/philo.c index 484257e..dfc9278 100644 --- a/common/philo.c +++ b/common/philo.c @@ -6,19 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 01:54:53 by cacharle #+# #+# */ -/* Updated: 2020/02/09 23:56:26 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 00:29:17 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -void philo_eat(int id, int timeout) +void philo_eat(int id, t_time timeout) { philo_put_state_change(id, EVENT_EATING); usleep(timeout * 1000); } -void philo_sleep(int id, int timeout) +void philo_sleep(int id, t_time timeout) { philo_put_state_change(id, EVENT_SLEEPING); usleep(timeout * 1000); |
