1
votes

I am an RTOS newbie and I am creating a simple real time system for automotive

I am wondering if it possible to create a task inside another task. I tried to do this by the following method but it doesn't work.

 void vTask1 { *pvParameters){
unsigned portBASE_TYPE taskPriority;
taskPriority=uxTaskPriorityGet( NULL );
char x;
while (1){
 x= 5 ;
if (x==5)
xTaskCreate( vTask2 , "task2", 1000, "task2 is running", taskPriority+5 , NULL );
}

when I debug that code it hangs at xTaskCreate without executing the new task and I searched the manual and the internet for something about this but I didn't find any.

would anyone tell me is that possible to do in RTOS or I am doing it in a wrong way?

2
Try creating the task outside of the while loop. xTaskCreate(...); while(1); If you get to the while(1), then your scheduler isn't switching tasks or you haven't created the task properly.Ross
What is the response code you're getting from the xTaskCreate() call? If it's not pdPASS the task is not being created.Ross
@Ross XTaskCreate() is not giving me pdPASS unfortunately , what are the possible reasons in your opinion?Jay Shenawy
I want to note that I am using xTaskCreate in a more complicated code. The code I wrote upwards was just a simple example for what I am doingJay Shenawy

2 Answers

2
votes

Tasks can be created before the scheduler has been started (from main), or after the scheduler has been started (from another task). The xTaskCreate() API documentation is here: http://www.freertos.org/a00125.html . You will also find a set of demo tasks that demonstrate creating and deleting tasks from another task in the main FreeRTOS .zip file download. Look in the FreeRTOS/Demo/Common/Minimal/death.c file (death for suicidal tasks as they delete themselves after creation).

If xTaskCreate() returns NULL then you will probably have run out of heap space. See http://www.freertos.org/a00111.html. I think most of the hundreds or pre-configured examples that come in the zip file download have comments to this effect.

-1
votes

Check the return value of xTaskCreate api.

one more thing the second task which you are creating is vtask2 which is having lower priority than vtask1 the one who is creating . And vtask1 is running in while(1) scheduler will not schedule vtask2. you can delay or suspend the vtask1 after creating vtask2.

then vtask2 may execute.