diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-02-09 03:33:24 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-02-09 03:33:24 +0100 |
| commit | dfbafce6506fe5c47b0e60289a9d4629e502c9ac (patch) | |
| tree | bff26d329d67b4d5f4f74dff4099743836ac8efb /common | |
| parent | 34058c2901f18fd11a6e50e9fa8e3faefdcc6c7b (diff) | |
| download | philosophers-dfbafce6506fe5c47b0e60289a9d4629e502c9ac.tar.gz philosophers-dfbafce6506fe5c47b0e60289a9d4629e502c9ac.tar.bz2 philosophers-dfbafce6506fe5c47b0e60289a9d4629e502c9ac.zip | |
Added common files
Diffstat (limited to 'common')
| -rw-r--r-- | common/common.c | 56 | ||||
| -rw-r--r-- | common/common.h | 96 | ||||
| -rw-r--r-- | common/helper.c | 72 | ||||
| -rw-r--r-- | common/philo.c | 66 |
4 files changed, 290 insertions, 0 deletions
diff --git a/common/common.c b/common/common.c new file mode 100644 index 0000000..be35e69 --- /dev/null +++ b/common/common.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* common.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ +/* Updated: 2020/02/09 01:36:40 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +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 || + (philo_args->timeout_death = h_strtoposint(argv[2])) == -1 || + (philo_args->timeout_eat = h_strtoposint(argv[3])) == -1 || + (philo_args->timeout_sleep = h_strtoposint(argv[4])) == -1) + return (FALSE); + if (argc == 6) + { + if ((philo_args->meal_num = h_strtoposint(argv[5])) == -1) + return (FALSE); + } + else + philo_args->meal_num = -1; + return (TRUE); +} + +void philo_put_state_change(t_philo *philo, t_philo_event event) // not correct for philo3 +{ + struct timeval tv; + + if (gettimeofday(&tv, NULL) == -1) + return ; + h_putnbr(tv.tv_sec); + h_putnbr(tv.tv_usec / 1000); + h_putchar(' '); + h_putnbr(philo->id); + if (event == EVENT_FORK) + h_putstr(" has taken fork\n"); + else if (event == EVENT_EATING) + h_putstr(" is eating\n"); + else if (event == EVENT_SLEEPING) + h_putstr(" is sleeping\n"); + else if (event == EVENT_THINKING) + h_putstr(" is thinking\n"); + else if (event == EVENT_DIED) + h_putstr(" died\n"); +} diff --git a/common/common.h b/common/common.h new file mode 100644 index 0000000..ff371a9 --- /dev/null +++ b/common/common.h @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* common.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ +/* Updated: 2020/02/09 03:22:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COMMON_H +# define COMMON_H + +# include <stdlib.h> +# include <sys/time.h> + +# define FALSE 0 +# define TRUE 1 + +typedef int t_bool; + +typedef enum +{ + PSTATE_EATING = 0, + PSTATE_SLEEPING, + PSTATE_THINKING, + PSTATE_NUM +} t_philo_state; + +typedef enum +{ + EVENT_FORK, + EVENT_EATING, + EVENT_SLEEPING, + EVENT_THINKING, + EVENT_DIED +} t_philo_event; + +typedef struct +{ + int id; + t_philo_state state; + pthread_t thread; +} t_philo; + +typedef struct +{ + int philo_num; + int timeout_death; + int timeout_eat; + int timeout_sleep; + int meal_num; +} t_philo_args; + +typedef void (*t_philo_routine)(void *arg); + +typedef struct +{ + t_bool used; + t_philo *left; + t_philo *right; +} t_fork; + +/* +** common.c +*/ + +t_bool parse_args(t_philo_args *philo_args, int argc, char **argv); +void philo_put_state_change(t_philo *philo, t_philo_event event); + +/* +** philo.c +*/ + +t_philo *philos_new(int num); +void philos_destroy(t_philo *philo); + +void philo_eat(t_philo *philo); +void philo_sleep(t_philo *philo); +void philo_think(t_philo *philo); + +/* +** helper.c +*/ + +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); + + +#endif diff --git a/common/helper.c b/common/helper.c new file mode 100644 index 0000000..4c4b1f1 --- /dev/null +++ b/common/helper.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ +/* Updated: 2020/02/09 02:15:02 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +int h_strtoposint(char *s) +{ + int num; + + if (*s < '0' || *s > '9') + return (-1); + num = 0; + while (*s != '\0' && *s > '0' && *s < '9') + { + num *= 10; + num += *s - '0'; + s++; + } + if (*s != '\0') + return (-1); + return num; +} + +int h_strlen(char *s) +{ + int counter; + + counter = 0; + while (s[counter]) + counter++; + return (counter); +} + +void h_putnbr(unsigned long num) +{ + if (num <= 0) + return ; + h_putnbr(num / 10); + h_putchar(num % 10 - '0'); +} + +void h_putchar(char c) +{ + write(STDOUT_FILENO, &c, 1); +} + +void h_putstr(char *s) +{ + write(STDOUT_FILENO, s, h_strlen(s)); +} + +void *h_calloc(int count, int size) +{ + int i; + void *ptr; + + if ((ptr = malloc(count * size)) == NULL) + return (NULL); + i = count * size; + while (i-- > 0) + ((unsigned char*)ptr)[i] = 0x0; + return (ptr); +} diff --git a/common/philo.c b/common/philo.c new file mode 100644 index 0000000..b8d7caf --- /dev/null +++ b/common/philo.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/09 01:54:53 by cacharle #+# #+# */ +/* Updated: 2020/02/09 03:26:07 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +t_philo *philos_new(int num, t_philo_routine routine) +{ + int i; + t_philo *philos; + + if (num < 0) + return (NULL); + if ((philos = (t_philo*)h_calloc(num + 1, sizeof(t_philo))) == NULL) + return (NULL); + i = -1; + while (++i < num) + { + philos[i].id = num + 1; + if (pthread_create(&philos[i].thread, NULL, routine, philos + num) != 0) + { + philos_destroy(philos, i); + return (NULL); + } + } + return (philos); +} + +void philos_destroy(t_philo *philo, int num) +{ + if (philo == NULL) + return ; + while (num-- > 0) + pthread_join(philos[num].thread, NULL); + free(philos); +} + +void philo_eat(t_philo *philo) +{ + // take forks + // lock mutex + philo_put_state_change(philo, EVENT_EATING); + usleep(philo->timeout_eat * 1000); + // drop forks + // unlock mutex +} + +void philo_sleep(t_philo *philo) +{ + philo_put_state_change(philo, EVENT_SLEEPING); + usleep(philo->timeout_sleep * 1000); +} + +void philo_think(t_philo *philo) +{ + philo_put_state_change(philo, EVENT_THINKING); + // search a fork +} |
