Help: C system call that returns list of all processes in Unix 
Author Message
 Help: C system call that returns list of all processes in Unix

Hi,

I have read several C and Unix books, but can not find an
appropriate system call from C that can return the list of all processes
in a machine (i.e somewhat like ps -aux output).  I know I can use
system("ps -aux"); but I think this cost to much, especially if
it has to be executed very often.

Is there such call from C? If I have to write it, any suggestion
to make it easier?

Thanks in advance for any response.

Fredy
--
Fredy Tandiary



Fri, 16 Aug 1996 14:57:45 GMT  
 Help: C system call that returns list of all processes in Unix

Quote:

>Hi,

>I have read several C and Unix books, but can not find an
>appropriate system call from C that can return the list of all processes
>in a machine (i.e somewhat like ps -aux output).  I know I can use
>system("ps -aux"); but I think this cost to much, especially if
>it has to be executed very often.

>Is there such call from C? If I have to write it, any suggestion
>to make it easier?

I picked this up on the net a few weeks ago.  I am *not* the original author.
I think it does what you want:

#include <stdio.h>
#include <signal.h>
#include <errno.h>

extern int      errno;

#define MAX_PROCESSES   65535

int     main()
{
   int  i;

   i = 0;
   while (i < MAX_PROCESSES)
   {
      if (kill(i,0) < 0)
         if (errno != ESRCH)
            printf("%d\n",i);
         else;
      else
         printf("%d\n",i);
      i++;
   }

Quote:
}



Sat, 17 Aug 1996 22:49:24 GMT  
 Help: C system call that returns list of all processes in Unix
a previous followup included code to linearly and exhaustivly search
the space of available pids, using kill(pid,0) to find live processes.
while this works, that's a lot of system calls.  following is some code
which simply scans the output of (a sysv-ish, hpux) ps command, probably
incurring much less overhead, assuming a relativly small number of processes.

#include <stdio.h>
#include <unistd.h>
#define MAX_LINE 1000

int main() {
    char line[MAX_LINE];
    FILE *fp = popen("ps -elf","r");
    while (fgets(line,MAX_LINE,fp)) {
        int pid;
        if (sscanf(line,"%*s %*s %*s %d",&pid) == 1) {
            printf("%d\n",pid);
        }
    }
    return 0;

Quote:
}

regards, mark hahn.
--



Mon, 19 Aug 1996 05:34:38 GMT  
 Help: C system call that returns list of all processes in Unix

Quote:
>I picked this up on the net a few weeks ago.  I am *not* the original author.
>I think it does what you want:

Unless he

        1) only wants the process ID's of the processes, not all the
           other stuff you get from e.g. "ps -aux";

and

        2) doesn't want it all that quickly;

no, it doesn't do what he wants.



Tue, 20 Aug 1996 08:43:29 GMT  
 Help: C system call that returns list of all processes in Unix

|>
|>I have read several C and Unix books, but can not find an
|>appropriate system call from C that can return the list of all processes
|>in a machine (i.e somewhat like ps -aux output).  I know I can use
|>system("ps -aux"); but I think this cost to much, especially if
|>it has to be executed very often.
|>
|>Is there such call from C? If I have to write it, any suggestion
|>to make it easier?
|>

There is no system call or C library function for this.

There are several ways to do it, however. The usual one is peeking around
in /dev/kmem with knowledge of kernel internals. A somewhat better one is
to have a /proc filesystem and peek around there with normal file access
calls. However, both methods are badly broken by misdesign in the first
place. The first method requires access to kmem and is hardly any portable,
the second one requires such filesystem and knowlegde of its structure and
thus is hardly any portable. It's not the first time I cry out for a system
call aka VMS sys$getXXi() - I don't even expect someone to hear it :-/

--
+-o-+--------------------------------------------------------+-o-+
| o |               \\\- Brain Inside -///                   | o |
| o |                   ^^^^^^^^^^^^^^                       | o |

+-o-+--------------------------------------------------------+-o-+



Sat, 24 Aug 1996 00:17:22 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. directing unix system call "system"s output

2. Help trapping stdout from Unix system() call

3. unix system call to get PID

4. unix system call

5. Linux / Unix system call

6. sockets and select system call (UNIX)

7. Unix System calls

8. call system() in linux return code = 2560

9. HELP:perform system call or call an external program

10. Q: Return-values of system("...") calls

11. Catching termination of child process and system() call

12. Catching termination of child process and system() call

 

 
Powered by phpBB® Forum Software