Implementing task scheduling and event handling in RTOS+

5 downloads 9166 Views 390KB Size Report
Department of Electrical and Computer Engineering. University of Calgary. 2500 University Drive NW, Calgary, AB, Canada T2N 1N4. Email: {ngolah, wangyx ...
IMPLEMENTING TASK SCHEDULING AND EVENT HANDLING IN RTOS+ Cyprian F. Ngolah, Yingxu Wang, and Xinming Tan Theoretical and Empirical Software Engineering Research Center Department of Electrical and Computer Engineering University of Calgary 2500 University Drive NW, Calgary, AB, Canada T2N 1N4 Email: {ngolah, wangyx, tun$ @enel.ucalgary.ca ABSTRACT An important component in the kernel of a real-time operating system (RTOS) is the task scheduler. Various solutions on task scheduling such as priority inheritance and priority ceiling have been proposed to deal with the priority-inversion problem that arises when the preemptive priority driven scheduling scheme is used. However, the priority inheritance protocol introduces drawbacks such as multiple blocking of tasks and deadlock, while the priority ceiling protocol is hardly supported by most existing real-time operating systems. In this paper, we first present a formal description of a task scheduling algorithm that provides a solution to the priority inversion problem and eliminates multiple blocking of tasks running in a single processor platform. We use RTPA, a formal specification notation, to rigorously describe the architecture, static and dynamic behaviours of the task scheduler. Our method employs a multi-queue based scheduling system in which all tasks pending on a resource or event are put into different queues with preassigned priorities. This technique eliminates the problem of priority inversion and prevents multiple-blocking and deadlock from happing. Based on the new method, a framework for implementing task scheduling and event handling is implemented for RTOS+, which is a portable real-time operating system developed by the authors.

Keywords: Software engineering, tusk scheduling, realtime operating system, RTOS+, RTPA 1. INTRODUCTION A real-time operating system (RTOS) is an operating system that supports and guarantees timely responses to external and internal events of real-time systems. A major component of the kernel of an RTOS is the task scheduler that is responsible for assigning system resources to a task according to a suitable scheduling policy. Due to the unpredictable nature of events, care must be taken in

implementing the task scheduler so that it is capable of reacting to events in a strictly timed manner. Various task scheduling algorithms and schemes have been developed for implementing RTOS’s, which can be classified as the time-driven, priority-driven- and hybrid-driven schemes [SI. Most commercial RTOS’s use the priority-driven scheme in task scheduling. Priority-driven techniques assign priorities to tasks based on their requirements and the scheduling algorithms. A task with the highest priority is selected and executed by the scheduler. Priority-driven scheduling techniques can be further classified into static and dynamic priority policies. In the former, the priority of each task is assigned when it is created, and remains constant. In the latter, the priority of a task may change during run-time depending on system statuses and resource availability. Various algorithms exist for the priority-driven scheduling scheme, such as the rate monotonic scheduling algorithm, the earliest deadline first scheduling algorithm, and the least laxity first scheduling algorithm [2]. A common problem often encountered when implementing task scheduling in RTOS’s using the priority-driven scheme is priority inversion [3]. This problem arises when tasks with different priorities share common data. Much work has been done to address this problem and various solutions such as the basic priority inheritance and priority ceiling protocols have been proposed and implemented [ 1,3,4,]. However, these solutions often introduce other issues such as multiple blocking of tasks and deadlock [3]. This paper expands on the task scheduling and eventhandling algorithms proposed in RTOS+, a portable realtime operating system developed in [8,9]. It presents a formal description of task scheduling and event-handling techniques, and their implementation in RTOS+. The algorithms for task scheduling and event handling are formally specified using Real-Time Process Algebra (RTPA) [6, 71 in Section 2. The implementation of the task scheduler and event handler for RTOS+ is described in Section 3. Empirical conclusion and future work are discussed in Section 4.

CCECE 2004- CCGEI 2004, Niagara Falls, May/mai 2004 0-7803-8253-6/04/$17.0002004 E E E

- 1523 -

consideration in RTOS+, by which tasks are not only guaranteed to run based on their deadlines, priorities, and least laxity time [9], but also on their importance and criticality.

2. TASK SCHEDULING AND EVENT HANDLING TECHNIQUES OF RTOS+ ' RTOS's implemented by existing algorithms for prioritydriven task scheduling suffer from the problem ,of priority inversion [3]. Limited computing resources in' an RTOS require those resources must be shared by several processes running concurrently. Therefore, for an RTOS to fulfill its desired features of predictability, timeliness and reliability, it is necessary to use a scheduling algorithm that can eliminate priority inversion and avoid deadlock and multiple blockings of tasks. In [9], we presented a formal description of a portable real-time operating system, RTOS+. We expand this work on task scheduling and event handling by presenting a proposed algorithm and its formalization that minimizes the chance of priority inversion and deadlock in RTOS+.

'asBcheduhg () e~d\ ? (FCB(pmc#~.PmeStatus~ = Z)/iPrOeess is ready //Getcurrent rumingprccessn u m b e r u s i n g s y s t w f o n 1~urrenthingPrcc(d::CPuSl>, d PmeW >) // Pmecsm is idle ? hssor.CPUStatus = 2 i ( PCB[Prcc#).CurrentRu@nRunningTim$l= 0 //reset nmtime counta + P€B(Roe#~).ProcStatud := 3 I/ set to nmniog

2.1 Task Scheduling Techniques of RTOS+

i

PCB(Pme#W).RunningStatustabll:=T

i

P ~ E S S M . C P u s ~:=~IS/ ~/ h o g PCB(Proc# ).Runnings tatus = T

A state transition diagram as shown in Fig. 1 illustrates the task scheduling techniques as event-driven processes in RTOS+. Based on this framework, the task scheduling and event handling algorithms of RTOS+ can be explained. The RTPA specification for the architecture of RTOS+ has been provided in [9]. Based on the architecture, the task scheduling algorithm of RTOS+ is shown in Fig. 2.

i

R

[ //pmeessisstillming

20

i@:=p I

//aflerlmsinmmt~gmningtime

i P C B ( P r o e # ) . h e n t R ~ g T m d :=

PCB(Pme#).CmtRunningTimd t I

+ ProeessScheduledBI=I

)

I ?- // Pmewris busy so check priorities p m p t i o n ? ( (FCB(Pmc#Y).PmcRiorifl> FCB[M.PrccRionty) //Move pmeess to delayed@cue 1DelayedQ.Eoqueue(d:: Delayed@, PTCC#~>, d::@[email protected]) i FCB(Prodi)klayedTimd = 0 //Reset delay-counta PCB(Proc# ).Runnings tatus = F i

R

20 +@t:=$t+ I

[//mu= tinea //afierlmsmmatdelaytinx

i PCB(Pmc#).DelayedTimdl := PCB(Tm#).kla@Tme!

tI

) // Assigo CPU to new prccess i

professor.CPUS!abs :=I

+ PCB[Prcc#!).PmcStN := 3 + FCB(Roe#I).RunningSts :=1

//Running //setproeess state to wing

i @PmeessScheduledk=T

)

1 // [II) Schedulethe low priorityqueues // Similarto that of the high prioritytask scheduling B defined above +§

Fig. 1 The task scheduling state transition diagram of RTOS+

I

The task scheduling of RTOS+ is priority based and event driven. We assign fixed priorities to tasks, which are determined based on their importance or criticality. The importance of a task is the value imparted to the system when the task satisfies its timing requirement (deadline). Tasks are categorized into four priority types: the high and low priority interrupts; base and high priority processes. A process, when created, will be put in a proper queue corresponding to its predefined priority levels in one of the categories by the task dispatcher. The categorization of tasks based on their criticality is an important

Fig. 2 Specification of task scheduling in RTOS+

The task scheduler of RTOS+ is responsible for determining which task should be executed on the processor at a given time. It works in collaboration with the time manager and event manager to continuously switch the processor between processes. Tasks in the ready state of the RTOS+ transition diagram are stored in two queues (ReadyQH and ReadyQL) that hold high priority interrupt tasks and low priority event tasks respectively. The task scheduler first schedules all tasks in

- 1524 -

ReadyQH before scheduling those in ReadyQL. The task scheduler continuously checks the ready queues to see if there is any task ready to be run. If there are ready tasks in the queue, it compares the priorities of the currently executing task and that of the first task in the queue. If the first task in the queue has a higher priority, it pre-empts the CPU from the currently running task, assigns the CPU to the higher priority task from the queue, after sending the lower priority task to the delayed state as shown in Fig. 1. The delayed-task is eventually re-dispatched to the appropriate ready queue when the CPU is available. If there are two tasks of the same priority, the task scheduler dispatches them on a first-come-first-serve basis. Each task in RTOS+ has a run-time counter which keeps track of time used on the processor. Once a process is assigned to the processor, its run-time counter is incremented based on the system clock at a given frequency, say lms. In this way, the time manager may verify if a task using the processor has exhausted its timeslice so that it should be sent to the delayed queue. If a task is pre-empted and delayed, the system keeps track to see if a new time slice is available for the delayed , task. If so, the task will be rescheduled into the ready queue.

solution also eliminates deadlock and multiple blocking of tasks since no task can block each other if they have different priorities. 3. IMPLEMENTATION OF THE TASK SCHEDULER AND EVENT HANDLER IN RTOS+ The task scheduler of RTOS+ is implemented by two major components as shown in Fig. 3. The low-level scheduler called the CPU scheduler and a high-level scheduler called the Resource allocator. The CPU scheduler is responsible for switching the processor between tasks using the task scheduling algorithm discussed in Section 2.1. The resource allocator is responsible for assigning resources to different tasks running in the system. It also introduces new tasks into the system and deletes tasks that are completed or no longer needed.

I

W Task Scheduler

I

2.2 Event Handling Techniques of RTOS+ An event handler ensures the interaction between the RTOS+ and internayexternal requests and environments. The event handler of RTOS+ detects predefined events, and informs the scheduler to execute corresponding process(s). The event handler works in collaboration with the resource manager to ensure that tasks blocked for an unavailable resource eventually get it.

Scheduler

Handler Interrupts Dispatcher

Tasks

Hardware

Fig. 3 Interaction between task scheduler and event handler in

RTOS+

2.3 Resource Management Techniques of RTOS+ The priority inversion problem in operating systems can be solved by the following techniques in RTOS+. The data structure of the process control block (PCB) contains a field that indicates the resource it may need during execution. Whenever a task is pending on a resource, the resource identifier is written on this field, otherwise the field contains a null value. All tasks are placed in a queue by the resource manager, sorted according to priorities in descending order and only the first task (with the highest priority) can be assigned the resource. When the resource becomes available, the first task in the queue is assigned the resource. In this case, the problem of priority inversion will not arise as no intermediate task can pre-empt the resource from the high priority task because the task to be assigned the resource is simply the first task on the resource waiting queue. The only overhead involved here is that the sorting of the tasks in the various queues. However, this is far less than the overheads involved in the priority inheritance and priority ceiling protocols. The proposed

3.1 The CPU Scheduler of RTOS+ The CPU scheduler can be implemented by using the algorithm specified in Section 2.1. Two queues, ReadyQH and ReadyQL, are adopted to schedule tasks with different priorities. The task scheduler first checks the status of the CPU and the priority of the current running process. If the CPU is idle or a lower priority task is currently executing on it, a new task with higher priority in the queue will be assigned after the current task is suspended. Processes are listed in the ready queue by the task dispatcher, in order to be executed by the CPU scheduler. An event handler is adopted to inform the CPU scheduler when an event has occurred that triggers new tasks and results in changes of scheduling statuses in RTOS+. 3.2 The Task Dispatcher of RTOS+ The resource allocator is implemented using two components: the task dispatcher and the event handler.

- 1525 -

The task dispatcher is responsible for dispatching various tasks to their appropriate queues based on their current statuses and requirements. It is generally responsible for managing all task queues in the system and making sure that each task is sent to the appropriate queue as soon as an event is triggered. I The task dispatcher is implemented using the task scheduling algorithm described in Section 2.1. It continuously checks the status of each task, and dispatches them to the appropriate queue. Only tasks dispatched to the ready queue will be served by the CPU scqeduler for executing.

3.3 The Event Handler of RTOS+ The event handler monitors events acceptable to the system and ensures that tasks pending for certain events are put in the suspended queue. As another key component of the resource allocator, the event handler manages interrupts, 1/0 events and other events related to system resources, particularly the memory. It dispatches appropriate actions to be taken by informing the CPU scheduler and the task dispatcher when an event has occurred. The algorithm for event handling in RTOS+ has been described in Section 2.2. The event handler is implemented that works in collaboration with the task scheduler to provide solutions to the priority inversion problem.

4. CONCLUSIONS Real-time operating systems play an important role in controlling real-time computing applications. Their design and implementation should therefore be given great attention so that the desired features of predictability, timeliness and reliability can be delivered. There is the need to rigorously describe the task scheduler and its relations with other components in the kernel of a realtime operating system. This paper has reported the development of the task scheduler and the event handler of the RTOS+ real-time operating system. Techniques of RTOS+ task scheduling and event handling and their algorithms have been formally described in RTPA. The core components of RTOS+ have been implemented by a multi-queue based scheduling system in ‘which all tasks pending on a resource or event are put into different queues with preassigned priorities. This technology has helped to eliminate the problem of priority inversion and to prevent multiple-blocking and deadlock from happing.

ACKNOWLEDGEMENTS The authors would like to acknowledge the Natural Science and Engineering Council of Canada (NSERC) for its support to this work. We would like to thank the anonymous referees for their comments. REFERENCES 111 Goodenough, J.B., L. Sha, The priority ceiling protocol: A method for minimizing the blocking of high priority Ada tasks, ACM SIGAda Ada Letters, V.VIII n.7, p.20-31, June, pp.1-3, 1998. 121 Kreuzinger, J., Brinkschult, U., Uhrig, S., Ungerer, T., Real-Time Event Handling and Scheduling on a Multithread Java Microcontroller, Elsvier Science Publications, June, 2002. ~ 3 1Lee, J.H. and H. N. Kim, “Implementing Priority Semaphore in uC/OS Real-Time Kernel,” IEEE Workshop on Software Technologies for Future Embedded Systems, May, Japan, 2003. 141 Sha, L., R. Rajkumar, and J.P. Lehoczky, Priority Inheritance Protocols: An Approach to Real-Time Synchronization, IEEE Transactions on Computers, September, p. 1175, 1990 151 Wang, Y.C. and Kei-Jay Lin, Implementing a General Real-Time Scheduling Framework in RED-Linux real-time kernel, IEEE Real-Time Systems Symposium, 1999.

161 Wang, Y., The Real Time Process Algebra (RTPA), Annals of Software Engineering, Vol. 14, JSluwer Academic Publishers, pp.235-274,2002. [71 Wang, Y., A New Approach to Real-Time Systems Specification, Proc. of the 2002 IEEE Canadian Conference on Electrical and Computer Engineering (CCECE’02), Winnipeg, MEi, Canada, May, Vol. 2, pp. 663-668,2002. 181 Wang, Y., Operating Systems, Chapter 133, in R. Dorfed., The Engineering Handbook, 2nd ed., CRC Press, 2004. c91 Wang, Y., and Cyprian F. Ngolah, Formal Description of a Real-Time Operating System using RTPA, Proc. of the 2003 IEEE Canadian Conference on Electrical and Computer Engineering, (CCECE’03), Montreal, Canada, May, pp.1247-1250, 2003.

- 1526 -