4

I was asked to write a c/cpp program to find size of any process in an interview. can any one tell me how this could be achieve ?

p.s.- before marking the que as duplicate - plz read it carefully : I have asked how to find via c/cpp program not just with any unix/linux shell command

ashish
  • 211
  • 1
  • 7

3 Answers3

2

You can make use of getrusage. But keep in mind that it is not implemented on all systems.

Or by reading the /proc/[pid]/statm

Otherwise, try one of these (command line options).

Community
  • 1
  • 1
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
1

It's not part of standard C++ and thus depends on the operating system.

On linux for example that is done by accessing /proc filesystem.

Another option is of course to just call a system command like ps and parse its output (that is is what I'd do in a Python script).

Being able to interpret the numbers you can get is however another non trivial problem.

6502
  • 112,025
  • 15
  • 165
  • 265
0

Use

size <executable> 

Output

   text    data     bss     dec     hex filename
1361623    1984    2708 1366315  14d92b <executable>

It shows text, data, bss and total size

user207064
  • 665
  • 5
  • 14