www.pudn.com > Tornado_Train_Workshop_demo_program.rar > rdbLab.c


/* rdbLab.c - Using CrossWind, nyuk, nyuk, nyuk! */ 
 
#include "vxWorks.h" 
#include "stdio.h" 
#include "stdlib.h" 
#include "taskLib.h" 
#include "semLib.h" 
 
typedef struct stoogeStruct  
    { 
    char *name; 
    struct stoogeStruct *next; 
    } STOOGE, *STOOGE_LIST; 
 
#define NUM_STOOGES 4  
 
/* global variables */ 
char *stoogeName[NUM_STOOGES] = {"Larry", "Moe", "Curly", "Shempf"}; 
volatile int number = 0; 
SEM_ID mutex = NULL;	/* mutual exclusion semaphore for number */ 
 
/* foward declarations */ 
STOOGE_LIST stoogeMakeList (void); 
void stoogeTalk (STOOGE_LIST stoogeList); 
void stoogeFreeList (STOOGE_LIST stoogeList); 
void stoogeNumberInc (void); 
void stoogeMutex (void); 
 
/******************************************************************************* 
* 
* stoogeStart - do the stooge thing. 
* 
* This routine creates a linked list of stooges. Each stooge in turn says 
* "Nyuk, nyuk, nyuk". The list is then freed up. 
* 
* RETURNS: VOID 
*/ 
 
void stoogeStart (void) 
    { 
    STOOGE_LIST stoogeList; 
 
    if (mutex == NULL)		/* first time through, create mutex */ 
	stoogeMutex (); 
 
    stoogeList = stoogeMakeList (); 
    printf("\n%5s (%2d) : Oh, a wise guy, eh?\n", taskName(0), number); 
    stoogeTalk (stoogeList); 
    stoogeFreeList (stoogeList); 
    }  
 
/************************************************************************* 
* 
* stoogeMutex - create mutex for  
* 
* This routine creates a mutual exclusion semaphore  guarding 
* access to the global integer .  
*  
* Semaphores will be covered in detail in a later chapter. 
*/ 
 
void stoogeMutex (void) 
    { 
    if ( (mutex = semMCreate (SEM_Q_PRIORITY |  
		 	      SEM_INVERSION_SAFE | 
			      SEM_DELETE_SAFE)) 
	 == NULL) 
	{ 
	printf ("stoogeInit: Creation of mutex failed. Exiting.\n"); 
	exit(0); 
	} 
    } 
 
 
/******************************************************************************* 
* 
* stoogeMakeList - make a linked list of stooges. 
* 
* RETURNS: a pointer to this list. 
*/ 
 
STOOGE_LIST stoogeMakeList (void) 
    { 
    STOOGE *oldStooge = NULL; 
    STOOGE *newStooge; 
    int i; 
 
    for (i=0; inext = oldStooge; 
		newStooge->name = stoogeName[NUM_STOOGES - i - 1]; 
 
		oldStooge = newStooge; 
		} 
 
    stoogeNumberInc (); 
    return (newStooge); 
    } 
     
 
 
/******************************************************************************* 
* 
* stoogeTalk - let each stooge say "Nyuk, nyuk, nyuk" 
* 
* RETURNS: VOID 
*/ 
 
void stoogeTalk 
    ( 
    STOOGE_LIST stoogeList 
    ) 
    { 
    STOOGE *stooge = NULL; 
 
    while (stooge != NULL) 
		{ 
		printf("%10s : Nyuk, nyuk, nyuk\n", stooge->name); 
		stooge = stooge->next; 
		} 
 
 
    stoogeNumberInc (); 
    } 
 
 
 
/******************************************************************************* 
* stoogeFreeList - free the linked list of stooges. 
* 
* RETURNS: VOID 
*/ 
 
void stoogeFreeList 
    ( 
    STOOGE_LIST stoogeList 
    ) 
    { 
    STOOGE *stooge, *nextStooge; 
    stooge = stoogeList; 
     
    while (stooge!=NULL) 
		{ 
		nextStooge = stooge->next; 
		free (stooge); 
		stooge = nextStooge; 
		} 
 
    stoogeNumberInc (); 
    } 
 
 
/******************************************************************************* 
* stoogeListSize - find out the size of the stoogeList. 
* 
* RETURNS: the size of the linked list. 
*/ 
 
int stoogeListSize 
    ( 
    STOOGE_LIST stoogeList 
    ) 
    { 
    int i=0; 
    STOOGE *stooge = stoogeList; 
 
    while(stooge!=NULL) 
		{ 
		i++; 
		stooge = stooge->next; 
		} 
 
    return(i); 
    } 
 
/************************************************************************** 
* 
* stoogeNumberInc - increment ye olde number, modulo ye olde 100 
* 
* The increment is done within a mutual exclusion critical region 
* to avoid a possible race condition when more than one task executes 
* this program concurrently. Mutual exclusion issues will be discussed 
* in a later chapter. 
*/ 
 
void stoogeNumberInc (void) 
    { 
    if (semTake (mutex, WAIT_FOREVER) != OK) 
	return; 
 
    if (++number == 100) 
	number = 0; 
 
    semGive (mutex); 
    }