tomjoy75 / Philosophers

42 subject Philosophers
0 stars 0 forks source link

[VALGRIND ERROR] #1

Open tomjoy75 opened 2 months ago

tomjoy75 commented 2 months ago
➜  Philosophers git:(main) ✗ valgrind --tool=drd ./a.out 5 4 4 4 5
==496319== drd, a thread error detector
==496319== Copyright (C) 2006-2020, and GNU GPL'd, by Bart Van Assche.
==496319== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==496319== Command: ./a.out 5 4 4 4 5
==496319== 
Philosopher created
==496319== Thread 3:
==496319== Conflicting store by thread 3 at 0x04ac4302 size 1
==496319==    at 0x4874C96: mempcpy (in /usr/libexec/valgrind/vgpreload_drd-amd64-linux.so)
==496319==    by 0x4923664: _IO_new_file_xsputn (fileops.c:1235)
==496319==    by 0x4923664: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1196)
==496319==    by 0x4918F1B: puts (ioputs.c:40)
==496319==    by 0x10943B: routine (philo.c:55)
==496319==    by 0x485437A: ??? (in /usr/libexec/valgrind/vgpreload_drd-amd64-linux.so)
==496319==    by 0x492CAC2: start_thread (pthread_create.c:442)
==496319==    by 0x49BDA03: clone (clone.S:100)
==496319== Address 0x4ac4302 is at offset 18 from 0x4ac42f0. Allocation context:
==496319==    at 0x484DBD9: malloc (in /usr/libexec/valgrind/vgpreload_drd-amd64-linux.so)
==496319==    by 0x4916BA3: _IO_file_doallocate (filedoalloc.c:101)
==496319==    by 0x4925CDF: _IO_doallocbuf (genops.c:347)
==496319==    by 0x4924F5F: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:744)
==496319==    by 0x49236D4: _IO_new_file_xsputn (fileops.c:1243)
==496319==    by 0x49236D4: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1196)
==496319==    by 0x4918F1B: puts (ioputs.c:40)
==496319==    by 0x10943B: routine (philo.c:55)
==496319==    by 0x485437A: ??? (in /usr/libexec/valgrind/vgpreload_drd-amd64-linux.so)
==496319==    by 0x492CAC2: start_thread (pthread_create.c:442)
==496319==    by 0x49BDA03: clone (clone.S:100)
==496319== Other segment start (thread 2)
==496319==    (thread finished, call stack no longer available)
==496319== Other segment end (thread 2)
==496319==    (thread finished, call stack no longer available)
==496319== 
Philosopher created
Philosopher created
Philosopher created
Philosopher created
==496319== Thread 1:
==496319== Conflicting store by thread 1 at 0x04ac4301 size 1
==496319==    at 0x4874C96: mempcpy (in /usr/libexec/valgrind/vgpreload_drd-amd64-linux.so)
==496319==    by 0x4923664: _IO_new_file_xsputn (fileops.c:1235)
==496319==    by 0x4923664: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1196)
==496319==    by 0x4918F1B: puts (ioputs.c:40)
==496319==    by 0x109607: main (philo.c:94)
==496319== Address 0x4ac4301 is at offset 17 from 0x4ac42f0. Allocation context:
==496319==    at 0x484DBD9: malloc (in /usr/libexec/valgrind/vgpreload_drd-amd64-linux.so)
==496319==    by 0x4916BA3: _IO_file_doallocate (filedoalloc.c:101)
==496319==    by 0x4925CDF: _IO_doallocbuf (genops.c:347)
==496319==    by 0x4924F5F: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:744)
==496319==    by 0x49236D4: _IO_new_file_xsputn (fileops.c:1243)
==496319==    by 0x49236D4: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1196)
==496319==    by 0x4918F1B: puts (ioputs.c:40)
==496319==    by 0x10943B: routine (philo.c:55)
==496319==    by 0x485437A: ??? (in /usr/libexec/valgrind/vgpreload_drd-amd64-linux.so)
==496319==    by 0x492CAC2: start_thread (pthread_create.c:442)
==496319==    by 0x49BDA03: clone (clone.S:100)
==496319== Other segment start (thread 3)
==496319==    (thread finished, call stack no longer available)
==496319== Other segment end (thread 3)
==496319==    (thread finished, call stack no longer available)
==496319== Other segment start (thread 4)
==496319==    (thread finished, call stack no longer available)
==496319== Other segment end (thread 4)
==496319==    (thread finished, call stack no longer available)
==496319== Other segment start (thread 5)
==496319==    (thread finished, call stack no longer available)
==496319== Other segment end (thread 5)
==496319==    (thread finished, call stack no longer available)
==496319== Other segment start (thread 6)
==496319==    (thread finished, call stack no longer available)
==496319== Other segment end (thread 6)
==496319==    (thread finished, call stack no longer available)
==496319== 
Philosopher joined
Philosopher joined
Philosopher joined
Philosopher joined
Philosopher joined
==496319== 
==496319== For lists of detected and suppressed errors, rerun with: -s
==496319== ERROR SUMMARY: 166 errors from 2 contexts (suppressed: 300 from 89)
tomjoy75 commented 2 months ago

Code quoi_Tips for testing

There are some tools we can use to help us detect thread-related errors like possible data races, deadlocks and lock order violations:

The -fsanitize=thread -g flag we can add at compilation. The -g option displays the specific files and line numbers involved. The thread error detection tool Helgrind that we can run our program with, like this: valgrind --tool=helgrind ./philo . DRD, another thread error detection tool that we can also run our program with, like this: valgrind --tool=drd ./philo .

Beware of using both -fsanitize=thread and valgrind, they do not play well together!

And as always, we can’t forget to check for memory leaks with -fsanitize=address and valgrind!