/* source.cpp */ /* includes */ #include "vxWorks.h" #include "stdio.h" #include "stdlib.h" #include "semLib.h" #include "taskLib.h" #include "assert.h" #include "sched.h" #include "kernelLib.h" #define STACK_SIZE 20000 #define NUM_PHILOSOPHERS 5 int tidGetForks; /* Task IDs */ int tidProgStop; SEM_ID forks[NUM_PHILOSOPHERS]; #define START 1 #define STOP 2 volatile UINT8 runState; /* forward declarations */ void GetForks (int); void progStop(void); STATUS progStart (void); /************************************************************************* * * progStart - start the sample program. * * Create various semaphores and spawn various tasks, while doing * incredibly little error checking. * * RETURNS: OK */ STATUS progStart (void) { runState = START; kernelTimeSlice(100); // create semaphores // spawn necessary tasks tidGetForks = taskSpawn ("GetForks", 1, 0, STACK_SIZE, (FUNCPTR) GetForks,0,0,0,0,0,0,0,0,0,0); return (OK); } // implement GetForks function(s) void GetForks(int i) { while (runState != STOP) { // causes a short delay before trying to eat again taskDelay(rand() % 100); } } /************************************************************************* * * progStop - stops the program * * Call this routine to end it all. */ void progStop (void) { runState = STOP; // make sure to delete semaphores }