Deleting task and creating again

hemangthakkar wrote on Sunday, February 19, 2012:

I am in a process of creating software watchdog using software timers. I need to monitor current status and if any task takes long time then I want to delete that task and recreate it. Can someone help me with this???

richard_damon wrote on Sunday, February 19, 2012:

I am not sure you really want to do that. There are a couple of issues with this.

1) Forcefully just terminating a task will not release any resources owned by it, especially any mutexs or dynamic memory.

2) If there is a problem that is causing a task to take to long, just reseting it isn’t apt to fix whatever real problem caused that to happen.

If you really want to do this, the simplest would probably just have each task as part of its loop write the current timer tick into a mail box (distinct for each task), and have a periodic task/timer hook scan the list and delete any task that has gotten too old.  You will have to watch out in this case for any task that isn’t activated often enough.

If you only want to look at elapsed time since they “started” an operation, then you could have too mailboxes. one for the start time, and a second for having completed. This assumes that once a task gets a “request” there shouldn’t be any other significant delays.  Here right after the task gets the request, it sets the start time and clears the completed flag. When it finishes, just before waiting for another operation, set the completed flag. Now the watchdog only wants to see if any non-completed task has gotten too old.

hemangthakkar wrote on Monday, February 20, 2012:

I have tried that mechanism which monitors counters for each task and timer routine. I want to delete that task and restart the same task. Can you help me with recreating same task

richard_damon wrote on Monday, February 20, 2012:

You need to remember how to create the thread. If you are just checking on or two, you can just replicate the code. If you want it to be a bit more general purpose, than you probably want all these flags to be in an array (either an array of structs, with each task being an array element and the struct having the data for each task, or just a set of arrays and a given task has a common index), and you save either the data needed to recreate the task (name, priority, stack size, task function address, parameter), or a address of a function to create the task.

hemangthakkar wrote on Tuesday, February 21, 2012:

The main function looks like mentioned below

int main(){

……
    /* Create timers and tasks*/
  
  
   
    xTimer_soft_WD = xTimerCreate((const signed char*)“soft_WD”,
    (SOFT_WD_TICKS),
    pdTRUE,
    (void *) 0,
    soft_WD);

  
  

    xTaskCreate( eth_task,
    (signed portCHAR *)“eth_task”,
    configMINIMAL_STACK_SIZE * 2.5,
    NULL,
    tskIDLE_PRIORITY + 1,
    &ethTaskHndl  );
   
    xTaskCreate( ser_task,
        (signed portCHAR *)“serial_task”,
        configMINIMAL_STACK_SIZE/5,
        NULL,
        tskIDLE_PRIORITY + 1,
        &serTaskHndl  );
   
     

    xTimerStart( xTimer_soft_WD,portMAX_DELAY );

  
    /* Start the scheduler. */
    vTaskStartScheduler();
……

Soft_WD timer routine looks like below which basically monitors task and if some task takes too long to finish execution then I am using the below steps to delete task and creating it again.  The task create function call is the same which I have used in main function. But, it doesnot work here and system hangs up.

vTaskSuspendAll();
vTaskDelete(ethTaskHndl);
    xTaskCreate( eth_task,
    (signed portCHAR *)“eth_task”,
    configMINIMAL_STACK_SIZE * 2.5,
    NULL,
    tskIDLE_PRIORITY + 1,
    &ethTaskHndl  );
    xTaskResumeAll();

richard_damon wrote on Wednesday, February 22, 2012:

First a side point, why does the serial task get configMINIMAL_STACK_SIZE/5 for its stack size, why does it need so much less stack than the idle task? This could be the source of your problem if it i is corrupting things.

Your code should recreate the eth task properly. Of course, what ever is making it take too long hasn’t been fixed, and if that problem has been caused  by some sort of corruption, who knows what will be happening.