Running processes

This is a discussion on Running processes within the Linux Programming forums, part of the Platform Specific Boards category; Is there a function which could tell me the number of running processes? Thank you....

  1. #1
    pfs
    pfs is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Smile Running processes

    Is there a function which could tell me the number of running processes?

    Thank you.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    72
    wont "ps x" list the running proccesses? ;x or am i wrong?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by CornedBee View Post
    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.
    Then again, invoking "ps" and counting the number of lines produced is a decently POSIX-portable way of determining the number of running processes, instead of tying yourself to a particular /proc format.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by CornedBee View Post
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 01:26 PM
  2. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 11:59 PM
  3. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  4. Running simultaneous processes
    By marksw in forum Windows Programming
    Replies: 5
    Last Post: 05-19-2003, 06:07 AM
  5. RE: Find out running processes from command prompt
    By sampatel in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-18-2001, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21