[TUT][C] Multi-tasking tutorial part 1

98 posts / 0 new
Last post
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

So Does it mean we can't get interrupts in
a Co-operative Task Switcher? since you've mention that each task must run and finish at a definite amount of time?


Just think of a co-op tasker as one big while(1) main() loop and in that sense it's no different to any other kind of program and there are no limits about also running ISRs alongside.

The usual rules about trying to keep ISRs as short as possible apply of course. If an ISR were to take 100ms for example then that would stop all the co-operating foreground tasks for all that time. So, as always, make it just a few time sensitive instructions and put any "long work" in another foreground task that usually does nothing but is triggered into action by some flag set in the ISR. The usual rules for foreground task timing apply to it too - so if it will take more than the usual task time limit then break its work up into a multi-stage state machine too.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you again Cliff. it makes sense.
I am getting to know how things should really be done

If we can in some of the pseudo code that Kartman gave for ex.

1. for timers=0 to 7
we want to do
2. if timer[timers] >0, decrement timer[timers].
if timer[timers] = 0 then set_task timers.
3. next timers

i was concerned on this piece of code.


while (task < NUM_TASKS ) 
    { 
    if (task_timers[task]) 
      { 
        task_timers[task]--;            /* dec the timer */ 
       if (task_timers[task] == 0 ) 
            { 
          set_task(task); /* if ==0 activate the task bit */ 
         } 
      } 
    task++; 
    } 

shouldn't on the 1st if statement have been

   if (task_timers[task]>0)   
     {  
        task_timers[task]--; 
      }  // etc..

rather than if (task_timers[task])
Since Kartman said earlier that we want to decrement
the associated timer if it's found to be greater than 0?

Cheers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since task_timers is unsigned it can never be < 0, so the test is good as Kartman gave it.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you answer Johan

You are right unsigned int can never be < 0,
but it can be greater than zero, which is the case
i was talking about that if is ever found to be > 0.

  if (task_timers[task]>0)
     { 
       task_timers[task]--; 
      } 

then we would decrement it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But testing with just

    if (task_timers[task])

for unsigned does just the same! Remember that as long as something is != 0 it is true.

So, technically it will do just as fine as testing for > 0.

Whether it would be clearer for the reader of the code to use > 0 is probably a matter of opinion.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you Johan,

very well explained :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Shouldn't also code

while (task <= NUM_TASKS ) 
    { 
     if ((task_bits & pgm_read_byte(&bit_mask[task]))) 
           { 
           break; /* if activate task found..*/ 
         } 
      task++;         /* else try the next one */ 
    } 

really be

while (task < NUM_TASKS )  // a slight change here for "<="
    { 
     if ((task_bits & pgm_read_byte(&bit_mask[task]))) 
           { 
           break; /* if activate task found..*/ 
         } 
      task++;         /* else try the next one */ 
    } 

since

unsigned int task_timers[NUM_TASKS]={0,0,0,0,0,0,0,0}; 

has 8 elements starting from 0 which makes the last element be the 7th?

thus from while (task <= NUM_TASKS ) this allows the 8th element aswell no? which might probably lead to access of the memory outside the array boundary when compiler gets to this if statement, as the last element is the 7th

if ((task_bits & pgm_read_byte(&bit_mask[task]))) 

Thanks all.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi again all,

Kartman said that for example task 1 might need task 2 to take (run) 1sec!
So i understood that creating task_timers[NUM_TASKS]={0,0,0,0,0,0,0,0}; gives us extra opportunities to specify the time we want that task to run plus to keep in track if its flag is ON.
I was concerned about initializing the task to a certain amount of time! like some task run in 250 ms? won't this affect/delay other tasks as we had 10 ms for each task to run?
If i understood right we might want some tasks to take longer than others as long as the finish and other can also follow?
(which is the matter of the facts Kartman said that the worst case scenario would be having the task which will need to be done on every single 10 ms basis)
But then if the task could take longer than others as long as it finishes and allow other also to run would be fine?

(just like Cliff suggested like in the while (1) loop, the most important thing is that every single task will be done although some may take longer than others) right?

Thank all.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just out of curiosity question, Does anyone knows What adding a RTOS in this Cooperative Multitasking would benefit?

I am just learning on Multitasking RTOS.

Cheers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Does anyone knows What adding a RTOS in this Cooperative Multitasking would benefit?

Your question makes little sense - are you saying you would run a secondary co-operative tasking system within a single thread of a pre-emptive, time-slicing tasking system? To what gain? If you have a pre-emptive scheduler then why not make all tasks separate - that's kind of the point of pre-emption isn't it?

 

Pages