📄 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 <number>
*
* This routine creates a mutual exclusion semaphore <mutex> guarding
* access to the global integer <number>.
*
* 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; i<NUM_STOOGES; i++)
{
newStooge = (STOOGE *)malloc(sizeof(STOOGE));
newStooge->next = 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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -