I want to parse /proc/net/tcp/
, but is it safe?
How should I open and read files from /proc/
and not be afraid, that some other process (or the OS itself) will be changing it in the same time?
I want to parse /proc/net/tcp/
, but is it safe?
How should I open and read files from /proc/
and not be afraid, that some other process (or the OS itself) will be changing it in the same time?
The procfs API in the Linux kernel provides an interface to make sure that reads return consistent data. Read the comments in
__proc_file_read
. Item 1) in the big comment block explains this interface.That being said, it is of course up to the implementation of a specific proc file to use this interface correctly to make sure its returned data is consistent. So, to answer your question: no, the kernel does not guarantee consistency of the proc files during a read but it provides the means for the implementations of those files to provide consistency.