Newsgroup: comp.unix.programmer


Date: Sat, 29 Apr 2006 11:35:39 +0300
From: Diomidis Spinellis <dds@aueb.gr>
Organization: Athens University of Economics and Business
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060404 SeaMonkey/1.0.1
MIME-Version: 1.0
Newsgroups: comp.unix.programmer
Subject: Re: Get number of running processes
References: <1146291748.035944.106200@j33g2000cwa.googlegroups.com>
In-Reply-To: <1146291748.035944.106200@j33g2000cwa.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Chris wrote:
> Hi,
> 
> I am looking for an easy way to find the total number of running
> processes on a given machine in a C program (in Linux).  Currently I am
> doing this:
> 
> system("ps -e | wc -l > count.txt");
> 
> then reading the number back into the program from the text file.  I
> was wondering if there was a function call that would do this (ie. int
> count = get_process_count()) rather than perform this  hackish
> workaround.

There is no portable way to do what you want.  Have a look at the source 
of ps(1) or top to see how specific systems store that information. 
However, you can use popen(3) instead of system to avoid going through a 
file.

-- 
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality>cup



Newsgroup comp.unix.programmer contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.