Is there a function which could tell me the number of running processes?
Thank you.
Is there a function which could tell me the number of running processes?
Thank you.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}