# Problem with multiple tasks

**URL:** <https://forums.freertos.org/t/problem-with-multiple-tasks/12808>\
**Category:** Kernel\
**Created:** [June 8, 2021, 7:54am UTC](https://forums.freertos.org/t/problem-with-multiple-tasks/12808 "2021-06-08T07:54:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PierreBoss](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@PierreBoss](https://forums.freertos.org/u/PierreBoss)\
**Post date:** [June 8, 2021, 7:54am UTC](https://forums.freertos.org/t/problem-with-multiple-tasks/12808/1 "2021-06-08T07:54:21Z")

</div>

Hello everyone,

I am a beginner about FreeRTOS and I only want to do a simple program with 2 tasks.  
The problem is that when I start the Scheduler with this 2 tasks. The task with the higher priority will start and then without the end of this task the 2nd task will start…

This is my code :

```auto
xTaskCreate(TFT_task_coil, "Coils_task", 128, NULL, 2, NULL);
xTaskCreate(TFT_task_display, "Display", 128, NULL, 1, NULL);
	
/* Scheduler */
vTaskStartScheduler();

void TFT_task_coil(void *param)
{	
	
	int i = 0;
	uint16_t test_coil = 25;
        uint16_t test;
	
	while(1)
	{	
		if (tft_active != 0)
		{
			printf("Task coil start : %lu \r\n", xTaskGetTickCount());
			
			if(xQueueReceive(Queue_USB_Coils, (void *) &item_coil, 10) == pdTRUE) {
				test= item_coil;
			}
			else if(xQueueReceive(Queue_Ethernet_Coils, (void *) &item_coil, 10) == pdTRUE) {
				test= item_coil;
			}
			else{
				test= 300;			
			}
			
			if(uxQueueSpacesAvailable(Queue_Coils_Display) != 0){
				xQueueSend(Queue_Coils_Display, (void *) &test_coil, 10);
			}
			
			i++;
			if(i>19){
				test_coil = (uint16_t)rand();
				i = 0;
			}
			
			printf("Task coil end : %lu \r\n", xTaskGetTickCount());
			vTaskDelay(100);
			
		}
	}
}

void TFT_task_display(void *param)
{
	while(1)
	{
		if (tft_active != 0)
		{
			printf("Task display start : %lu \r\n", xTaskGetTickCount());
		
			EVE_cmd_text_burst(10, 190, 27, 0, "Coils current");
			EVE_cmd_text_burst(220, 190, 27, 0, "mA");
					
			/* Display of our variables */
			static uint16_t Value_coil = 0.0;
				
			xQueueReceive(Queue_Coils_Display, (void *) &Value_coil, 0);
			
			EVE_cmd_number_burst(170, 190, 27, 0, (uint16_t)Value_coil);
					
			printf("Task display end : %lu \r\n", xTaskGetTickCount());
			vTaskDelay(75);
		}
	}
}

```

My queue are working well.  
The task are working well when I start the Scheduler just with 1 but when I put the 2 tasks this is what I get :  
 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/8/852eeea622f71d6b8e83d81c69261da9ffaf88a2.png)

The coil task is not finish that the display task is launch and that make crash my program…

Do you have an idea ?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 8, 2021, 8:17am UTC](https://forums.freertos.org/t/problem-with-multiple-tasks/12808/2 "2021-06-08T08:17:20Z")

</div>

Especially when e.g. using usually stack hungry `printf` in tasks you have to provide a (much) bigger stack.  
I strongly recommend to define [configASSERT](https://www.freertos.org/a00110.html?_ga=2.213540052.1971135108.1609110559-800452554.1575115350#configASSERT) and also enable [stack overflow checking](https://www.freertos.org/Stacks-and-stack-overflow-checking.html)for development/debugging.  
Note that the stack overflow checks can’t catch **all** overflows in **all** situations, but it helps a lot.

---

<div class="post-metadata">

**Author:** ![PierreBoss](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@PierreBoss](https://forums.freertos.org/u/PierreBoss)\
**Post date:** [June 8, 2021, 8:21am UTC](https://forums.freertos.org/t/problem-with-multiple-tasks/12808/3 "2021-06-08T08:21:45Z")

</div>

Thank you ! I will try to raise my stack and to define the variables which you gave me.

Are you sure is a stack problem ? Because when I launch the scheduler with 1 of the 2 task that works perfectly…

---

<div class="post-metadata">

**Author:** ![PierreBoss](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@PierreBoss](https://forums.freertos.org/u/PierreBoss)\
**Post date:** [June 8, 2021, 8:27am UTC](https://forums.freertos.org/t/problem-with-multiple-tasks/12808/4 "2021-06-08T08:27:03Z")

</div>

You have right with a bigger stack that works ! Thank you man.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 8, 2021, 9:01am UTC](https://forums.freertos.org/t/problem-with-multiple-tasks/12808/5 "2021-06-08T09:01:44Z")

</div>

Great that it works 👍  
The problem with stack or buffer overflows is, that other/internal data structures might get corrupted which might cause any possible problem and sometimes even much later. Programs _seem_ to work but in fact are broken causing headaches later on. Therefore it’s essential to figure out the right stack sizes and other resource usages like heap during development.

---

<div class="post-metadata">

**Author:** ![PierreBoss](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@PierreBoss](https://forums.freertos.org/u/PierreBoss)\
**Post date:** [June 8, 2021, 9:46am UTC](https://forums.freertos.org/t/problem-with-multiple-tasks/12808/6 "2021-06-08T09:46:47Z")

</div>

Is it normal that, sometimes a task is start before the end of an other ?

![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/7/7cbdeccd0ef19fee93c7f97ef66f36a4780fa655.png)

I thought it was only one task by one…  
Is it because my stack is still not big enough ?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 8, 2021, 10:26am UTC](https://forums.freertos.org/t/problem-with-multiple-tasks/12808/7 "2021-06-08T10:26:50Z")

</div>

In case `printf` waits internally for something it’d allow the other tasks to run.  
Put aside fatal bugs like memory corruption or something FreeRTOS applications behave as configured and/or documented. There are not much surprises.  
Did you already find the pretty good documentation like [Priority based preemptive RTOS scheduler](https://www.freertos.org/implementation/a00005.html) ? There is a lot more interesting stuff worth reading and helping to get more familiar with FreeRTOS 🙂

---

<div class="post-metadata">

**Author:** ![PierreBoss](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@PierreBoss](https://forums.freertos.org/u/PierreBoss)\
**Post date:** [June 8, 2021, 10:30am UTC](https://forums.freertos.org/t/problem-with-multiple-tasks/12808/8 "2021-06-08T10:30:51Z")

</div>

Ok thank you man for your help !  
Yeah I have already saw how works the scheduler of FreeRTOS but I am a beginner 🙂
