- RTOS

RTOS program to generate Round Bin Multitasking algorithm

In this article let’s learn to assign tasks in round robin fashion. In the following program first, it assigns the highest priority to task1 and then it creates task2,task3. It continued the execution and waits for an event in task1, then suspends task1 and then enters into task2. Next, it waits for an event in task2, then suspend task2 and then enters task 3. it does the same in task3 function. Then it returns main and resumes the task1 function and sets task2. It resumes the task2 function and sets for task3. It repeats the same.

So task1——task2——–task3—–task1—— so on

Algorithm:

1)start
2)Declare task1,task2,task3
3)Call the task1 function
4)Assign the highest priority to task1, next priority to task2, last priority to task3
5)Wait for an event in task1 and set for task 2
6)Wait for an event in task2 and set for task 3
7)Wait for an event in task3 and set for task 1
8)end

Code goes here:

#include<lpc214x.h>
#include<rtl.h>
OS_RESULT rel1,rel2,rel3;
OS_TID t1,t2,t3;
int cnt1,cnt2,cnt3,i;
__task void task2(void);
__task void task1(void);
__task void task3(void);
int main()
{
   cnt1=0;
   cnt2=0;
   os_sys_init(task1); //starts with task1
   while(1);
}
__task void task1(void)
{
   t1=os_tsk_self();
   os_tsk_prio_self(3);
   t2=os_tsk_create(task2,2); //creates task2
   t3=os_tsk_create(task3,1);//creayes task3
   while(1)
   {
      rel1=os_evt_wait_or(0x0001,0x0030);//wait state
      if(rel1==OS_R_EVT)
      {
         for(i=0;i&lt;15000;i++)
         {
             cnt1++;
         }
      }
      os_dly_wait(1);
      os_evt_set(0x0002,t2);//sets for task2
   }
}
__task void task2(void)
{
   while(1)
   {
      rel2=os_evt_wait_or(0x0002,0x0030);//wait state
      if(rel2==OS_R_EVT)
      {
         for(i=0;i&lt;15000;i++)
         {
            cnt2++;
         }
      }
      os_dly_wait(1);
      os_evt_set(0x0003,t3);//sets for task3
   }
}
__task void task3(void)
{
   while(1)
   {
      rel3=os_evt_wait_or(0x0003,0x0030);//wait state
      if(rel3==OS_R_EVT)
      {
         for(i=0;i&lt;15000;i++)
         {
            cnt3++;
         }
      }
      os_dly_wait(1);
      os_evt_set(0x0001,t1);//sets for task1
   }
}

Leave a Reply