VxWorks学习笔记-Real-Time Multitasking

贾越
2023-12-01

VxWorks 学习笔记

§ Real-Time Multitasking

1.Introduction

Real-Time

  • A system is described as being deterministic if its response time is predictable.

  • Deterministic response is the key to real-time performance.

单一任务系统

  • 缺点:1)各个构件无法按照不同的速率执行;2)没有优先级;3)无法抢占;

  • 优点:没有任务切换的开销。

VxWorks如何满足实时要求

  • 多任务

  • 执行速度快:1) 轻量级的任务,上下文切换快; 2)没有系统调用的开销

  • 运行的确定性:基于优先级的抢占式调度确保了高优先级任务的响应速度。

任务的状态

  • Ready

  • Delay: waiting for a mount of time to pass

  • Pend: waiting for an object event

  • Pend & Delay

  • Suspend

多任务内核

  • VxWorks中管理任务的是Wind Kernal

  • 使用TCB跟踪管理任务;

  • TCB数据结构定义:WIND_TCB

  • 获取TCB指针:函数taskTcb()

  • 对每一个PendEvent,有独立的Queue

上下文切换

  • 分为两种类型:SynchronousAsynchronous

  • Asynchronous切换是由ISR引起的任务切换

  • Synchronous需要保存的寄存器少,所以速度更快

  • ISR的优先级比任务高

优先级调度

  • 抢占式调度是基于优先级的,高优先级抢占低优先级的任务

  • 重新调度发生的时刻:1) Kernal calls2) Interrupts (e.g. System clock ticks)

  • 优先级的高低取决于任务计时的要求,而不是任务的重要性

Round-Robin 调度

  • 作用于相同优先级的任务

  • Round-robin scheduling is disabled by default. To allow equal priority tasks to preempt each other, time slicing must be turned on: kernelTimeSlice (ticks)

  • -> clockRate = sysClkRateGet( )

Performance Enhancements

  • All tasks reside in a common address space. 1) Makes intertask communication fast and easy. 2) Makes context switch faster (don’t need to save and restore virtual address contexts). 3) A deviant task can corrupt other tasks.

  • All tasks run in supervisor (privileged) mode. No system call overhead.

Multitasking Facilities

  • Message passing queues for intertask communication within a CPU

  • Network routines for intertask communication between CPU’s

  • Semaphores to protect data which is shared by tasks

  • Timers and other interrupts to trigger task execution

  • I/O system facilities to read/write data to hardware devices

2.任务基础

任务的组成

  • TCB

  • Stack:存储自动变量和函数参数

Creating a Task

  • Allocate memory for the stack and TCB from a memory pool.

  • Initialize the stack.

  • Initialize the TCB.

  • Put task into ready queue.

  • newTid = taskSpawn (“tMyTask”, 150, 0, 20000, myRoutine, arg1, arg2, 0, 0, 0, 0, 0, 0, 0, 0)

Task ID

  • A task id of zero refers to task making call (self).

  • 相关函数:taskIdSelf, taskIdListGet, taskIdVerify

Task Name

  • 相关函数:taskName, taskNameToId

Task Priority

  • Timing requirements rather than hazy ideas about task importance should govern priorities.

  • One can manipulate priorities dynamically with: taskPriorityGet (tid, &priority)taskPrioritySet (tid, priority)

Task Stack

  • Fixed size after creation.

  • The kernel reserves some space from the stack, making the stack space actually available slightly less than the stack space requested.

  • Each byte in a task’s stack is filled with 0xee when the task is created. The checkStack( ) function searches from the end of the stack for this value in order to find the maximum stack usage.

  • VxSim targets add 8000 bytes to the requested stack size, to deal with simulated interrupts, which are handled on the stack of the current task.

Task Option

  • taskOptionsGet

  • taskOptionsSet

Task Creation

  • During time critical code, task creation can be unacceptably time consuming.

  • To reduce creation time, a task can be spawned with the VX_NO_STACK_FILL option bit set.

  • Alternatively, spawn a task at system start-up which blocks immediately, and waits to be made ready when needed.

Task Deletion

  • 相关函数:taskDelete, exit

3.任务控制

Restart

  • 相关函数:taskRestart

  • 通常用于错误恢复

  • 一个任务重起自身时,是由任务tRestart完成

Suspend & Resume

  • 相关函数:taskSuspend, taskResume

Delay

  • 相关函数:taskDelay(ticks)

  • 延迟1/7 秒:taskDelay (sysClkRateGet( ) / 7)

  • sysClkRateGetGet the number of ticks per second of the system clock

Reentrancy and Task Variables

  • Use only stack variables in applications.

  • Protect the resource with a semaphore.

  • Use task variables to make the variable private to a task.

  • Task Variables cause a 32-bit value to be saved and restored on context switches, like a register.

Task Hooks

  • User-defined code can be executed on every context switch, at task creation, or at ask deletion: taskSwitchHookAdd ( ) , taskCreateHookAdd( ), taskDeleteHookAdd ( )

  • VxWorks uses a switch hook to implement task variables.

Task Information

  • -> ti

4.错误码的使用

  • 全局变量errno用来保存当前执行任务的错误状态;

  • 任务上下文切换时,errno会保存到任务的TCB中;

  • 底层函数根据情况设置errno,上层调用函数检查errno的值确定函数调用失败的原因;

  • 错误码格式:模块编码(16bits)+错误类型(16bits)

  • 由错误码获取描述信息:shell命令printErrno, 函数strerror( )

  • 定义用户自己的错误码;

5.系统任务

  • tUsrRoot

  • tLogTask

  • tExcTask

  • tWdbTask

 类似资料: