Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yielding from linux kernel

I have a real-time thread in Linux (3.4). Under certain conditions, I want it to relinquish control to other threads with the same priority, even if it hasn't finished using up its current timeslice. I was thinking of using the following code:

if (condition) {
    resched_task();
    cond_resched();
}

however, I don't see anyone else in the code doing this, making me think there's some other (better?) way of doing this. Is there a standard way to do this?

like image 458
John Avatar asked Feb 13 '14 16:02

John


People also ask

What is yielding in Linux?

You can use the sched_yield() function to yield the rest of your time slice, as discussed here. sched_yield() causes the calling thread to relinquish the CPU. The thread is moved to the end of the queue for its static priority and a new thread gets to run.

How do you use sched yield?

I know sched_yield() is supposed to relinquish access to the thread so another thread can run. So it should move the thread it executes on to the back of the running queue. The code below is supposed to print 'abc' in order and then the newline after all three threads have executed.

Why use sched_ yield?

The sched_yield() function allows a thread to give up control of a processor so that another thread can have the opportunity to run.

Is yield a system call?

The sched_ yield( ) system call allows a process to relinquish the CPU voluntarily without being suspended; the process remains in a task_running state, but the scheduler puts it at the end of the runqueue list. In this way, other processes that have the same dynamic priority have a chance to run.


2 Answers

You can use the sched_yield() function to yield the rest of your time slice, as discussed here.

sched_yield() causes the calling thread to relinquish the CPU. The thread is moved to the end of the queue for its static priority and a new thread gets to run.

like image 143
TypeIA Avatar answered Oct 18 '22 05:10

TypeIA


The question sounds like it's asking about kernel programming but the accepted answer is a function user mode API sched_yield(). I think the kernel answer to this question is schedule()

like image 32
vharron Avatar answered Oct 18 '22 07:10

vharron