Linux c++: apis vs /proc files?

非 Y 不嫁゛ 提交于 2019-12-03 11:53:27

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.

I though that /proc was the API (everything is a file...)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!