2

The code snippet below creates two tasks, that is trying to print the word. Task 2 is created with a higher priority. Because both tasks run in an infinite cycle and do not give up voluntarily start time, then Task 1 will never get (start time...If that's the right word).

How can i modify "Task 2" callback-function, that "Task 1" will also get start time ?

void vTaskFunction ( void *pvParameters ) {
      for( ;; ) {
            vPrintString((char *) pvParameters );
      }
}

static const char *pcTextForTask1 = "Task 1 is running\r\n";
static const char *pcTextForTask2 = "Task 2 is running\r\n";

int main( void ) {
      /* ...MCU initialization...*/
      xTaskCreate( vTaskFunction, "Task 1", 1000, (void*)pcTextForTask1, 1, NULL ); // Task with priority 1
      xTaskCreate( vTaskFunction, "Task 2", 1000, (void*)pcTextForTask2, 2, NULL ); // Task with priority 2
      vTaskStartScheduler();

      for( ;; );
}

I hope it's make sense

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
Vlad Paskevits
  • 388
  • 2
  • 12
  • Is this desired behavior? Why not change `configUSE_PREEMPTION`? What behavior do you _want_ to get? How often do you _want_ the other function to run? and why not `vTaskDelay(10)`? – KamilCuk Oct 22 '20 at 08:33
  • This is quite expectable, that a for-ever running task of higher priority blocks any task of lower priority. You need to think about a way to give time for these lower priorizes tasks. Delays is one way. – the busybee Oct 22 '20 at 09:13

1 Answers1

1

Your code does exactly what you told it to - print given string as often as possible. Because Task 2 has higher priority than Task 1 and it always has "work" to do, there's no time left for Task 1.

The real question is - what's the behavior that you expect? From the code you wrote, I don't see a clear reason what the expected behavior should be, given one of the tasks has higher priority than the other. Should Task 2 print the string more often than Task1? If so, how's "more often" defined?

If you want both tasks to have some time to do "work", then you have the following options:

  • Assign same priority to both tasks and run in preemptive configuration,

  • Add vTaskDelay(time) to higher priority task (Task 2). In this case you'll make Task 2 sleep for given amount of time, which time will be used by lower priority task (Task 1) to do its work. In this case Task 1 will do as much work (print as many strings) as it can. Once this time finishes, it'll be preempted and the execution will continue exclusively in Task 2 until the next delay happens,

  • Add some kind of synchronization between tasks (semaphore, queue, ...). For example, you can add two semaphores: SemaphoreA and SemaphoreB. Task 1 could in this case do: Wait for SemaphoreA -> Print string -> Give SemaphoreB. Task 2: Wait for SemaphoreB -> Print string -> Give SemaphoreA. This arrangement would cause the strings to be printed interleaved with each other (Task 1 is running -> Task 2 is running -> Task 1 is running -> ...) as the tasks effectively would each wait for the other task to finish its job before continuing. In this cause however there's no added benefit from having different priorities between the tasks.

J_S
  • 2,985
  • 3
  • 15
  • 38