You are on page 1of 5

EVENT FLAG MANAGEMENT

Aim
To understand the concept of event flag management in C/OS-II using various event
flag management kernel primitives.
Requirements
C/OS-II Real-Time Kernel
Personal Computer
Theory
Event flags are used when a task needs to synchronize with the occurrence of multiple
events. The task can be synchronized when any of the events have occurred, which is called
as disjunctive synchronization (logical OR). A task can be synchronized when all events have
occurred, which is called as conjunctive synchronization (logical AND).
Common events can be used to signal multiple tasks. Events are typically grouped.
Depending on the kernel, a group consists of 8, 16 or 32 events, each represented by a bit.
Tasks and ISRs can set or clear any event in a group. A task is resumed when all the events it
requires are satisfied. C/OS-II offers services to SET event flags, CLEAR event flags and
WAIT for event flags.
C/OS-II event flags consist of two elements: a series of bits (8, 16 or 32 bits) used to
hold the current state of the events in the group and a list of tasks waiting for a combination
of these bits to be either set (1) or cleared (0). C/OS-II provides six services to access event
flags: OSFlagCreate(), OSFlagDel(), OSFlagPend(), OSFlagPost(), OSFlagAccept() and
OSFlagQuery().
OSFlagCreate() is used to create and initialize an event flag group. Event flag groups
must be created by this function before they can be used by the other services.
OSFlagPend() is used to have a task wait for a combination of conditions (ie. events
or bits) to be set (or cleared) in an event flag group. The task can wait for any condition to be
set or cleared or for all conditions to be set or cleared.
If the events that the calling task desires are not available, then the calling task is
blocked until the desired conditions are satisfied or the specified timeout expires.
The event flag bits can be set or cleared by calling OSFlagPost(). The bits set or
cleared are specified in a bit mask. OSFlagPost() makes each task, that has its desired bits
satisfied by this call, ready. The bits that are already set or cleared can be set or cleared.
Pseudocode
Constant and Variable declarations
#define EQUAL 0x01
#define LESSER 0x02
#define GREATER 0x04
INT16U a,b;
OS_FLAG_GRP *EvStatus;
Function prototypes
void Task1(void *data); /* Function prototypes of tasks */
void Task2(void *data);
void Task3(void *data);
void Task4(void *data);
void TaskStart(void *data); /* Function prototypes of Startup task */
static voidTaskStartCreateTasks(void);
static voidTaskStartDispInit(void);
static voidTaskStartDisp(void);
Main Function
void main (void)
{
PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); /* Clear the
screen */
OSInit(); /* Initialize uC/OS-II */
PC_DOSSaveReturn(); /* Save environment to return to DOS */
PC_VectSet(uCOS, OSCtxSw); /* Install uC/OS-II's context switch vector */
EvStatus=OSFlagCreate(0x00,&err); /* Creation of Event Flag */
OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 0);
OSStart(); /* Start multitasking */
}
Creating Tasks
static voidTaskStartCreateTasks (void)
{
OSTaskCreate(Task1, (void *)0, &TaskStk[1][TASK_STK_SIZE - 1], 4);
OSTaskCreate(Task2, (void *)0, &TaskStk[2][TASK_STK_SIZE - 1], 5);
OSTaskCreate(Task3, (void *)0, &TaskStk[3][TASK_STK_SIZE - 1], 6);
OSTaskCreate(Task4, (void *)0, &TaskStk[4][TASK_STK_SIZE - 1], 7);
}
Definition of Tasks
void Task1 (void *pdata)
{
\* Getting two numbers from the user and comparing them *\
\* If the two numbers are equal, first bit of the event flag is set*\
\* If the first number is lesser than the second number, second bit of the event flag is set*\
\* If the first number is greater than the second number, fourth bit of the event flag is set*\
}
void Task2 (void *pdata)
{
\* First Bit of Event Flag is checked and if it is set, display a is equal to b *\
}

void Task3 (void *pdata)
{
\* Second Bit of Event Flag is checked and if it is set, display a is lesser than b *\
}
void Task4 (void *pdata)
{
\* Fourth Bit of Event Flag is checked and if it is set, display a is greater than b *\
}
Output
The output windows for the implementation of various event flag management
functions are shown below:













Result
Thus the concept of event flag management in C/OS-II using various event flag
management kernel primitives is understood and the same has been demonstrated.

You might also like