How to programatically get uid from pid in osx using c++?

前端 未结 5 1994
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 14:01

Given a pid, I want to find the owner of the process (as uid). Is there a way to get this in osx (or any unix) using C++?

Google didn\'t help. \'ps\' is able to do i

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 14:35

    You could look at how ps does it. It looks like it uses the kvm_getprocs function.

    However, it's much more portable (you said "any unix", but e.g. the Linux and Solaris way is to look in the /proc filesystem - and other unixes may have different APIs) to just parse the output of ps (ps -o user= -p (pid) for example, to eliminate any extraneous output) than to do any system-specific process stuff

提交回复
热议问题