## 2 Hybrid Real-Time Scheduler in the MIROS
### MIROS Hybrid Scheduling Structure  
In MIROS, the hybrid scheduling is developed for the purpose of combining the advantages of both event-driven scheduler's low memory consumption and multithreaded scheduler's high real-time performance. Firstly, the event-driven scheduling is implemented to keep the low data memory consumption. However, the real-time performance of the event-driven scheduling is poor as the preemption is not enabled. Thus, the multithreaded scheduler is also implemented and it is used dedicatedly to schedule the RT tasks. Consequently, the hybrid scheduling (both the event-driven scheduling and the multithreaded scheduling) is realized in MIROS. With this hybrid scheduling, the real-time OS can be achieved and the OS data memory usage is not high.  
The hybrid scheduling structure of MIROS is depicted in Figure 1. Both the event-driven scheduler and the multithreaded scheduler are implemented in parallel. The system tasks are classified into two kinds: the RT ones and the non-RT ones. The RT tasks are scheduled by the multithreaded scheduler while the non-RT tasks are scheduled by the event-driven scheduler. Two schedulers can switch to each other, but only one scheduler can be active at any time. The multithreaded scheduler has the priority higher than the event-driven one, thus it can preempt the event-driven scheduler whenever necessary. In case all the RT tasks are inactive, the OS will run in the pure event-driven scheduling model. However, if any RT task becomes active, the event-driven scheduler will be suspended, and then the OS will switch to the multithreaded scheduling model. When running in the multithreaded scheduling model, the _RMS_ (Rate Monotonic Scheduling) [11] scheduling algorithm is applied. Thisalgorithm is chosen because it uses the static priority scheme. Thus, it is simple to implement and the execution overhead is low. Since the computation ability and the memory resources of the WSN nodes are constrained, the applying of this algorithm is appropriate.  
The advantage of this MIROS hybrid scheduling is that a RTOS can be achieved with low data memory consumption. It is assumed that there exist nine tasks _T1, T2,..., T9_ and three of them (_T7, T8, T9_) are RT ones (Figure 1). If the pure multithreaded OS (mantisOS, uCOS, _etc._) is used to schedule these tasks, nine threads along with nine stacks need to be created, as in the pure multithreaded OS one thread will be created for the execution of one task whether or not this task is RT (the difference may be that the RT task is executed by a high-priority thread while the non-RT task is executed by a low-priority one). However, the non-RT tasks have loose constraint in the response time, and the preemption is not needed among their executions. Thus, they can be scheduled one after one by the event-driven scheduler. Consequently, only four stacks rather than nine need to be allocated: three of them are for the multithreaded scheduler to execute the three RT tasks (_T7, T8, T9_), while the left one is for the event-driven scheduler to execute all the non-RT tasks (_T1_ to _T6_). As a result, the number of the stacks in MIROS can be decreased greatly. Since the size of each thread stack is commonly large, e.g., in mantisOS each stack is set to 120 bytes, the data memory usage in the MIROS becomes smaller if compared with that in the pure multithreaded WSN OSes.