Is there a function which could tell me the number of running processes?
Thank you. :D
Printable View
Is there a function which could tell me the number of running processes?
Thank you. :D
I don't believe there is a direct function to do that - and even if you do get a number for it, there is nothing to say that some process hasn't been started or killed between you asking for the count, and your use of the count (e.g. displaying it, storing it somewhere, or whatever you wanted to do with it), so at the very best, it would be an INDICATION.
From what I understand, you'd be wanting to scan the /proc directory for numeric entries - the entries are the process ID of the process itself. Or if you ONLY need the count of processes, you could read /proc/loadavg
--
Mats
wont "ps x" list the running proccesses? ;x or am i wrong?
Yes, but how does ps do it?
Generally, calling upon an external process and parsing its output is a very slow method of doing things, more appropriate for a shell script than a C/C++ program.
That's true, too. Insofar as ps is reliable, because it's one of the more quirky Unix commands.
The fundamental problem is still the race condition, of course. During the build of a large project, my system tends to spawn and terminate several dozen processes per second.
Well, sure, but aside from having some kind of global process-creation lock, that's just an insolvable problem. I can't imagine any real-world scenario where it's absolutely critical to know the number of processes running at any particular instant, but having a general idea could be useful.