Chinaunix首页 | 论坛 | 博客
  • 博客访问: 363833
  • 博文数量: 53
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 1143
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-14 11:53
文章分类

全部博文(53)

文章存档

2011年(1)

2008年(52)

我的朋友

分类:

2008-09-22 19:56:44

/* includes */
#include "vxWorks.h"
#include "taskLib.h"
#include "sysLib.h"
int tid;
 
/* task function */
void myFunc(void)
{
    int i;
    printf("Hello, I am task %d\n", taskIdSelf()); /* Print task Id */
    taskSafe();
    for (i = 0; i < 10; i++)
    {
        printf("%d ", i);
        taskDelay(sysClkRateGet() / 2);
    }
    taskUnSafe();
}
 
/* another task function:delete my task */
void delMyTaskFunc(void)
{
    taskDelay(sysClkRateGet() *4);
    printf("ready to delete task\n");
    taskDelete(tid);
}
 
/* user entry */
void user_start()
{
    printf("ready to begin new tasks\n");
    tid = taskSpawn("myTask", 90, 0x100, 2000, (FUNCPTR) myFunc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    taskSpawn("delMyTask", 90, 0x100, 2000, (FUNCPTR)delMyTaskFunc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
 
taskDelete()函数:终止任务并释放任务占用的内存(堆栈和任务控制块空间),其原型为:
extern STATUS taskDelete (int tid);
 
运行输出:
Hello, I am task 14868640
0 1 2 3 4 5 6 7 ready to begin a new task
程序为运行输出8、9,这是因为在此之前,myTask已经被另一个任务――delMyTask删除。
任务可能被taskDelete()调用删除掉,但这一行为也不一定是安全的。如果我们删除一个获得了某些资源(如二进制信号量等)的任务,则对应的资源将不被释放,到站其它正在等待该资源的任务永远不能获得资源,系统会挡掉。我们可以用 taskSafe()和 taskUnsafe ()来保护这种区域,例如对myFunc作修改:
    taskSafe();
    for (i = 0; i < 10; i++)
    {
        printf("%d ", i);
        taskDelay(sysClkRateGet() / 2);
    }
    taskUnSafe();
运行输出:   
Hello, I am task 15700472
0 1 2 3 4 5 6 7 ready to delete task
8 9
 
 

 
阅读(6237) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~