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

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有