1. System interruption and clock cycle
1.1 system interruption
Interrupt is a hardware mechanism that notifies the cpu Of an asynchronous event. Once the interrupt is identified by the system, the CPU stores partial (or all) context values, that is, the values of some (or all) registers, and jumps to a special subroutine, it is called the interrupt service subroutine (ISR ). Interrupt Service subprograms are used to process events. After processing is completed, the task is scheduled and the program returns to the task with the highest priority in the ready state to start running (for the deprived kernel ).
Interruption allows the CPU to handle the event only when it occurs, instead of having the microprocessor continuously query for events. Two special commands are used: Disable interrupt and enable interrupt, which can prevent the microprocessor from responding or interrupt the response. In the real-time environment, the disconnection time should be as short as possible. The disconnection affects the interrupt response time. If the disconnection time is too long, the interruption may be lost. The service interruption processing time should be as short as possible, and the service interruption should do as little as possible. Most of the work should be left for the task.
1.2 system clock cycle
The clock cycle is a specific periodic interrupt (clock interrupt), which can be seen as the pulsation of the system's heart. The operating system determines the time interval through clock interruption, achieves the time delay and determines the task timeout. The interval between interruptions depends on different applications, generally ranging from 10 ~ 200 ms. The cycle interrupt of the clock allows the kernel to delay the task by several integers, and provides the basis for waiting for timeout when the task wait event occurs. The faster the clock frequency, the more overhead the system has. The system defines a 32-bit unsigned integer ostime to record the number of clock latencies after the system starts. You must enable the timer after the multi-task system is started, that is, after osstart () is called. % 26mu; C/osi2's clock cycle service is implemented by calling ostimetick () in the interrupt service subroutine. The sample code for the clock cycle interruption subprogram is as follows:
Void ostickisr (void ){
Saves the value of the Processor register;
Call osintenter () or add osintnesting to 1;
Call ostimetick ();
Call osintexit ();
Restores the value of the Processor register;
Execution of the interrupted return command;
}
2. Clock management system
2.1 ucos ii clock Management System
The original clock Management System of ucos ii is similar to Linux, but much simpler than Linux. It only provides users with a periodic signal ostime, and the clock frequency can be set to 10 ~ 100Hz, the clock hardware periodically interrupts the CPU, and the system periodically responds to the clock interruption. Every time the clock is interrupted, the interrupt handler updates a global variable ostime. The core of the ucos ii clock interrupt service program is to call the ostimetick () function. The ostimetick () function is used to determine whether a delayed task ends with a delay and place it in the ready state. The program pseudocode is as follows:
Void ostimetick (void ){
Ostimetickhook (); // call the User-Defined clock cycle function
While {(all tasks except idle tasks)
OS _enter_critical (); // Guanzhong disconnection
The delay time for all tasks is decreased;
Scan a task that expires and wake up the task;
OS _exit_critical (); // interrupt
The Pointer Points to the next task;
}
Ostime ++; // The total time since startup
}
In the ucos ii clock cycle function, you need to execute the User-Defined clock cycle external connection function ostimetickhook (), and scan the task linked list and reduce the task latency. In this way, the clock cycle function ostimetick () has two points: No
Foot:
① Processing additional tasks ostimeiickhook () during clock interruption, which increases the burden of Interrupt Processing and affects the accuracy of the scheduled service; ② scanning the task linked list when the clock is disconnected, the longer the task is, the longer it takes. A long disconnection has an adverse impact on the interrupt response, which should be avoided.
2.2 improved clock Management System
To address the shortcomings of the above ostimetick (), we need to improve it to optimize the clock cycle function. In Linux, the interrupt response is generally divided into two parts: immediate service interruption and bottom half ). Immediate service interruption is only important and can be completed quickly, and less important work that requires a long time to be completed is completed in the half-processing part, in this way, the interrupt response speed can be improved.
Ucos ii does not support half-processing. To reduce the workload of the clock interrupt handler and improve the clock accuracy of ucos ii, you can put part of the work to be processed during each clock interruption at the task level. In this way, the CPU consumption of each clock interrupt processing can be reduced, and the interrupt response speed and the clock precision of ucos ii can be improved. To this end, define the task ostimetask () to process the operations originally needed in ostimetick. Because % 26mu; C/osii adopts a priority-based preemptible scheduling policy, the task must be scheduled to run after each time the clock interrupt processing program ends, so the task ostimetask () has the highest priority in the system. It executes the User-Defined clock cycle function ostimetickhook (), reduces the latency of all tasks, and links expired tasks to the linked list ostcbrlist. The ostcbrlist manages all expired tasks. The pseudo code of the ostimetask () function is as follows:
Void ostimetask (){
Ostimetickhook () // user-defined time processing function
While {(all tasks except idle tasks)
Decrease the latency of all tasks;
Link all tasks to the ostcbrlist linked list;
}
When the task status changes to sleep, ossched () is called to schedule the task;
}
In the task ostimetask (), execute the User Function ostimeiickhook () that originally handled the clock interruption, and link the delayed tasks to the ostcbrlist linked list, in this way, in the clock interrupt program, you only need to scan the linked list that has expired the task, instead of scanning the entire linked list, reducing the disconnection time. Ostcbrlist is a new linked list that manages all expired tasks.
At the same time, it is necessary to reduce the execution workload of ostimetick () and only scan the ostcbrlist linked list, which also reduces the disconnection time. The ostimetick () pseudocode is as follows:
Void ostimetick (void ){
Ostime ++;
OS _tcb * ptcb = ostcblist; // The ostcbrlist directs to the linked list of all expired tasks.
While (ptchb! = NULL ){
Guanzhong disconnection;
Wake up a task;
Open interrupt;
The Pointer Points to the next task;
}
}
3 Summary
Taking the open-source embedded operating system ucos ii as an example, this paper analyzes the interrupt mechanism of the operating system and the conditions that the interrupt should meet. This paper introduces the ucos ii system clock cycle, discusses the shortcomings in the clock interrupt function, and provides a solution, which effectively improves the interrupt response speed and the clock accuracy of ucos ii.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.