# Task is going in an infinite loop

I’m new here. I’ve searched the forum, but can’t find anything related to this. So, I thought about posting here.

The problem statement is that, we have 2 tasks - matrix and communication.

1. Matrix has higher priority than communication.
2. Calculate the execution time of both the tasks using vApplicationTickHook()
3. If communication has execution time >1000ms, increase it’s priority to 4 or if it’s execution time is <200ms, decrease it’s priority to 2. This has to be done in a separate task.

My problem is that first priority_set_task() is executed then matrix() and then communication. After that the simulator is caught in an infinite loop ( I think so, because I don’t receive any output after it. I think it’s priority_set_task(), but I’m not sure about it) I can’t seem to understand where I’m going wrong. I’m sure you guys will notice it, because it’s definitely something silly.

``````#define SIZE 10
#define ROW SIZE
#define COL SIZE

int count_matrix = 0;
int count_communication = 0;
int count_priority = 0;

uint32_t count = 0;
int comm_priority = 1;

{
vApplicationTickHook();
fflush(stdout);
int i;
double** a = (double**)pvPortMalloc(ROW * sizeof(double*));
for (i = 0; i < ROW; i++) a[i] = (double*)pvPortMalloc(COL * sizeof(double));
double** b = (double**)pvPortMalloc(ROW * sizeof(double*));
for (i = 0; i < ROW; i++) b[i] = (double*)pvPortMalloc(COL * sizeof(double));
double** c = (double**)pvPortMalloc(ROW * sizeof(double*));
for (i = 0; i < ROW; i++) c[i] = (double*)pvPortMalloc(COL * sizeof(double));

double sum = 0.0;
int j, k, l;

for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
a[i][j] = 1.5;
b[i][j] = 2.6;
}
}

while (1) {
/*
* In an embedded systems, matrix multiplication would block the CPU for a long time
* but since this is a PC simulator we must add one additional dummy delay.
*/
count_matrix = count;
long simulationdelay;
for (simulationdelay = 0; simulationdelay < 1000000000; simulationdelay++)
;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
c[i][j] = 0.0;
}
}

for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
sum = 0.0;
for (k = 0; k < SIZE; k++) {
for (l = 0; l < 10; l++) {
sum = sum + a[i][k] * b[k][j];
}
}
c[i][j] = sum;
}
}
count_matrix = count - count_matrix;
fflush(stdout);
printf("Execution time of matrix_task is %i ms\n", count_matrix);
fflush(stdout);
count_matrix = count;

}

}

{
while (1) {
count_priority = count;
printf("Sending data..................................................................\n");
fflush(stdout);
printf("Data sent!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
fflush(stdout);
count_communication = count - count_communication;
printf("Execution time of communication_task is %i ms\n", count_communication);
fflush(stdout);
count_communication = count;
}

}

{
printf("Priority task has begun execution. Comm_priority is : %d. ", comm_priority);

if ((count_communication > 1000) && (comm_priority != 4))
{
comm_priority = 4;
printf("Execution time of communication_task is %i ms, increasing priority to 4\n", count_communication);
fflush(stdout);
//count_communication = 0;
}
else if ((count_communication < 200) && (comm_priority != 2))
{
comm_priority = 2;
printf("Execution time of communication_task is %i ms, decreasing priority to 2\n", count_communication);
fflush(stdout);
//count_communication = 0;
}

printf("priority set task has finished execution once. Its Execution time is %d ms\n", count_priority);
fflush(stdout);
count_priority = 0;

}

int main(void)
{
prvInitialiseHeap();

vTraceEnable(TRC_START);

for (;;);
return 0;
}

void vApplicationTickHook(void)
{
if (count == 0xFFFFFFFF)
{
count = 0;
count_matrix = 0;
count_communication = 0;
}
else
{
count++;
}

#if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
{
vFullDemoTickHookFunction();
}