|
|
| Author |
Message |
DocJC
Joined: 16 Mar 2006
Posts: 84
Location: Cleveland, OH
|
|
Posted: 06 November 2006, 17:50 PM Post subject: Task Prioritization |
|
|
I have a program with ~ 6 tasks running. Several of them are very low priority tasks, whereas others clearly require more cpu time. I would like to be able to have a task give up part of its time slice. The tasks all run forever, and therefore each task loops. I would like to begin the task with a counter, so that every 4th (or 10th, etc.), time through the loop the rest of the task code executes, but the majority of the times the task just gives up its time slice, skipping execution of its real code. The majority of its time slice then becomes available for the other tasks to utilize.
The effect of this is to DE-prioritize a low priority task. This effectively gives more cpu time to the other, higher priority tasks. Essentially a backwards way to elevate the priority of key tasks, without having a priority value attached to them at the time the Call Task is invoked.
Reading the manual I saw Lock and Unlock, which I do not wish to do, as I do not want to completely pre-empt other tasks.
I also saw a discussion on Task Management, and the Task Control Block. I did not see any detailed description of the Remaining Time to Sleep element, but it sounded interesting.
So, the question is, is it possible to pre-maturely exit a task's time slot, (for a task which runs forever, and does not terminate) ?
Is it possible to reset the sequencer so that it thinks the current time slice has completed?
Is there a better way to implement a form of Task Prioritization?
As always, I appreciate everyones thoughts on this.
JC |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 06 November 2006, 19:07 PM Post subject: |
|
|
There is currently no direct way to change the priority of a task. That said, there may be a way to accomplish your goal.
The main line of the VM is a loop that executes instructions from the current task. On each iteration, a flag is checked to see if it is time to switch to the next available task. That flag is set on each RTC tick but it can also be set in the process of executing certain instructions, e.g. Sleep, Delay, WaitForInterrupt, InputCapture, etc. When the flag is set and the current task is not locked, the scheduler attempts to find another task that is ready to run. The search begins with the task whose Task Control Block is pointed to by the "next" field of the TCB of the current task and continues until a "ready" task is found or the search returns to the TCB of the current task (the task list is a circular linked list).
The "remaining time to sleep" field of the TCB is set whenever a task executes the Sleep or Delay instruction. The value is the number of RTC ticks remaining in the sleep interval and that value is decremented on each RTC tick. Consequently, a sleeping task becomes ready to run when its "remaining time to sleep" reaches zero.
With this information in hand, you can see that tasks can roughly prioritize themselves by specifying various intervals in a Sleep call. A high priority task could use Sleep(0), a medium priority could use Sleep(5) and a low priority task could use Sleep(30).
You can adjust the effective priority of sleeping tasks by changing the "remaining time to sleep". The value of the field only has an effect for those tasks that are actually sleeping (status = TaskSleeping). Setting the field to zero will raise the task's priority, setting it to a higher value lowers the task's priority. Obviously, it is possible to starve out a sleeping task completely by continually setting its "time to sleep" to a high value, causing it to never awaken. |
|
| Back to top |
|
 |
DocJC
Joined: 16 Mar 2006
Posts: 84
Location: Cleveland, OH
|
|
Posted: 06 November 2006, 19:18 PM Post subject: |
|
|
Thank you!
That will work fine.
My Tasks are feeling sleeeeepy, veeeery sleeeeepy.
JC |
|
| Back to top |
|
 |
|