2

I'm trying to run an application using valgrind tool. But the application needs the parent process id which invoke the application as an argument.

valgrind --leak-check=no --error-limit=no MyApplication {Parent_Process_ID}

But since valgrind process is still starting, i cannot get the pid from bash. I tried the following way as taking the previous process pid.

valgrind --leak-check=no --error-limit=no MyApplication $!

But it gives the process id of process ran before valgrind.

Can someone help me get the pid of valgrind here?

PraAnj
  • 899
  • 1
  • 10
  • 27
  • Parent Process ID is not required to run valgrind normally – piyushj Sep 14 '16 at 12:10
  • You can re-write your application to get the parent process ID inside your program using `getppid()`. If you don't have control over your source code of your application, you can control the PID in Linux using ns_last_pid proc interface. Check http://efiop-notes.blogspot.de/2014/06/how-to-set-pid-using-nslastpid.html and http://stackoverflow.com/questions/18122592/how-to-set-process-id-in-linux-for-an-specific-program. With that proc file you can force the system to use the specific PID for next process creation (not sure how much guaranteed). – Parthiban Sep 14 '16 at 13:00
  • I dont have access to codes to rewrite the appication and it has the limitation of invoking through a parent process ID. – PraAnj Feb 09 '17 at 10:42

1 Answers1

0

You can try

( exec valgrind --leak-check=no --error-limit=no MyApplication $$ )

The parentheses create a subshell. $$ expands to the PID of the shell itself.

Then exec loads the valgrind program into the current process, which keeps its PID the same, i.e. valgrind will have the same PID as the (sub-)shell.

melpomene
  • 84,125
  • 8
  • 85
  • 148