Date post: | 14-May-2018 |
Category: |
Documents |
Upload: | truongdang |
View: | 242 times |
Download: | 3 times |
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1
REAL TIME OPERATING SYSTEM REAL TIME OPERATING SYSTEM PROGRAMMINGPROGRAMMING--I: I: µµµµµµµµC/OSC/OS--II and II and
VxWorks VxWorks
LessonLesson--11: 11: VxWorks Signal and Semaphore
Functions
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2
1. 1. Signal FunctionSignal Function
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3
• sigNum− identifies a signal • void sigHandler (int sigNum); −to declares a signal servicing routine for a signal identified by sigNum• signal (sigNum, sigISR)−to registers a signal sigNum with a signal servicing routine sigISR• Refer Section 9.3.3.4
Signal (an Exception or Interrupt) handling Functions
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4
2. Semaphore Functions
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
5
• semBCreate ( ) Creates a binary semaphore. Example 9.21 Step 4• semMcreate ( ) Creates a mutex semaphore. Example 9.22 Step 2• semC-Create Creates a counting semaphore. Example 9.23 Step 5
Semaphore Creation FunctionsSemaphore Creation Functions
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
6
• semDelete ( ) −to deletes a semaphore• semFlush ( ) −to resume all waiting blocked tasks.
[Semaphore value for eah waiting task becomes 1]
Semaphore Deletion and Flush FunctionsSemaphore Deletion and Flush Functions
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
7
Semaphore Take and Give FunctionsSemaphore Take and Give Functions
semGive ( ) Releases a semaphore.[Example 9.21 Step 7, 9.22 Step 7 and 9.23 Step 12 ]
semTake ( ) Takes a semaphore. Example 9.22 Step 6
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
8
(1)SEM_Q_FIFO for specifying that take the semaphore in FIFO
mode (task blocked first get that first)(2) SEM_Q_PRIORITY for specifying that take the semaphore in
priority mode (higher priority blocked task get that first)
OPTIONS in Semaphore FunctionsOPTIONS in Semaphore Functions
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
9
• SEM_ID semBCreate (options, initialState)' −to create an ECB for binary semaphore
SEM_ID with options and initialStatevalue• Refer section 9.3.4.1 and example 9.21 Step 4
Semaphore Create for Event FlagSemaphore Create for Event Flag
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
10
SEM_ID semMKeyID; semMKeyID = semBCreate (SEM_Q_PRIORITY, SEM_FULL); /*creates a Mutex using a binary sem.*/• Refer section 9.3.4.5 and example 9.22 Step 2
Mutex Creation Statements
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
11
SEM_ID semMReadPortAKey; semMReadPortAKey = semMCreate (SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);/* To initial count = SEM_FULL, which means 1 and create a mutex semaphore with priority option for taking that, priority inversion safe, and deletion safe */
ExampleExample
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
12
• SEM_ID semCCreate (options, unsigned byte initialCount) −to create an ECB pointed by the SEM_ID.• SEM_ID semCID; •. SEM_ID = semCCreate(SEM_Q_PRIORITY, 0); /* To initial count = 0 and create a counting
semaphore with priority option for taking that*/
ExampleExample
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
13
• semPxLibInit ( );/* initializes the VxWorks library to permit use of these Functions */• sem_open ( );/* initialize a semaphore*/• sem_close ( ); /*close the semaphore*/• sem_unlink ( ); /*remove a semaphore*/
POSIX SemaphoresPOSIX Semaphores
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
14
• sem_post ( ); • sem_wait ( ); • sem_unlock;• sem_lock a semaphore;• sem_trywait ( ); /* to lock a semaphore if not locked*/ • sem_getvalue ( ) /* to retrieve the value of semaphore /*
POSIX Semaphore Functions POSIX Semaphore Functions
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
15
• sem_init ( ) and sem_destroy ( ) initialise and destroy an unnamed semaphore. Destroy means de-allocate associated
memory with its ECB. This effect is the same as first closing a semaphore and then unlinking it by sem_close and sem_unlink functions, respectively.
Init and DestroyInit and Destroy
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
16
VxWorks Semaphore Special FeaturesVxWorks Semaphore Special Features
(i) Options of protection from priority inversion and task deletion.
(ii) Single task may take a semaphore multiple times and recursively.
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
17
VxWorks Semaphore Special FeaturesVxWorks Semaphore Special Features
(iii) Mutually exclusive ownership can be defined.
(iv) Two options, FIFO and task priority for semaphores wait by multiple tasks.
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
18
SummarySummary
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
19
We learntWe learnt• signal-servicing routines • A signal-servicing routine is a C
function and that executes on signal (interrupt or exception) in a task.
• A connect function connects the signal number with the signal ISR
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
20
We learntWe learnt� two ways in which a pending task
among the pending tasks can unblock. One is as per task priority and another is as a FIFO when accepting (taking) an IPC
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
21
• three different semaphore functions for use as IPC for event flag, for resource key and counting semaphore.
• supports POSIX semaphores.
We learntWe learnt
2008Chapter-9 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
22
End of Lesson-11 on VxWorks Signal and Semaphore VxWorks Signal and Semaphore
functionsfunctions