Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a system call to get list of processes

I'm new on modules programming and I need to make a system call to retrieve the system processes and show how much CPU they are consuming.

How can I make this call?

like image 954
douglasd3 Avatar asked Aug 23 '13 17:08

douglasd3


People also ask

How do I list all processes in Linux?

To list processes in Linux, use one of the three commands: ps, top or htop. Ps command provides static snapshot of all processes, while top and htop sorts by CPU usage.

Which system call is used for process?

Processes use the fork() system call to create processes that are a copy of themselves. This is one of the major methods of process creation in operating systems.

What is system call list and explain the process control system calls?

System call provides the services of the operating system to the user programs via Application Program Interface(API). It provides an interface between a process and operating system to allow user-level processes to request services of the operating system. System calls are the only entry points into the kernel system.

Which command in Unix is used to display the list of currently running processes?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time. This will display the process for the current shell with four columns: PID returns the unique process ID.


2 Answers

Why would you implement a system call for this? You don't want to add a syscall to the existing Linux API. This is the primary Linux interface to userspace and nobody touches syscalls except top kernel developers who know what they do.

If you want to get a list of processes and their parameters and real-time statuses, use /proc. Every directory that's an integer in there is an existing process ID and contains a bunch of useful dynamic files which ps, top and others use to print their output.

If you want to get a list of processes within the kernel (e.g. within a module), you should know that the processes are kept internally as a doubly linked list that starts with the init process (symbol init_task in the kernel). You should use macros defined in include/linux/sched.h to get processes. Here's an example:

#include <linux/module.h>
#include <linux/printk.h>
#include <linux/sched.h>

static int __init ex_init(void)
{
    struct task_struct *task;

    for_each_process(task)
        pr_info("%s [%d]\n", task->comm, task->pid);

    return 0;
}

static void __exit ex_fini(void)
{
}

module_init(ex_init);
module_exit(ex_fini);

This should be okay to gather information. However, don't change anything in there unless you really know what you're doing (which will require a bit more reading).

like image 57
eepp Avatar answered Oct 05 '22 19:10

eepp


There are syscalls for that, called open, and read. The information of all processes are all kept in /proc/{pid} directories. You can gather process information by reading corresponding files.

More explained here: http://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html

like image 45
Kevin Hu Avatar answered Oct 05 '22 19:10

Kevin Hu