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

📄 cobble.c

📁 VxWorks开发实践,包括: 《Tornado初步.doc》、《WindView简介及其用法0.doc》、《工具演示l.doc》
💻 C
字号:
/* cobble.c - sample program for tutorial in Tornado Getting Started Guide *//* This code is intentionally left buggy! *//*modification history--------------------01a,01apr99,ems  written*//*DESCRIPTION:This module is used as a simple example multitask applicationin the Tornado Getting Started Guide. It is intended to helpthe new Tornado user quickly become familiar with variousTornado tools that are available for debugging suchapplications.The program simulates a data collection system in whichdata is retrieved from an external source (e.g. a devicewhich generates interrupts as data come in). A task simulatingthe ISR gives a semaphore each time a new datum arrives. (Thistask could easily be replaced by a VxWorks watchdog timer.)The data are processed in two stages. First, a sample consistingof NUM_SAMPLE data is collected. Second, various arcanearithmetical operations are performed on the sample to obtaina result value. Each stage is represented by a task; thefirst task gives a binary semaphore when the second task shouldbegin the second stage work.The value of the result of the processing is monitored by yetanother task, which prints a warning messages whenever the resultis out of the safety range.INCLUDE FILES: None.*//* includes */#include "vxWorks.h"#include "stdio.h"#include "stdlib.h"#include "semLib.h"#include "taskLib.h"/* defines */#define NUM_SAMPLE 	10#define LUCKY 		7#define HOT 		20#define DELAY_TICKS     4#define STACK_SIZE	20000/* run states, provides for shutdown in stages; ensures that no    routine tries to take a semaphore that no longer exists */#define ALL_GO 		0	#define COSMOS_STOP 	1#define SCHLEP_STOP 	2#define CRUNCH_STOP	3#define ALL_STOP	4/* typedefs */typedef struct byLightning     {    int 		  data;    int 		  nodeNum;    struct byLightning * pPrevNode;    } LIST_NODE;/* globals */int tidCosmos;			/* Task IDs */int tidSchlep;int tidCrunch;int tidMonitor;int cosmicData = 0;		/* Holds a datum available to read */int result = 0;                 /* Holds result of calculation */volatile UINT8 runState = ALL_STOP;	/* controls shutdown */	LIST_NODE * pCurrNode = NULL;	/* head of list of data */SEM_ID dataSemId;		/* Given when a datum is available          */SEM_ID syncSemId;		/* Given when a data sample can be crunched */SEM_ID currNodeSemId;           /* Given when pointer to current node can 				   be accessed                              *//* forward declarations */void cosmos (void);void nodeAdd (int data, int nodeNum);void schlep (void);void nodeScrap (void);void crunch (void);void monitor (void);void progStop (void);/*************************************************************************** progStart - start the sample program.** Create various semaphores and spawn various tasks, while doing* incredibly little error checking.** RETURNS: OK*/STATUS progStart (void)    {    syncSemId = semBCreate (SEM_Q_FIFO, SEM_EMPTY);    dataSemId = semBCreate (SEM_Q_FIFO, SEM_EMPTY);    currNodeSemId = semMCreate (  SEM_Q_PRIORITY				| SEM_INVERSION_SAFE				| SEM_DELETE_SAFE);        pCurrNode = NULL;	/* just in case */    /* get started */    runState = ALL_GO;    tidCosmos = taskSpawn ("tCosmos", 200, 0, STACK_SIZE,        (FUNCPTR) cosmos,0,0,0,0,0,0,0,0,0,0);    tidSchlep = taskSpawn ("tSchlep", 220, 0, STACK_SIZE,        (FUNCPTR) schlep,0,0,0,0,0,0,0,0,0,0);    /*     * priority mis-assignment provides desired educational     * malfunction of never allowing tCrunch to run because      * tMonitor is higher priority     */    tidCrunch = taskSpawn ("tCrunch", 240, 0, STACK_SIZE,        (FUNCPTR) crunch,0,0,0,0,0,0,0,0,0,0);    tidMonitor = taskSpawn ("tMonitor", 230, 0, STACK_SIZE,        (FUNCPTR) monitor,0,0,0,0,0,0,0,0,0,0);    return (OK);    }/*************************************************************************** cosmos - simulate data arrival interrupt** This routine is executed by a task which serves as a replacement for* an ISR which signals the availability of data from some device.* It periodically gives the semaphore dataSemId to indicate that a* datum is available and may be read from the cosmicData variable.*/void cosmos (void)    {    int nadaNichtsIdx = 0;    while (runState == ALL_GO)	{        if (nadaNichtsIdx != LUCKY)            cosmicData = rand ();        else	    {            cosmicData = 0; /* because you can't wait forever for nothing */            nadaNichtsIdx = 0; 	    }        ++nadaNichtsIdx;               /* semaphore and delay ensure only new data will be read */        semGive (dataSemId);        taskDelay (DELAY_TICKS);	}    runState = SCHLEP_STOP;    semGive (dataSemId);    }/*************************************************************************** nodeAdd - link a new node to front of data chain.** Allocates and initializes a node, puts it onto the data chain. * * RETURNS: N/A*/void nodeAdd    (    int data,    int nodeNum    )    {    LIST_NODE * node;    if ( (node = (LIST_NODE *) malloc (sizeof (LIST_NODE))) != NULL)	{	node->data = data;	node->nodeNum = nodeNum;	semTake (currNodeSemId, WAIT_FOREVER); 	node->pPrevNode = pCurrNode;	pCurrNode = node;	semGive (currNodeSemId);	}    else	{	printf ("cobble: Out of Memory.\n");	taskSuspend (0);	}    }/*************************************************************************** schlep - collect data into a sample to be processed** Repeatedly, wait for and place NUM_SAMPLE data onto the list,* then awaken the cruncher.*/void schlep (void)    {    int nodeIdx;      FOREVER	{	for (nodeIdx = 0; nodeIdx < NUM_SAMPLE; nodeIdx++)            {	    semTake (dataSemId, WAIT_FOREVER);	/* Wait for datum */	    if (runState == SCHLEP_STOP)		{		runState = CRUNCH_STOP;		semGive (syncSemId);		return;		}	    nodeAdd (cosmicData, nodeIdx);            }	semGive (syncSemId); 		/* Wake up the cruncher! */	}         }/*************************************************************************** nodeScrap - relegate a node to the dust bin of history**/void nodeScrap (void)    {    LIST_NODE * pTmpNode;    pTmpNode = pCurrNode;    pCurrNode = pCurrNode->pPrevNode;     free (pTmpNode);             }/*************************************************************************** crunch - process data samples** Applies dubious transformations to data.* But first it waits for the schlepper to send it a sample.*/void crunch (void)    {    int	sampleSum =0;    int	div;    BOOL quit = FALSE;    while (!quit)	{	semTake (syncSemId, WAIT_FOREVER);	/* Wait for dinner */	if (runState == CRUNCH_STOP)	    quit = TRUE;	semTake (currNodeSemId, WAIT_FOREVER); /* reserve access to 	                                          pCurrNode */	while (pCurrNode != NULL)	    {	    sampleSum += pCurrNode->data;	    div = pCurrNode->data;	    nodeScrap ();	    }		semGive (currNodeSemId);   /* release access to pCurrNode */        result = sampleSum / div;  /* exception problem and the silly fix *//*	if (div != 0)            result = sampleSum/div;*/          sampleSum = 0;		   /* Clean up for the next round. */	}    runState = ALL_STOP;    }/*************************************************************************** monitor - monitors results of calculation** Checks results of calculation and prints warning if too hot.*/void monitor (void)    {    int isHot = 0;    int isNot = 0;    while (runState == ALL_GO)	{        if (!isHot && result >= HOT)            {            isHot = 1;  	    printf ("WARNING: HOT!\n");	    }	else if (isHot && result < HOT)	    {                 	    isHot = 0;	    printf ("OK\n");	    }	}    }/*************************************************************************** progStop - stops the program ** Call this routine to end it all.*/void progStop (void)    {    runState = COSMOS_STOP;                                  /* Wait for everyone to finish up */    while (runState != ALL_STOP)	taskDelay (1);                                  /* clean up */    semDelete (dataSemId);    semDelete (syncSemId);    semDelete (currNodeSemId);        printf ("BYE!TSCHUESS!ADIEU!\n");    }

⌨️ 快捷键说明

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