implemenation droite gauche fonctionne en changeant technique de temps

This commit is contained in:
hugogogo
2022-01-31 00:17:07 +01:00
parent 7dc4c5a84f
commit c3ef767bdb
5 changed files with 340 additions and 225 deletions

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:27:23 by hulamy #+# #+# */ /* Created: 2022/01/26 15:27:23 by hulamy #+# #+# */
/* Updated: 2022/01/30 15:47:58 by hulamy ### ########.fr */ /* Updated: 2022/01/31 00:13:26 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -28,6 +28,7 @@ typedef struct s_params
int t_eat; int t_eat;
int t_slp; int t_slp;
int n_eat; int n_eat;
long start_time;
} t_params; } t_params;
typedef struct s_global typedef struct s_global

View File

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

View File

@@ -1,17 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */
/* Updated: 2022/01/30 15:43:19 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int get_time(t_params *params)
{
struct timeval tp;
long time;
if (gettimeofday(&tp, NULL) < 0)
{
return (-1);
}
time = tp.tv_sec * 1000 + tp.tv_usec / 1000;
if (!params->start_time)
{
params->start_time = time;
return (0);
}
time -= params->start_time;
return (time);
}
/*
static void action_delay(t_philo *philo, int action_time) static void action_delay(t_philo *philo, int action_time)
{ {
struct timeval stime; struct timeval stime;
@@ -24,20 +32,68 @@ static void action_delay(t_philo *philo, int action_time)
else if (death_time > 0) else if (death_time > 0)
usleep(philo->params->t_die * 1000); usleep(philo->params->t_die * 1000);
} }
*/
/*
static int ret_err_unlock(t_philo *philo, int nbr_fork) static int ret_err_unlock(t_philo *philo, int nbr_fork)
{ {
pthread_mutex_unlock(&philo->m_fork);
if (nbr_fork == 2) if (nbr_fork == 2)
pthread_mutex_unlock(&philo->next->m_fork); pthread_mutex_unlock(&philo->next->m_fork);
pthread_mutex_unlock(&philo->m_fork);
return (1);
}
*/
static int ret_err_unlock(t_mtx *fork1, t_mtx *fork2)
{
if (fork2)
pthread_mutex_unlock(fork2);
if (fork1)
pthread_mutex_unlock(fork1);
return (1); return (1);
} }
static int eat(t_philo *philo, t_mtx *fork1, t_mtx *fork2)
{
int time;
pthread_mutex_lock(fork1);
if (print_message(philo, WHITE, "has taken a fork"))
return (ret_err_unlock(fork1, NULL));
if (fork1 == fork2)
return (ret_err_unlock(fork1, NULL));
pthread_mutex_lock(fork2);
if (print_message(philo, WHITE, "has taken a fork"))
return (ret_err_unlock(fork1, fork2));
// update_time(philo);
time = get_time(philo->params);
if (print_message(philo, B_YELLOW, "is eating"))
return (ret_err_unlock(fork1, fork2));
// action_delay(philo, philo->params->t_eat);
while (get_time(philo->params) - time < philo->params->t_eat)
usleep(100);
pthread_mutex_lock(&(philo->m_eat));
philo->eat_count++;
pthread_mutex_unlock(&(philo->m_eat));
pthread_mutex_unlock(fork2);
pthread_mutex_unlock(fork1);
return (0);
}
/*
static int eat(t_philo *philo) static int eat(t_philo *philo)
{ {
pthread_mutex_lock(&(philo->m_fork)); pthread_mutex_lock(&(philo->m_fork));
if (print_message(philo, WHITE, "has taken a fork")) if (print_message(philo, WHITE, "has taken a fork"))
return (ret_err_unlock(philo, 1)); return (ret_err_unlock(philo, 1));
if (philo->next == philo)
return (ret_err_unlock(philo, 1));
pthread_mutex_lock(&(philo->next->m_fork)); pthread_mutex_lock(&(philo->next->m_fork));
if (print_message(philo, WHITE, "has taken a fork")) if (print_message(philo, WHITE, "has taken a fork"))
return (ret_err_unlock(philo, 2)); return (ret_err_unlock(philo, 2));
@@ -52,22 +108,65 @@ static int eat(t_philo *philo)
pthread_mutex_unlock(&(philo->m_fork)); pthread_mutex_unlock(&(philo->m_fork));
return (0); return (0);
} }
*/
/*
==10069== Observed (incorrect) order is: acquisition of lock at 0x54541F8
==10069== at 0x4C3603C: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==10069== by 0x401188: eat (exec.c:38)
==10069== by 0x4010E7: philo_exec (exec.c:68)
==10069== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==10069== by 0x4E4B6DA: start_thread (pthread_create.c:463)
==10069== by 0x518471E: clone (clone.S:95)
==10069==
==10069== followed by a later acquisition of lock at 0x54542E8
==10069== at 0x4C3603C: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==10069== by 0x401209: eat (exec.c:43)
==10069== by 0x4010E7: philo_exec (exec.c:68)
==10069== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==10069== by 0x4E4B6DA: start_thread (pthread_create.c:463)
==10069== by 0x518471E: clone (clone.S:95)
*/
void *philo_exec(void *arg) void *philo_exec(void *arg)
{ {
int time;
t_philo *philo; t_philo *philo;
t_mtx *fork1;
t_mtx *fork2;
philo = (t_philo *)arg; philo = (t_philo *)arg;
init_time(philo);
// init_time(philo);
get_time(philo->params);
// if (philo->p_nbr % 2 == 0)
// usleep(10 * 1000);
fork1 = &(philo->m_fork);
fork2 = &(philo->m_fork);
// if (philo->global->t_start.ts == 0)
// if (philo->p_nbr < philo->params->n_phi / 2)
if (philo->p_nbr % 2 == 0) if (philo->p_nbr % 2 == 0)
usleep(10 * 1000); fork1 = &(philo->next->m_fork);
else
fork2 = &(philo->next->m_fork);
while (1) while (1)
{ {
if (eat(philo) != 0) if (eat(philo, fork1, fork2) != 0)
// if (eat(philo) != 0)
break ; break ;
time = get_time(philo->params);
if (print_message(philo, B_BLUE, "is sleeping")) if (print_message(philo, B_BLUE, "is sleeping"))
break ; break ;
action_delay(philo, philo->params->t_slp);
// action_delay(philo, philo->params->t_slp);
while (get_time(philo->params) - time < philo->params->t_slp)
usleep(100);
if (print_message(philo, B_GREEN, "is thinking")) if (print_message(philo, B_GREEN, "is thinking"))
break ; break ;
} }

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
/* Updated: 2022/01/30 15:48:11 by hulamy ### ########.fr */ /* Updated: 2022/01/31 00:07:05 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -85,6 +85,7 @@ static t_params *init_params(int ac, char **av)
params->n_eat = -1; params->n_eat = -1;
if (ac == 6) if (ac == 6)
params->n_eat = ft_atoi(av[5]); params->n_eat = ft_atoi(av[5]);
params->start_time = 0;
return (params); return (params);
} }

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
/* Updated: 2022/01/30 15:50:27 by hulamy ### ########.fr */ /* Updated: 2022/01/30 19:57:46 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -87,11 +87,11 @@ void launch(t_philo *philo, pthread_t *id)
i++; i++;
} }
pere_fouettard(philo); pere_fouettard(philo);
i = 0; // i = 0;
while (i < philo->params->n_phi) // while (i < philo->params->n_phi)
{ // {
pthread_mutex_unlock(&(philo->m_fork)); // pthread_mutex_unlock(&(philo->m_fork));
philo = philo->next; // philo = philo->next;
i++; // i++;
} // }
} }