possible race conflicts resolut

This commit is contained in:
hugogogo
2022-01-30 15:53:45 +01:00
parent d9142b9af6
commit 7dc4c5a84f
7 changed files with 245 additions and 11 deletions

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:27:13 by hulamy #+# #+# */
/* Updated: 2022/01/26 17:50:11 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:13:21 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -25,7 +25,7 @@ void *philo_exec(void *arg);
// generic.c
void init_time(t_philo *philo);
void update_time(t_philo *philo);
int diff_time(t_time *old, struct timeval *new);
int diff_time(t_philo *philo, struct timeval *new);
int print_message(t_philo *philo, char *clr, char *msg);
// utils.c

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:27:23 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:29:09 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:47:58 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -36,6 +36,8 @@ typedef struct s_global
int satiated_count;
t_time t_start;
t_mtx m_print;
t_mtx m_init_time;
t_mtx m_stop;
} t_global;
typedef struct s_philo
@@ -44,6 +46,8 @@ typedef struct s_philo
t_global *global;
int p_nbr;
t_mtx m_fork;
t_mtx m_time;
t_mtx m_eat;
t_time t_last_meal;
int eat_count;
struct s_philo *next;

199
philo/race.log Normal file
View File

@@ -0,0 +1,199 @@
==26535== Helgrind, a thread error detector
==26535== Copyright (C) 2007-2017, and GNU GPL'd, by OpenWorks LLP et al.
==26535== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==26535== Command: ./philo 5 500 200 200 1
==26535==
==26535== ---Thread-Announcement------------------------------------------
==26535==
==26535== Thread #1 is the program's root thread
==26535==
==26535== ---Thread-Announcement------------------------------------------
==26535==
==26535== Thread #6 was created
==26535== at 0x518470E: clone (clone.S:71)
==26535== by 0x4E4BEC4: create_thread (createthread.c:100)
==26535== by 0x4E4BEC4: pthread_create@@GLIBC_2.2.5 (pthread_create.c:797)
==26535== by 0x4C38A27: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400E19: launch (launch.c:101)
==26535== by 0x4007DD: main (main.c:24)
==26535==
==26535== ----------------------------------------------------------------
==26535==
==26535== Thread #1 unlocked lock at 0x54545B8 currently held by thread #6
==26535== at 0x4C36534: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400E6E: launch (launch.c:109)
==26535== by 0x4007DD: main (main.c:24)
==26535== Lock at 0x54545B8 was first observed
==26535== at 0x4C39F2A: pthread_mutex_init (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C75: lst_add_philo (init.c:25)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Address 0x54545b8 is 24 bytes inside a block of size 176 alloc'd
==26535== at 0x4C32F2F: malloc (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C21: lst_add_philo (init.c:19)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Block was alloc'd by thread #1
==26535==
==26535==
==26535== ---Thread-Announcement------------------------------------------
==26535==
==26535== Thread #3 was created
==26535== at 0x518470E: clone (clone.S:71)
==26535== by 0x4E4BEC4: create_thread (createthread.c:100)
==26535== by 0x4E4BEC4: pthread_create@@GLIBC_2.2.5 (pthread_create.c:797)
==26535== by 0x4C38A27: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400E19: launch (launch.c:101)
==26535== by 0x4007DD: main (main.c:24)
==26535==
==26535== ----------------------------------------------------------------
==26535==
==26535== Thread #1 unlocked lock at 0x54541F8 currently held by thread #3
==26535== at 0x4C36534: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400E6E: launch (launch.c:109)
==26535== by 0x4007DD: main (main.c:24)
==26535== Lock at 0x54541F8 was first observed
==26535== at 0x4C39F2A: pthread_mutex_init (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C75: lst_add_philo (init.c:25)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Address 0x54541f8 is 24 bytes inside a block of size 176 alloc'd
==26535== at 0x4C32F2F: malloc (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C21: lst_add_philo (init.c:19)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Block was alloc'd by thread #1
==26535==
==26535==
==26535== ---Thread-Announcement------------------------------------------
==26535==
==26535== Thread #4 was created
==26535== at 0x518470E: clone (clone.S:71)
==26535== by 0x4E4BEC4: create_thread (createthread.c:100)
==26535== by 0x4E4BEC4: pthread_create@@GLIBC_2.2.5 (pthread_create.c:797)
==26535== by 0x4C38A27: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400E19: launch (launch.c:101)
==26535== by 0x4007DD: main (main.c:24)
==26535==
==26535== ----------------------------------------------------------------
==26535==
==26535== Thread #1 unlocked lock at 0x54542E8 currently held by thread #4
==26535== at 0x4C36534: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400E6E: launch (launch.c:109)
==26535== by 0x4007DD: main (main.c:24)
==26535== Lock at 0x54542E8 was first observed
==26535== at 0x4C39F2A: pthread_mutex_init (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C75: lst_add_philo (init.c:25)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Address 0x54542e8 is 24 bytes inside a block of size 176 alloc'd
==26535== at 0x4C32F2F: malloc (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C21: lst_add_philo (init.c:19)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Block was alloc'd by thread #1
==26535==
==26535==
==26535== ----------------------------------------------------------------
==26535==
==26535== Thread #1 unlocked lock at 0x54543D8 currently held by thread #4
==26535== at 0x4C36534: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400E6E: launch (launch.c:109)
==26535== by 0x4007DD: main (main.c:24)
==26535== Lock at 0x54543D8 was first observed
==26535== at 0x4C39F2A: pthread_mutex_init (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C75: lst_add_philo (init.c:25)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Address 0x54543d8 is 24 bytes inside a block of size 176 alloc'd
==26535== at 0x4C32F2F: malloc (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C21: lst_add_philo (init.c:19)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Block was alloc'd by thread #1
==26535==
==26535==
==26535== ----------------------------------------------------------------
==26535==
==26535== Thread #1 unlocked lock at 0x54544C8 currently held by thread #6
==26535== at 0x4C36534: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400E6E: launch (launch.c:109)
==26535== by 0x4007DD: main (main.c:24)
==26535== Lock at 0x54544C8 was first observed
==26535== at 0x4C39F2A: pthread_mutex_init (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C75: lst_add_philo (init.c:25)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Address 0x54544c8 is 24 bytes inside a block of size 176 alloc'd
==26535== at 0x4C32F2F: malloc (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C21: lst_add_philo (init.c:19)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Block was alloc'd by thread #1
==26535==
==26535==
==26535== ----------------------------------------------------------------
==26535==
==26535== Thread #3: Bug in libpthread: write lock granted on mutex/rwlock which is currently wr-held by a different thread
==26535== at 0x4C3603C: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x40122E: eat (exec.c:41)
==26535== by 0x401137: philo_exec (exec.c:66)
==26535== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x4E4B6DA: start_thread (pthread_create.c:463)
==26535== by 0x518471E: clone (clone.S:95)
==26535==
==26535== ----------------------------------------------------------------
==26535==
==26535== Thread #3 unlocked lock at 0x54542E8 currently held by thread #4
==26535== at 0x4C36534: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x40141F: ret_err_unlock (exec.c:32)
==26535== by 0x401265: eat (exec.c:43)
==26535== by 0x401137: philo_exec (exec.c:66)
==26535== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x4E4B6DA: start_thread (pthread_create.c:463)
==26535== by 0x518471E: clone (clone.S:95)
==26535== Lock at 0x54542E8 was first observed
==26535== at 0x4C39F2A: pthread_mutex_init (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C75: lst_add_philo (init.c:25)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Address 0x54542e8 is 24 bytes inside a block of size 176 alloc'd
==26535== at 0x4C32F2F: malloc (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==26535== by 0x400C21: lst_add_philo (init.c:19)
==26535== by 0x400B8D: init_chain_philo (init.c:49)
==26535== by 0x4008CD: init (init.c:126)
==26535== by 0x4007B5: main (main.c:21)
==26535== Block was alloc'd by thread #1
==26535==
==26535==
5 5 has taken a fork
15 5 has taken a fork
16 5 is eating
27 3 has taken a fork
28 3 has taken a fork
29 3 is eating
37 2 has taken a fork
222 5 is sleeping
225 1 has taken a fork
232 3 is sleeping
235 4 has taken a fork
236 4 has taken a fork
238 4 is eating
240 2 has taken a fork
240 2 is eating
==26535==
==26535== For counts of detected and suppressed errors, rerun with: -v
==26535== Use --history-level=approx or =none to gain increased speed, at
==26535== the cost of reduced accuracy of conflicting-access information
==26535== ERROR SUMMARY: 7 errors from 7 contexts (suppressed: 2350 from 144)

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */
/* Updated: 2022/01/26 18:51:44 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:43:19 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,7 +18,7 @@ static void action_delay(t_philo *philo, int action_time)
int death_time;
gettimeofday(&stime, NULL);
death_time = philo->params->t_die - diff_time(&philo->t_last_meal, &stime);
death_time = philo->params->t_die - diff_time(philo, &stime);
if (death_time > action_time)
usleep(action_time * 1000);
else if (death_time > 0)
@@ -45,7 +45,9 @@ static int eat(t_philo *philo)
if (print_message(philo, B_YELLOW, "is eating"))
return (ret_err_unlock(philo, 2));
action_delay(philo, philo->params->t_eat);
pthread_mutex_lock(&(philo->m_eat));
philo->eat_count++;
pthread_mutex_unlock(&(philo->m_eat));
pthread_mutex_unlock(&(philo->next->m_fork));
pthread_mutex_unlock(&(philo->m_fork));
return (0);

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */
/* Updated: 2022/01/26 16:29:10 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:47:44 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,28 +16,41 @@ void init_time(t_philo *philo)
{
struct timeval stime;
pthread_mutex_lock(&(philo->global->m_init_time));
if (philo->global->t_start.ts == 0)
{
gettimeofday(&stime, NULL);
philo->global->t_start.ts = stime.tv_sec;
philo->global->t_start.tu = stime.tv_usec;
}
pthread_mutex_lock(&(philo->m_time));
philo->t_last_meal.ts = philo->global->t_start.ts;
philo->t_last_meal.tu = philo->global->t_start.tu;
pthread_mutex_unlock(&(philo->m_time));
pthread_mutex_unlock(&(philo->global->m_init_time));
}
void update_time(t_philo *philo)
{
struct timeval stime;
pthread_mutex_lock(&(philo->m_time));
gettimeofday(&stime, NULL);
philo->t_last_meal.ts = stime.tv_sec;
philo->t_last_meal.tu = stime.tv_usec;
pthread_mutex_unlock(&(philo->m_time));
}
int diff_time(t_time *old, struct timeval *new)
int diff_time(t_philo *philo, struct timeval *new)
{
return ((new->tv_sec - old->ts) * 1000 + (new->tv_usec - old->tu) / 1000);
int t_diff;
t_time old;
pthread_mutex_lock(&philo->m_time);
old = philo->t_last_meal;
t_diff = (new->tv_sec - old.ts) * 1000 + (new->tv_usec - old.tu) / 1000;
pthread_mutex_unlock(&philo->m_time);
return (t_diff);
}
int print_message(t_philo *philo, char *clr, char *msg)
@@ -46,11 +59,14 @@ int print_message(t_philo *philo, char *clr, char *msg)
struct timeval stime;
pthread_mutex_lock(&(philo->global->m_print));
pthread_mutex_lock(&(philo->global->m_stop));
if (philo->global->stop)
{
pthread_mutex_unlock(&(philo->global->m_stop));
pthread_mutex_unlock(&(philo->global->m_print));
return (1);
}
pthread_mutex_unlock(&(philo->global->m_stop));
gettimeofday(&stime, NULL);
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
/* Updated: 2022/01/27 15:01:30 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:48:11 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,6 +24,10 @@ static t_philo *lst_add_philo(t_params *params, t_global *global, int i)
new->p_nbr = i + 1;
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
return (NULL);
if (pthread_mutex_init(&(new->m_time), NULL) != 0)
return (NULL);
if (pthread_mutex_init(&(new->m_eat), NULL) != 0)
return (NULL);
new->t_last_meal.ts = 0;
new->t_last_meal.tu = 0;
new->eat_count = 0;
@@ -97,6 +101,10 @@ static t_global *init_global(void)
global->t_start.tu = 0;
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
return (NULL);
if (pthread_mutex_init(&(global->m_init_time), NULL) != 0)
return (NULL);
if (pthread_mutex_init(&(global->m_stop), NULL) != 0)
return (NULL);
return (global);
}

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
/* Updated: 2022/01/26 18:52:32 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:50:27 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,10 +19,12 @@ static int is_finished(t_philo *philo, struct timeval *stime)
if (philo->global->satiated_count == philo->params->n_phi)
{
pthread_mutex_lock(&(philo->global->m_stop));
philo->global->stop = 1;
pthread_mutex_unlock(&(philo->global->m_stop));
return (1);
}
time = diff_time(&philo->t_last_meal, stime);
time = diff_time(philo, stime);
if (time >= philo->params->t_die)
{
pthread_mutex_lock(&(philo->global->m_print));
@@ -38,11 +40,14 @@ static int is_finished(t_philo *philo, struct timeval *stime)
int is_satiated(t_philo *philo)
{
pthread_mutex_lock(&(philo->m_eat));
if (philo->eat_count == philo->params->n_eat)
{
philo->global->satiated_count++;
pthread_mutex_unlock(&(philo->m_eat));
return (1);
}
pthread_mutex_unlock(&(philo->m_eat));
return (0);
}