Make a system call to get list of processes

2019-02-10 08:03发布

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?

2条回答
疯言疯语
2楼-- · 2019-02-10 08:51

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).

查看更多
爷的心禁止访问
3楼-- · 2019-02-10 09:02

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

查看更多
登录 后发表回答