From d44cb96a76b1c5b410aed2fd270cdc6bbe85dc5a Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Thu, 3 Aug 2017 20:49:43 +0530 Subject: [PATCH] Evaluating valgrind in multiprocess scenarios --- CMakeLists.txt | 10 ++++++++++ README | 6 ++++++ childA.c | 12 ++++++++++++ childB.c | 14 ++++++++++++++ parent.c | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 README create mode 100644 childA.c create mode 100644 childB.c create mode 100644 parent.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ac6e76f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required (VERSION 2.8.11) + +set (CMAKE_BUILD_TYPE Debug) +# Add -O0 to remove optimizations when using gcc +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0") + +add_executable (parent parent.c) +add_executable (childA childA.c) +add_executable (childB childB.c) diff --git a/README b/README new file mode 100644 index 0000000..138e63a --- /dev/null +++ b/README @@ -0,0 +1,6 @@ +RUNNING WITH VALGRIND +===================== + +mkdir -p build +cd build; cmake ..; make +valgrind --trace-children=yes --leak-check=full --show-reachable=yes --error-limit=no --log-file=valgrind.log ./parent diff --git a/childA.c b/childA.c new file mode 100644 index 0000000..d283b00 --- /dev/null +++ b/childA.c @@ -0,0 +1,12 @@ +#include +#include +#define WELCOME_MSG "Hello, from child A\n" + +int main (int argc, char *argv[]) +{ + char *str = strdup (WELCOME_MSG); + + printf ("%s", str); + + return 0; +} diff --git a/childB.c b/childB.c new file mode 100644 index 0000000..0925f7e --- /dev/null +++ b/childB.c @@ -0,0 +1,14 @@ +#include + +int main (int argc, char *argv[]) +{ + int i = 0; + printf ("Hello, from child A\n"); + + while (i < argc) { + printf ("%s\n", argv[i]); + i++; + } + + return 0; +} diff --git a/parent.c b/parent.c new file mode 100644 index 0000000..74b7601 --- /dev/null +++ b/parent.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +#define PARENT_MEM_LEAK + +#define WELCOME_MSG "Hello, World\n" + +void launch(char *app) +{ + pid_t pid; + + if ((pid = fork()) == 0) { + /* child */ + execl(app, app, NULL); + perror ("app spawn failed\n"); + } else { + /* parent, wait for child to exit */ + int status = 0; + waitpid(pid, &status, 0); + } +} + +int main( void ) +{ +#ifdef PARENT_MEM_LEAK + char *c = strdup (WELCOME_MSG); +#else + char *c = WELCOME_MSG; +#endif + printf ("%s", c); + launch ("childA"); +}