gpt4 book ai didi

linux - 如何找到所有可运行的进程

转载 作者:行者123 更新时间:2023-12-03 09:51:38 25 4
gpt4 key购买 nike

我正在学习调度程序并尝试打印所有可运行的进程。所以我写了一个使用 for_each_process 的内核模块宏迭代所有进程,并打印处于“可运行”状态的进程。但这似乎是一种愚蠢(且效率低下)的方法。所以我考虑过获取对所有正在运行的队列的引用并使用它们的红黑树遍历可运行的进程,但找不到执行此操作的方法。

我发现每个 CPU 都有一个 sched_class 列表,它们是 stop_sched_class->rt_sched_class->fair_sched_class->idle_sched_class 并且每个 CPU 都有它有自己的运行队列。但无法找到一种方法来达到他们所有人。

我使用了使用 tasks_timeline 的模块来查找所有可运行的进程,打印正在运行的队列的地址 - 似乎我有 3 个正在运行的队列(虽然只有两个处理器)。

模块:

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/sched.h>

MODULE_LICENSE("GPL");

struct cfs_rq {
struct load_weight load;
unsigned int nr_running, h_nr_running;
};

void printList(void){
int count;
struct task_struct * tsk;

count = 0;
for_each_process(tsk){
if(tsk->state)
continue;
printk("pid: %d rq: %p (%d)\n", tsk->pid, tsk->se.cfs_rq, tsk->se.cfs_rq->nr_running);
count++;
}
printk("count is: %d\n", count);
}

int init_module(void)
{
printList();

return 0;
}

void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world proc.\n");
}

输出:

[ 8215.627038] pid: 9147 ffff88007bbe9200 (3)
[ 8215.627043] pid: 9148 ffff8800369b0200 (2)
[ 8215.627045] pid: 9149 ffff8800369b0200 (2)
[ 8215.627047] pid: 9150 ffff88007bbe9200 (3)
[ 8215.627049] pid: 9151 ffff88007bbe9200 (3)
[ 8215.627051] pid: 9154 ffff8800a46d4600 (1)
[ 8215.627053] count is: 6
[ 8215.653741] Goodbye world proc.

关于电脑:

$ uname -a
Linux k 3.13.0-39-generic #66-Ubuntu SMP Tue Oct 28 13:30:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ cat /proc/cpuinfo | grep 'processor' | wc -l
2

所以我的问题是:

  1. 如何以更好的方式打印所有可运行的进程?
  2. 运行队列是如何创建和管理的?
  3. 正在运行的队列是否以某种方式相互关联? (如何?)

最佳答案

$ps -A -l 并找到进程状态 (R) 和进程标志 (1) 都如前所述的实例。

关于linux - 如何找到所有可运行的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26862357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com