像我儿子一样永远保持好奇和梦想

linux是抢占性的操作系统么?

上一篇 / 下一篇  2012-06-28 12:14:49 / 个人分类:technique_knowall

最近到一家公司面试嵌入式职位【公司名字就不提了】,被这个问题给问蒙了:
bX6s'E,G(S0
他问:ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台7IA3O4X2|W!e1n;t4N]
UCOS 和 Linux 那个是抢占式的操作系统:ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台m6o5a@~0z{ `
一直认为没去研究过这个概念,结果混乱答到:linux应该是强占式的吧?
-u+_c!\-K7y!Uu.S0
4F9y.t%e7F uN0结果,面试官回答,错,ucos是,而linux不是。。。。Linux是按照时间片輪巡。。。
O6Hl7{"F$t0汗,这个职位本来很适合,结果黄了ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台$D3~2~bF5j Z;|

今天研究了一下定义:简单讲,ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台_2^)Xd"I
所谓非抢占,就是如果在当前进程被中断剥夺控制权后,无论如何都能回到原来的任务。任务有优先级别。可用不可重入函数ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台3z3FZj7sm+d*X
反之,抢占任务就是当前低优先级别的任务会被高优先级别任务打断。ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台1b!ba3Z/GI)yD

YfJ7?+HL5~#]H0
Sb#?AD/u8J)`-E-G,_0
至于 linux ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台o%P*A t q

P#Fg^9}t^C0Linux抢占式内核就是由Robert Love修改实现的。在他的书中有如下描述:ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台7j7W2] dV){n:B
-----------
PL_.M9m0 User Preemption
n/QK!N th3yhF0 User preemption occurs when the kernel is about to return to user-space, need_resched is set, and therefore, the scheduler is invoked. If the kernel is returning to user-space, it knows it is in a safe quiescent state. In other words, if it is safe to continue executing the current task, it is also safe to pick a new task to execute. Consequently, whenever the kernel is preparing to return to user-space either on return from an interrupt or after a system call, the value of need_resched is checked. If it is set, the scheduler is invoked to select a new (more fit) process to execute. Both the return paths for return from interrupt and return from system call are architecture dependent and typically implemented in assembly in entry.S (which, aside from kernel entry code, also contains kernel exit code).
rT$i0~+T1}9Y0 In short, user preemption can occur
9Q7o'BP wZ6Z(i0 When returning to user-space from a system call
y"E;h6\ c"R-Sl0 When returning to user-space from an interrupt handlerET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台aD1b'dCj;y(VMK

/lS9y2f;KN`0 Kernel Preemption
3O/n%I$P3JD!u0 The Linux kernel, unlike most other Unix variants and many other operating systems, is a fully preemptive kernel. In non-preemptive kernels, kernel code runs until completion. That is, the scheduler is not capable of rescheduling a task while it is in the kernelkernel code is scheduled cooperatively, not preemptively. Kernel code runs until it finishes (returns to user-space) or explicitly blocks. In the 2.6 kernel, however, the Linux kernel became preemptive: It is now possible to preempt a task at any point, so long as the kernel is in a state in which it is safe to reschedule.
2g1Ao)it n;i+P0 So when is it safe to reschedule? The kernel is capable of preempting a task running in the kernel so long as it does not hold a lock. That is, locks are used as markers of regions of non-preemptibility. Because the kernel is SMP-safe, if a lock is not held, the current code is reentrant and capable of being preempted.
S']-d5vg$a,G0 The first change in supporting kernel preemption was the addition of a preemption counter, preempt_count, to each process's thread_info. This counter begins at zero and increments once for each lock that is acquired and decrements once for each lock that is released. When the counter is zero, the kernel is preemptible. Upon return from interrupt, if returning to kernel-space, the kernel checks the values of need_resched and preempt_count. If need_resched is set and preempt_count is zero, then a more important task is runnable and it is safe to preempt. Thus, the scheduler is invoked. If preempt_count is nonzero, a lock is held and it is unsafe to reschedule. In that case, the interrupt returns as usual to the currently executing task. When all the locks that the current task is holding are released, preempt_count returns to zero. At that time, the unlock code checks whether need_resched is set. If so, the scheduler is invoked. Enabling and disabling kernel preemption is sometimes required in kernel code and is discussed in Chapter 9.ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台VY*hig9u&n
Kernel preemption can also occur explicitly, when a task in the kernel blocks or explicitly calls schedule(). This form. of kernel preemption has always been supported because no additional logic is required to ensure that the kernel is in a state that is safe to preempt. It is assumed that the code that explicitly calls schedule() knows it is safe to reschedule.ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台$U;_Sb C x&S)^Y
Kernel preemption can occur
A/?^O5Hc,h0 When an interrupt handler exits, before returning to kernel-space
h(Qd }~A4AI0 When kernel code becomes preemptible again
-Jk~Y9}7N"d,}8@ S0 If a task in the kernel explicitly calls schedule()
}\F0jzSv0 If a task in the kernel blocks (which results in a call to schedule())
AzApMU/E0
综述:
eep9U:{+b;qFV K&C Q0所谓Linux是不是抢占式应该是伪命题,下次,在有那个面试官自以为是的吓唬你,你就反问:ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台7eaDV GQIW
您知道Robert Love么,他如果不知道,你就教育一下他:上面的内容,然后告诉他,纠结抢占式和非抢占式意义不大。或者教育一下,您知道什么是抢占式么?ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台ST3i g2l(M-C*i
正真系统嵌入式构架考虑的还是系统整个应用的需求ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台"Un'h)g-SoYLT}
ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台VHMPSKDg

ref:ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台XZ9U/V;hMY8?
1 ucosll the real time kernel by jean j .labrosse / 贝贝翻译ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台 Atq q?6qD O
2 Robert Love the linux kernel designer ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台{8B ~ns[L*s
3 small rtos51 立功兄ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台 W;LQ fk+X
4 http://www.oldlinux.org/oldlinux/viewthread.php?tid=3024
Ob A3iy+u0ET创芯网(EETOP)-电子设计威尼斯人网址、博客、超人气的电子工程师资料分享平台%[#x3R!A8W6_m6\mm?

$R/rN9^"k9N^(LHc0

TAG: linux Linux LINUX 操作系统

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar
博聚网