Reputation: 860
I am trying to read information from the /proc/<pid>/status
file (to get the memory used).
To do this, I open the file in read mode:
file = fopen("/proc/self/status", "r");
After this step, to get the memory, I read the line that starts with "VmRSS".
My problem is this:
Each time I read this line, it's the same value, even if the file has changed.
I am doing this to get real-time memory usage of my program. So I call fopen()
1 time,
and then I call fseek()
to go to the beginning of my file when I need updated information.
char line[128];
fseek(file, 0, SEEK_SET);
while (fgets(line, 128, file) != NULL)
{
//...
}
But, the file is not updated, unless I reopen it. I don't want to reopen it for performance reasons.
I tried to change "r" to "r+" (to have a "Open a file for update", according to the documentation of fopen()), but fopen returns NULL in this case.
So my question:
Do you have any idea on how my program can open a file and see changes from made by another programme (here the kernel) using only one call to fopen()
?
I use Ubuntu 12.04
Upvotes: 2
Views: 2006
Reputation: 41
Maybe you can open the /proc/<pid>/status
file with open but not fopen.
int fd = open('/proc/<pid>/status', O_RDONLY, MYF(0));
// read
seek(fd, 0L, MY_SEEK_SET, MYF(0));
// read
Upvotes: 0
Reputation: 6739
You need to reopen the file To avoid race conditions, proc is a file system in memeory and most /proc content are being fixed on open.
Upvotes: 4