VxWorks下消息、信号量的使用和任务创建

类别:编程语言 点击:0 评论:0 推荐:

/****************************************************************************************************/

//Test.c

#include <stdlib.h>
#include <stdio.h>

#include "Test.h"
#include <taskLib.h>
#include <msgQLib.h>
#include <semLib.h>


#define MAX_MESSAGE_COUNT  100
#define MAX_MESSAGE_LENGTH 400


int offset;
unsigned char SharedBuf[2*1024];


/*任务1   消息的发送*/
void loop1(MSG_Q_ID id)/*the function for task 1, like a thread under Windows or Unix*/
{
 unsigned char msg[MAX_MESSAGE_LENGTH] = {1};
 for( ; ;)
 {
  taskDelay(10);//快速发送消息,10ms,直至消息队列满
  if (msgQSend(id, msg, MAX_MESSAGE_LENGTH, WAIT_FOREVER, MSG_PRI_NORMAL) == OK)
  {
   printf("longping1!!   Send %d\n", msg[0]);
  }
  else
  {
   printf("Sending message ERROR!\n");
  }
 }

}
/*任务2   消息的接收*/
void loop2(MSG_Q_ID id)/*the function for test 2, like a thread under Wondows or Unix*/
{
 unsigned char msg[MAX_MESSAGE_LENGTH] = {0};
 for( ; ;)
 {
  taskDelay(30);//满速取消息,消息队列满了以后,收发达到平衡,发消息的10ms和30ms效果相同
  if (msgQReceive(id, msg, MAX_MESSAGE_LENGTH, WAIT_FOREVER) != ERROR)
  {
   printf("looping2!! Receive %d\n", msg[0]);
  }
  else
  {
   printf("Receiving message ERROR!\n");
  }
 }
 
}
/*任务3          获取信号量,信号量减1操作*/
void loop3(SEM_ID semID)
{
 for ( ; ;)
 {
  taskDelay(100);//获取信号量,速度快
  if (semTake(semID, WAIT_FOREVER) == OK)
  {
   offset = (++offset) % 2048;
   printf("looping3!!  Take SemID is: %d.Now I get one element: %d\n", (int)semID, SharedBuf[offset]);
  
  }
 }
}
/*任务4         释放信号量,信号量加1操作*/
void loop4(SEM_ID semID)
{
 for ( ; ;)
 {
  taskDelay(200);//释放信号量,速度慢,可以控制获取信号量,虽然获取延时短,但是没有释放信号量,只能等待
  if (semGive(semID) == OK)
  {
 
   printf("looping4!!  Gave SemID is: %d.Now I get one element: %d\n", (int)semID, SharedBuf[offset]);
  }
 }
}

void InitialSharedBuf()
{
 int i;
 offset = 0;
 for (i=0; i<2048; i++)
 {
  SharedBuf[i] = i;
 }
}

int main()
{

 MSG_Q_ID msgID;
 SEM_ID semID;
 signed int taskArray[100] = {0};
 char ch;
 int i;
 /*to build a message Queue.*/
 InitialSharedBuf();
 msgID = msgQCreate(MAX_MESSAGE_COUNT, MAX_MESSAGE_LENGTH, MSG_Q_FIFO);
 semID = semBCreate(SEM_Q_FIFO, SEM_FULL);
 
/*下面启动4个任务*/
 /*start a task(thread)*/
 taskArray[0] = taskSpawn("loop1 ", 80, 0, 64*1024, (FUNCPTR)loop1, (MSG_Q_ID)msgID, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 /*start another task(thread)*/
 taskArray[1] = taskSpawn("loop2 ", 80, 0, 64*1024, (FUNCPTR)loop2, (MSG_Q_ID)msgID, 0, 0, 0, 0, 0, 0, 0, 0, 0);

 /*start another task(thread)*/
 taskArray[2] = taskSpawn("loop3 ", 80, 0, 64*1024, (FUNCPTR)loop3, (SEM_ID)semID, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 
 /*start another task(thread)*/
 taskArray[3] = taskSpawn("loop4 ", 80, 0, 64*1024, (FUNCPTR)loop4, (SEM_ID)semID, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 
 /*主任务循环,当接收到'e'结束所有任务*/
 for ( ; ;)
 {
  ch = getchar();
  if (ch == 'e')
  {
   i = 0;
   while (taskArray[i])
   {
    taskDelete(taskArray[i]);
    printf("The task: %d is ended.\n", taskArray[i]);
    taskArray[i] = 0;
    i ++;
   }
   break;
  }
 }

 
 printf("The Main Function is Ended.\n");
 return 0;
}

/**************************************************************************************/

上面演示了消息、信号量和任务的创建,在Turnado下编译调试过,没有问题。

                                                  zhangggdlt
                                                 2005.04.28

本文地址:http://com.8s8s.com/it/it22043.htm