A kind of dynamic timing method of real-time embedded operating system
Technical field
The present invention relates to a kind of dynamic timing method of real-time embedded operating system.
Background technology
Real time operating system need adopt the clock timing function to carry out the management of delay task recovery time and to the management of same priority task sharing processor resource.(like FreeRTOS, uc/os-2) adopted the management method of fixed time slicing, CPU revives on each timeslice and the task of each delay is carried out upgrading operation time delay and same priority task wheel changes operation etc. present real time operating system.The size of this timeslice is the minimum value during all tasks postpone, and concrete operation method is following: at first, utilize hardware timer to set fixing timing cycle, timing cycle interrupts to then producing regularly.Then, update system time and visit the delayed tasks chained list in the handling procedure that regularly interrupts, upgrade the residue delay time of delayed tasks.If delay time arrives, then join task in the ready queue.Although this method is simple and realize that easily there are some performance issues in it.At first, when the minimum time-delay of system hour, need to adopt little timing cycle, low even if minimum like this delayed tasks is carried out frequency, still need system frequently to get into regularly interruption, increased the processor burden; Secondly, the timing cycle of fixing has limited the resolution of system delay.For example, 1 millisecond timing cycle has determined that the task time-delay can be unit with the millisecond only, and this needs the application of accurate delay to constitute obstacle to some.
Summary of the invention
The objective of the invention is provides a kind of dynamic timing method of real-time embedded operating system for overcoming the deficiency of above-mentioned prior art.
For realizing above-mentioned purpose, the present invention adopts following technical proposals:
A kind of dynamic timing method of real-time embedded operating system, this method may further comprise the steps:
Step1: when timer interrupts, record system time at this moment;
Step2: operating system need be carried out moment ascending order with each task and rearrange the delayed tasks chained list; The task of linked list head need be carried out constantly and system time compares; If the task of linked list head need be carried out constantly smaller or equal to system time; Then the task with linked list head shifts out, and adds in the corresponding ready task chained list; Otherwise then the task of linked list head need be carried out and constantly deduct system time, the task delay time of difference as linked list head;
Step3: if this delay time greater than the maximum cycle of timer setting, then is set at down this maximum cycle the timer interrupt cycle of subtask; If the maximum cycle that delay time is set less than timer, then with this delay time as the timer interrupt cycle of subtask down, withdraw from the timer interruption;
Step4: judge whether the delayed tasks chained list is empty,, continue to carry out otherwise then return step1 as interrupting for sky then withdraws from timer.
It is the maximum timing cycle that hardware timer is set that said timer is set maximum cycle.
Among the said step2; All there is a variable to represent the moment that the needs of this task are carried out in the description block of each task; When task need be delayed time; Current time in system adds that task needs delay time, and in the moment that the task of obtaining need be carried out, the sequencing that need carry out according to task then is inserted in the delayed tasks chained list.
Beneficial effect of the present invention: dynamic timing method of the present invention adopts the variable timing time to replace existing fixedly timing; With the hardware timer clock source cycle is that to replace existing be chronomere with the hardware timing in chronomere; Reduced the unnecessary expense of maintenance system time when system is idle; Improved the response time of system simultaneously, reduced processor load and improved timing accuracy.
Description of drawings
Fig. 1 is a process flow diagram of the present invention;
Fig. 2 is the task delay time is set maximum cycle MaxPeriod less than timer embodiment;
Fig. 3 is the task delay time is set maximum cycle MaxPeriod greater than timer embodiment.
Embodiment
Below in conjunction with accompanying drawing and embodiment the present invention is described further:
As shown in Figure 1: when the timer timing cycle then, at first the maintenance system time, system time comes record by a variable, each timing arrives, this variable adds this timing.The chained list of inquiry time delay task then, this chained list are single-track link tables, and element is for needing the task of time-delay in the chained list, and this storage of linked list is in hardware RAM storer.The operation of shifting out of chained list is that the linked list head pointer variable is pointed to the next task of this chained list; The insertion work of chained list is; Begin from linked list head; Compare the task execution moment that task is carried out the moment and current insertion chained list in the chained list one by one; Carry out constantly big task up to finding, make the next pointer variable of the task of current insertion chained list point to finding of task, make the next pointer variable of the previous task of finding of task point to the task of current insertion chained list than the task of current insertion chained list.Because the delayed tasks chained list is need carry out ascending order arrangement constantly according to each task; So it constantly is the minimum execution moment in all delayed tasks that the delayed tasks of linked list head need be carried out; If should minimum carry out constantly smaller or equal to current system time; Explain that then this task needs to carry out, shift out the delayed tasks chained list to this task, add in the corresponding ready task chained list.Generally speaking, the minimum execution equals current system time constantly, but when certain task is carried out, turn-offed system break, causes system when timer interrupts, in time not carry out and interrupts, and can occur this moment and should minimum carry out constantly less than current system time.
Up to being arranged, need carry out constantly delayed tasks greater than the current time in system; Need the execution time to deduct system time with delayed tasks; Calculate this delayed tasks and also need delay time how long just need execution; (timer maximum timing cycle MaxPeriod is the maximum timing cycle that hardware timer is set if this delay time is greater than timer maximum timing cycle MaxPeriod; Each hardware timer maximum timing cycle is according to actual hardware timer performance this timing cycle to be set), then MaxPeriod is set at down the timer interrupt cycle of subtask; If delay time is less than MaxPeriod, then with this delay time as the timer interrupt cycle of subtask down, withdraw from timer and interrupt.
Each task delay time computing method is all to have a variable to represent the moment that the needs of this task are carried out in the description block of each task.When task need be delayed time, add that task needs delay time to the current time in system, the moment that the task of obtaining need be carried out, and then be inserted in the delayed tasks chained list according to the sequencing that task need be carried out.
According to Fig. 2 and Fig. 3 the practical implementation of technical scheme once is described below.Suppose a certain moment, 4 tasks are arranged, A, B, C, D in the delayed tasks chained list.Among Fig. 2, the task A that need carry out the earliest in the delayed tasks chained list 30ms that need delay time, it is 30ms that system is provided with current timer value.Timer time arrives behind the 30ms; The time that this moment, task A need carry out is shifted out the delayed tasks chained list smaller or equal to the current time in system, and task B becomes first task in the delayed tasks chained list; The time that task B need the carry out 20ms that also need delay time, it is 20ms that timer value is set.Among Fig. 3, the task delay time is greater than timer maximum timing time MaxPeriod1.6s, and it is 1.6s that timer value is set, and waits for that then delay time arrives, and task A delay time is 0.4s, and less than the timer maximum delay time, it is 0.4s that timer value is set.