一个线程可以被pre抢占在系统调用之中内核? [英] Can a thread be pre-empted in the midst of a system call to kernel?
问题描述
我运行2个线程(假设他们是暂时的pthreads)。 Thread_1()使一个用户定义的API调用最终确实在内核一些工作。 Thread_2()是完全在用户空间。
I'm running 2 threads ( assume they are pthreads for the moment) . Thread_1() makes a user-defined API call which ultimately does some work in the kernel . Thread_2() is totally in user-space.
我的问题是:能否Thread_2()开始,而API调用过程中,控制地方在内核中由pre-先发制人Thread_1()执行?如果不是,为什么,如果我想发生这种情况(因任何理由),我有什么做的?
My question is : Can Thread_2() start executing by pre-empting Thread_1() while the API call is in progress , the control is somewhere inside the kernel ? If not , why , and if I want this scenario to occur ( for any reasons ) , what do I have to do ?
推荐答案
如果你问是否像阻塞内核调用一个 FREAD()
要求磁盘IO可pre抢占,那么是的。
If you are asking whether a blocking kernel call like an fread()
which requires disk IO can be pre-empted, then yes.
更具体地说阻塞调用将基本上把Thread_1等待不管它在等待入睡。如果Thread_1是睡着了,然后Thread_2将被安排运行(除非有更高优先级的等待运行的东西)。
More specifically a blocking call will basically put Thread_1 to sleep while waiting for whatever it's waiting for. If Thread_1 is asleep then Thread_2 will be scheduled to run (unless there's something of higher priority waiting to run).
编辑:如果您希望的方式是相当有信心的Thread_1正在执行阻塞调用,使Thread_2优先级低于Thread_1(因此一般的不运行,除非Thread_1被阻塞),它运行时,它提升它的优先级比Thread_1一个更高的水平,直到硬件中断已交付使用,此时它降低其优先级和呼叫 SCHED_YIELD()
。
If you want a way to be "fairly confident" that Thread_1 is performing a blocking call, make Thread_2 lower priority than Thread_1 (so that it generally doesn't run unless Thread_1 is blocked) and when it runs, it elevates its priority to a higher level than Thread_1 until the hardware interrupt has been delivered, at which point it lowers its priority and calls sched_yield()
.
这篇关于一个线程可以被pre抢占在系统调用之中内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!