Sei sulla pagina 1di 8

Tounderstandtheuseoftheroutine"timex()"

Tounderstandtheuseoftheroutine"taskSpawn()"formultitasking
Tounderstandtheconceptofbinarysemaphoresanditsapplication
Towriteanapplicationbasedontheconceptofsemaphores
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
SampleProgram
#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;
}
Reference
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
)
#include "vxWorks.h"
2]MultiTasking
Prototype
SampleProgram
#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
Semaphorespermitmultitaskingapplicationstocoordinatetheiractivities.Themostobviouswayfortasksto
communicateisviavariousshareddatastructures.BecausealltasksinVxWorksexistinasinglelinearaddress
space,shareddatastructuresbetweentasksistrivial.Globalvariables,linearbuffers,ringbuffers,linklists,and
Reference
3]Semaphores
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 */
)
STATUS semTake
(
SEM_ID semId, /* semaphore ID to take */
int timeout /* timeout in ticks */
)
Prototype
#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);
/* 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);
}
SampleProgram
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
Reference
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
4]Solve

Potrebbero piacerti anche