分享
 
 
 

VxWorks操作系统指南(2.6) 应用示例分析

王朝other·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

1.1.应用示例分析 下面通过对一具体实例的分析,对任务的创建、任务间通信、内存分配、消息管理等VxWorks系统应用更进一步的了解。(示例选自demo例子程序windDemo.c)

/* windDemo - repeatedly test various kernel function */

/*

modification history

--------------------

02c,23aug93,jcf fixed synchronization.

02b,01aug93,dvs fixed loop count printing.

02a,18mar93,dvs took out timer/benchmark information.

ansified code.

general cleanup of code to use as MicroWorks demo.

01a,12nov90,shl written.

*/

/*

DESCRIPTION

This program repeatedly exercises different kernel facilities of

the Wind kernel.

The functions involved include the use of semaphores as sychronization

and mutual exclusion primitives, the use of taskSuspend()/taskResume() for task control, the use of message queues for communication and the

use of watchdogs for task timeouts.

To exercise these kernel facilities two tasks are used, a high priority task and a low priority task. The high priority task executes functions with which the resources are not available. As the high priority task blocks, the low priority task takes over and makes available the resources that the high priority task is waiting for. This may sound simple at first but the underlying execution of this test program involves context switching, rescheduling of tasks, and shuffling of the ready queue, pend queue, and the timer queue.

These functions are chosen because they are the most commonly used

functions in sychronization, mutual exclusion, task control, inter-task communication and timer facilities. These are the basic building blocks of the operating system itself and also used in applications. Repeatedly execution of this "death loop" is a good indication of how the system will perform in real-life as these functions are utiltized heavily in every application.

The following is the thread of execution of this test program.

Higher Priority Lower Priority

Task1 Task2

=============== ==============

|

V

/----->semGive()

| semTake()

| |

| V

| semTake()

| | \

| \-------------> semGive()

| /

| /

| taskSuspend()<-------------/

| | | \-------------> taskResume()

| /

| /

| msgQSend()<-------------/

| msgQReceive()

| |

| V

| msgQReceive()

| | \

| \-------------> msgQSend()

| /

| /

| wdStart() <-------------/

| wdCancel()

\---------|

V

exit

\

\-------------> exit

*/

#include "vxWorks.h"

#include "semLib.h"

#include "taskLib.h"

#include "msgQLib.h"

#include "wdLib.h"

#include "logLib.h"

#include "tickLib.h"

#include "sysLib.h"

#include "stdio.h"

/* defines */

#if FALSE

#define STATUS_INFO /* define to allow printf() calls */

#endif

#define MAX_MSG 1 /* max number of messages in queue */

#define MSG_SIZE sizeof (MY_MSG) /* size of message */

#define DELAY 100 /* 100 ticks */

#define HIGH_PRI 150 /* priority of high priority task */

#define LOW_PRI 200 /* priority of low priority task */

#define TASK_HIGHPRI_TEXT "Hello from the 'high priority' task"

#define TASK_LOWPRI_TEXT "Hello from the 'low priority' task"

/* typedefs */

typedef struct my_msg

{

int childLoopCount; /* loop count in task sending msg */

char * buffer; /* message text */

} MY_MSG;

/* globals */

SEM_ID semId; /* semaphore ID */

MSG_Q_ID msgQId; /* message queue ID */

WDOG_ID wdId; /* watchdog ID */

int highPriId; /* task ID of high priority task */

int lowPriId; /* task ID of low priority task */

int windDemoId; /* task ID of windDemo task */

/* forward declarations */

LOCAL void taskHighPri (int iteration);

LOCAL void taskLowPri (int iteration);

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

*

*

* windDemo - parent task to spawn children

*

* This task calls taskHighPri() and taskLowPri() to do the

* actual operations of the test and suspends itself.

* Task is resumed by the low priority task.

*

*/

void windDemo

(

int iteration /* number of iterations of child code */

)

{

int loopCount = 0; /* number of times through windDemo */

#ifdef STATUS_INFO

printf ("Entering windDemo\n");

#endif /* STATUS_INFO */

if (iteration == 0) /* set default to 10,000 */

iteration = 10000;

/* create objects used by the child tasks */

msgQId = msgQCreate (MAX_MSG, MSG_SIZE, MSG_Q_FIFO);

semId = semBCreate (SEM_Q_PRIORITY, SEM_FULL);

wdId = wdCreate ();

windDemoId = taskIdSelf ();

FOREVER //while(1)

{

/* spawn child tasks to exercise kernel routines */

highPriId = taskSpawn ("tHighPri", HIGH_PRI, VX_SUPERVISOR_MODE, 1000,(FUNCPTR) taskHighPri, iteration,0,0,0,0,0,0,0,0,0);

lowPriId = taskSpawn ("tLowPri", LOW_PRI, VX_SUPERVISOR_MODE, 1000,(FUNCPTR) taskLowPri, iteration,0,0,0,0,0,0,0,0,0);

taskSuspend (0); /* to be waken up by taskLowPri */

#ifdef STATUS_INFO

printf ("\nParent windDemo has just completed loop number %d\n",

loopCount);

#endif /* STATUS_INFO */

loopCount++;

}

}

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

*

*

* taskHighPri - high priority task

*

* This tasks exercises various kernel functions. It will block if the

* resource is not available and relingish the CPU to the next ready task.

*

*/

LOCAL void taskHighPri

(

int iteration /* number of iterations through loop */

)

{

int ix; /* loop counter */

MY_MSG msg; /* message to send */

MY_MSG newMsg; /* message to receive */

for (ix = 0; ix < iteration; ix++)

{

/* take and give a semaphore - no context switch involved */

semGive (semId);

semTake (semId, 100); /* semTake with timeout */

/*

* take semaphore - context switch will occur since semaphore

* is unavailable

*/

semTake (semId, WAIT_FOREVER); /* semaphore not available */

taskSuspend (0); /* suspend itself */

/* build message and send it */

msg.childLoopCount = ix;

msg.buffer = TASK_HIGHPRI_TEXT;

msgQSend (msgQId, (char *) &msg, MSG_SIZE, 0, MSG_PRI_NORMAL);

/*

* read message that this task just sent and print it - no context

* switch will occur since there is a message already in the queue

*/

msgQReceive (msgQId, (char *) &newMsg, MSG_SIZE, NO_WAIT);

#ifdef STATUS_INFO

printf ("%s\n Number of iterations is %d\n",

newMsg.buffer, newMsg.childLoopCount);

#endif /* STATUS_INFO */

/*

* block on message queue waiting for message from low priority task

* context switch will occur since there is no message in the queue

* when message is received, print it

*/

msgQReceive (msgQId, (char *) &newMsg, MSG_SIZE, WAIT_FOREVER);

#ifdef STATUS_INFO

printf ("%s\n Number of iterations by this task is: %d\n",

newMsg.buffer, newMsg.childLoopCount);

#endif /* STATUS_INFO */

/* test watchdog timer */

wdStart (wdId, DELAY, (FUNCPTR) tickGet, 1);

wdCancel (wdId);

}

}

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

*

*

* taskLowPri - low priority task

*

* This task runs at a lower priority and is designed to make available

* the resouces that the high priority task is waiting for and *subsequently unblock the high priority task.

*

*/

LOCAL void taskLowPri

(

int iteration /* number of times through loop */

)

{

int ix; /* loop counter */

MY_MSG msg; /* message to send */

for (ix = 0; ix < iteration; ix++)

{

semGive (semId); /* unblock tHighPri */

taskResume (highPriId); /* unblock tHighPri */

/* build message and send it */

msg.childLoopCount = ix;

msg.buffer = TASK_LOWPRI_TEXT;

msgQSend (msgQId, (char *) &msg, MSG_SIZE, 0, MSG_PRI_NORMAL);

taskDelay (60);

}

taskResume (windDemoId); /* wake up the windDemo task */

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有