VxWorks 任务间通信的简单例子
色郎中 2006-04-22 01:45:06 include "vxWorks.h"
#include "msgQLib.h"
#include "taskLib.h"
#include "timers.h"
#include "string.h"
#include "stdio.h"
void recvMSG_Task(void);
void sendMSG_Task(void);
void CreateTask(void);
MSG_Q_ID TestMSGQId;
void sendMSG_Task(void)
{
/* struct Tag_timespec
{
time_t tv_sec;
long tv_nsec;
}current_time;
*/
原来的 书中程序 注释掉了
struct timespec current_time; // 这个是我从新声明结构;
int maxMsgs;
int maxMsgLength;
int options;
long loop_num;
char MSGbuffer[64];
int timeout;
unsigned long CurrentTick;
maxMsgs=64;
maxMsgLength=1024;
options=MSG_Q_FIFO;
strcpy(MSGbuffer,"The Great China");
timeout=100;
TestMSGQId=msgQCreate(maxMsgs,maxMsgLength,options);
if(TestMSGQId==NULL)
{
printf("it is error for test msgQCreate!");
return;
}
for(loop_num=0;;loop_num++)
{
if(loop_num==0x1000000)
{
/*clock_gettime(CLOCK_REALTIME,(timespec *)&(current_time));*/ 这个是书中的原程序 有错误 编译不通过
clock_gettime(CLOCK_REALTIME,&(current_time)); 改了的
printf("current time is %d.\n",current_time.tv_nsec);
msgQSend(TestMSGQId,MSGbuffer,sizeof(MSGbuffer),timeout,MSG_PRI_NORMAL);
CurrentTick=tickGet();
printf("current tick is %d;current loop num is %d.\n",CurrentTick,loop_num);
clock_gettime(CLOCK_REALTIME,&(current_time)); // 这也是改了后的
printf("current time is %d.\n",current_time.tv_nsec);
loop_num=0;
}
}
printf("ERROR occurs is Task Sending MSG");
}
void recvMSG_Task(void)
{
int maxMsgs;
unsigned int maxBytes;
char MSGbuffer[64];
int timeout;
int revBytes;
maxMsgs=64;
maxBytes=1024;
timeout=100;
for(;;)
{
revBytes=msgQReceive(TestMSGQId,MSGbuffer,maxBytes,timeout);
if(0!=revBytes)
printf("recv msg is \n %s\n",MSGbuffer);
}
printf("ERROR occurs is Task Sending MSG");
}
void CreateTask(void)
{
int RetValue;
RetValue=taskSpawn("send MSG Task",100,VX_PRIVATE_ENV,512,(FUNCPTR)sendMSG_Task,0,0,0,0,0,0,0,0,0,0);
if (RetValue==ERROR)
printf("ERROR occurs is Task Ctreating");
RetValue=taskSpawn("recv MSG Task",100,VX_PRIVATE_ENV,512,(FUNCPTR)recvMSG_Task,0,0,0,0,0,0,0,0,0,0);
if (RetValue==ERROR)
printf("ERROR occurs is Task Ctreating");
}
这程序有错误,改动的地方 在程序中注释了
调试结果
正常 呵呵
tornado2.2 环境调试