I'm implementing an alternative to top, and thought that I should preallocate an appropriately sized buffer to hold the read contents of /proc/PID/stat.

However, knowing that it's a pseudo filesystem where contents of files will be generated on-demand, will simply statting that file result in the kernel doing work to generate its contents, so that a file size is returned?

1 Answer 1


From stat(2):

For most files under the /proc directory, stat() does not return the file size in the st_size field; instead the field is returned with the value 0.

So, no, it does not, and so it seems to be impossible to exactly allocate the buffer size needed. But given the small and predictable nature of these files, a couple KiB seems to be more than enough.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .