Sei sulla pagina 1di 13

Tounderstandtheuseoftheroutine"timex()"

Tounderstandtheuseoftheroutine"taskSpawn()"formultitasking
Tounderstandtheconceptofbinarysemaphoresanditsapplication
Tounderstandcommunicationbetweentasksbyuseofmessagequeues
timex()routinedisplaystheexecutiontimeandamarginoferrorinmiliseconds
void timex
(
FUNCPTR func, /* function to time (optional) */
int arg1, /* first of up to 8 args to call function */
/* with (optional) */
int arg2,
int arg3,
int arg4,
int arg5,
int arg6,
int arg7,
int arg8
)
RTS:Lab0
Objective
1]Timex
Prototype
timex()routineisusedtoanalyzeandtimethetask"print_it()"
#include "vxWorks.h" /* Always include this as the first thing in every program */
#include "timexLib.h"
#include "stdio.h"
#include "taskLib.h"
#define ITERATIONS 100
int printit(void);
void timing() /* Function to perform the timing */
{
/* a pointer to the function "printit" */
FUNCPTR function_ptr = printit;
/* Timing the "print" function */
timex(function_ptr,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
}
int printit(void) /* Function being timed */
{
int i;
for(i=0; i < ITERATIONS; i++) /* Printing the task id number and the increment variabl
e "i" */
printf("Hello, I am task %d and is i = %d\n",taskIdSelf(),i);
return 0;
SampleProgram
}
http://www.vxdev.com/docs/vx55man/vxworks/ref/timexLib.html#timex
taskSpawn()routinecreatesandactivatesanewtaskwithaspecifiedpriorityandoptionsandreturnsasystem
assignedID.
int taskSpawn
(
char * name, /* name of new task (stored at pStackBase) */
int priority, /* priority of new task */
int options, /* task option word */
int stackSize, /* size (bytes) of stack needed plus name */
FUNCPTR entryPt, /* entry point of new task */
int arg1, /* 1st of 10 req'd task args to pass to func */
int arg2,
int arg3,
int arg4,
int arg5,
int arg6,
int arg7,
int arg8,
int arg9,
int arg10
)
Reference
2]MultiTasking
Prototype
multipleinstancesoftask"print_task()"arespawned
#include "vxWorks.h"
#include "timexLib.h"
#include "taskLib.h"
#include "stdio.h"
#define ITER 15
void print_task();
void main(){
int taskId,i=1;
for(;i<ITER;i++)
taskId = taskSpawn("tprint_task",i*50,0x100,200,
(FUNCPTR)print_task,i,0,0,0,0,0,0,0,0,0);
}
void print_task(void){
printf("\nTID : %d",taskIdSelf());
}
http://www.vxdev.com/docs/vx55man/vxworks/ref/taskLib.html#taskSpawn
SampleProgram
Reference
Semaphorespermitmultitaskingapplicationstocoordinatetheiractivities.Themostobviouswayfortasksto
communicateisviavariousshareddatastructures.BecausealltasksinVxWorksexistinasinglelinearaddress
space,shareddatastructuresbetweentasksistrivial.Globalvariables,linearbuffers,ringbuffers,linklists,and
pointerscanbereferenceddirectlybycoderunningindifferentcontext.
semBCreate()Thisroutineallocatesandinitializesabinarysemaphore.Thesemaphoreisinitializedtothe
initialStateofeitherSEM_FULLorSEM_EMPTY.
semGive()routineperformsthegiveoperationonaspecifiedsemaphore.Dependingonthetypeofsemaphore,the
stateofthesemaphoreandofthependingtasksmaybeaffected.Ifnotasksarependingonthesempahoreanda
taskhaspreviouslyregisteredtoreceiveeventsfromthesemaphore,theseeventsaresentinthecontextofthiscall.
Thismayresultintheunpendingofthetaskwaitingfortheevents.
semTake()routineperformsthetakeoperationonaspecifiedsemaphore.Dependingonthetypeofsemaphore,the
stateofthesemaphoreandthecallingtaskmaybeaffected.
SEM_ID semBCreate
(
int options, /* semaphore options */
SEM_B_STATE initialState /* initial semaphore state */
)
STATUS semGive
(
SEM_ID semId /* semaphore ID to give */
)
3]Semaphores
Prototype
STATUS semTake
(
SEM_ID semId, /* semaphore ID to take */
int timeout /* timeout in ticks */
)
TwotasksT1andT2editthevalueofasharedvariableinacoordinatedfashionsuchthatitsvalueismaintainedas
'0'
#include "vxWorks.h"
#include "taskLib.h"
#include "semLib.h"
#include "stdio.h"
void taskOne(void);
void taskTwo(void);
/* globals */
#define ITER 10
SEM_ID semBinary;
int global = 0;
void main(void){
int taskIdOne, taskIdTwo;
/* create semaphore with semaphore available and queue tasks on FIFO basis */
semBinary = semBCreate(SEM_Q_FIFO, SEM_FULL);
SampleProgram
/* Note 1: lock the semaphore for scheduling purposes */
semTake(semBinary,WAIT_FOREVER);
/* spawn the two tasks */
taskIdOne = taskSpawn("t1",90,0x100,2000,(FUNCPTR)taskOne,0,0,0,0,0,0,0,0,0,0);
taskIdTwo = taskSpawn("t2",90,0x100,2000,(FUNCPTR)taskTwo,0,0,0,0,0,0,0,0,0,0);
}
void taskOne(void)
{
int i;
for (i=0; i < ITER; i++){
semTake(semBinary,WAIT_FOREVER); /* wait indefinitely for semaphore */
printf("I am taskOne and global = %d......................\n", ++global);
semGive(semBinary); /* give up semaphore */
}
}
void taskTwo(void)
{
int i;
semGive(semBinary); /* Note 2: give up semaphore(a scheduling fix) */
for (i=0; i < ITER; i++){
semTake(semBinary,WAIT_FOREVER); /* wait indefinitely for semaphore */
printf("I am taskTwo and global = %d----------------------\n", --global);
semGive(semBinary); /* give up semaphore */
}
}
http://www.vxdev.com/docs/vx55man/vxworks/ref/semBLib.html#semBCreate
http://www.vxdev.com/docs/vx55man/vxworks/ref/semLib.html
WriteaprogramthatwillspawnthreetasksT1,T2,T3ofequalpriorities.T1runsaloopthatwillassignavaluetoa
sharedglobalvariable'var'(say)1to20.Foreachvalueassignedto'var'byT1,T2willprintvar*2andT3willprint
var/2.Useoftheconceptofsemaphorestodotheabove.Theoutputoftheprogramshouldlooklikebelow:
T1 : 1
T2 : 2
T3 : 0
T1 : 2
T2 : 4
T3 : 1
T1 : 3
T2 : 6
T3 : 3
theprimaryintertaskcommunicationmechanismwithinasingleCPUismessagequeues.Messagequeuesallowa
variablenumberofmessages,eachofvariablelength,tobequeued(FIFOorprioritybased).Anytaskcansenda
messagetoamessagequeueandanytaskcanreceiveamessagefromamessagequeue.Multipletaskscansend
Reference
4]Solve
5]MessageQueues
toandreceivefromthesamemessagequeue.Twowaycommunicationbetweentwotasksgenerallyrequirestwo
messagequeues,oneforeachdirection.
msgQCreate()routinecreatesamessagequeuecapableofholdinguptomaxMsgsmessages,eachupto
maxMsgLengthbyteslong.TheroutinereturnsamessagequeueIDusedtoidentifythecreatedmessagequeue.
msgQSend()routinesendsthemessageinbufferoflengthnBytestothemessagequeuemsgQId.Ifanytasksare
alreadywaitingtoreceivemessagesonthequeue,themessagewillimmediatelybedeliveredtothefirstwaiting
task.Ifnotaskiswaitingtoreceivemessages,themessageissavedinthemessagequeueandifataskhas
previouslyregisteredtoreceiveeventsfromthemessagequeue,theseeventsaresentinthecontextofthiscall.
msgQReceive()routinereceivesamessagefromthemessagequeuemsgQId.Thereceivedmessageiscopiedinto
thespecifiedbuffer,whichismaxNBytesinlength.IfthemessageislongerthanmaxNBytes,theremainderofthe
messageisdiscarded(noerrorindicationisreturned).Thetimeoutparameterspecifiesthenumberoftickstowaitfor
amessagetobesenttothequeue,ifnomessageisavailablewhenmsgQReceive()iscalled.Thetimeout
parametercanalsohavethefollowingspecialvalues:
NO_WAIT(0)
returnimmediately,whetheramessagehasbeenreceivedornot.
WAIT_FOREVER(1)
nevertimeout.
MSG_Q_ID msgQCreate
(
int maxMsgs, /* max messages that can be queued */
int maxMsgLength, /* max bytes in a message */
int options /* message queue options */
)
Prototype
STATUS msgQSend
(
MSG_Q_ID msgQId, /* message queue on which to send */
char * buffer, /* message to send */
UINT nBytes, /* length of message */
int timeout, /* ticks to wait */
int priority /* MSG_PRI_NORMAL or MSG_PRI_URGENT */
)
int msgQReceive
(
MSG_Q_ID msgQId, /* message queue from which to receive */
char * buffer, /* buffer to receive message */
UINT maxNBytes, /* length of buffer */
int timeout /* ticks to wait */
)
STATUS msgQDelete
(
MSG_Q_ID msgQId /* message queue to delete */
)
T1sendsamessagetoT2throughmessageQueue.T2waitsforit,printsitwhenitsavailable.
/* includes */
#include "vxWorks.h"
#include "msgQLib.h"
SampleProgram
/* function prototypes */
void taskOne(void);
void taskTwo(void);
/* defines */
#define MAX_MESSAGES 100
#define MAX_MESSAGE_LENGTH 50
/* globals */
MSG_Q_ID mesgQueueId;
void message(void) /* function to create the message queue and two tasks */
{
int taskIdOne, taskIdTwo;
/* create message queue */
if ((mesgQueueId = msgQCreate(MAX_MESSAGES,MAX_MESSAGE_LENGTH,MSG_Q_FIFO))
== NULL)
printf("msgQCreate in failed\n");
/* spawn the two tasks that will use the message queue */
if((taskIdOne = taskSpawn("t1",90,0x100,2000,(FUNCPTR)taskOne,0,0,0,0,0,0,0,
0,0,0)) == ERROR)
printf("taskSpawn taskOne failed\n");
if((taskIdTwo = taskSpawn("t2",90,0x100,2000,(FUNCPTR)taskTwo,0,0,0,0,0,0,0,
0,0,0)) == ERROR)
printf("taskSpawn taskTwo failed\n");
}
void taskOne(void) /* task that writes to the message queue */
{
char message[] = "Received message from taskOne";
/* send message */
if((msgQSend(mesgQueueId,message,MAX_MESSAGE_LENGTH, WAIT_FOREVER,
MSG_PRI_NORMAL)) == ERROR)
printf("msgQSend in taskOne failed\n");
}
void taskTwo(void) /* tasks that reads from the message queue */
{
char msgBuf[MAX_MESSAGE_LENGTH];
/* receive message */
if(msgQReceive(mesgQueueId,msgBuf,MAX_MESSAGE_LENGTH, WAIT_FOREVER)
== ERROR)
printf("msgQReceive in taskTwo failed\n");
else
printf("%s\n",msgBuf);
msgQDelete(mesgQueueId); /* delete message queue */
}
http://www.vxdev.com/docs/vx55man/vxworks/ref/msgQLib.html
Implement2waycommunicationbetween2tasksusingMessageQueues.
Reference
6]Solve:2waycommunication
Usemessagequeuesforestablishingcommunicationbetween2tasksT1andT2.T1generatesprimenumbers,T2
waitsforaprimeandwhenitsavailable,itprintsit.
T2 : 2
T2 : 3
T2 : 5
T2 : 7
7]Solve:PrintPrimes
SampleOutput

Potrebbero piacerti anche