问题
Im working on an app to collect and send various bits of system info (partition space/free, laptop battery info, etc). Im not having much success getting this information in the form of direct c++ api.. though its all available via files in /proc (or similar).
So - I'm wondering whether reading/parsing these files in my c++ app is the appropriate way to get this info or should I keep trying to discover APIs? ( NOTE: I am working with statvfs ).
So far it looks like it's easier to gather this sort of info in Win32. Seems strange.
回答1:
It is best practice by far to stick with an API in the following order of precedence.
Your language API (not much help for you here, but say for strings, a C99 string function is better to use than a library string facility specified by a Posix or other OS standard.)
Posix operating software APIs
Documented kernel API's
Undocumented kernel APIs (at least these will break, say, ioctl users if they change, so they probably won't change)
/proc
/dev/kmem
,/dev/mem
There is no reason to believe that /proc
trolling will be portable or even the same from release to release. Not every system will even have a /proc
mounted!
Having said all that, it is much easier to just scrape stuff off of /proc
and if it's the only available interface then you should go ahead and use it.qa
Finally, the ordering of the last two isn't completely clear, because /proc
isn't available for a post-mortem kernel crash dump analysis, but tools that can peek in the core dump will still work.
回答2:
I though that /proc was the API (everything is a file...)
回答3:
As you have noticed, a lot of Linux systems information is in /proc
. And you're correct that there often isn't a C API for retrieving that information (though there is usually a shell command if you're inclined to stick with bash instead of C++). In the worst-case scenario, you might be stuck parsing through /proc
, though you might be able to get some sample code in the form of open-source shell commands for the particular item you want.
来源:https://stackoverflow.com/questions/4650998/linux-c-apis-vs-proc-files