-6

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?

Siva
  • 141
  • 1
  • 2
  • 12

3 Answers3

2

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.

Chinna
  • 3,930
  • 4
  • 25
  • 55
2

Why not the getpid() method defined in unistd.h?

#include <unistd.h>
int main(){
    int pid = getpid();
    int parentsPID = getppid();
    return 0;
}
Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
1

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 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
  • I've answered to that above. Please follow the links – Basile Starynkevitch Jan 02 '14 at 09:22