Mutlitasking: Actually giving up the timeslice

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Caleb1994
Member
Member
Posts: 83
Joined: Sun Feb 13, 2011 4:55 pm

Mutlitasking: Actually giving up the timeslice

Post by Caleb1994 »

Is it normal when two tasks are executing this code:

Code: Select all

void task()
{
     while( 1 ) k_printf("%i", task_get_pid());
}
that they print like this (yes, I have semaphores protecting the console):


11111111112222222222222222222111111111111111111111111222222222222222222


or should it be more like this:

1212121212121212121212121212121212121212121212121212121212121212121212121

My first thought is that they should each execute evenly, and print like the second, but then again, it all has to do with timeslices, so maybe I shouldn't make it work like that.

The title is because when a task unlocks a mutex, I originally programmed it to give up it's timeslice (since another task was just released, and needs to execute), but the way I do it is:

Code: Select all

sched.timeslice = 0;
which is the variable holding the remaining ticks of the current timeslice, but in my code, the loop take such little time to execute that it executes like the first example because of the amount of time between timer interrupt fires.

If it is executing like it should, then there is nothing else, but if it should be executing as the second example, how could I force a task switch without artificially firing the timer interrupt (I assume you can do by just calling "int 8" since IRQ0-7 are mapped to INT8-15) ? I don't want to do that because that will effect my up-time counter; I know it's not a big deal, but I would prefer not to mess that up. :P
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Mutlitasking: Actually giving up the timeslice

Post by gerryg400 »

Caleb1994 wrote:The title is because when a task unlocks a mutex, I originally programmed it to give up it's timeslice (since another task was just released, and needs to execute), but the way I do it is:

Code: Select all

sched.timeslice = 0;
which is the variable holding the remaining ticks of the current timeslice, but in my code, the loop take such little time to execute that it executes like the first example because of the amount of time between timer interrupt fires.
I don't think that's ideal. You should be able to force the re-schedule immediately without waiting for the next timeslice. You need a scheuler function that blocks the current task and immediately switches to the one that now has the mutex.
If a trainstation is where trains stop, what is a workstation ?
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: Mutlitasking: Actually giving up the timeslice

Post by piranha »

What if you did

Code: Select all

while(1) {
print("hi");
schedule();
}
That should make it print like the second. But its fine for it to do the first. It happens that way cause the code executes so quickly that it can do multiple iterations before the next interrupt.

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Post Reply