how to get list of processes

Hi,

How can I get a list of all the running processes, in C?

Short answer, depends on platform and a bunch of other things.

The full, unadorned ugly truth is the source for ps(1) for your computer.

Linux: procps - Home Page
FreeBSD (some version): http://ftp.nl.freebsd.org/os/FreeBSD/development/FreeBSD-CVS/src/bin/ps/ps.c,v
(sorry, that's the raw CVS file, but you can cope)

really? no cross-unix way to do it? I thought it was pretty fundamental...

Thanx anyway.

Linux is not Unix. I guess the kvm_* stuff in BSD is pretty much portable to other "real" Unices.

You can use the popen function to get the output of the ps command.

Regards

era,

whats "kvm_*" stuff?

Hmm, you didn't look at the code? It's the FreeBSD link up there; click click.

If your flavor of Unix supports /proc filesystem then it should be pretty easy to get a little bit of information.

do "cd /proc" and if it works, do an "ls" you should see a bunch of folders, one per running pid. You can read this directory in c like any other (see man page for opendir).

If the flavor of Unix you are using doesn't support this, then you are hosed and back to either a popen for "ps" or looking up the source for it.

Hm, this indeed seems an easy way to get a list of process-IDs. However, the information inside /proc/<processID> seems to be platform-dependent (and hard to understand anyway). Is there perhaps a way to get information about a process with a specific known ID?

There are different variants of how /proc works but on Linux for example, /proc/12345 contains information about process 12345; if that is not what you want, then perhaps you can explicate on what in particular you are looking for.

Also look at the proc(5) manual page, which documents the facility. (On Linux, at least, the documentation there was somewhat stale, last time I looked; but in general, it's probably correct.)

$ top

$ pstree

Good luck

Can someone please answer my query?

I need to know whether I can find out the complete list of processes that ran on a server instance in a UNIX Box under a particular user ID on a particular date.

This should have been a new thread. Please don't club this under an existing one.

Coming back to the question.

Yes, its possible. Only tweak needed is to extract process information and apply all the filters that you are interested in.

just try in ur c code
system(ps -eaf);
it will fill ur screen with the list of process running on ur unix system..