schedulingrtospreemption

Priority Preemptive Scheduling of infinite loop tasks


There is much of material available regarding priority preemptive scheduling on Google and Stackoverflow, but I am still having confusion regarding scheduling of infinite loop tasks in a priority preemptive scheduling kernel. Let's consider the following case:

An RTOS starts two tasks T1 and T2 with priority 50 and 100 respectively. Both tasks look like:

void T1()
{
    while(1)
    {
        perform_some_task1();
        usleep(100);
    }
}

and

void T2()
{
    while(1)
    {
        perform_some_task2();
        usleep(100);
    }
}

As far as I understood, the kernel will schedule T2 because of its higher priority and suspend T1 because of its lower priority. Now because T2 is an infinite loop, it will never relinquish CPU to T1 until some other high priority task preempts T2.

BUT, it seems that my understanding is not correct because I have tested the above case in an RTOS and I get output on console printed by both tasks.

Can somebody comment on my understanding on the matter and the actual behavior of RTOS in above case?


Solution

  • In that case, both tasks are being suspended once perform_some_taskN(); have been executed (releasing the resources to be used by another threads). According to the documentation:

    The usleep() function will cause the calling thread to be suspended from execution until either the number of real-time microseconds specified by the argument useconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested due to the scheduling of other activity by the system.

    BTW, usleep() is deprecated (use nanosleep() instead) :

    POSIX.1-2001 declares this function obsolete; use nanosleep(2) instead. POSIX.1-2008 removes the specification of usleep().