narnaud
3 years ago
6 changed files with 220 additions and 229 deletions
@ -1,58 +0,0 @@ |
|||||
/* ************************************************************************** */ |
|
||||
/* */ |
|
||||
/* ::: :::::::: */ |
|
||||
/* philo_init.c :+: :+: :+: */ |
|
||||
/* +:+ +:+ +:+ */ |
|
||||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
||||
/* +#+#+#+#+#+ +#+ */ |
|
||||
/* Created: 2022/04/11 10:33:01 by narnaud #+# #+# */ |
|
||||
/* Updated: 2022/05/16 18:07:56 by narnaud ### ########.fr */ |
|
||||
/* */ |
|
||||
/* ************************************************************************** */ |
|
||||
|
|
||||
#include "philosophers.h" |
|
||||
|
|
||||
t_param *init_param(char **argv) |
|
||||
{ |
|
||||
t_param *ret; |
|
||||
|
|
||||
ret = malloc(sizeof(t_param)); |
|
||||
ret->philo_amount = mini_atoi(argv[0]); |
|
||||
ret->die_time = mini_atoi(argv[1]); |
|
||||
ret->eat_duration = mini_atoi(argv[2]); |
|
||||
ret->sleep_duration = mini_atoi(argv[3]); |
|
||||
if (argv[4]) |
|
||||
ret->eat_amount = mini_atoi(argv[4]); |
|
||||
else |
|
||||
ret->eat_amount = 2147483647; |
|
||||
return (ret); |
|
||||
} |
|
||||
|
|
||||
t_room *init_room(char **argv) |
|
||||
{ |
|
||||
t_room *room; |
|
||||
static int i = 0; |
|
||||
|
|
||||
room = malloc(sizeof(t_room)); |
|
||||
room->param = init_param(argv); |
|
||||
room->safe = malloc(sizeof(pthread_mutex_t)); |
|
||||
pthread_mutex_init(room->safe, NULL); |
|
||||
room->philos = \ |
|
||||
(t_philo **)malloc(room->param->philo_amount * sizeof(t_philo *)); |
|
||||
room->running = 1; |
|
||||
room->i = 0; |
|
||||
while (i < room->param->philo_amount) |
|
||||
{ |
|
||||
room->philos[i] = (t_philo *)malloc(sizeof(t_philo)); |
|
||||
room->philos[i]->fork = malloc(sizeof(pthread_mutex_t)); |
|
||||
room->philos[i]->safe = malloc(sizeof(pthread_mutex_t)); |
|
||||
room->philos[i]->thd = malloc(sizeof(pthread_t)); |
|
||||
room->philos[i]->eat_time = gettime(); |
|
||||
room->philos[i]->eat_amount = 0; |
|
||||
room->philos[i]->i = i; |
|
||||
pthread_mutex_init(room->philos[i]->fork, NULL); |
|
||||
pthread_mutex_init(room->philos[i++]->safe, NULL); |
|
||||
} |
|
||||
return (room); |
|
||||
} |
|
||||
|
|
@ -1,90 +0,0 @@ |
|||||
/* ************************************************************************** */ |
|
||||
/* */ |
|
||||
/* ::: :::::::: */ |
|
||||
/* philo_utils.c :+: :+: :+: */ |
|
||||
/* +:+ +:+ +:+ */ |
|
||||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
||||
/* +#+#+#+#+#+ +#+ */ |
|
||||
/* Created: 2022/04/11 10:29:50 by narnaud #+# #+# */ |
|
||||
/* Updated: 2022/05/16 18:03:12 by narnaud ### ########.fr */ |
|
||||
/* */ |
|
||||
/* ************************************************************************** */ |
|
||||
|
|
||||
#include "philosophers.h" |
|
||||
|
|
||||
|
|
||||
int mini_atoi(char *nbr) |
|
||||
{ |
|
||||
int ret; |
|
||||
|
|
||||
ret = 0; |
|
||||
while (*nbr >= '0' && *nbr <= '9') |
|
||||
{ |
|
||||
ret = (*nbr - '0') + (10 * ret); |
|
||||
nbr++; |
|
||||
} |
|
||||
return (ret); |
|
||||
} |
|
||||
|
|
||||
size_t gettime(void) |
|
||||
{ |
|
||||
static struct timeval start = {0,0}; |
|
||||
struct timeval actualtime; |
|
||||
size_t ret; |
|
||||
|
|
||||
if(start.tv_sec == 0 && start.tv_usec == 0) |
|
||||
gettimeofday(&start, NULL); |
|
||||
gettimeofday(&actualtime, NULL); |
|
||||
ret = (size_t)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000)\ |
|
||||
- (start.tv_sec * 1000 + start.tv_usec / 1000)); |
|
||||
return (ret); |
|
||||
} |
|
||||
|
|
||||
void safe_print(char *str, t_room *room, int id) |
|
||||
{ |
|
||||
pthread_mutex_lock(room->safe); |
|
||||
printf(str, gettime(), id); |
|
||||
pthread_mutex_unlock(room->safe); |
|
||||
} |
|
||||
|
|
||||
int safe_wait(t_room *room, size_t start, size_t duration) |
|
||||
{ |
|
||||
int ret; |
|
||||
|
|
||||
ret = 1; |
|
||||
while (ret && (gettime() < start + duration)) |
|
||||
{ |
|
||||
pthread_mutex_lock(room->safe); |
|
||||
if (!room->running) |
|
||||
ret = 0; |
|
||||
pthread_mutex_unlock(room->safe); |
|
||||
usleep(5000); |
|
||||
} |
|
||||
return (ret); |
|
||||
} |
|
||||
|
|
||||
int clean_memory(t_room *room) |
|
||||
{ |
|
||||
t_param *param; |
|
||||
int i; |
|
||||
|
|
||||
i = 0; |
|
||||
param = room->param; |
|
||||
while (i < param->philo_amount) |
|
||||
{ |
|
||||
pthread_mutex_destroy(room->philos[i]->safe); |
|
||||
pthread_mutex_destroy(room->philos[i]->fork); |
|
||||
free(room->philos[i]->safe); |
|
||||
free(room->philos[i]->fork); |
|
||||
free(room->philos[i]->thd); |
|
||||
free(room->philos[i]); |
|
||||
i++; |
|
||||
} |
|
||||
pthread_mutex_destroy(room->safe); |
|
||||
free(room->safe); |
|
||||
free(room->philos); |
|
||||
free(room->param); |
|
||||
free(room); |
|
||||
return (0); |
|
||||
} |
|
||||
|
|
Loading…
Reference in new issue