# Preemptive scheduling  

**URL:** <https://forums.freertos.org/t/preemptive-scheduling/620>\
**Category:** Kernel\
**Created:** [July 9, 2014, 2:44pm UTC](https://forums.freertos.org/t/preemptive-scheduling/620 "2014-07-09T14:44:51Z")\
**Posts on this page:** 9\
**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:** [July 9, 2014, 2:44pm UTC](https://forums.freertos.org/t/preemptive-scheduling/620/1 "2014-07-09T14:44:51Z")

</div>

**[manish9161](https://sourceforge.net/u/manish9161/)** wrote on [Wednesday, July 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/1acff08f/#9ccd):

Hello I am Manish Baing working on stm32f3Discovery with freeRTOS .  
What I have implemented  
1.Created 4 task with priority 4,3,2,2 respectively  
2.Toggle LED 3 ,LED 4,LED 5,LED 8 in task callback function of respective task.  
3.set #define configUSE\_PREEMPTION 1 in FreeRTOSConfig.h

what I will be expecting from this code  
1.I am trying to implement preemptive scheduling of task but in this case only task with priority 4 is running and get competed successfully.  
2.Why other task should not be running if task with highest priority has already completed with its work.  
3.how do I test preemptive scheduling…?

Thanks in advance  
my code…  
###########…  
#include “FreeRTOS.h”  
#include “timers.h”  
#include “task.h”  
#include “stm32f3\_discovery.h”  
#include \<string.h\>  
#include \<stdio.h\>  
/\*!\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

- \fn init()
- \brief LED init function
- 
- 

```
\param -

```

- 

```
\return -

```

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
void init()  
{

STM\_EVAL\_LEDInit(LED3 );  
STM\_EVAL\_LEDInit(LED4 );  
STM\_EVAL\_LEDInit(LED5 );  
STM\_EVAL\_LEDInit(LED8 );  
}

/\*!\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

- \fn
- \brief taskA callback function
- 

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
void TaskA()  
{  
for(uint8\_t i= 0; i\< 100 ; i++)  
//while(1)  
{  
STM\_EVAL\_LEDOn(LED3);  
vTaskDelay(500);  
STM\_EVAL\_LEDOff(LED3);  
vTaskDelay(500);  
}

}  
/\*!\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

- \fn
- \brief taskB callback function
- 

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
void TaskB()  
{  
for(uint8\_t i= 0; i\< 100 ; i++)  
//while(1)  
{  
STM\_EVAL\_LEDOn(LED4);  
vTaskDelay(500);  
STM\_EVAL\_LEDOff(LED4);  
vTaskDelay(500);  
}  
}

/\*!\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

- \fn
- \brief taskC callback function
- 

\*\*\***_/  
void TaskC()  
{  
for(uint8\_t i= 0; i\< 100 ; i++)  
// while(1)  
{  
STM\_EVAL\_LEDOn(LED5);  
vTaskDelay(500);  
STM\_EVAL\_LEDOff(LED5);  
vTaskDelay(500);  
}  
}  
/_!**

- \fn
- \brief taskD callback function
- 

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
void TaskD()  
{  
for(uint8\_t i= 0; i\< 100 ; i++)  
// while(1)  
{  
STM\_EVAL\_LEDOn(LED8);  
vTaskDelay(500);  
STM\_EVAL\_LEDOff(LED8);  
vTaskDelay(500);  
}

}  
int main(void) {  
init();  
xTaskHandle xHandle1 ,xHandle2,xHandle3,xHandle4;  
xHandle1 = NULL;  
xHandle2 = NULL;  
xHandle3 = NULL;  
xHandle4 = NULL;  
/_! TaskA is having highest priority /  
xTaskCreate(TaskA, (signed char)“TaskA”, 1000, NULL,4, &xHandle1);  
if( xHandle1 != NULL )  
{  
vTaskDelete( xHandle1 );  
}  
/_! TaskB having medium priority _/  
xTaskCreate(TaskB, (signed char_)“TaskB”, 1000, NULL, 3, &xHandle2);  
if( xHandle2 != NULL )  
{  
vTaskDelete( xHandle2 );  
}  
/_! TaskC ,and TaskD Idle tasks /  
xTaskCreate(TaskC, (signed char)“TaskC”, 1000, NULL, 2, &xHandle3);  
if( xHandle3 != NULL )  
{  
vTaskDelete( xHandle3 );  
}  
xTaskCreate(TaskD, (signed char_)“TaskD”, 1000, NULL, 2, &xHandle4);  
if( xHandle4 != NULL )  
{  
vTaskDelete( xHandle4 );  
}  
vTaskStartScheduler();  
return 0;  
}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 9, 2014, 5:46pm UTC](https://forums.freertos.org/t/preemptive-scheduling/620/2 "2014-07-09T17:46:18Z")

</div>

**[edwards3](https://sourceforge.net/u/edwards3/)** wrote on [Wednesday, July 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/1acff08f/#b997):

Looks like you tasks dont use an infinite loop. See this page for how to write a task. [http://www.freertos.org/implementing-a-FreeRTOS-task.html](http://www.freertos.org/implementing-a-FreeRTOS-task.html)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 9, 2014, 9:11pm UTC](https://forums.freertos.org/t/preemptive-scheduling/620/3 "2014-07-09T21:11:41Z")

</div>

**[markwrichardson](https://sourceforge.net/u/markwrichardson/)** wrote on [Wednesday, July 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/1acff08f/#7dac):

This was my first mistake too. I haven’t used any other small RTOSes, but it was hard to adjust my brain to remember that the delay in task create is the only initial delay. Afterwards, the task itself must regulate it’s own loop rate or block while waiting on some flag.

The task has it’s own stack and the locals there are persistent. Returning from a task will crash the system because there is no return value on the stack for the task-function to return to.

Your sample will probably blink all four LEDs evenly 500ms On, 500 ms Off.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 10, 2014, 5:38am UTC](https://forums.freertos.org/t/preemptive-scheduling/620/4 "2014-07-10T05:38:19Z")

</div>

**[manish9161](https://sourceforge.net/u/manish9161/)** wrote on [Thursday, July 10, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/1acff08f/#7dac/a109):

Thanks a lot again.Its really help me to clear my doubts.Actually I have test preemptive sheduling beacause I have to built real time application where  
1.single higher priority task and multiple general or medium priority task.  
2.But how do i test preemptive sheduling using LED blink sample code mentioned above.  
3.Please suggest some changes in mentioned code so as to test preemptive sheduling of task.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 10, 2014, 8:09am UTC](https://forums.freertos.org/t/preemptive-scheduling/620/5 "2014-07-10T08:09:34Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Thursday, July 10, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/1acff08f/#14a1):

Rather than ask people to change your code, why don’t you look at and use the code that has already been provided for you. Nearly every demo in the download flashes an led, and there is even a flash.c file in FreeRTOS/demo/common/minimal that creates multiple tasks that do nothing but flash an led.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 10, 2014, 10:05am UTC](https://forums.freertos.org/t/preemptive-scheduling/620/6 "2014-07-10T10:05:41Z")

</div>

**[manish9161](https://sourceforge.net/u/manish9161/)** wrote on [Thursday, July 10, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/1acff08f/#41da):

Hello Dave don’t take me wrong .Actually I am new to FreeRTOS andI don’t want to just blink LED .But I have to test and analyse preemptive shedling using LED code .so main goal is to test preemptive sheduling algorithum not to flash an LED using FreeRTOS.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 10, 2014, 10:31am UTC](https://forums.freertos.org/t/preemptive-scheduling/620/7 "2014-07-10T10:31:07Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, July 10, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/1acff08f/#9ed0):

What is it about the preemptive scheduling algorithm you want to test. It is a traditional algorithm, used by nearly all small schedulers. It will always run the highest priority task that is able to run (not blocked) and time slice tasks of equal priority if there are multiple ready tasks of the same priority. If you can say exactly which aspect you want to test then I will be able to point you to a file that will already test it - you can then just run that file rather than writing your own.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 10, 2014, 10:41am UTC](https://forums.freertos.org/t/preemptive-scheduling/620/8 "2014-07-10T10:41:19Z")

</div>

**[manish9161](https://sourceforge.net/u/manish9161/)** wrote on [Thursday, July 10, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/1acff08f/#5bdc):

Thanks again.  
1.I have created 4 task one with higest priority and other 3 with same priority (less than 1st task).  
2.I have to just test that 1st task shedule first and then remaining task will get sheduling time.  
3.I am not tring to test sheduling algorithums i know its already verified by experts.I just want to test my application with that algorithum.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 11, 2014, 5:24am UTC](https://forums.freertos.org/t/preemptive-scheduling/620/9 "2014-07-11T05:24:23Z")

</div>

**[manish9161](https://sourceforge.net/u/manish9161/)** wrote on [Friday, July 11, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/1acff08f/#b111):

hello Real Time Engineers ltd ,  
Can I get that file which you already mentioned so as to proceed further …? if possible please provide .I think I need to work more to get familiar with FreeRTOS.I will try my best.

Reagrds
