How to find number of children of a process in C? -
i doing quite lot of forking in process (and children of process further forking), , want keep acceptable limit on total number of processes create.
is there (preferably efficient) way of finding total number of children of process (including children of children, children of children of children, etc.) c?
i code work on both linux , mac, no /proc
i'm afraid!
there no way enumerate children of process, except enumerating processes of system , checking ppid. (of course, parent itself, can keep track of fork.) there no way @ enumerate descendants of process: if p forks q forks r q dies, there no more information relate p r.
the portable way obtain information processes call ps
utility , parse output.
if want limit number of descendants of process, can using dedicated user run process, , starting ancestor desired limit on processes per user (setrlimit(rlimit_nrproc, …)
).
you can use shared resource of kind; work long descendant processes don't close resource. example, can open file (without o_cloexec
flag), if descendants don't call fcntl
fd_cloexec
flag on file nor go , close it. think on osx you'll need fork fuser
or lsof
(either work on linux too) find out how many processes have file open, don't know of way without forking on osx. might investigate other mechanisms such shared memory (shm_open
, friends) or memory mappings (mmap
, friends), these don't know of way use count without forking either.
Comments
Post a Comment