1

I want to fetch the memory consumed by each process in OSX. I have tried to call below function but "proc_pidinfo" call is getting failed for some of the processes. It returns value 0 when it fails.

    int main(int argc, char **argv)
    {
        if(argc != 2) {
            puts("Usage: process_detail <pid>");
            return 1;
        }

        struct proc_taskallinfo info;

        int ret = proc_pidinfo(atoi(argv[1]), PROC_PIDTASKALLINFO, 0, (user_addr_t) &info, sizeof(struct proc_taskallinfo));
        printf("ret=%d, result=%s, rss memory: %lld\n", ret, (char *) info.pbsd.pbi_comm, info.ptinfo.pti_resident_size);

        return 0;
    }

May i know why "proc_pidinfo" is getting failed for some of the pids. ? Can i implement any other way ?

Thank you in advance.

Neel
  • 451
  • 1
  • 9
  • 23
  • (a) Provide a [mre]. (b) See [this question](https://stackoverflow.com/questions/37512486/osx-proc-pidinfo-returns-0-for-other-users-processes). (c) What value did `proc_pidinfo` return in the failing cases? (d) `proc_pidinfo` does not appear to be a function documented by Apple as a public interface. If so, why do you expect it to do anything for you? – Eric Postpischil May 20 '20 at 17:30
  • a) updated minimal program. b) that link will not be useful because that structure does not contain memory information for each process. c) It returns 0 value when fail, I updated main description d) Ok, is there any other alternative, we can fetch the those informations. – Neel May 21 '20 at 09:01
  • A [mre] should compile without error, and preferably without warnings (except when the problem being asked about is itself a compiler error). When compiling the code currently in the question with `clang -o file file.c`, I get four errors and four warnings. – Eric Postpischil May 21 '20 at 10:33
  • Nonetheless, when I include ``, ``, ``, and `` and insert `perror(NULL);` after the call to `proc_pidinfo`, the program compiles with only one warning, and the output shows “Operation not permitted” for a case I tested. When the same case is tested using `sudo` to run the program, the program succeeds in getting the information. This is consistent with the permissions issue mentioned in the question I linked to (`proc_pidinfo` works only for your own processes), and you should evaluate using alternative mentioned in one of the answers to that question. – Eric Postpischil May 21 '20 at 10:42
  • Right. That will be alternative mentioned but that "proc_bsdshortinfo" structre is not giving memory information about the process.In that solution they have passed "PROC_PIDT_SHORTBSDINFO" to proc_pidinfo which is allowed in its implementation. – Neel May 21 '20 at 11:16

0 Answers0