【发布时间】:2012-12-21 11:56:57
【问题描述】:
我正在尝试了解 Linux 内核的调度程序是如何工作的
如链接所示
http://books.google.co.in/books?id=NXVkcCjPblcC&lpg=PP1&pg=PA47#v=onepage&q&f=false 和以下链接 http://www.informit.com/articles/article.aspx?p=101760&seqNum=2
struct runque 是调度程序运行的基本数据结构
是的
struct runqueue {
spinlock_t lock; /* spin lock which protects this
runqueue */
unsigned long nr_running; /* number of runnable tasks */
unsigned long nr_switches; /* number of contextswitches */
unsigned long expired_timestamp; /* time of last array swap */
unsigned long nr_uninterruptible; /* number of tasks in
uinterruptible sleep */
struct task_struct *curr; /* this processor's currently
running task */
struct task_struct *idle; /* this processor's idle task */
struct mm_struct *prev_mm; /* mm_struct of last running task
*/
struct prio_array *active; /* pointer to the active priority
array */
struct prio_array *expired; /* pointer to the expired
priority array */
struct prio_array arrays[2]; /* the actual priority arrays */
int prev_cpu_load[NR_CPUS];/* load on each processor */
struct task_struct *migration_thread; /* the migration thread on this
processor */
struct list_head migration_queue; /* the migration queue for this
processor */
atomic_t nr_iowait; /* number of tasks waiting on I/O
*/
}
上面有两个成员
struct prio_array *active; /* pointer to the active priority
array */
struct prio_array *expired; /* pointer to the expired priority array */
struct prio_array 定义为
struct prio_array {
int nr_active; /* number of tasks */
unsigned long bitmap[BITMAP_SIZE]; /* priority bitmap */
struct list_head queue[MAX_PRIO]; /* priority queues */
};
下面这句话我不清楚
问题1)Each priority array contains one queue of runnable processors per priority level.
在上述struct prio_array的定义中可运行处理器的队列在哪里
然后它说
The priority arrays also contain a priority bitmap used to高效地发现系统中优先级最高的可运行任务。
然后它说 “有 140 个优先级和 32 位字,这是五个。”
怎么得出结论,这就是5 背后的数学计算是什么?
以上内容摘自本书第 4 章,在第二个链接中发布,均包含相同的文字。为清楚起见,仅在此处发布。
*更新1 * 基于 cmets 我只是想澄清我在问什么 作者说
BITMAP_SIZE 是 unsigned long 类型数组的大小 变量必须为每个有效优先级提供一位 等级。有 140 个优先级和 32 位字,这是五个。
问题 2)
我不清楚的是每个优先级都给出了一位,并且有 140 个优先级,所以数组大小如何 5 我没有得到 BITMAP_SIZE 计算的逻辑不是 140/32=5
跟后面的段落有些关系
When a task of a given priority becomes runnable (that is,
its state becomes TASK_RUNNING), the corresponding bit in the
bitmap is set to one. For example, if a task with priority seven is
runnable, then bit seven is set
在数组所在的链接上
unsigned long bitmap[BITMAP_SIZE]; /* priority bitmap */
设置所以基本上我不清楚的是这个数组是如何设置的,如果我能够正确解释,也请参阅问题 1。
UPDATE 2 和下面的答案解释
通过下面的答案,我只是添加一个小解释,它可能会在未来帮助一些人 如果他们基本上来这里
调度程序维护一个运行队列和可运行进程列表,每个可运行进程恰好在一个运行队列上,我给出的链接的文章考虑了具有许多运行队列的多处理器系统,回到我们的情况,一个处理器和一个运行队列具有不同优先级的进程
有 140 个优先级,每个优先级在 TASK_RUNNING 状态下都有不同的进程,例如可以有许多优先级为 8 的进程,依此类推(我以 8 为例) struct runque 指向优先级数组,它告诉
btimap[BITMAP] /* this is the priority level
struct list_head /* points to the start of list of processes of that run level
所以 runque 指向优先级数组,从优先级数组中你可以很容易地得到进程 需要在 O(1) 时间内执行。
【问题讨论】:
-
你需要 5 个字来保存 140 位?
-
看到 2 的 5 次方是 32 那么它是 140 是多少
-
我宁愿把它看成 140/32 略多于 4 个字,所以让它变成 5 个。
-
一个 word 是 32 位。作者说 140 位需要五个 单词。
-
好吧,我明白了,作者说 `BITMAP_SIZE 是无符号长类型变量数组必须为每个有效优先级提供一个位的大小。有 140 个优先级和 32 位字,这是 5 个。我不清楚的是每个优先级都给出一个位,并且有 140 个优先级,所以数组大小如何达到 5 我没有得到 BITMAP_SIZE 的逻辑计算不是 140/32=5
标签: c linux-kernel operating-system scheduler