views:

450

answers:

3

How and Where does linux-kernel read proc/pid file which shows all processes in the system. I found linux-source-2.6.31/fs/proc/ Here there are files, but it is hard to understand because it is really complicated. Can someone knows, how it works?

A: 

Look in your /proc directory, there is a virtual file in there that lists all processes running in the system, even the binary program ps actually opens up that file in the /proc directory to output the listing of processes/pids..

Linux ProcFs Guide Linux Proc Filesystem as a Programmer's Tool

tommieb75
+5  A: 

/proc is a pseudo-filesystem, meaning that its contents are not "real" files. Instead, the contents are a representation of the kernel's internal data structures. Therefore the kernel doesn't need to read them - it can already access the data directly.

/proc is used by user-mode (i.e. non-kernel) programs such as ps to find out (for instance) about processes running on the system. There's a man page that describes much of what's available.

SimonJ
+1  A: 

You're looking in the right place.

Specifically, the function proc_pid_readdir() in fs/proc/base.c is used to fill out the list of pid entries when the /proc root directory is read. You can see the basic loop around all processes and tasks in that function:

ns = filp->f_dentry->d_sb->s_fs_info;
iter.task = NULL;
iter.tgid = filp->f_pos - TGID_OFFSET;
for (iter = next_tgid(ns, iter);
     iter.task;
     iter.tgid += 1, iter = next_tgid(ns, iter)) {
    filp->f_pos = iter.tgid + TGID_OFFSET;
    if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
        put_task_struct(iter.task);
        goto out;
    }
}
caf