FreeRTOS freezes (Demo Simulator)

Hi,
I have implemented 3 tasks with different priorities. When I run them, after some time (random time from 5 seconds to 1 minute) the freeRTOS freezes, and all tasks stop executing. But when I set them equal priorities then all of them work without freezing.

I am using the FreeRTOS TCP minimal windows simulator in Visual studio. Since I am new to freeRTOS, I am not able to capture the fault. My goal is to run these 3 tasks with different priorities and any hint regarding this would be helpful.

Thanks

First hint would be to show your code :grinning:

Have you read the documentation on the website and the book? https://www.freertos.org/Documentation/RTOS_book.html

Thanks for getting in touch. I tried to debug it a little more.
After setting the priorities same, the program worked fine for about hour after that it got frozen again.
Then i changed all the three tasks and just put printf in side all the tasks. It still gets freeze with just printf in task body. I am attaching the main blocks of code below.

static void matrix_task()
{
	while (1)
	{
		printf("TASK-0 Complete\n");
		vTaskDelay(10);
	}
}
static void matrix_task1()
{
	while (1)
	{
		printf("TASK-1 Complete\n");
		vTaskDelay(10);
	}

}
static void matrix_task2()
{
	while (1)
	{
		printf("TASK-2 Complete\n");
		vTaskDelay(10);
	}
}


int main( void )
{
const uint32_t ulLongTime_ms = pdMS_TO_TICKS( 1000UL );
TimerHandle_t xCheckTimer;

	/*
	 * Instructions for using this project are provided on:
	 * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
	 */

	/* Miscellaneous initialisation including preparing the logging and seeding
	the random number generator. */
	prvMiscInitialisation();

	/* Initialise the network interface.
	 
	***NOTE*** Tasks that use the network are created in the network event hook
	when the network is connected and ready for use (see the definition of
	vApplicationIPNetworkEventHook() below).  The address values passed in here
	are used if ipconfigUSE_DHCP is set to 0, or if ipconfigUSE_DHCP is set to 1
	but a DHCP server cannot be	contacted. */
	FreeRTOS_debug_printf( ( "FreeRTOS_IPInit\n" ) );
	FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );
	
	
	/* A timer is used to periodically check the example tasks are functioning
	as expected.  First create the software timer ... */
	xCheckTimer = xTimerCreate("Check",				/* Text name used for debugging only. */
		1000,
		pdTRUE,					/* This is an auto-reload timer. */
		NULL,					/* Parameter not used. */
		prvCheckTimerCallback); /* The timer callback function. */

/* ... assert if the timer was not created, ... */
	configASSERT(xCheckTimer);

	/* ... then start the timer. */
	xTimerStart(xCheckTimer, 0);


	// CREATE FREERTOS TASKS

	xTaskCreate(matrix_task, (signed char*)"Matrix", 1000, NULL, 3, &TASK_HANDLER);
	xTaskCreate(matrix_task1, (signed char*)"Matrix1", 1000, NULL, 3, &TASK1_HANDLER);
	xTaskCreate(matrix_task2, (signed char*)"Matrix2", 1000, NULL, 3, &TASK2_HANDLER);

	/* Start the RTOS scheduler. */
	FreeRTOS_debug_printf( ("vTaskStartScheduler\n") );
	vTaskStartScheduler();

	/* If all is well, the scheduler will now be running, and the following
	line will never be reached.  If the following line does execute, then
	there was insufficient FreeRTOS heap memory available for the idle and/or
	timer tasks	to be created.  See the memory management section on the
	FreeRTOS web site for more details (this is standard text that is not not
	really applicable to the Win32 simulator port). */
	for( ;; )
	{
		Sleep( ulLongTime_ms );
	}
}

Did the original code have printf() in it too? If so then that is most likely your problem. You cannot call windows system calls from FreeRTOS tasks. You can get away with printf() if you call it infrequently and only from one task as then it is almost certain to succeed without blocking the windows thread it is called from.

That makes sense. Yes, I had printf in original code too and may be that’s the reason my program hangs with 3 or 2 tasks running not 1. I will remove all the printf’s and check. Thanks for the help.

One thing to add here: it won’t help to put a mutex around the function printf(). Still then it is not safe to do any (possibly blocking) Windows system call.

In the demo project on AWS, you will see in aws_demo_logging.c that logging is printed using printf() from a Windows thread ( prvWin32LoggingThread() ).
FreeRTOS tasks send their logging to the Windows thread using a stream buffer ( xLogStreamBuffer ).

So I did further experimentation I thought I should share them here.
Regarding the tasks, it is an assignment of an online course. The functionality of 3 tasks are,

Tasks functionality:

  • TASK-1:- Involve image processing on 2d grayscale image. Send the resulting data over the queue to task 2.

  • TASK-2:- Involve encryption of resultant grayscale image. Sends the resulting data to task 3.

  • TASK-3:- Send the encrypted image to the server via TCP socket.

Experiment scenarios and results

  • Experiment-1:- Removed all the printfs from original tasks. Kept the priorities of tasks same. Worked perfectly fine. Tested by running over 5 hours.

  • Experiment-2:- Removed all the printfs from original tasks. Kept different priorities. All the 3 tasks stopped executing. After some time (random within a few minutes).

  • Experiment-3:- Create two projects of experiment 1 and ran these two simultaneously (With different MAC and IP). Both projects got frozen within a minute.

I have achieved my target but I still don’t understand the reason for getting the tasks frozen. Removing printf definitely helped in one case. Maybe it’s all because of a simulating environment and it won’t happen on actual device with release code.