Hello,
I have the following code:
#include <std.h>
#include<stdio.h>
#include <log.h>
#include "TestTaskcfg.h"
int T1_Value=0;
int T2_Value=0;
void main()
{
printf("Program started\n");
}
Void Task_1(Arg id_arg) // send some data to task 2
{
int i;
for(i=0;;i++)
{
printf("Value sent by Task 1- %d\n",i);
T1_Value=i*2;
SEM_postBinary(&T1_W); // tell that value is updated
SEM_pendBinary(&T1_R, SYS_FOREVER); // wait until it is read by kernel
}
}
Void Task_2(Arg id_arg) // get data from task1 and print it.
{
int i;
for(i=0;;i++)
{
SEM_postBinary(&T2_R); // tell the kernel that you are ready to read value
SEM_pendBinary(&T2_W, SYS_FOREVER); // wait until value become ready
printf("value read In Task 2- %d\n",i);
printf("Value= %d\n",T2_Value);
}
}
Void Task_Kernel(Arg id_arg)
{
for(;;)
{
int T1_W_Count=SEM_count(&T1_W);
int T2_R_Count=SEM_count(&T2_R);
if(T1_W_Count==1 && T2_R_Count==1)
{
T2_Value=T1_Value;
SEM_reset(&T1_W,0);
SEM_reset(&T2_R,0);
SEM_post(&T2_W);
SEM_post(&T1_R);
}
TSK_sleep(1);
// TSK_yield();
}
}
It consists of 3 tasks.
The priority for task_1 and task_2 are 1 and for task_kernal it is 2.
If I run this code, I am getting this output:
Program started
Value sent by Task 1- 0
value read In Task 2- 0
Value= 0
Value sent by Task 1- 1
value read In Task 2- 1
Value= 2
Value sent by Task 1- 2
value read In Task 2- 2
Value= 4
Value sent by Task 1- 3
value read In Task 2- 3
Value= 6
Value sent by Task 1- 4
value read In Task 2- 4
Value= 8
If I comment out TSK_sleep(1) and uncomment TSK_yield(), I am not getting any output (other than program started).
So what is the difference between TSK_sleep(1) and TSK_yield()?
I believe using TSK_sleep() is a waste of CPU time, is there any other way to do this without using TSK_sleep()?
Regards