[uTenux] task management

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The task is an infinite loop. UTenux provides task management function is very powerful, including establishing and delete a task, start or quit the task, request to cancel a task start, changing task priority and the query task state, so the task into the sleep and wakeup, canceling wakeup request, forced release task wait state, the task state into a suspended state, delay the execution of the invoking task and no task wait state.

The task is a recognised by the ID object, each task has a base priority and the priority, which can be used to control task execution order

The core task of management is the scheduling. UTenux initial attribute and task running according to create task to task set is dynamic properties are obtained, for scheduling the task.

Create a task needs to provide the task structure of type T_CTSK. The definition of this structure are as follows:

typedef struct t_ctsk {
    VP                  exinf;                   /* Additional information, OS does not concern*/
    ATR                 tskatr;                  /* Task attribute*/
    FP                  task;                    /* Task service function entrance*/
    PRI                 itskpri;                 /* The initial priority*/
    W                   stksz;                   /* User stack size (byte) */
    UB                  dsname[8];               /* Object name */
    VP                  bufptr;                  /* User cache address*/

Task attribute is the number of binary unsigned a long type, it is the low system properties, high implementation dependent information. The task properties are as follows:



That task is written in assembly language


That task is written in a high-level language


That task is a region of memory user specified as the stack space


Specifies the DS object name


The specified tasks running in the level of protection n

Details of the task structure, can refer to the uTenux kernel code.

The task structure passed to the task start function tk_cre_tsk can create a task.

ID tskid=tk_cre_tsk(T_CTSK* pk_ctsk);

The return value is the mission of the ID, can be used as the task index. Create a task is assigned to a task control block TCB, and not the task immediately run or ready, this task is in a stationary state. In order to task into the ready state, also need to use the tk_sta_tsk to manually start the task:

ER  ercd= tk_sta_tsk(ID tskid,INT stacd);

Among them, tskid to create task given the task ID, stacd is the task startup code. Stacd is used for parameters passed to the task at startup, this parameter can be obtained from the start the task of query, use this feature for simple message passing.

With the above concept, you can start today's experiment.

This experiment provides reference the uTenux strength of sample 01.Task. Organic process is as follows:

1,For the initialization of the hardware in the application of main function in the entrance, after starting the operating system and create a task initctsk.

2,In the initctsk function call usermain (); function, and through the usermain function to call the TaskSample function. The main part in experiment.

3,In TaskSample, create three tasks first, task ID were: TaskID_A, TaskID_B, TaskID_C. Priority is respectively 24, 26, 28 after starting the TaskC.

4,In the service function of TaskC, the first outputs a message, and then start TaskA. More than TaskA of higher priority TaskC steals, immediately start running.

5,The service function of TaskA, to complete the output some information, after TaskA entered the dormant state. TasKC to obtain the highest priority to run.

6,TaskC to resume operation after the outputs a message, and then start TaskB. The same TaskB will start to steal TaskC operation.

7,After the TaskB performs some action, also will enter the sleep state. TaskC to continue. Enter a cycle.

8,The experimental output through the serial port. The output function, see

The main code experiment are as follows:

//The file TaskSample.c
#include "TaskSample.h"
#include <dev/ts_devdef.h>

void TaskSampleTaskA(W stacd,VP exinf);
void TaskSampleTaskB(W stacd,VP exinf);
void TaskSampleTaskC(W stacd,VP exinf);

static ID TaskID_A;
static ID TaskID_B;
static ID TaskID_C;

ER TaskSample( void)
    ER ercd=E_OK;
    T_CTSK ctsk;
  //Create task A
  ctsk.exinf = NULL;
  ctsk.tskatr = TA_HLNG | TA_RNG0 | TA_DSNAME;
  ctsk.task = (FP)&TaskSampleTaskA;
  ctsk.itskpri = 24;
  ctsk.stksz = 512;
  ctsk.bufptr = NULL;
  TaskID_A = tk_cre_tsk(&ctsk);
  if(TaskID_A < E_OK)
    ercd = TaskID_A;
    return ercd;
  //Create task B
  ctsk.exinf = NULL;
  ctsk.tskatr = TA_HLNG | TA_RNG0 | TA_DSNAME;
  ctsk.task = (FP)&TaskSampleTaskB;
  ctsk.itskpri = 26;
  ctsk.stksz = 512;
  ctsk.bufptr = NULL;
  TaskID_B = tk_cre_tsk(&ctsk);
  if(TaskID_B < E_OK)
    ercd = TaskID_B;
    return ercd;
  //Create task C
  ctsk.exinf = NULL;
  ctsk.tskatr = TA_HLNG | TA_RNG0 | TA_DSNAME;
  ctsk.task = (FP)&TaskSampleTaskC;
  ctsk.itskpri = 28;
  ctsk.stksz = 512;
  ctsk.bufptr = NULL;
  TaskID_C = tk_cre_tsk(&ctsk);
  if(TaskID_C < E_OK)
    ercd = TaskID_C;
    return ercd;
  tm_putstring((UB*)"Now start task C\n");
   return E_OK;

void TaskSampleTaskA(W stacd,VP exinf)
    tm_putstring((UB*)"This is in TaskA\n");
    tm_putstring((UB*)"Task_A will sleep\n");

void TaskSampleTaskB(W stacd,VP exinf)
   tm_putstring((UB*)"Task_B is running\n");
   tm_putstring((UB*)"Task_B will sleep\n");

void TaskSampleTaskC(W stacd,VP exinf)
  tm_putstring((UB*)"TaskC will start task A\n");  
  tm_putstring((UB*)"TaskC will start task B\n");  
  tm_putstring((UB*)"Input Cmd,'e' for exit\n");
    if('e' == tm_getchar(-1))
      tm_putstring((UB*)"task A will wup\n");
      tm_putstring((UB*)"task B will wup\n");
    tm_putstring((UB*)"Input Cmd,'e' for exit\n");
  tm_putstring((UB*)"Task A will stop\n");
  tm_putstring((UB*)"Task B will stop\n");
  tm_putstring((UB*)"Task C will stop\n");

The following output:


micro Tenux Version 1.6.00(build 0180)

Supported MCU is ST STM32F407VG

  Copyright(c) 2008-2013 by Dalian uLoong Co.,Ltd. 


Now start task C

TaskC will start task A

This is in TaskA

Task_A will sleep

TaskC will start task B

Task_B is running


Task_B will sleep

Input Cmd,'e' for exit


task A will wup

This is in TaskA

Task_A will sleep

task B will wup

Task_B is running


Task_B will sleep

Input Cmd,'e' for exit

Task A will stop

Task B will stop

Task C will stop

Push any key to shutdown the micro Tenux.


[Report of 1] uTenux task state

1,Operation: the task is running

2,The ready state: the task has completed preparations for running, but due to the higher priority task is running, so it cannot run

3,Wait state: because the operation conditions did not reach the task will not run. Three:

1,Wait state: waiting for resources

2,Pending: perform other tasks are forced to interrupt. Waiting for priority

3,Waiting for a pending state; tasks at the same time is in a wait state, also in a suspended state

4,The stationary state: task has not started or has completed execution status

5,There is no state: the task is not registered in the system

[Report of 2] uTenux task API


Start the kernel. This is something uT your add in, must be executed before any other system call. The need for a task done parameters. Start the kernel immediately when you create and start the task, control of the upcoming CPU to this task.


Exit the kernel


Start the task, the task from static state into the ready state. The need to provide TaskID and another parameter stacd. The parameter Stacd is not much use.


The task of dormancy. The parameter tmout refers to the period of OS clock number. If the period before the end of the call to tk_wup_tsk, then the task is to awaken. If in the tmout period did not receive tk_wup_tsk wake up, then the task of automatic wake-up. But will return an error code.


Wake up dormant task by tk_slp_tsk. The task in wait state is released


Call this function to suspend the execution of tasks. But now, the status of the task or wait state rather than a dormant state.. If you want to terminate the wait time, tk_rel_wai can be used to perform.


Other task termination. Forced to specify the tskid task becomes stationary state. Resources used by the task does not automatically release.


Delete the task. The task from the static state to state does not exist. The deleted task must be to the stationary state


Exit the call task. This function is called the task themselves out. Dutch act, a static state. However, this function will not release the task by using the resources, the need to manually release.

[Report of 3] on the example task exit

The uTenux sample will usually have two tasks in TaskA and TaskB. When I first started to do the experiment had asked zhangzl a question: when all the task enters wait state, the system is it right? Quit. When this problem, because I will be the two tasks are dormant, OS will return to the waiting exit status. Today again look at the code, find sample project OS exit is not because of the problem! The sample project execution process is like this:

1,In the main function, call the tk_sta_sys ((T_CTSK *) & initctsk; OS). The first task of the OS is initctsk

2,In initctsk call usermain () to handle other tasks, such as TaskA, TaskB. Inittask is a task, usermain is equivalent to a function.

3,Start calling TaskSample in usermain and other tasks. Because initctsk has the lowest priority, when all tasks in TaskSample are not in a wait state, initctsk will continue to execute. After the TaskSample code, only one line: tk_ext_sys (); is to exit the kernel

4,To this, the kernel will exit.

5,If the tk_ext_sys (); for while (1); then when TaskA, TaskB timeout woke up, will steal initctsk to continue.

The problem in the final analysis is the beginning, do not quite understand the task calling method: the priority is a high priority steals, returns will return to be continue steals.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Tammy at November 16, 2013 - 3:26 PM