【正文】
Semaphore is given or Timeout expires. Returns OK if successful, ERROR on timeout (or invalid semId). 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Taking a Binary Semaphore semaphore available ? task pends until sem is given or timeout timeout task unpends semTake() returns ERROR no task continues semTake() returns OK yes task unpends, semTake() returns OK semaphore given 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Giving a Semaphores STATUS semGive (semId) Unblocks a task waiting for semId. If no task is waiting, make semId available. Returns OK, or ERROR if semId is invalid. 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Giving a Binary Semaphore tasks pended ? semaphore made available no task at front of queue made ready semaphore remains unavailable yes 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Information Leakage Fast event occurrences can cause lost information. Suppose a VxWorks task (priority=100) is executing the following code, with semId initially unavailable: FOREVER { semTake (semId, WAIT_FOREVER)。 waitForData ()。 ? 信號(hào)量機(jī)制必須有公共內(nèi)存,不能用于分布式操作系統(tǒng),這是它最大的弱點(diǎn)。如果將信號(hào)量看作共享變量,則 pv操作為其臨界區(qū),多個(gè)進(jìn)程不能同時(shí)執(zhí)行,一般用硬件方法保證。將 1時(shí),可以用來(lái)實(shí)現(xiàn)進(jìn)程的互斥。 } 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 PV原語(yǔ)(二) ? 其中用到兩個(gè)標(biāo)準(zhǔn)過(guò)程: asleep()。 { =+1。 if (0) asleep()。 =0時(shí), ; 0時(shí), 的個(gè)數(shù); 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 PV原語(yǔ)(一) ? 對(duì)一個(gè)信號(hào)量變量可以進(jìn)行兩種原語(yǔ)操作: p操作和 v操作,定義如下: procedure p(var s:samephore)。 end。它的類(lèi)型定義如下:(用類(lèi)PASCAL語(yǔ)言表述) semaphore = record value: integer。 ? 信號(hào)量作為任務(wù)間同步和互斥的機(jī)制,是快速和高效的,它們除了被應(yīng)用在開(kāi)發(fā)設(shè)計(jì)過(guò)程中外,還被廣泛地應(yīng)用在 VxWorks 高層應(yīng)用系統(tǒng)中。 default: startEmergProc ()。 case S_rctorLib_LEAK_POSSIBLE: checkVessel ()。 case S_rctorLib_TEMP_CRITICAL_ZONE: logMsg (“Run!”)。 } ... } 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Examining Errno Examine errno to find out why a routine failed. if (reactorOk() == ERROR) { switch (errno) { case S_rctorLib_TEMP_DANGER_ZONE: startShutDown ()。 } … pMem=malloc(sizeof(myStruct))。 ... } STATUS taskDelay (ticks) Use sysClkRateSet() to change the clock rate. Accurate only if clock rate is a multiple of seven ticks / seconds. Can suffer from “drift.” 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Reentrancy and Task Variables If tasks access the same global or static variables, the resource can bee corrupted (called a race condition). Possible Solutions: Task Variables cause a 32bit value to be saved and restored on context switchs, like a register. Use only stack variables in applications. Protect the resource with a semaphore. Use task variables to make the variable private to a task Caveat: task variables increase context switch times. See the taskVarLib manual pages for details. 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Task Hooks Userdefined code can be executed on every context switch, at task creation, or at task deletion: taskSwitchHookAdd () taskCreateHookAdd () taskDeleteHookAdd () VxWorks uses a switch hook to implement task variables. See manual pages on taskHookLib for details. 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Task Information Like i(), but also displays: Can also use show (): show (tNetTask, 1) Stack information Task options CPU registers FPU registers (if the VX_FP_TASK option bit is set) ti (taskNameOrId) 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Task Browser To obtain information about a specific task, click on the task?s summary line in the main target browser. 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 What is POSIX ? Originally, an IEEE mittee convened to create a standard interface to UNIX for: VxWors supports almost all of the POSIX Realtime Extensions. Increased portability. Convenience. Context switch times are very fast. Text, data, and bss are stored in a mon, global address space. The POSIX realtime extensions are based on implicit assumptions about the UNIX process model which do not always hold in VxWorks. In VxWorks, 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 What does VxWorks support ? Library Description aioPxLib Asynchornous I/O semPxLib POSIX Semaphores mqPxLib POSIX Message Queues mmanPxLib POSIX Memory Management schedPxLib POSIX Scheduler Interface sigLib POSIX Signals timerLib, clockLib POSIX Timer/Clock Interface dirLib File/Directory Information 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 RealTime Multitasking Introduction Task Basics Task Control Error Status System Tasks 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Error Status ? VxWorks里使用一個(gè)全局整型變量 errno來(lái)描述錯(cuò)誤信息 – 程序執(zhí)行過(guò)程中我們可以設(shè)置并調(diào)用一些函數(shù)例程來(lái)檢測(cè)錯(cuò)誤信息,并針對(duì)錯(cuò)誤信息設(shè)置相應(yīng)的錯(cuò)誤號(hào) – 然后調(diào)用一些函數(shù)例程檢測(cè)錯(cuò)誤號(hào),當(dāng)程序執(zhí)行異常時(shí)可以根據(jù)錯(cuò)誤號(hào)發(fā)現(xiàn)相應(yīng)的錯(cuò)誤 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Errno and Context Switches At each context switch, the kernel saves and restores the value of errno. TCB errorStatus errno 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Setting Errno Lowest level routine to detect an error sets errno and returns ERROR: STATUS myRoutine () { ... if (myNumFlurbishes = MAX_FLURBISH) { errno = s_myLib_TOO_MANY_FLURBISHES。 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 VxWorks怎樣滿(mǎn)足實(shí)時(shí)性需求 ? 多任務(wù)特性 ? 基于搶占式多任務(wù)調(diào)度 ? 快速的任務(wù)上下文切換 ? 快速確定的系統(tǒng)響應(yīng) ? 高效的任務(wù)間通訊機(jī)制 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 RealTime Multitasking Introduction Task Basics Task Control Error Status System Tasks 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Overview Low level routines to create and manipulate tasks are found in taskLib. Do not confuse executable code with the task(s) which execute it. A VxWorks task consists of: A stack (for local storage of automatic variables and parameters passed to routines). A TCB (for OS control) Code is downloaded before tasks are spawned. Several tasks can execute the same code (., printf()). 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Creating a Task taskSpawn Stack TCB PC foo () { ... } 嵌入式培訓(xùn)專(zhuān)題 微迪軟件培訓(xùn)中心 Creating a Task int taskSpawn (name, priority, options, stackSize, entryPt, arg1, ..., arg10) name Task name, if NULL gives a default name. priority Task priority,