⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fifoq0.c

📁 北美一所职业培训学院
💻 C
字号:
/* fifoQ0.c - demonstrate the Haas effect with WindView */

#include "vxWorks.h"
#include "msgQLib.h"
#include "taskLib.h"
#include "semLib.h"
#include "wvLib.h"
#include "stdio.h"

#define MSGS_MAX (5)		/* Queue dimensions */
#define MSG_SIZE_MAX (64)

#define PRI_RECV_LOW (150)	/* Task priorities */
#define PRI_RECV_MED (125)
#define PRI_SEND     (100)

#define OPTS	     (0)
#define STACK_SIZE   (20000)

MSG_Q_ID theQ = NULL;
SEM_ID   syncSem = NULL;

int tSend;			/* Task IDs */
int tRecvLow;
int tRecvMed;

char message [] = "My bonnie lies over the ocean.";

int countLow;
int countMed;

LOCAL BOOL started = FALSE;

LOCAL void send (void);		/* Task entry point functions */
LOCAL void recvLow (void);
LOCAL void recvMed (void);

STATUS fifoQStart (void)
    {
    if (started)
	{
	printf ("Already started.\n");
	return ERROR;
	}

    countLow = 0;
    countMed = 0;

    if (theQ == NULL)
	theQ = msgQCreate (MSGS_MAX, MSG_SIZE_MAX, MSG_Q_FIFO);

    if (syncSem == NULL)
	syncSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY);

    if (theQ == NULL || syncSem == NULL)
	{
	printf ("Couldn't start demonstration.\n");
	return ERROR;
	}

    if (   (tRecvLow = taskSpawn ("tRecvLow", PRI_RECV_LOW, OPTS, STACK_SIZE,
				  (FUNCPTR) recvLow, 0,0,0,0,0,0,0,0,0,0)) 
	    == ERROR
	|| (tRecvMed = taskSpawn ("tRecvMed", PRI_RECV_MED, OPTS, STACK_SIZE,
				  (FUNCPTR) recvMed, 0,0,0,0,0,0,0,0,0,0))
	    == ERROR
	|| (tSend =    taskSpawn ("tSend", PRI_SEND, OPTS, STACK_SIZE,
				  (FUNCPTR) send,    0,0,0,0,0,0,0,0,0,0))
	    == ERROR)
	{
	printf ("Couldn't spawn tasks.\n");
	return ERROR;
	}

    started = TRUE;
    return OK;
    }

STATUS fifoQStop (void)
    {
    char dummy [1];

    if (!started)
	{
	printf ("Not started.\n");
	return ERROR;
	}

    taskDelete (tSend);
    taskDelete (tRecvMed);
    taskDelete (tRecvLow);

    while (semTake (syncSem, NO_WAIT) == OK)
	;

    while (msgQReceive (theQ, dummy, 1, NO_WAIT) != ERROR)
	;

    started = FALSE;
    return OK;
    }

LOCAL void send (void)
    {
    FOREVER
	{
	taskDelay (1);

	msgQSend (theQ, message, sizeof (message), MSG_PRI_NORMAL, WAIT_FOREVER);

	semGive (syncSem);
	}
    }

LOCAL void recvLow (void)
    {
    char msg [MSG_SIZE_MAX];

    FOREVER
	{
	msgQReceive (theQ, msg, sizeof (msg), WAIT_FOREVER);

	++countLow;

	wvEvent (1, (char *) &countLow, sizeof (countLow));

	printf ("Message received: %s\n", msg);
	}
    }

LOCAL void recvMed (void)
    {
    char msg [MSG_SIZE_MAX];

    FOREVER
	{
	semTake (syncSem, WAIT_FOREVER);

	msgQReceive (theQ, msg, sizeof (msg), WAIT_FOREVER);

	++countMed;

	wvEvent (2, (char *) &countMed, sizeof (countMed));
	}
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -