问题描述
我试图理解Linux系统调用了sched_setaffinity()是如何工作的。这是一个后续的从我的问题here.
I am trying to understand how the linux syscall sched_setaffinity() works. This is a follow-on from my question here.
我有,这也解释了如何使用系统调用,并具有pretty整齐的(工作!)的例子。
I have this guide, which explains how to use the syscall and has a pretty neat (working!) example.
所以,我下载在Linux 2.6.27.19 。
So I downloaded the Linux 2.6.27.19 kernel sources.
我做了一个包含系统调用行的'grep'可以,我得到91结果。不看好的。
I did a 'grep' for lines containing that syscall, and I got 91 results. Not promising.
最后,我想理解内核是如何能够设置指令指针的为特定的核心的(或处理器)。
Ultimately, I'm trying to understand how the kernel is able to set the instruction pointer for a specific core (or processor.)
我熟悉如何单核心单线程程序的工作。有人可能会发出'JMP富'的指令,这基本上设置IP到'富'标签的内存地址。但是,当一个人有多个内核,一个人说取内存地址foo的下一条指令,并设置指令指针的芯数2 的开始执行那里。
I am familiar with how single-core-single-thread programs work. One might issue a 'jmp foo' instruction, and this basically sets the IP to the memory address of the 'foo' label. But when one has multiple cores, one has to say "fetch the next instruction at memory address foo, and set the instruction pointer for core number 2 to begin execution there."
在哪里,在装配code,是我们指定哪些核心执行该操作?
Where, in the assembly code, are we specifying which core performs that operation?
回到内核code:这里有什么重要呢?文件'的核心/ sched.c中有一个叫了sched_setaffinity()函数,但返回类型为长 - 这是与它的。那么,这里是重要的?显示了汇编指令发布了这些模块的?什么模块读'的task_struct',看'cpus_allowed成员,然后把这一到一个指令? (我还通过glibc的源翻阅 - 但我认为它只是使内核code调用来完成这个任务。)
Back to the kernel code: what is important here? The file 'kernel/sched.c' has a function called sched_setaffinity(), but returns type "long" - which is inconsistent with its manual page. So what is important here? Which of these modules shows the assembly instructions issued? What module is reading the 'task_struct', looking at the 'cpus_allowed' member, and then translating that into an instruction? (I've also thumbed through the glibc source - but I think it just makes a call to the kernel code to accomplish this task.)
推荐答案
了sched_setaffinity()
只是告诉调度它的CPU是允许运行在该进程/线程,然后要求重新安排。
sched_setaffinity()
simply tells the scheduler which CPUs is that process/thread allowed to run on, then calls for a re-schedule.
调度实际运行对CPU的每一个,所以它得到一个机会来决定什么任务特定CPU旁执行。
The scheduler actually runs on each one of the CPUs, so it gets a chance to decide what task to execute next on that particular CPU.
如果你有兴趣,你怎么能实际调用一些code上,其它CPU,我建议你先看看 smp_call_function_single()
。如果我们要调用的东西另一个CPU,这叫 generic_exec_single()
。后者只是添加功能目标CPU的呼叫队列,并强制通过一些的东西重新安排(如果队列为空)。
If you're interested in how you can actually call some code on other CPUs, I suggest you take a look at smp_call_function_single()
. In case we want to call something on another CPU, this calls generic_exec_single()
. The latter simply adds the function to the target CPU's call queue and forces a reschedule through some IPI stuff (if the queue was empty).
底线是:那里的 _jmp _
指令没有实际的SMP变体。相反,在其他CPU code运行合作,以完成任务。
Bottom line is: there no actual SMP variant of the _jmp_
instruction. Instead, code running on other CPUs cooperates in order to accomplish the task.
这篇关于怎么了sched_setaffinity()工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!