问题
Is it possible to advice the kernel to perform an immediate context switch to a different thread, bypassing the scheduler logic?
For example, if I've just created a thread/process with clone
and I want to begin its execution straight away, renouncing the current context immediately, instead of waiting for the next time the kernel reschedules stuff.
Ideally, I'm looking for something identical to the thread_switch
call on the Mach kernel.
回答1:
You shouldn't be trying to control context switches, as the kernel probably knows much better than you when to do it. Specially since a context switch is considered a costly operation (due to cache invalidation, mostly).
But, if you really need to force a scheduler cycle, you can use the sched_yield() system call.
来源:https://stackoverflow.com/questions/10362953/forcing-a-context-switch-from-the-userland-on-linux