Search

using "for_each_process" in proc entry

The linux kernel, as any other system, has a lot of proccesses running at any given time.

The task_struct structure defined in sched.h stores all the details of every process that exists in the system, and all the processes in turn are stored in a circular double linked list.

Just to undertand the how to access the fields of task_struct and iterate through the various tasks in the system, here is a simlple module that creates a proc entry which when read, lists out all name and pid of all the processes running currently.

The code makes use of a macro called "for_each_process" which is defined in "linux/sched.h". This function iterate through the list of all the processes.

In the read_proc function, "for_each_process" is called, which assigns to the variable task_list, which is of the kind "struct task_struct", the task_struct structure of one process after another as they are stored in the linked list in the kernel.

The task_list in turn is used to print the name ,task_list->comm, and the pid, task_list->pid of the process.

To see the output use the following makefile.



Run the following commands


In the output you should see a list of process names and their pids for eg.



To understand about proc entries and creating proc entries you can refer to the posts "Creating proc entry"

Follow by Email