2008 Chapter-9 L6: "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--6:6:µµµµµµµµC/OS-II Semaphore Functions
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2
1. Semaphore Functions 1. Semaphore Functions
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3
� Provides for using same semaphore functions as an event signaling flag or mutex or counting semaphore.
µC/OS-II Semaphore functions
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4
� When a semaphore created is used as a an event signaling flag or as counting semaphore, semaphore value at start = 0, which means event yet to occur and 1 will mean event occurred.
Semaphore for event flag functionsSemaphore for event flag functions
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
5
Semaphore as mutex functions Semaphore as mutex functions � When a semaphore created is used as a
resource acquiring key, Semaphore value at start = 1, which means resource available and 0 will mean not available.
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
6
• OS_Event OSSemCreate (unsigned short semVal)−To create and initialize semaphores
Refer examples 9.16 to 9.18 Steps7, 2 and 6 for event-flag, key and count, respectively.
OSSemCreate (semVal)
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
7
• void OSSemPend (OS_Event *eventPointer, unsigned short timeOut, unsigned byte *SemErrPointer)To check whether semaphore is pending or not pending (0 or >0). If pending (=0), then suspend the task till >0 (released). If >0, decrement the value of semaphore and run the waiting codes (Example 9.16 Step 31)
OSSemPendOSSemPend (*(*eventPointereventPointer, , timeOuttimeOut, , **SemErrPointerSemErrPointer))
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
8
-
OSSemAcceptOSSemAccept (*(*eventPointereventPointer))
• unsigned short OSSemAccept(OS_EVENT *eventPointer)
− To check whether semaphore value > 0 and if yes, then retrieve and decrement. Used when there is no need to suspend a task, only decrease it to 0 if value is not already zero
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
9
• unsigned byte OSSemPost(OS_EVENT*eventPointer)− SemVal if 0 or more, increment. Increment makes the semaphore again not pending for the waiting tasks. (Examples 9.16, 9.17 and 9.18 Steps 19, 10, 16, respectively)
OSSemPostOSSemPost (*(*eventPointereventPointer))
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
10
unsigned byte OSSemQuery (OS_EVENT *eventPointer, OS_SEM_DATA *SemData) −To get semaphore information
OSSemOSSemQuery (*(*eventPointereventPointer))
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
11
2. Macros for semaphore functions to find 2. Macros for semaphore functions to find status after execution of OS status after execution of OS semaphore semaphore
FunctionsFunctions
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
12
• OS_NO_ERR, if semaphore signaling succeeded. [SemVal > 0
or 0.] or when when querying succeeds; • OS_ERR_EVENT_TYPE, if *eventPointer is not pointing to the semaphore. • OS_SEM_OVF, when semVal overflows (cannot increment and is
already 65535.)
Macros for semaphore functionsMacros for semaphore functions
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
13
3. Example of semaphore function 3. Example of semaphore function Applications as event signaling flag Applications as event signaling flag
2008
Chapter-9 L6: "Embedded Systems -Architecture, Programming and
Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
14
•• Programming Example of two Programming Example of two application tasks in a chocolate vending application tasks in a chocolate vending machine. One task is to Read and get machine. One task is to Read and get coins (coins (ReadTaskReadTask) and other to deliver the ) and other to deliver the chocolate (chocolate (DeliveryTask)
Example of semaphore function Example of semaphore function ApplicationsApplications
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
15
#define OS_MAX_EVENTS 8 /*When total number of IPCs needed in an application = 8*/ #define OS_SEM_EN 1 /*When the use of semaphores is contemplated */
Step Step ii: Initiating the Semaphores: Initiating the Semaphores
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
16
OS_EVENT *SemFlagChocolate;/* When Sem is to be used as flag for the delivery of chocolate, initial value = 0 (means not released) */SemFlagChocolate = OSSemCreate (0);
Step j: Global IPC functions and their Step j: Global IPC functions and their parameters declarationsparameters declarations
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
17
void main (void) {OSInit ();/* Create First task */OSTaskCreate (FirstTask, void (*) 0,(void *)&FirstTaskStack[ FirstTaskStackSize], FirstTaskPriority);OSStart ( );}
Step k: Main functionStep k: Main function
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
18
static void FirstTask (void *taskPointer) {/*System clock time set *//* Create apllication related highest prio task */OSTaskCreate (ReadTask, void (*) 0,(void *)&ReadTaskStack [ReadTaskStackSize], ReadTaskPriority);
Step l: First taskStep l: First task
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
19
OSTaskCreate (DeliveryTask, void (*) 0,(void *)&DeliveryTaskStack[DeliveryTaskStackSize], DeliveryTaskPriority);OSTimeSet (presetTime);OSTickInit (); /* Initiate system timer ticking*/
Step l: First taskStep l: First task
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
20
static void FirstTask (void *taskPointer) {...while (1) {OSTaskSuspend (FirstTaskPriority);}
Step l: First task suspend indefinitely after Step l: First task suspend indefinitely after creating application tasks and initiating creating application tasks and initiating
system timer ticks… system timer ticks…
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
21
static void ReadTask (void *taskPointer) {..while (1) {...; ...; ...;.OSSemPost (SemFlagChocolate);/* after this instruction executes SemFlagChocolate = 1 (released state)*/
Step m: Step m: ReadTaskReadTask releasing semaphore on releasing semaphore on finding appropriate coinsfinding appropriate coins
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
22
OSTimeDelay (3000)/* Write delay function 3000 ticks to let the control transfer to deliver task (next priority task)*/....; }}
Step m: Step m: ReadTaskReadTask delaying to enable delaying to enable lower priority lower priority DeliveryTaskDeliveryTask to runto run
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
23
static void DeliveryTask (void *taskPointer) {.while (1) {OSSemTake (SemFlagChocolate);/* after this instruction executes SemFlagChocolate = 0 (taken state)*/...; ...; ...; /*Deliver Chocolate*/
Step n: Chocolate delivery task Step n: Chocolate delivery task waiting to take the required semaphorewaiting to take the required semaphore
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
24
...; ...; ...; OSTimeDlyResume (ReadTaskPriority);/ * Resume ReadTask for next read*/}; }
Step n: Chocolate delivery task Step n: Chocolate delivery task resuming delayed resuming delayed TaskReadTaskRead ……
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
25
4. Example of semaphore function 4. Example of semaphore function Applications as mutex Applications as mutex
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
26
Programming ExampleProgramming Example
� Mutex m used to let a running task critical section 1 using a resource (for example, print buffer) not used by another task critical section 2 waiting to take same m
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
27
#define OS_MAX_EVENTS 8 /*When total number of IPCs needed in an application = 8*/ #define OS_SEM_EN 1/*When the use of semaphores is
contemplated */
Step A: Creating a Mutex SemaphoreStep A: Creating a Mutex Semaphore
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
28
OS_EVENT *Sem_mCoin;/* Declare a pointer to data structure for semaphore and other IPC events */
Step B: Global IPC functions and their Step B: Global IPC functions and their parameters declarationsparameters declarations
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
29
SemFlagChocolate = OSSemCreate (0);/* When Sem is to be used as a event
signaling flag, initial value = 0 (means not yet released) */Sem _mCoin = OSSemCreate (1);/* When Sem is to be used as a mutex initial value = 1 (means is in released state) */
Step B: Global IPC functions and their Step B: Global IPC functions and their parameters declarations…parameters declarations…
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
30
void main (void) {OSInit ();/* Create First task */OSTaskCreate (FirstTask, void (*) 0,(void *)&FirstTaskStack[ FirstTaskStackSize], FirstTaskPriority);OSStart ( );}
Step C: Main functionStep C: Main function
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
31
static void FirstTask (void *taskPointer) {/* Create apllication related highest prio task */OSTaskCreate (ReadTask, void (*) 0,(void *)&ReadTaskStack [ReadTaskStackSize], ReadTaskPriority);
Step D: First taskStep D: First task
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
32
OSTaskCreate (DeliveryTask, void (*) 0,(void *)&DeliveryTaskStack[DeliveryTaskStackSize], DeliveryTaskPriority); /*System clock time set */OSTimeSet (presetTime);OSTickInit (); /* Initiate system timer ticking*/
Step D: First task…Step D: First task…
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
33
static void FirstTask (void *taskPointer) {...while (1) {OSTaskSuspend (FirstTaskPriority);}
Step D: First task…Step D: First task…
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
34
static void ReadTask (void *taskPointer) {...while (1) {...OSSemtake (Sem_mCoin); /* after this instruction executes the amount can be incremented by ReadTask as Sem_mCoin = 0 (not in released state)*/.....
Step E: Read task taking the mutexStep E: Read task taking the mutex--semaphore before critical section startssemaphore before critical section starts
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
35
/* Critical section/* Critical section run for collecting the coins for collecting the coins and releasing the semaphore for permitting a and releasing the semaphore for permitting a reset later on the coinreset later on the coin--amount after delivery of amount after delivery of chocolate by other task critical section*/chocolate by other task critical section*/• OSSemPost (Sem_mCoin); /* after this instruction executes the next task section can reset the amount */• OSSemPost (SemFlagChocolate); •....... } }
Step E: Read task posting the mutexStep E: Read task posting the mutex--semaphore…semaphore…
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
36
static void DeliveryTask (void *taskPointer) {..while (1) {OSSemTake (SemFlagChocolate);...; ...; ...; OSSemTake (Sem_mCoin);/* after these instruction executes Sem_mCoin
and SemFlagChocolate = 0 (taken state) Critical Critical section 2 starts section 2 starts */
Step F: Delivery task taking the required Step F: Delivery task taking the required semaphoressemaphores
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
37
.../*Reset amount by setting amount = 0 after the chocolate delivery*/OSSemPost(Sem_mCoin); /* Release mutex to let read task section increment the amounts */...;/* code for resuming Delayed ReadTask*/ }}
Step F: Delivery task taking the required Step F: Delivery task taking the required semaphoressemaphores
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
38
SummarySummary
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
39
We learntWe learnt• Use of the OS Functions • Initial setting of system-time, starting
system ticks, • Initiating OS Event semaphore IPC,
initialing semaphore, taking and releasing a semaphore when using it as event flag.
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
40
We learntWe learnt• Initiating OS Event semaphore IPC,
initializing semaphore, taking and releasing a semaphore, when using it for a mutex fro exclusive access to run one of the two task sections .
2008 Chapter-9 L6: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
41
We learntWe learnt• A simplicity feature of µC/OS-II is that
same semaphore functions are used for binary semaphore, for event signaling flag, resource key (mutex) and counting.