From 4ffaf6a61f328fba35dea7b2d950fa54f2fa4a7a Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Fri, 24 Mar 2023 16:41:34 +0100 Subject: [PATCH] Avancement, crash message a corriger --- mandatory/.threads.c.swp | Bin 0 -> 20480 bytes mandatory/threads.c | 98 +++++++++++++++++++++------------------ utils/Makefile | 2 +- utils/ft_itoa.c | 29 ++++++++++++ utils/utils.h | 3 +- 5 files changed, 84 insertions(+), 48 deletions(-) create mode 100644 mandatory/.threads.c.swp create mode 100644 utils/ft_itoa.c diff --git a/mandatory/.threads.c.swp b/mandatory/.threads.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..341ed2bf3a0162f18fef145c735d9157c5678978 GIT binary patch literal 20480 zcmeI3ZHya78OJvbg>IS@NGU3*R3>-HVf*Z}&ljrH$sM5~l>(tuk*+Nyt?N6l@0Rn~ zS9^WQk(Z_t6okZ=rZlA=kcv=<_)w}!yp;+*@PU^QK%%rr&{UClDMBq#T7(3s;Wx80 zYp?Gv_#g-s%|^f5_3rF5&-`aRo_S_6k-KB}hsDi9IfvtIj&m|`efipB53l)qbCu&Y zyoJn!7syAKI)-=UhqmNPwW)fm>dksXmB!Mkj~av7pi!DHwGL)?wi?Y=b4xxuH|4sM-H-{#dwrB5J1*`%+D$oilnXT)_NAkn!tKQ$YQCz!z zyvLVqGOK`9z$#!BunJfOtO8a6tAJJD|3QIZZnbki1-U{OW|Kaz>N>wkf4)75E=0P%1ml7uPt>2d^e` z*8k1?|7&k`oO@s$JadiX%)kct(K^RD2=9UC);i9o;FEA4%)wr`9d3dxFakHi4R9r# zC!asVFW^b|JUjsR!%?^g_QOZuHrNBww+(PLtbq&o`viOj0=OGi!RxN$d=qA13i{yZ zS2@mUI0oC`dv9@^Z$ki6P=dQ44Qt`qH#^Sb@GMHv%GmftTTF z_y#-$b5MZ^D8dKe{V)J85hVE?yZ~Q?$KX*o0Tn32e%J?l;eBvDyc4d2a|CH#gy-Qo zI0H|^Bk(XB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&mgt)d7r%UU`Y8QqXU9R7nc z@tmT*yUYrfab77X8H5)&GtxbMN2EGx zFD#f)i*acY6qN@l`8OVbQg_H+o>k;+lvSjnS|H?Z%3R}^3W{}nZfma{MJ2t8sH`nr z7L}hViqtjAg6u*!UFBw2Fqzy<#aoQLr{ikzvPdPR=G#%FKa`)y2wNn zkJI%d=l@s9wQbhB<^135@4v`A{{{Fp{1~2ulW+nafukT}0`C9^{>Z%kbMRT%34dj- z{wh2H`{7Pl3qN3f{vp@}4!p{|`(^kodlIa4YnIfOo?Mv^)>z;D_*a_zIi^rSlu@SJe7z6|f3e1^x>Pn33S3AF6QFqJO3I zIL+=wQX8%0P{Qs-vU`!_7*B20imgh^5u`lqUL-T}m)j*ew^W(!5^6KnindEgGvTyP z?|HO)k<>mzbUCC%mb3KsR)OxVOPAbkX7?g3vv<+%MQZN>wR@4G34DC3BAtHwiTq)Z ZK@+w2IowbcM)inQsR)wZ{*-i_e*$30D1!h1 literal 0 HcmV?d00001 diff --git a/mandatory/threads.c b/mandatory/threads.c index 7b0f7e8..2dc61e2 100644 --- a/mandatory/threads.c +++ b/mandatory/threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */ -/* Updated: 2023/03/13 18:33:50 by erey-bet ### ########.fr */ +/* Updated: 2023/03/24 16:38:39 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,68 +17,94 @@ long get_usec(long long time) return (time % 1000000); } +long long get_time() +{ + struct timeval time; + long long new_time; + + gettimeofday(&time, NULL); + new_time = time.tv_sec * 1000000; + new_time += time.tv_usec; + return (new_time); +} + +void message(int id, char *msg) +{ + char *all; + char *id_str; + char *time; + int i; + + id_str = ft_itoa(id); + time = ft_itoa(get_time()); + all = ft_calloc(ft_strlen(id_str) + ft_strlen(msg) + ft_strlen(time) + 1, 1); + i = -1; + while (time[++i]) + all[i] = id_str[i]; + while (id_str[++i - ft_strlen(time)]) + all[i] = id_str[i - ft_strlen(time)]; + while (msg[++i - ft_strlen(time) - ft_strlen(id_str)]) + all[i] = msg[i - ft_strlen(time) - ft_strlen(id_str)]; + write(1, all, ft_strlen(all)); +} void *philosopher(t_philo *philo) { - long long *time; + long long time; long long last_eat_time; int id; int next_id; - time = philo->same->time; id = *philo->only->id; next_id = (*philo->only->id + 1) % philo->same->config->nbr_philo; - while (*time <= 0) - ; - usleep((*philo->only->id % 2)); - last_eat_time = *time; - while (1) + usleep((*philo->only->id % 2) * 50); + time = get_time(); + last_eat_time = time; + while (get_time() - last_eat_time < philo->same->config->time_die && !*philo->same->death) { - printf("%ld %d is thinking\n" , get_usec(*time), id); + message (id, "is thinking\n"); if (!*philo->same->death && !pthread_mutex_lock(&philo->same->mutex[id])) { - // printf("%ld %d is taking %d fork\n" , get_usec(*time), id, id); + // printf("%ld %d is taking %d fork\n" , get_usec(time), id, id); if (!*philo->same->death && !pthread_mutex_lock(&philo->same->mutex[next_id])) { - //printf("%ld %d is taking %d fork\n" , get_usec(*time), id, next_id); - printf("%ld %d is eating\n" , get_usec(*time), id); - last_eat_time = *time; - if (*time - last_eat_time + philo->same->config->time_eat >= philo->same->config->time_die || *philo->same->death) + //printf("%ld %d is taking %d fork\n" , get_usec(time), id, next_id); + printf("%ld %d is eating\n" , get_usec(get_time() - time), id); + if (get_time() - last_eat_time + philo->same->config->time_eat >= philo->same->config->time_die || *philo->same->death) { - printf("%lld | %d dieing 1\n",*time - last_eat_time, id); - break; + printf("%lld | %d dieing 1\n", get_time() - last_eat_time, id); + break ; } else usleep(philo->same->config->time_eat); - printf("after eat last_eat_time: %lld\n" , *time - last_eat_time); + last_eat_time = get_time(); pthread_mutex_unlock(&philo->same->mutex[next_id]); pthread_mutex_unlock(&philo->same->mutex[id]); - printf("%ld %d is sleeping\n" , get_usec(*time), id); - if (*time - last_eat_time + philo->same->config->time_sleep >= philo->same->config->time_die || *philo->same->death) + printf("%ld %d is sleeping\n" , get_usec(get_time() - time), id); + if (get_time() - last_eat_time + philo->same->config->time_sleep >= philo->same->config->time_die || *philo->same->death) { - printf("%lld | %d dieing 2\n",*time - last_eat_time, id); + printf("%lld | %d dieing 2\n",get_time() - last_eat_time, id); break; } else usleep(philo->same->config->time_sleep); - printf("after sleep last_eat_time: %lld\n" , *time - last_eat_time); + printf("after sleep last_eat_time: %lld\n" , get_time() - last_eat_time); } else pthread_mutex_unlock(&philo->same->mutex[id]); } - if (*time - last_eat_time >= philo->same->config->time_die || *philo->same->death) { - printf("%lld | %d dieing 3\n",*time - last_eat_time, id); + printf("%lld | %d dieing 3\n",get_time() - last_eat_time, id); break; } } if (!*philo->same->death) { *philo->same->death = 1; - printf("%ld %d died\n" , get_usec(*time), id); + printf("%ld %d died\n" , get_usec(get_time() - time), id); } //else - //printf("%ld %d stop\n" , get_usec(*time), id); + //printf("%ld %d stop\n" , get_usec(time), id); return (NULL); } @@ -100,25 +126,6 @@ void destroy_mutex(pthread_mutex_t *mutex, int nbr) pthread_mutex_destroy(&mutex[i]); } -long long get_time() -{ - struct timeval time; - long long new_time; - - gettimeofday(&time, NULL); - new_time = time.tv_sec * 1000000; - new_time += time.tv_usec; - return (new_time); -} - -void *timer(t_philo *timer) -{ - *timer->same->time = get_time(); - while (!*timer->same->death) - *timer->same->time = get_time(); - return (NULL); -} - t_same *create_struct_same(t_config *config) { t_same *same; @@ -170,8 +177,7 @@ int manage_threads(t_config *config) threads = ft_calloc(config->nbr_philo + 1, sizeof(pthread_t)); if (threads == NULL) return (1); - same = create_struct_same(config); - pthread_create(&threads[0], NULL, (void *)&timer, init_philo(same, create_struct_only(-1))); + same = create_struct_same(config); i = -1; while (++i < config->nbr_philo) { diff --git a/utils/Makefile b/utils/Makefile index 76e2e06..9f40347 100644 --- a/utils/Makefile +++ b/utils/Makefile @@ -11,7 +11,7 @@ # **************************************************************************** # SRCS = ft_atoi.c ft_calloc.c ft_putstr_fd.c ft_putnbr_fd.c ft_atoi_check.c \ -ft_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c ft_bzero.c +ft_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c ft_bzero.c ft_itoa.c OBJS = ${SRCS:.c=.o} CC = clang CFLAGS = -Wall -Wextra -Werror diff --git a/utils/ft_itoa.c b/utils/ft_itoa.c new file mode 100644 index 0000000..c47acdf --- /dev/null +++ b/utils/ft_itoa.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/04 17:12:52 by erey-bet #+# #+# */ +/* Updated: 2023/03/24 16:35:39 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" + +char *ft_itoa(long long n) +{ + char *str; + long long q; + long long i; + + q = 10; + while (n / q > 0) + q = q * 10; + str = malloc(sizeof(char) * q + 1); + i = 0; + while (q > 0) + str[i++] = (n / q % 10) + 48; + return (str); +} diff --git a/utils/utils.h b/utils/utils.h index a51bcf2..9c88f16 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/09/26 16:31:10 by erey-bet #+# #+# */ -/* Updated: 2023/03/12 16:49:53 by erey-bet ### ########.fr */ +/* Updated: 2023/03/24 16:26:31 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,5 +25,6 @@ void *ft_memset(void *ptr, int v, size_t count); void ft_putnbr_fd(int n, int fd); void ft_putstr_fd(char *s, int fd); void ft_putchar_fd(char c, int fd); +char *ft_itoa(long long n); #endif