Skip to content

Commit

Permalink
Evaluating valgrind in multiprocess scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrinivas Kamath committed Aug 3, 2017
1 parent ef464a3 commit d44cb96
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions childA.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
#include <string.h>
#define WELCOME_MSG "Hello, from child A\n"

int main (int argc, char *argv[])
{
char *str = strdup (WELCOME_MSG);

printf ("%s", str);

return 0;
}
14 changes: 14 additions & 0 deletions childB.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

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;
}
34 changes: 34 additions & 0 deletions parent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>

#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");
}

0 comments on commit d44cb96

Please sign in to comment.