How to find the PID of a C code which is being executed So that I can find its stack and heap memory usage? And also can anyone ellaborate me about the sections in the map file?
-
2what do u mean by 'pid of a c code' ? – tristan Jan 02 '14 at 06:10
-
http://stackoverflow.com/questions/6437602/shell-script-to-get-the-process-id-on-linux Maybe this link can help you – Omer Obaid Jan 02 '14 at 06:13
-
@tristan: pid of a C code means the PID assigned by the linux kernel for the execution of the code – Siva Jan 02 '14 at 06:35
-
It's confusing. kernel doesn't assign id for a piece of code. It's process ID. The same code might be used by multiple processes. – tristan Jan 02 '14 at 06:38
-
@tristan: Ok fine ! can you help me find the stack and heap memory used by the C code I'm building in linux platform? – Siva Jan 02 '14 at 06:42
-
@Siva: you should understand that a process is not the same as an executable. Follow the links I gave you in my answer! – Basile Starynkevitch Jan 02 '14 at 06:45
-
@OMerObaid: Nope.. It wasn't useful :( – Siva Jan 02 '14 at 06:46
-
@BasileStarynkevitch: Ok bro ! My aim is to get the details of the usage of stack and heap memory of a C code I'm compiling in Linux.. Could u plz help me? – Siva Jan 02 '14 at 06:48
-
Again, stack and heap exist only for running processes, not for passive C code. I added even more HTML links in my answer for you. – Basile Starynkevitch Jan 02 '14 at 06:53
-
@BasileStarynkevitch: For a running process only I'm asking bro ! When the C code is getting compiled it'll be in running state right? – Siva Jan 02 '14 at 06:56
-
No, only when you run the compiled ELF executable. What happens at compile time is irrelevant! – Basile Starynkevitch Jan 02 '14 at 06:57
-
@BasileStarynkevitch: Ok.. So there is any way to find the space that our code occupies in stack and heap when running in linux? – Siva Jan 02 '14 at 08:19
-
It is one of the worst asked questions I answered to .... If I could I would downvote more than once. – Basile Starynkevitch Jan 02 '14 at 09:28
-
1@Siva i think Basile wanna say you can not do this. "**stack and heap exist only for running processes, not for passive C code.**" – Omer Obaid Jan 02 '14 at 09:33
-
@BasileStarynkevitch: Then y the hell did u answer? :D – Siva Jan 02 '14 at 11:19
3 Answers
PID exists only for running process. That means your c code should be running. In your c code can use getpid();
to get current process ID. This should be call from your c code which you need pid. You can print the pid and can look in to /proc
entries of that pid. Or you can read /proc
entries from your program after gettign PID. (OR) When yoou run your c code, Use ps -ef command to print details of all running processes. In that you'll find a field called pid. you can identify your executable pid from its name.

- 3,930
- 4
- 25
- 55
-
-
-
I'm using Linux vmbox terminal window.. And moreover my code is running in a target board.. So plz explain how'll I find the stack and heap memory usage of the C code which I'm executing? – Siva Jan 02 '14 at 08:50
-
You could and should install a shell and some command line utilities (perhaps with telnet and sshd) on the target board. – Basile Starynkevitch Jan 02 '14 at 09:44
Why not the getpid() method defined in unistd.h?
#include <unistd.h>
int main(){
int pid = getpid();
int parentsPID = getppid();
return 0;
}

- 7,449
- 2
- 29
- 45
A C code is only a bunch of textual source files.
It can be built, i.e. compiled then linked (and perhaps installed), as an executable program (often in ELF on Linux).
That executable may be executed by the execve(2) syscall from inside some fork-ed process. Each process has its pid (a unique positive number of type pid_t
). On Linux, you may have several different processes running the same executable (in particular, you usually have several running /bin/bash
shells, e.g. one for each terminal).
From inside a process, use the getpid(2) syscall to get its pid. Read also more about proc(5) and fork(2). Use also the top(1) and ps(1) commands (in particular, try ps auxw
)
The /proc/$PID/maps
(which, from inside the application, you can access as /proc/self/maps
) gives the address space in virtual memory of the process. That space has been set-up by execve
and changed by various syscalls (mostly mmap(2) and related syscalls, which also get called by the dynamic loader ld-linux(8), by malloc(3), etc... etc...). Be aware of ASLR...
Your question shows that you probably should take several hours to read Advanced Linux Programming to understand more the relevant concepts and the terminology.
Stack and heap don't exist inside the C code, they makes sense only inside a process! If you want to measure them from inside your application, use various files from /proc/self/
notably read sequentially /proc/self/maps
, /proc/self/status
, /proc/self/stat
, /proc/self/statm
etc... From outside the application, use /proc/$PID/
(e.g. /proc/1234/
for the process of pid 1234), and also use the pmap(1) command (which queries /proc/
). See also this.
BTW, one cannot in general get the lowest upper and highest lower bound[s] of stack and heap usage by static examination and analysis of the C source code (this is an intractable or an undecidable problem). See Rice's theorem...
PS. The ps
, top
, pmap
(etc ....) commands should be run (and the /proc
filesystem should be accessed) on the Linux system running your executable built from your C code. If you are using Linux/x86-64 to cross-compile for an Android/ARM or a embedded Linux/ARM board, you should have /proc/
mounted on that embedded board, and you should run (remotely, using ssh
or telnet
etc....) the commands on that board.

- 1
- 1

- 223,805
- 18
- 296
- 547
-
Its not clear for me.. Could you plz explain me where will I find the stack and heap memory usage of the C code that I'm executing? – Siva Jan 02 '14 at 08:48
-
Please take several hours to follow all the references. As you ask it, **your question does not make any sense**: process, stack, heap are dynamic (at runtime), C code is static (at compile time). – Basile Starynkevitch Jan 02 '14 at 09:11
-
I'm asking about the procedure to find the memory used by the code(i.e stack and heap) during the run time only. – Siva Jan 02 '14 at 09:21
-