Varsha
I am using OpenPLC editor in ubuntu and trying to generate C code for a program having two tasks with different priorities. I would like to know about the task scheduling algorithm used.

Also when I glanced through the generated C code I couldn't find details regarding task priorities. Can you direct me on this?
Reply 0 0
thiagoralves
The scheduling algorithm is very simple. Just a round robin between tasks. OpenPLC has a fixed cycle time (internally called tick time), no matter how many tasks you have. When you generate the C code from your program, the IEC compiler calculates the maximum tick time that works for all tasks. For example, if you have Task1 = 20ms and Task2 = 50ms, the maximum tick time will be 10ms, where Task1 will be called every 2 ticks and Task2 will be called every 5 ticks. You see that any number bigger than 10ms will not satisfy the timings for both tasks. For example, if tick was calculated as 20ms, Task1 would be called every tick, and that would be ok, but Task2 would be off by either -10ms (being called every 2 ticks) or +10ms (being called every 3 ticks).

In terms of priority, if I'm not mistaken, the only arrangement made is that, in case tasks coincide to start on the same tick, the task with the lower priority will be called first. For example, if you have Task1 = 20ms, priority =1, and Task2 = 40ms, priority 0, the maximum tick time calculated will be 20ms. This means that Task1 will be called every tick, and Task2 will be called every 2 ticks. In that scenario, Task1 and Task2 will both be called at the same time every other tick. However, since Task2's priority is lower, once it is its time to run, it will be called before Task1 on the same tick. This doesn't mean that Task1 will only run on the next tick, they both will run on the same tick interval, but Task2 will execute first, and only after Task2 is done, Task1 will start executing.
Reply 0 0
Rekha
@thiagoralves
(I am a colleague of the original author.)

We did a skim of the C code generated by the compiler.
We located the scheduler in Res0.c to be

void RES0_run__(unsigned long tick) {
  TASK0 = !(tick % 1);  //PRIORITY 2
  TASK1 = !(tick % 2);  //PRIORITY 1
  if (TASK0) {
    PROGRAM0_body__(&INSTANCE0);
  }
  if (TASK1) {
    PROGRAM1_body__(&INSTANCE1);
  }
}


where
task0 has INTERVAL = T#20ms PRIORITY = 2
task1 has INTERVAL = T#40ms PRIORITY = 1
It seems the scheduler does not take into account the priorities of the tasks,
counter to your explanation.

Can you point to us in the code where the priority is factored in for scheduling tasks?
Reply 0 0
thiagoralves
I guess this is not implemented in MatIEC then. It is possible that the priority is completely ignored. You can check for more details with the MatIEC developers here https://bitbucket.org/mjsousa/matiec/src/default/
Reply 0 0
schlumpf
I think the current upstream repository of MatIEC is: https://bitbucket.org/automforge/matiec_git
Your link is dead. There is another personal repo of mjsousa, but without the possibility to create issues: https://bitbucket.org/mjsousa/matiec_git
Reply 0 0
Reply