Background technology
CBTC, it is based on the train automatic controlling system of wireless communication technology.It is in Urban Rail Transit Signal control
In system, its vehicle-mounted ATP (time interval between to trains device) is responsible for obtaining and safeguarding train position, receive mobile authorization and calculate safety
The functions such as rate curve, supervision train status.The realization of these functions needs vehicle-mounted ATP system to have high real-time,
Response in real time and quickly scheduling can be carried out to each task.Therefore vehicle-mounted ATP system generally adopts embedded real-time operation
System, for example:TKCG-08 type vehicle-mounted ATP devices just employ μ C/OS-II real time operating systems.
μ C/OS-II are a kind of based on the increasing income of priority, Preemptive multitask real time operating system, contain real-time kernel,
Task management, time management, the synchronous function such as (semaphore, mailbox, message, queue) and memory management of intertask communication.It can
So that each task works independently, it is non-interference, it is easy to realize punctual and errorless execution, make the design of real-time application
Become easy with extension, the design process for making application program is greatly simplified.Just because of these features so that μ C/OS-II are very
It is adapted for use with the high occasion of real-time, reliability and security requirement.In fact, μ C/OS-II are used in numerous areas, such as
Military project, Aeronautics and Astronautics, armarium and Industry Control etc..
μ C/OS-II operating systems belong to preemptive kernel system, and its functional realiey is task based access control, current car
Carry the communication in the task scheduling system of ATP between task mainly has following several ways with scheduling:
(1) the task management mode based on semaphore
Semaphore in μ C/OS-II is made up of two parts:One be semaphore count value, it is the nothing of 16
Symbol integer (between 0 to 65,535);Another wait task list being made up of the waiting the semaphore of the task.Semaphore
Semaphore, waiting signal amount, inquiry semaphore and the several functions of sending signal amount are set up using including.Communication between each task according to
Ensure the uniqueness that data are used between task by judging the value of semaphore.Such as task A uses variable C simultaneously with task B, with
This related semaphore be D, task A using need before variable by waiting signal measure semaphore D be " effective " instruction
Just can normally run.If semaphore idle task A will be into sleep state, until semaphore is effective or waits time-out time to arrive.
(2) the task management mechanism based on mailbox
Mailbox is another kind of communication mechanism in μ C/OS-II, and it can make a task or interrupt service subroutine to another
One task sends the variable of a pointer type.The pointer points to a data structure for containing specific " message ".Using postal
Before case, it is necessary to first set up the mailbox.The operation can be completed (see next section) by calling OSMboxCreate () function,
And specify the initial value of pointer.Generally, this initial value is NULL, but it is also possible to initializes a mailbox, makes
It is most starting just comprising a piece of news.If the use of the purpose of mailbox being for notifying that the generation of an event (sends one
Message), then it is NULL that will initialize the mailbox, because when starting, event does not also occur.If user with mailbox come
Share some resources, then the pointer that the mailbox is a non-NULL will be initialized.In this case, mailbox is treated as one
Individual binary signal amount is used.Each task sends control information in mailbox, before information of other tasks using mailbox association, needs
The whether available mandate of task is obtained from mailbox.If mailbox message is unavailable, task suspension until mailbox message is available or
Time-out is waited to reach.
Therefore, existing μ C/OS-II systems are provided task communication and scheduling mechanism, although can effectively protect not
With using identical data between task simultaneously, but have the disadvantage that:
(1) substantial amounts of data exchange can cause dispatch command very loaded down with trivial details between task, and program structure is complicated;
(2) exception occurs in individual task can cause other task deadlocks;
(3) without special real-time property preventing mechanism, once semaphore or mailbox operation irregularity, data safety cannot
Ensure.
The content of the invention
In order to solve the above problems, it is an object of the invention to provide a kind of method for scheduling task of succinct vehicle-mounted ATP
And its task scheduling system, to reduce the communication latency between task and improve switching efficiency.
For achieving the above object, the present invention adopts technical scheme as follows:
A kind of method for scheduling task of vehicle-mounted ATP, including:Generate at least one and be available for the shared global change of multiple tasks
Amount, each global variable specifies a task to carry out data modification renewal to it;Priority level highest in calling task list
First task is run into kernel, when operating first doing honest work carry out the global variable of data modification renewal by it to specifying
When carrying out data modification renewal, the locked task switching of kernel is until data modification renewal is completed;In first task operation
Whether the second task is newly entered in monitoring task list simultaneously;After the second task is newly entered, compare just the of kernel operation
The priority level height of one task and the second task of the new entrance;If the priority of second task is less than described first
Task, then continue to be run into kernel according to each task in priority level height successively calling task list;If described
Whether the priority of two tasks is higher than the first task, then judge kernel in locked task switching state;If being not in lock
Dead task switching state, then directly interrupt first task and transfer to run the second task;If being in locked task switching state,
Wait kernel to release after task switching locking state, then interrupt first task and transfer to run the second task.
Further, before the generation at least one is available for the shared global variable of multiple tasks, the method is also wrapped
Include:Multiple tasks are created, and according to the different priority of the relation allocation between each task;Activate each task to perform respectively
The corresponding program code of each task, and task list is entered into after the completion of task timer counter wait kernel calls operation.
Further, before generation at least one is available for the shared global variable of multiple tasks, the method also includes:Swash
A hardware timer living, the hardware timer is incremented by build a time reference axle according to the cycle of setting;It is complete at each
After being updated when office's variable is generated or by modification, the time numerical value on the time reference axle is read immediately as the global variable
Timestamp;When each task is run by kernel calls also from the time reference axle read a time numerical value as this
The timestamp of business;In each task call global variable, the timestamp for first obtaining this task and the global variable that called
The time difference of timestamp, then compare the time difference and default time threshold to determine the effectiveness of corresponding global variable.
Further, it is described to compare the time difference with default time threshold to determine the effective of corresponding global variable
Property, specifically:When the time difference is less than or equal to default time threshold, assert that the global variable is effective;When this
Between difference be more than default time threshold when, assert that the global variable is invalid.
Further, operating first task uses the specified global variable for being responsible for data modification renewal by other tasks
Concrete mode be:For using, wherein the local becomes the local variable of a mapping defined in the storehouse of the first task
Measure with specify by the first task be responsible for data modification renewal global variable it is corresponding.
A kind of task scheduling system of vehicle-mounted ATP, including:
Global variable generation module, for generating at least one the shared global variable of multiple tasks is available for, and each is global
Variable specifies a task to carry out data modification renewal to it;
Kernel runs module, runs into kernel for priority level highest first task in calling task list, when
It is operating it is first do honest work to specify by its global variable for carrying out data modification renewal carry out data modification update when, kernel
Locked task switching is until data modification renewal is completed;It is whether new in monitoring task list while the first task is run
Into the second task;
Priority comparison module, after the second task is newly entered, for comparing first task and institute just in kernel operation
State the priority level height of the second task of new entrance;If the priority of second task is less than the first task, refer to
Task execution module is made to continue to be run into kernel according to each task in priority level height successively calling task list;If
The priority of second task is higher than the first task, then start kernel running status judge module;
Kernel tasks running status judge module, for judging kernel whether in locked task switching state;If not locating
In locked task switching state, then instruct kernel operation module directly to interrupt first task and transfer to run the second task;If just locating
In locked task switching state, then instruct kernel operation module to wait kernel to release after task switching locking state, then interrupt the
One task transfers to run the second task.
Further, the system also includes task creation module and mission-enabling module;Wherein, task creation module, uses
In establishment multiple tasks, and according to the different priority of the relation allocation between each task;Wherein, mission-enabling module, is used for
Each task is activated to perform the corresponding program code of each task respectively, and task list is entered into after the completion of task timer counter
Wait kernel calls operation.
Further, the system also includes hardware timer, timestamp load-on module and timestamp comparation module;Its
In, hardware timer, for being incremented by build a time reference axle according to the cycle of setting;Wherein, timestamp load-on module,
After updating when each global variable is generated or by modification, the time numerical value conduct on the time reference axle is read immediately
The timestamp of the global variable;When each task is run by kernel calls also from the time reference axle read a time
Timestamp of the numerical value as this task;Wherein, timestamp comparation module, in each task call global variable, first obtaining
The time difference of the timestamp that takes this task and the timestamp of the global variable for being called, then compare the time difference with it is default
Time threshold corresponds to the effectiveness of global variable to determine.
Further, in the timestamp comparation module of the system, the described time difference that compares is with default time threshold
To determine the effectiveness of correspondence global variable, specifically:When the time difference is less than or equal to default time threshold, recognize
The fixed global variable is effective;When the time difference is more than default time threshold, assert that the global variable is invalid.
Further, the system also includes:Local variable mapping block, for when the operation the run in kernel in module
One task using specify be responsible for the global variable of data modification renewal by other tasks when, the local variable mapping block this
Defined in the storehouse of one task one mapping local variable for using, wherein the local variable with specify by the first task
The global variable that responsible data modification updates is corresponding.
Invention defines multiple global variables, modify more in task running to its corresponding global variable
When new, the locked task switching of kernel does not allow other task preemption kernels, but as long as task is not in locked task switching state
(be operating first task be not changing update its management global variable when), than operating first task
The second high task of priority just can at any time seize kernel, so as to simplify Task Scheduling Mechanism, further reduce certainly
Communication latency between task, just improves switching efficiency naturally, changeable such that it is able to meet CBTC system onboard ATP subsystems
Amount, multitask, the control task of hard real time need.
Specific embodiment
The present invention is described in detail below in conjunction with specific implementation method, be used in the exemplary embodiment and description of the present invention
The present invention is explained, but it is not as a limitation of the invention.
Fig. 1 is the ultimate principle schematic flow sheet of method for scheduling task of the present invention.As shown in figure 1, the dispatching method 100 is wrapped
Include:
S110, generates at least one and is available for the shared global variable of multiple tasks, each global variable to specify a task
Data modification renewal is carried out to it;
S120, priority level highest first task is run into kernel in calling task list, when operating first
Task just to specify carry out data modification by its global variable for carrying out data modification renewal and update when, the switching of kernel locked task
Until data modification updates completing;Whether second is newly entered while the first task is run in monitoring task list
Business;
After the second task is newly entered, then order performs S130 steps;
S130, compares the priority level height of the second task of the first task and new entrance for just running in kernel;
Its comparative result:
If the priority of second task is less than the first task, S120 steps are continued executing with, namely are to continue with
Run into kernel according to each task in priority level height successively calling task list;
If the priority of second task is higher than the first task, order performs S140 steps;
Whether S140, judge kernel in locked task switching state;
If judged result is not in locked task switching state, order performs S151 steps, if being in locked task
Switching state, then sequentially perform S152 steps;
S151, directly interrupts first task and transfers to run the second task
S152, waits kernel to release after task switching locking state, then interrupts first task and transfer to run the second task.
Known according to above-described embodiment method, the present invention is again based on the scheduling mechanism of task priority, but difference is
When a higher priority personage arrives, the mode of interrupt call is entirely different.For example in task list have five it is different
Task:T1, T2, T3, T4, T5, if the priority of the distribution of T1-T5 is successively from high to low, then normal condition calls first T1
Into kernel operation, T2, T3, T4, T5 are then called in successively and is run into kernel;When new task Tx enters task list, just
In task T3 of execution;If the priority level of Tx is less than T3, continue the operation of T3, certainly after the completion of T3 operations, similarly
Be according to T4, T5, Tx they three priority level height call in successively kernel operation;But if the priority level of Tx is higher than
Whether T3, then first judge kernel in locked task switching state, straight if kernel is not in locked task switching state
Meet interruption T3 to transfer to run Tx, kernel releasing task switching is needed to wait for if kernel is not in locked task switching state locked
After state, T3 could be interrupted and transfer to run Tx.
The locked switching state of so-called task, refers to operating T3 just to specifying the global variable updated by its modification to enter
State when row modification updates, once is finished in modification renewal release at once task switching locking state.Because, in T3 runnings
In, comprising many specific execution actions, also include that data modification update action (including data write by so-called data modification certainly
Enter process), it is breakneck to be performed and read the data by other tasks interrupts in data modification renewal process, so this reality
The strategy that example just takes the locked switching of task is applied, prevents other tasks from seizing kernel at the moment;Equally, in T3 runnings not
Only include data modification renewal process, more times are to perform other actions, if always locked kernel is not cut
Change, then the Tx waiting time is longer long, the requirement that emergency is processed in time can not be reached completely, so the present embodiment task lock
Extremely switch only in data modification renewal process, if it is corresponding complete to need repeatedly modification to update its in T3 implementation procedures certainly
Office's variable, then perform repeatedly locked and releasing task switching state.
Therefore, the locked task handover operation that the present embodiment is carried out by the more new stage in data, can be prevented effectively from
The risk read by other tasks in data modification renewal process.Meanwhile, it is this only in data maintenance stage control task scheduling
Method the time that task is mutually waited is reduced for semaphore or mailbox.
Certainly, for the locked switchover policy of task for preferably coordinating embodiment to introduce, the present embodiment additionally uses shared
Global variable+mapping local variable mode, be:Generate at least one and be available for the shared global variable of multiple tasks,
Each global variable specifies a task to modify to it renewal, and other task calls global variable data are just in its storehouse
Defined in one mapping local variable it is corresponding with shared global variable;For example:Five different tasks and respective global change
Amount (task can be to reply global variable, but a global variable can only be updated modification by a task)
It is:T1-a, T2-b, T3-c, T4-d, T5-(e, f), if need to use variable in T3 runnings by a, c, e, f, T3
Except in addition to it can directly invoke c, a, e, f all can only map in storehouse a local variable a1 corresponding with them, e2,
F1 is for using.Essence is that the reading of data and modification are updated to be divided into different authorities, and each data is by generating its task
Safeguarded, global variable of the task directly to sharing is modified.Other tasks then should be in this task using the data
The local variable of a mapping is corresponding with shared global variable defined in storehouse.
Fig. 2 is the entire flow schematic diagram of method for scheduling task of the present invention.As shown in Fig. 2 the method for scheduling task 200 with
The difference of method for scheduling task 100 is also to include and existing μ C/OS-II system scheduling methods identical conventional steps S210:
Multiple tasks are created, and according to the different priority of the relation allocation between each task;It is each to perform respectively to activate each task
The corresponding program code of task, and task list wait kernel calls operation is entered into after the completion of task timer counter.The task
Step 220 in dispatching method 200 to 261 (262) is identical to 151 (152) with step S110 in method for scheduling task 100,
Here is not repeated.Because many data need to gather before system operation or generate, these are all to have to appoint by establishment
The execution of business difference, such as:Door contact interrupter state correspondence defines a vehicle door status Detection task, certainly before train operation
The on off state of car door is known for other task calls, so first the corresponding program code of vehicle door status Detection task to refer to
Corresponding hardware is made to obtain on off state, other tasks (such as vehicle launch task) are obtained could basis after the status data
It makes corresponding action.
In order to ensure the ageing security performance to improve system of data, when the present embodiment also introduces effective data
Effect property checking mechanism, does not rely only on the scheduling mechanism of operating system to ensure data validity.The present embodiment is in Fig. 2 basis
On, timestamp mechanism has been incorporated herein, the timestamp of each task is generated based on hardware timer, protect by timestamp monitoring
It is ageing that card task works, and is:Before generation at least one is available for the shared global variable of multiple tasks, one is activated
Hardware timer, the hardware timer is incremented by build a time reference axle according to the cycle of setting, the time cycle according to
It can be 1 millisecond or several milliseconds that actual system needs;After updating when each global variable is generated or by modification, read immediately
The time numerical value on the time reference axle is taken as the timestamp of the global variable;When each task is run by kernel calls
Also a time numerical value is read from the time reference axle as the timestamp of this task;In each task call global variable
When, the time difference of the timestamp of the timestamp for first obtaining this task and the global variable for being called, then compare the time difference
With default time threshold determining the effectiveness of corresponding global variable.Can be specifically:When the time difference is less than or waits
When default time threshold, assert that the global variable is effective;When the time difference is more than default time threshold, assert should
Global variable is invalid.
With specific reference to shown in Fig. 3:
Generate in moment t1 or have updated global variable a, b, then obtain timestamp on a timeline and cause the data to be
t1+a、t1+b;
Generate in moment t2 or have updated global variable c, d, then obtain timestamp on a timeline and cause the data to be
t2+c、t2+d;
In moment t3 operation task 1, in the same manner the timestamp of task 1 is t3, if the operation of task 1 need to call data a,
B, c, d, then use compare the time difference obtained between them first:
R1=t3-t1, R2=t3-t2
Then R1 is compared with default time threshold Y1, if R1 is less than or equal to preset time threshold Y1
When, it is believed that a, b are effective;
R2 is compared with default time threshold Y2 again, if R2 is more than default time threshold Y2, it is believed that c, d
It is invalid.
The present embodiment is substantially:Independent hardware timer is introduced as system time axle, each task can be by reading
The depositor for taking hardware timer obtains own timestamp.On the one hand the timestamp is responsible for safeguarding the life of data as this task
Starting mark is checked for other tasks, while being also to check that other tasks are supplied to this task to use the ageing foundation of data.
Even if such mechanism ensure that operating system breaks down causes task time delay execution, the delay of data renewal, also will not
The safety of impact system, equivalent to system one insurance is increased.
It should be noted that its time threshold is the different numerical value arranged according to actual different situations, such as:Start train
Task is task 1, needs to call door contact interrupter state variable to be that (a=1 represents pass to a, and a=0 represents pass, and the moment, difference a was continuous
Change update), our given thresholds are 1000 milliseconds;If it is constantly t3 that task 1 brings into operation, obtain first between them
Time difference R2=t3-t2, only R2 be less than or equal to 1000 milliseconds, just think that a data are valid data.If this when
Between threshold value arrange too big such as 10 minutes, then when within 10 minutes switch gate repeatedly and status data do not update when, it is this
It is abnormal just abnormally dangerous, in fact it could happen that car door opens, and train is but travelled always.
Once the system malfunctions in existing system, it is likely that cause data to update delay, it is longer that data update delay
Great potential safety hazard will be brought, such as train arrival tail gate opens a period of time, but data never update, also
In the closed mode at a certain moment before, due to going checking without timestamp, then will be considered to the data effectively, then will be direct
Cause car door not close just to start with regard to train.But the present embodiment is added after timestamp mechanism, and it is to use independently firmly
Part intervalometer, can effectively verify the effectiveness of each data, so as to further improve the security performance of system.
Task scheduling system corresponding with preceding method is described below.
Fig. 4 is the corresponding scheduling system principle diagram of Fig. 1 dispatching methods.As shown in figure 4, this enforcement also example discloses a kind of car
Carry the task scheduling system 400 of ATP, including global variable generation module 410, kernel operation module 420, priority comparison module
430th, kernel tasks running status judge module 440 and local variable mapping block 450.Wherein, global variable generation module
410, it is available for the shared global variable of multiple tasks, each global variable to specify a task to enter it for generating at least one
Row modification updates;Wherein, local variable mapping block 450, for using when the operation first task in kernel operation module
When being responsible for changing the global variable for updating by other tasks, the local variable mapping block is defined in the storehouse of the first task
For using, wherein the local variable is responsible for data modification renewal to the local variable of one mapping with specifying by the first task
Global variable is corresponding.Wherein, kernel operation module 420, for priority level highest first task in calling task list
Into kernel operation, when operating first doing honest work carry out data modification renewal by the global variable of its modification renewal to specifying
When, the locked task switching of kernel is until data modification renewal is completed;The monitoring task list while first task is run
In whether newly enter the second task;Wherein, priority comparison module 430, it is interior for comparing after the second task is newly entered
The priority level height of the first task of core operation and the second task of the new entrance;If the priority of second task is low
In the first task, then task execution module is instructed to continue according to each in priority level height successively calling task list
Task is run into kernel;If the priority of second task is higher than the first task, starts kernel running status and sentence
Disconnected module;Wherein, kernel tasks running status judge module 440, for judging kernel whether in locked task switching state;
If being not in locked task switching state, instruct kernel operation module directly to interrupt first task and transfer to run the second task;
If being in locked task switching state, instruction kernel operation module waits kernel to release after task switching locking state, then
Interrupt first task to transfer to run the second task.
Fig. 5 is the corresponding scheduling system principle diagram of Fig. 2 dispatching methods.As shown in figure 5, this scheduling system 500 with tune
The difference of degree system 400 is also to include and existing μ C/OS-II system calls system identical conventional modules:Task creation module
510 and mission-enabling module 520.Other modules 530-570 in the system all with scheduling system 400 in module 410-450
It is identical, it is not repeated.Wherein, task creation module 510, for creating multiple tasks, and according to the relation between each task
With different priority;Wherein, mission-enabling module 520, for activating each task to perform the corresponding journey of each task respectively
Sequence code, and task list wait kernel calls operation is entered into after the completion of task timer counter.
Fig. 6 is the corresponding scheduling system principle diagram of the corresponding dispatching methods of Fig. 3.As shown in figure 5, this scheduling system 600
With scheduling system 500 difference be, also including hardware timer 608, timestamp load-on module 609, timestamp comparation module
610.Other modules 601-607 in the system are all identical with module 510-570 in scheduling system 500, are not repeated.Its
In, hardware timer 608, for being incremented by build a time reference axle according to the cycle of setting;Wherein, timestamp loading mould
Block 609, after updating when each global variable is generated or by modification, reads immediately the time number on the time reference axle
It is worth the timestamp as the global variable;Also from the time reference axle one is read when each task is run by kernel calls
Timestamp of the individual time numerical value as this task;Wherein, timestamp comparation module 610, in the global change of each task call
During amount, the time difference of the timestamp of the timestamp for first obtaining this task and the global variable for being called, then compare the time difference
Value and default time threshold are determining the effectiveness of corresponding global variable.It is described in the timestamp comparation module 610 of the system
The relatively time difference and default time threshold to determine the effectiveness of corresponding global variable, specifically:When the time difference
During less than or equal to default time threshold, assert that the global variable is effective;When the time difference is more than default time threshold
During value, assert that the global variable is invalid.
The technical program is enclosed within μ C/OS-II operating systems for the 1 of the practice proposition of vehicle-mounted ATP system and carries out
The solution of task scheduling, it can simplify the interface between application software and operating system, be conducive to Software Architecture Design;
The waiting time that can also be communicated between reduction task, improve switching efficiency;The real-time of data exchange between its increase task
Protection, can improve security of system.
To sum up embodiment understands that the present invention mainly takes following to realize more safe and efficient task scheduling
2 points of improvement:
First, the present invention adopts succinct task scheduling and communication mechanism, based on μ C/OS-II operating system practical application rings
The analysis in border, abandon that μ C/OS-II systems carry based on the task schedulings such as semaphore, mailbox and communication mechanism.Only used
Most basic task based access control priority and the function of time management, the priority and execution cycle during by creating to different task
Definition, the operating mechanism for realizing task determines.Meanwhile, the globally shared variable task level self-locking implemented by run duration is become
More the data exchange between task is realized with local mapping variable call-by mechanism, so as to meet CBTC system onboard ATP subsystems
Multivariate, multitask, the control task of hard real time need.
2nd, the present invention is the real-time property protection based on timestamp, introduces independent hardware timer as system
Time shafts, each task can pass through to read the depositor of hardware timer and obtain own timestamp.The timestamp one side conduct
This task is responsible for safeguarding the life starting mark of data for other task inspections, while being also to check that other tasks are supplied to this
The ageing foundation make sure with data, so as to further increase the security performance of system.
The technical scheme for being provided the embodiment of the present invention above is described in detail, specific case used herein
The principle and embodiment of the embodiment of the present invention are set forth, the explanation of above example is only applicable to help and understands this
The principle of inventive embodiments;Simultaneously for one of ordinary skill in the art, according to the embodiment of the present invention, in specific embodiment party
Will change in formula and range of application, in sum, this specification content should not be construed as limiting the invention.