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.(as 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 as follows: 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 task is joined 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 enter 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 above-mentioned the deficiencies in the prior art.
For achieving the above object, 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; Constantly deduct system time otherwise then the task of linked list head need be carried out, with 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 described timer is set maximum cycle.
Among the described 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, in the moment that the task of obtaining need be carried out, the sequencing that need carry out according to task is inserted in the delayed tasks chained list then.
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
The invention will be further described below in conjunction with drawings and Examples:
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.Chained list shift out the operation be that the linked list head pointer variable is pointed to the next task of this chained list; The insertion work of chained list is, 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 task than current insertion chained list, 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.Because the delayed tasks chained list is need carry out ascending order arrangement constantly according to each task, so it constantly is that minimum in all delayed tasks is carried out constantly 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, illustrate that then this task needs to carry out, this task is shifted out the delayed tasks chained list, add in the corresponding ready task chained list.Generally speaking, the minimum execution equals current system time constantly, but turn-offed system break when certain task is carried out, and causes system in time not carry out when timer interrupts and interrupts, and this minimum can occur this moment and 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, calculating this delayed tasks also needs time-delay how long just to need to carry out, if this delay time is the maximum timing cycle that hardware timer is set greater than the maximum timing cycle MaxPeriod of the maximum timing cycle MaxPeriod(of timer timer, the maximum timing cycle of each hardware timer 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, the current time in system is added that task needs delay time, 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 concrete enforcement 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 of needing the earliest in the delayed tasks chained list the to carry out 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 carry out also needs the 20ms that delays time, and it is 20ms that timer value is set.Among Fig. 3, the task delay time is greater than the maximum timing MaxPeriod1.6s of timer, 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.