# Issue with two task with different priority

**URL:** <https://forums.freertos.org/t/issue-with-two-task-with-different-priority/10603>\
**Category:** Kernel\
**Created:** [September 4, 2020, 11:36am UTC](https://forums.freertos.org/t/issue-with-two-task-with-different-priority/10603 "2020-09-04T11:36:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![enggparth](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/enggparth/32/851_2.png) [@enggparth](https://forums.freertos.org/u/enggparth)\
**Post date:** [September 4, 2020, 11:36am UTC](https://forums.freertos.org/t/issue-with-two-task-with-different-priority/10603/1 "2020-09-04T11:36:01Z")

</div>

Hello,

I am implementing two tasks code as follow  
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*_/  
{  
/ Two task created/_  
xTaskCreate(vtask\_1, “Task-1”, 500, NULL, 2, &xTaskHandle1);  
xTaskCreate(vtask\_2, “Task-2”, 500, NULL, 5, &xTaskHandle2);

/\* Schedule the task \*/  
vTaskStartScheduler();  
}

/\* Task\_1 Handler\*/  
void (vtask\_1)( void \*params )  
{

```
UBaseType_t p1,p2;
sprintf(usr_msg,"TASK 1 is runnung\r\n");
printmsg(usr_msg);

   sprintf(usr_msg,"TASK 1 priority is : %ld\r\n",uxTaskPriorityGet(xTaskHandle1));
printmsg(usr_msg);

sprintf(usr_msg,"TASK 2 priority is : %ld \r\n",uxTaskPriorityGet(xTaskHandle2));
printmsg(usr_msg);

while(1)
{
	if(prio)
	{
		prio = FALSE;
		 p1 = uxTaskPriorityGet(xTaskHandle1);
		 p2 = uxTaskPriorityGet(xTaskHandle2);

		 //switch prio
		 vTaskPrioritySet(xTaskHandle1, p2);
		 vTaskPrioritySet(xTaskHandle2, p1);
	}else
	{
		HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
		HAL_Delay(1000);
	}
}

```

}

/\* Task\_2 Handler\*/  
void (vtask\_2)( void \*params )  
{  
UBaseType\_t p1,p2;  
sprintf(usr\_msg,“TASK 2 is runnung\r\n”);  
printmsg(usr\_msg);

```
sprintf(usr_msg,"TASK 1 priority is : %ld\r\n",uxTaskPriorityGet(xTaskHandle1));
printmsg(usr_msg);

sprintf(usr_msg,"TASK 2 priority is : %ld \r\n",uxTaskPriorityGet(xTaskHandle2));
printmsg(usr_msg);

while(1)
	{
		if(prio)
		{
			prio = FALSE;
			 p1 = uxTaskPriorityGet(xTaskHandle1);
			 p2 = uxTaskPriorityGet(xTaskHandle2);

			 //switch prio
			 vTaskPrioritySet(xTaskHandle1, p2);
			 vTaskPrioritySet(xTaskHandle2, p1);

		}else
		{
			HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
			HAL_Delay(100);
		}
}

```

}

I was under the impression that each time a task runs it runs the whole handler body. Meaning from the open brace of task\_1 to the closing brace of the function body. However, i realized that after Task1 has switched in from Task2 or Task2 has switched in from Task1, for subsequent context switches, only the while loop or for loop gets executed.

Thus, subsequent button presses to switch priority between the 2 tasks will not print out the messages to Teraterm.

Only the 1st run context switching will run the whole handler body whereas subsequent switching won’t?

What is the reason?

Please refer the screenshot

Thanks  
 ![Error](https://d2v6vdsk2p900z.cloudfront.net/original/1X/e1c466e60d2354bfb742a4ecfe21b41a22e4594c.jpeg)

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [September 4, 2020, 11:58am UTC](https://forums.freertos.org/t/issue-with-two-task-with-different-priority/10603/2 "2020-09-04T11:58:53Z")

</div>

When tasks switch in and out, you don’t want them to ‘restart’ at the beginning but the keep executing from where they were. That is why we put the loop in the task, to keep it running in that loop. Maybe you want the code that you think should be running each time within the loop.

---

<div class="post-metadata">

**Author:** ![enggparth](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/enggparth/32/851_2.png) [@enggparth](https://forums.freertos.org/u/enggparth)\
**Post date:** [September 4, 2020, 1:02pm UTC](https://forums.freertos.org/t/issue-with-two-task-with-different-priority/10603/3 "2020-09-04T13:02:50Z")

</div>

one more small query like after calling vTaskStartScheduler() API, as per my understanding my higher priority tasks will run as in my case printmsg() of task2.

But I observe that when I start my program all printmsg() of task1 and task2 executes, so why task1 executes printmsg() function.

Inside while all is working fine for both case

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [September 4, 2020, 3:56pm UTC](https://forums.freertos.org/t/issue-with-two-task-with-different-priority/10603/4 "2020-09-04T15:56:13Z")

</div>

Not knowing how printmsg works, it could easily be blocking while waiting for a message to finish going out, thus giving the other task time to run.

FreeRTOS runs the current highest priority ready task. When that task blocks for some reason, or if some other task of higher priority becomes ready, then FreeRTOS will switch to that task (at least if Preemption is turned on, otherwise it will wait for the current task to do something which indicates it is ready to be preempted before switching).

---

<div class="post-metadata">

**Author:** ![enggparth](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/enggparth/32/851_2.png) [@enggparth](https://forums.freertos.org/u/enggparth)\
**Post date:** [September 7, 2020, 4:44am UTC](https://forums.freertos.org/t/issue-with-two-task-with-different-priority/10603/5 "2020-09-07T04:44:42Z")

</div>

1. Preemption is turned ON.

void printmsg(char _msg)  
{  
HAL\_USART\_Transmit(&husart2, (uint8\_t_)msg, strlen(msg), 1000);  
}

Actually I have two tasks with priority with 2 and 5 respectively.  
So as PREMPTOIN is turned on so my task with priority 5 should only run from time t = 0;  
but I am observing that task with priority 2 is running till before it goes into a while loop.

So I am facing difficulty in understanding the issue.
