# A lower priority task starting first ?

**URL:** <https://forums.freertos.org/t/a-lower-priority-task-starting-first/216>\
**Category:** Kernel\
**Created:** [August 22, 2013, 6:23pm UTC](https://forums.freertos.org/t/a-lower-priority-task-starting-first/216 "2013-08-22T18:23:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 22, 2013, 6:23pm UTC](https://forums.freertos.org/t/a-lower-priority-task-starting-first/216/1 "2013-08-22T18:23:10Z")

</div>

**[diptopal](https://sourceforge.net/u/diptopal/)** wrote on [Thursday, August 22, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/07dd568e/#6f12):

Here is a relevant part of the code

\*\*xSemaphoreHandle employee\_signal = 0;

void employee\_task() {

;

}

void boss(void \*p){  
&nbsp; for(;;){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LPC\_printf(“1\n\r”);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xSemaphoreGive(employee\_signal);  
&nbsp; LPC\_printf(“3\r\n”);  
&nbsp; vTaskDelay(2000);  
&nbsp;  
&nbsp; }  
}

void employee(void \*p){  
&nbsp;&nbsp; for(;;){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(xSemaphoreTake(employee\_signal, portMAX\_DELAY) == pdTRUE ){  
&nbsp;&nbsp; employee\_task();  
&nbsp;&nbsp; LPC\_printf(“2\n\r”);  
&nbsp;&nbsp; }  
&nbsp;&nbsp; }

}

int main (void) {

vSemaphoreCreateBinary(employee\_signal);  
&nbsp;&nbsp;&nbsp; LPC\_UART0-\>IER = IER\_THRE | IER\_RLS;  
SystemInit();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* initialize clocks \*/  
&nbsp; UARTInit(0, 38400); /\* baud rate setting \*/  
&nbsp; UARTInit(1, 38400); /\* baud rate setting \*/  
xTaskCreate( boss,&nbsp; ( signed char\* ) “t1”,&nbsp; 1024, NULL, 1, NULL&nbsp; );  
xTaskCreate( employee,&nbsp; ( signed char\* ) “t2”,&nbsp; 1024, NULL, 1, NULL&nbsp; );  
vTaskStartScheduler();  
&nbsp;&nbsp;&nbsp; return 0;  
}\*\*

I am expecting the boss task to start printing first, but as I can see, once immediately after the program starts the employee task is printing _once_ to the console, then it falls into the right sequence. I am expecting the _boss_ task to start instead, give a semaphore to the employee task and then the employee task to wake up. Pasting a part of the output here. This is on LPC1768 hardware, I hope I have configured the RTOS properly.

**2&nbsp; \<- _This should not be here_  
1 \<- The printing should start from here  
2  
3  
1  
2  
3  
1  
2  
3**

I am only able to avoid this situation id I am making the priority od the boss task higher. Then I am getting the correct output. How is the employee task printing first when the boss has not even started and given it a semaphore?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 22, 2013, 6:25pm UTC](https://forums.freertos.org/t/a-lower-priority-task-starting-first/216/2 "2013-08-22T18:25:26Z")

</div>

**[diptopal](https://sourceforge.net/u/diptopal/)** wrote on [Thursday, August 22, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/07dd568e/#e926):

I am sorry, the topic should have been something else, like how can my task print even without getting a Semaphore?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 22, 2013, 7:32pm UTC](https://forums.freertos.org/t/a-lower-priority-task-starting-first/216/3 "2013-08-22T19:32:58Z")

</div>

**[edwards3](https://sourceforge.net/u/edwards3/)** wrote on [Thursday, August 22, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/07dd568e/#2e63):

The semaphore is created so the first take will pass. If you take the semaphore immediately after you create it (before your tasks start using it) then I think it will work the way your code expected it to.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 22, 2013, 7:41pm UTC](https://forums.freertos.org/t/a-lower-priority-task-starting-first/216/4 "2013-08-22T19:41:49Z")

</div>

**[mdkendall](https://sourceforge.net/u/mdkendall/)** wrote on [Thursday, August 22, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/07dd568e/#32cc):

Mutexes and binary semaphores are created in the already-given state, so the fact that your first take operation succeeds is expected behaviour.

For it to work as you want, you must take the semaphore immediately after creating it.
