semaphore.c

来自「嵌入式VxWorks开发所需典型例程源代码」· C语言 代码 · 共 89 行

C
89
字号
#include "vxWorks.h"
#include "stdio.h"
#include "stdlib.h"
#include "taskLib.h"
#include "semLib.h"



int		tidStart;
int		tidEnd;



void	helloStart();
void	helloEnd();
STATUS    helloStop();
STATUS    tHelloStart();
SEM_ID    semHello; 


STATUS tHelloStart() 
{

    /* Spawn start Task */


    semHello=semBCreate( SEM_Q_PRIORITY,SEM_FULL);
    if(semHello==NULL)
    {
	printf("Something is wrong when create semaphore\n");
	return ERROR;
    }


    tidStart = taskSpawn(NULL, 80, 0, 2000, (FUNCPTR)helloStart,
	      0,0,0,0,0,0,0,0,0,0);
    if(tidStart==NULL)
    {
	printf("Something is wrong when create taskStart\n");
	return ERROR;
    }
    
    /* Spawn end Task */
    tidEnd = taskSpawn(NULL, 80, 0, 2000, (FUNCPTR)helloEnd,
	      0,0,0,0,0,0,0,0,0,0);
    if(tidEnd==NULL)
    {
	printf("Something is wrong when create taskEnd\n");
	return ERROR;
    }

    
    return (OK);
}


void helloStart()
{
   for(;;)
   {
      semTake(semHello, WAIT_FOREVER);
      printf("Hello, I am Echo\n");
      semGive(semHello);
      taskDelay (sysClkRateGet ( ) / 2);
   }
}

void helloEnd()
{
   for(;;)
   {
      semTake(semHello, WAIT_FOREVER);
      printf("Hello, I am not Echo\n");
      semGive(semHello);
      taskDelay (sysClkRateGet ( ) / 2);
   }
  
}

STATUS helloStop() 
{

    /* delete tasks */
    taskDelete(tidStart);

    taskDelete (tidEnd);
    
return(OK);
}

⌨️ 快捷键说明

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