Background technology
Widespread use along with rapid development of computer technology and Internet technology, embedded system (Embedded System) is being brought into play more and more important effect, and it is widely used in aspects such as process industry, process control, communication, instrument, instrument, automobile, boats and ships, Aero-Space, military equipment, consumer product.Embedded system is defined as, and is application-centered, and based on computer technology, hardware and software can be reduced, and adapts to application system to function, reliability, cost, volume, the dedicated computer system that power consumption etc. are strict with.
Just because of the application of embedded system enlarges day by day, functional requirement also increases gradually, impels people to introduce embedded OS in embedded system.Embedded OS with real-time be exactly embedded real-time operating system (Embedded Real Time Operating System, ERTOS).The correctness of its operation not only depends on the correct degree of logical design, and the time of following these operations to carry out is relevant.That is to say that real time operating system be the more important thing is also will satisfy and used the real-time requirement that proposes except the functional requirement that will satisfy application.Its top priority is under the certain hour restriction, transfers all available resources and finishes real-time control task.Embedded real-time operating system is because the needs used and the restriction of hardware condition, but has characteristics such as solid state, volume are little, efficient, high reliability, high real-time.
The real-time of embedded real-time operating system and multiple task ability depend on its task scheduling algorithm to a great extent.From scheduling mode, but can be divided into preemption scheduling and can not dispatch by preemptive type (i.e. cooperation formula).
In preemptive type (Preemptive) scheduling, moving at present of task can be by other more urgent and prior tasks interrupt.Simultaneously, being seized of task can be resumed operation in future, and can not have influence on the whole time limit constraint of task.The advantage of preemption scheduling is more flexible, and the response time of its task level is able to optimization, and the utilization factor of resource is than higher, but because the context that often occurs switching (CPntextSwitch) makes that its system overhead is bigger, predictability is lower.
Fig. 1 has shown the implementation status of the preemption scheduling of two different priorities tasks and an interrupt service routine (ISR, InterruptService Routine).At first, when the task of low priority is being moved, ISR seizes low priority task and enters run mode, it makes the task of a higher priority enter ready attitude, so after ISR is complete, scheduler selects this high-priority task to enter operation, has so just postponed the operation that recovers to carry out low priority task.
MicroC/OS II and most of commercial real-time kernel all adopt preemption scheduling, MicroC/OS II is the embedded OS of the current free and open source code of being used widely, but it is based on priority preemptive type multiprocess system, supporting 64 priority, is an embedded real-time operating system that very typically adopts preemption scheduling.
And non-preemptive scheduling, i.e. cooperation formula (Cooperative) scheduling, the task of not allowing moving is by other tasks interrupt, and in a single day task has been occupied processor and just can have been moved until finishing always.Relatively be suitable for task run time relatively short system all.Its advantage is to have saved to carry out the expense that context switches, it is movable having only a task at any time, and this task run is up to finishing, has better predictability, reliability, be easier to test, but scheduling there is not preemption scheduling flexible like that, also relatively low to the utilization factor of resource.
Fig. 2 has shown the implementation status of the cooperation formula scheduling of two different priorities tasks and an ISR.At first, when low priority task is moved, ISR seizes low priority task and enters run mode, it makes the task of a higher priority enter ready attitude, the low priority task that ISR complete back recovery is seized, after this task was finished, scheduler just moved the high-priority task that is in ready attitude.
Michael J.Pont proposes a kind of cooperation formula (TTC, Time-TriggeredCooperative) scheduler of time triggering.In this scheduler, the order of magnitude in the time interval of a timeticks is generally millisecond (ms), and the task of embedded OS all can be moved in the time interval of a timeticks and finish.It is produced by the CPU timer conter, and in every time interval through a timeticks of system, just producing a time overflows interruption, and whether interrupt service routine detects has task to be in ready state this moment, then calls this task if detect.This cooperation formula scheduler is realized simple, has more reliability and security.But this scheduling mode is more weak to the real-time response ability of external event, and scheduling mode do not have preemption scheduling flexible like that, and is also relatively low to the utilization factor of resource.
In the embedded system, have two kinds of different task triggering modes in essence: Event triggered and time trigger.The Event triggered mode often uses multistage interruption to realize that its time of origin has randomness; Time triggering mode is then different, and it drives by a global clock, and owing to it is all determined in time with on the function, so its predictability is than higher.Two kinds of different task scheduling modes add two kinds of different task triggering modes, so embedded system, there is 4 types task, be respectively: the cooperation formula task that the time triggers, the preemptive type task that time triggers, Event triggered cooperation formula task and Event triggered the preemptive type task.
Though the TTC scheduler that Michael J.Pont proposes (the cooperation formula scheduling that the time triggers) can reduce the task jitter phenomenon in the embedded system effectively, the predictability height, realize simple, but this scheduling mode is more weak to the real-time response ability of external event, and scheduling mode do not have preemption scheduling flexible like that, and is relatively low to the utilization factor of resource yet.
The real-time response ability of the MicroC/OS II of employing preemption scheduling mode is more satisfactory, but can not in certain special time, accurately carry out specific task, the time determinacy is relatively poor, the context switching that often occurs makes system overhead bigger, and predictability is lower, dispatches not as the cooperation formula of time triggering based on the preemption scheduling device reliability of Event triggered.
Summary of the invention
The technical problem to be solved in the present invention is, above-mentioned defective at prior art, provide a kind of and have the time and trigger high predictability and the reliability of cooperation formula dispatching system, also have the mixed scheduling method of embedded real-time operating system of the high real-time response ability of Event triggered preemption scheduling system.
For realizing above purpose, the invention provides a kind of mixed scheduling method of embedded real-time operating system, comprise the preceding resource distribution of task scheduling and task scheduling, wherein, described task scheduling may further comprise the steps:
The mode that triggers by Event triggered or time makes task ready;
Search the priority decision table according to ready list and ready group, obtain current ready super objective priority;
According to described current ready super objective priority, obtain the task control block (TCB) structure of described ready limit priority task, and judge the classification of this task;
When described ready limit priority task is cooperation formula task, carry out this cooperation formula task, after described cooperation formula task is finished, remove its position in ready list and ready group, search the priority decision table according to ready list and ready group again, obtain current ready super objective priority.
Mixed scheduling method of the present invention, wherein, resource distribution specifically comprises the steps: before the described task scheduling
Creation task; Set up the task control block (TCB) structure; Set up task control block (TCB) address array; Set up and initialization ready list and ready group; Set up and the initialization bit mask table; Set up and initialization priority decision table; Set up and initialization interrupt priority level table; The installation timeticks interrupts.
Mixed scheduling method of the present invention, wherein, described method is treated interrupt routine with task, and distributes priority with other tasks.
Mixed scheduling method of the present invention wherein, describedly makes that task is ready specifically to be comprised:
By communication and synchronization mechanism or because interrupting making the task of Event triggered ready, or subtract 1 the time delay in the task control block (TCB) of the task correspondence that will trigger the time, when be kept to 0 time delay, make the task that the described time triggers ready.
Mixed scheduling method of the present invention wherein, describedly makes that task is ready specifically to be referred to:
To be 1 to position that should task priority in the ready list, and the position of corresponding task place group be 1 in ready group.
Mixed scheduling method of the present invention, wherein, described method also comprises:
When carrying out the timeticks interrupt routine, check whether the cooperation formula task of moving is interrupted by the timeticks interruption, be then to select Treatment Options according to user's request, described Treatment Options arranges when creation task, it comprises the uncompleted task of continuation execution, abandons carrying out uncompleted task or re-executes uncompleted task.
Mixed scheduling method of the present invention, wherein, described method is further comprising the steps of:
After described cooperation formula task is finished, check that the task requests that whether has interrupt event to trigger carries out the term of execution of described cooperation formula task, when the task requests that has interrupt event to trigger is carried out, make the task of the Event triggered of asking ready.
Mixed scheduling method of the present invention, wherein, whether described inspection has interrupt event request generation concrete steps to be:
Software is searched the interruption that takes place in cooperation formula task run process, utilize resulting interrupt source numbering, searches corresponding task priority in described interrupt priority level table, utilizes resulting priority to make corresponding task ready.
Mixed scheduling method of the present invention, wherein, described method is further comprising the steps of:
The time of described inspection interrupt event is set to the set time, and after cooperation formula task was finished, relatively set time and timeticks residue if the former is smaller, was then carried out this audit function, otherwise abandoned carrying out audit function at interval;
When carrying out this audit function, if described audit function surpasses described set time, the scheduling of executing the task immediately working time.
Mixed scheduling method of the present invention, wherein, described method is further comprising the steps of: when ready limit priority task is the preemptive type task, the preemptive type task of operation limit priority.
The invention has the beneficial effects as follows, by ISR is treated with task, and distribute priority with other tasks, and simultaneously compoundly utilize the time to trigger with the Event triggered framework and adopt the mode of preemptive type and cooperation formula task mixed scheduling, obtain a kind of mixed scheduling method.This mixed scheduling method had both had high predictability and the reliability of cooperation formula dispatching system, the advantage that also has the high real-time response ability of preemption scheduling system, by controlling the task number of two kinds of different scheduling modes, take full advantage of system resource, effectively the control system expense.
Embodiment
Below in conjunction with description of drawings, the present invention is explained in further detail.
To carry out the virtual mixed scheduling device that is called of program of described mixed scheduling method in the embodiment of the invention.
The embodiment of the invention mainly comprises two big steps, and namely the early-stage preparations of task scheduling and task scheduling are described in detail respectively below.
The early-stage preparations of task scheduling.At first, creation task, for each task creation task control block (TCB) (TaskControl Block, TCB) structure, and all TCB are connected into the TCB doubly linked list simultaneously, are set up TCB address array TCBPrioTbl[LowestPrio+1].As shown in Figure 3, introduced relation between TCB address array and the TCB doubly linked list among the figure.TCB address array is to deposit the array of all TCB addresses, the priority of the corresponding task of the subscript of array, and the size of array depends on minimum task priority LowestPrio, the task priority scope is generally: 0~63, i.e. 64 tasks.So the size of TCB address array is LowestPrio+1, its each element all points to the entry address of corresponding TCB structure, and task stack pointer just is with the operation of convenient rudimentary programming language to TCB.
The corresponding task of each TCB structure, behind a task creation, the corresponding TCB of this task determines thereupon.Each variable among the TCB determines that thereupon scheduler can only be realized by the operation to TCB the operation of task.Comprise time delay, performance period, task type and priority in the TCB structure of task, these variablees are absolutely necessary in the task scheduling process.Time delay and performance period are generally 0 in the TCB structure of Event triggered task; Represent that this task postpones to carry out behind what timeticks again the time delay in the TCB structure of time triggering task, the performance period represents the task of time triggering every what timeticks is carried out once.Task type generally is divided into 4 types task as described in the background art, and they are respectively: the cooperation formula task that the time triggers, the preemptive type task that the time triggers, Event triggered cooperation formula task and Event triggered the preemptive type task.The priority of task is one 8 unsigned variant, and it is the important sign whether task exists, the corresponding priority of each task, and this value scope generally is 0~63, namely always has 64 tasks that priority is different.Usually, priority 0 is super objective priority, and priority 63 is minimum task priorities, and other task priority height by that analogy.Each TCB structure is connected together with * Next and * Prev pointer chain, constitutes a doubly linked list, to improve search efficiency.
Then, set up and initialization ready list and ready group, can be expressed as respectively as them:
Ready list: unsigned char OSRdyTbl[LowestPrio/8+1];
Ready group: unsigned char OSRdyGrp.
Wherein the size of ready list is decided by task priority minimum in all tasks, and each element is one 8 variable, and ready list is one group with 8 priority, is an element of ready list, the corresponding task of one of them priority, as shown in Figure 4.The size of this ready list is 64, and 8 elements are arranged, and namely is divided into 8 groups, and every group is exactly 8 bit variables.The priority of the corresponding task of numeral in the grid increases progressively arrangement according to from right to left order.The priority of task is one 8 unsigned variant, and its low 3 are used for setting the tasks at the horizontal level of ready list, i.e. directions X, and its high 3 are used for setting the tasks in the upright position of ready list, i.e. Y-direction.For example, the priority of certain task is 35, and the binary value of its correspondence is 100011 so, and then its X coordinate is that 3, Y coordinate is 4, so several the 5th row from top to bottom in the corresponding ready list, from the position of several the 4th row in a right left side, i.e. the 35th grid is just to should task.If any position 1 in the ready list, the task of then representing this correspondence is in ready attitude, namely this task device that can be scheduled at any time calls, directly carry out, if should show any position 0, the task of then representing this correspondence is not in ready attitude, but is in pending state, and the device that then temporarily can not be scheduled calls.
Ready group is one 8 variable, each group in the ready list of the numeral correspondence in the grid, if group corresponding in the ready list is then represented in any position 1 in ready group, namely the group on the Y-direction has task to be in ready attitude; If put 0 for any one group in ready group, represent that then the task of corresponding group in the ready list is in pending state, wouldn't carry out.As shown in Figure 2, if ready group the 0th put 1, represent then in the 0th group of ready list that priority is that 0 to 7 any one or a plurality of task are in ready attitude; If the 2nd of ready group is put 0, represent then in the 2nd group of ready list that priority is that 10 to 23 task all is in pending state.
Then, set up also initialization bit mask table and priority decision table.The size of bitmask table is 8, and definition bitmask table is:
unsigned?char?const?OSMapTbl[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}。
The size of priority decision table is 256, and definition priority decision table is:
unsigned?char?const?OSUnMapTbl[]={
0,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0
}。
Set up and initialization interrupt priority level Table I SR_IDPrio[], be used for identifying the priority height of the corresponding ISR of various interrupt sources, be designated as interrupt source number it under, each element then is the ISR of the corresponding interruption priority during as task.
The installation timeticks interrupts, the performance period that the definition timeticks interrupts, interrupt type etc., system is produced once at set intervals interrupts, this timeticks interrupts being equivalent to a global clock, produce with the form of interrupting, can move corresponding timeticks interrupt routine automatically at set intervals, in order to carry out task scheduling.
More than introduce the details of the early-stage preparations of task scheduling, created task, the task control block (TCB) chained list, ready list, ready group, mask table and priority decision table, and the timeticks interruption has been installed, will introduce the method for task scheduling below.
As previously described, we can be divided into the task in the embedded system cooperation formula task that the time triggers, the preemptive type task that the time triggers, Event triggered cooperation formula task and Event triggered the preemptive type task.The task that time triggers need as time delay and performance period, be carried out respective handling according to the time response of this task.And the task of Event triggered mainly is interruption or intertask communication mechanism by embedded system, wait for or obtain a semaphore as task and trigger, most of form by interrupt event occurs, and is irrelevant with the time response of task, so separately handle in the task scheduling process.
Behind the task creation, the type of task among the task control block (TCB) TCB, priority also has determine time delay thereupon.
Task scheduling mainly is to be undertaken by the timeticks interrupt routine, and the operation of each timeticks interrupt routine all is the opportunity of carrying out task scheduling again, as shown in Figure 5, has introduced the process flow diagram that timeticks interrupts among the figure, may further comprise the steps:
Step S501 judges whether it is triggering time of task, if then jump to step S502, otherwise jumps to step S505.
Step S502, the time that subtracts is certainly triggered the task delay time;
Step S503 judges that the time after subtracting is triggered the task delay time, if be 0, and execution in step S504 then, otherwise jump to step S505;
Step S504 makes the task of time triggering ready;
Step S505 looks into ready list and obtains current ready super objective priority;
Step S506 judges that ready limit priority task is cooperation formula task or preemptive type task, if cooperation formula task, and execution in step S507 then, otherwise execution in step S508;
Step S507, the cooperation formula task of operation limit priority after operation is finished, makes this task be in pending state, then execution in step S505;
Step S508, the preemptive type task of operation limit priority.
Introduce this flow process below in detail.After entering the timeticks interrupt routine, the task that we at first trigger the time is handled, certainly subtracted the time delay among the task control block (TCB) TCB of correspondence, even subtract 1 time delay, when be kept to 0 time delay, then make this task ready, namely at ready list OSRdyTbl[] in position that should task priority is put 1, and the position of corresponding task place group puts 1 in ready group of OSRdyGrp.If the task of Event triggered is not then done any processing.
Wherein, 1 the method for in ready list and ready group the position of corresponding certain task being put is: as shown in Figure 4, at ready list OSRdyTbl[] in, because low 3 corresponding OSRdyTbl[of this task priority] the X coordinate, high 3 corresponding Y coordinates, so utilize bitmask table OSMapTbl[] search the bitmask of X coordinate and Y coordinate correspondence, to determine which element X, Y coordinate are in 8 bit variables, at last two bit-wise mask value are composed respectively to ready group of OSRdyGrp and ready list OSRdyTbl[] in the class value at this task place.
For example, making priority is that the detailed process that 33 task is set to ready attitude is: 33 binary value is 100001, low 3 of X table priority is 1, the bitmask of its correspondence is OSMapTbl[X], high 3 of priority of Y table is 4, the bitmask of its correspondence is OSMapTbl[Y], be OSRdyGrp=OSMapTbl[Y to ready group of set then], be OSRdyTbl[OSMapTbl[Y to ready list set]]=OSMapTbl[X].
Checking whether the cooperation formula task of moving is interrupted by the timeticks interruption, is then to deal with according to user's request.Because the time interval of each timeticks is limited, cooperation formula task might be interrupted by the timeticks interruption, so in order further to improve the rationality of cooperation formula scheduling, need be when the timeticks interrupt routine begins, check whether have the cooperation formula task moved by timeticks interrupt interrupt, can whether be that 0 method realizes by checking a zone bit.For example: have cooperation formula task to be interrupted when zone bit is 1, then carry out respective handling, not cooperation formula task is interrupted when zone bit is 0.The user is according to the actual demand of oneself, and Treatment Options can be selected when creation task, determines concrete which kind of disposal route that adopts, for example: continue to carry out uncompleted task, abandon carrying out uncompleted task, re-execute uncompleted task dispatching.
Usually, can be set a closing time for cooperation formula task during creation task, in general, equal its cycle of operation this closing time, when beginning, the timeticks interrupt routine carries out from subtracting, when be 0 closing time, to deal with according to user's request, Treatment Options can be selected when creation task.
Then, search the priority decision table according to ready list and ready group, obtain current ready super objective priority.When task was set to ready attitude, the position of the corresponding task of ready list and ready group will put 1, found out the highest task priority according to ready group and ready list.Detailed process is: search priority decision table OSUnMapTbl[with ready group of OSRdyGrp earlier], draw the high 3 of limit priority, namely be that the limit priority task is at ready list OSRdyTbl[] under the group bitmask, search priority decision table OSUnMapTbl[with this group again], what draw limit priority hangs down 3, at last, 3 of front and back being combined is exactly current ready super objective priority.For example, if the value of ready group of OSRdyGrp is 01010100, be 0x54, search priority decision table OSUnMapTbl[OSRdyGrp] value be 2, namely high 3 of super objective priority is 2, if OSRdyTbl[2] value be 01100010, be 0x62, search priority decision table OSUnMapTbl[OSRdyTbl[2]] value be 1, then the binary value of super objective priority is exactly 010001, namely 11.
Moreover, if ready limit priority task is cooperation formula task, then directly move this cooperation formula task.Detailed process is: search task control block (TCB) TCB according to super objective priority, and judge whether the task type of TCB is cooperation formula task, is then directly to move this task up to end, do not need to carry out context and switch.After this cooperation formula task is finished, remove it in the position of ready list and ready group, remove the ready attitude of this task, its method to put 1 very similar at ready list with ready group, be 0 with this task in the position of ready group and ready list just, rather than be set to 1, detailed step is no longer carefully stated.
Usually, after a cooperation formula task is finished, allow to check the task requests execution whether Event triggered is arranged the term of execution of this cooperation formula task, have then to make it ready.Because cooperation formula task the term of execution generally do not allow to interrupt, if there is interrupt event to take place, can not respond immediately, whether we can have the interrupt event request to take place the cooperation formula task term of execution by checking, be set to ready attitude in the Event triggered task of carrying out the request of in time will sending after cooperation formula task finishes, improve scheduler to the real-time response ability of external event as far as possible.
Whether have detailed process that interrupt event request take place be: search interrupt source from hardware if checking, utilize resulting interrupt source numbering, at interrupt priority level Table II SR_IDPrio[] search corresponding task priority, utilize resulting task priority, make this Event triggered task ready.In addition, because this audit function needs certain execution time, and the execution time has uncertainty, supervision time when not having interrupt request is than there being a lot of interruptions request of sending the time to lack, this produces uncertainty to the beginning execution time of the cooperation formula task of back with by the execution time, thereby the task jitter phenomenon takes place.Therefore we are set to a set time ChkIntrDuration supervision time, after a cooperation formula task is finished, then relatively set time and timeticks remain (the timeticks time interval and ChkIntrDuration's is poor) at interval, if the former is smaller, then carry out this audit function, otherwise abandon checking.When carrying out this audit function, if when surpassing ChkIntrDuration its working time, no matter scheduler checking whether is abandoned carrying out immediately, enters task scheduling,
Then, again jump to the step of searching current ready super objective priority, find out the task of current limit priority, if this task is cooperation formula task, carry out it, jump to after the inspection and search super objective priority step, if this task is the preemptive type task, then carrying out context switches, be about to all register holds of current processor in corresponding stack, and the content (depositing the content of corresponding all registers of this task) that will be kept at this preemptive type task stack is taken out the preemptive type task of execution limit priority.
The present invention is by treating ISR with task, and distribute priority with other tasks, simultaneously compound time triggering and Event triggered framework and the mode that adopts preemptive type and cooperation formula task mixed scheduling utilized, the advantage of comprehensive two kinds of scheduling modes, overcome shortcoming separately, obtain a kind of mixed scheduling method.This mixed scheduling method had both had high predictability and the reliability of cooperation formula dispatching system, the advantage that also has the high real-time response ability of preemption scheduling system, it passes through four kinds of different tasks of rational management, has reached to take full advantage of system resource, effectively the purpose of control system expense.
The above only is preferred embodiment of the present invention, not in order to limiting the present invention, all any modifications of doing within the spirit and principles in the present invention, is equal to and replaces and improvement etc., all should be included within protection scope of the present invention.