中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
VxWorks下消息、信号量的使用和任务创建
作者:未知 时间:2005-07-27 23:24 出处:CSDN 责编:chinaitpower
              摘要:VxWorks下消息、信号量的使用和任务创建

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

//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


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有