Linux API to list running processes?

I need a C/C++ API that allows me to list the running processes on a Linux system, and list the files each process has open.

I do not want to end up reading the /proc/ file system directly.

Can anyone think of a way to do this?


http://procps.sourceforge.net/

http://procps.cvs.sourceforge.net/viewvc/procps/procps/proc/readproc.c?view=markup

Is the source of ps and other process tools. They do indeed use proc (indicating it is probably the conventional and best way). Their source is quite readable. The file

/procps-3.2.8/proc/readproc.c

May be useful. Also a useful suggestion as posted by ephemient is linking to the API provided by libproc , which should be available in your repo (or already installed I would say) but you will need the "-dev" variation for the headers and what-not.

Good Luck


If you do not want to read from '/proc. Then you can consider writing a Kernel module which will implement your own system call. And your system call should be written so that it can obtain the list of current processes, such as:

/* ProcessList.c 
    Robert Love Chapter 3
    */
    #include < linux/kernel.h >
    #include < linux/sched.h >
    #include < linux/module.h >

    int init_module(void)
    {
    struct task_struct *task;
    for_each_process(task)
    {
    printk("%s [%d]n",task->comm , task->pid);
    }

    return 0;
    }

    void cleanup_module(void)
    {
    printk(KERN_INFO "Cleaning Up.n");
    }

The code above is taken from my article here at http://linuxgazette.net/133/saha.html.Once you have your own system call, you can call it from your user space program.


If you don't do it, then I guess whatever API you will use will end up reading the /proc filesystem. Here are some examples of program doing this:

  • qps
  • htop
  • procps
  • But unfortunately, that does not constitute an API.

    链接地址: http://www.djcxy.com/p/79812.html

    上一篇: 在MacOS X上检索系统信息?

    下一篇: Linux API列出正在运行的进程?