如何找到所有可运行的进程 [英] How to finding all runnable processes
问题描述
我正在了解调度程序,并尝试打印所有可运行的程序.因此,我编写了一个内核模块,该模块使用 for_each_process 宏以遍历所有进程,并在可运行"状态下打印这些进程.但这似乎是一种愚蠢(且效率低下)的方法.因此,我想到了对所有正在运行的队列的引用,并使用它们的Red-Black-Tree来遍历可运行的进程,但是找不到找到此方法的方法.
I'm learning about the scheduler and trying to print all runnable proceeses. So I have written a kernel module that uses the for_each_process macro to iterate over all processes, and prints the ones at "runnable" state. But this seems like a stupid (and inefficient) way of doing this. So I thought about getting a reference to all running queues and use their Red-Black-Tree to go over the runnable processes, but couldn't find a way to do this.
我发现每个CPU都有一个sched_class
列表,它们是stop_sched_class->rt_sched_class->fair_sched_class->idle_sched_class
,并且每个都有自己的运行队列.但是找不到一种方法可以达到所有目标.
I have found out that there is a list of sched_class
s for each CPU which are stop_sched_class->rt_sched_class->fair_sched_class->idle_sched_class
and each one of them has it's own running queue. But couldn't find a way to reach them all.
我使用了使用tasks_timeline
的模块来查找所有可运行的进程,以打印正在运行的队列的地址-看来我有3个正在运行的队列(而只有两个处理器).
I have used the module that uses the tasks_timeline
to find all runnable processes, to print the address of the running queues - seems I have 3 running queues (while having only two processors).
模块:
#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
所以我的问题是:
- 如何更好地打印所有可运行的进程?
- 如何创建和管理正在运行的队列?
- 正在运行的队列是否以某种方式相互链接? (如何?)
推荐答案
$ps -A -l
并找到实例,其中提到了过程状态(R)和过程标志(1).
$ps -A -l
and find the instance where both the process state (R) and the Process Flags (1) are as mentioned.
这篇关于如何找到所有可运行的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!