Thread: Free rtos code example 1

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    80

    Free rtos code example 1

    I am looking help to understand free rtos example code. I am trying to figure out how scheduler works in the following code

    Code:
    /* FreeRTOS.org includes. */#include "FreeRTOS.h"
    #include "task.h"
    
    
    /* Demo includes. */
    #include "supporting_functions.h"
    
    
    /* Used as a loop counter to create a very crude delay. */
    #define mainDELAY_LOOP_COUNT		( 0xffffff )
    
    
    /* The task functions. */
    void vTask1( void *pvParameters );
    void vTask2( void *pvParameters );
    
    
    /*-----------------------------------------------------------*/
    
    
    int main( void )
    {
    	/* Create one of the two tasks. */
    	xTaskCreate(	vTask1,		/* Pointer to the function that implements the task. */
    					"Task 1",	/* Text name for the task.  This is to facilitate debugging only. */
    					1000,		/* Stack depth - most small microcontrollers will use much less stack than this. */
    					NULL,		/* We are not using the task parameter. */
    					1,			/* This task will run at priority 1. */
    					NULL );		/* We are not using the task handle. */
    
    
    	/* Create the other task in exactly the same way. */
    	xTaskCreate( vTask2, "Task 2", 1000, NULL, 1, NULL );
    
    
    	/* Start the scheduler to start the tasks executing. */
    	vTaskStartScheduler();	
    
    
    	/* The following line should never be reached because vTaskStartScheduler() 
    	will only return if there was not enough FreeRTOS heap memory available to
    	create the Idle and (if configured) Timer tasks.  Heap management, and
    	techniques for trapping heap exhaustion, are described in the book text. */
    	for( ;; );
    	return 0;
    }
    /*-----------------------------------------------------------*/
    
    
    void vTask1( void *pvParameters )
    {
    const char *pcTaskName = "Task 1 is running\r\n";
    volatile uint32_t ul;
    
    
    	/* As per most tasks, this task is implemented in an infinite loop. */
    	for( ;; )
    	{
    		/* Print out the name of this task. */
    		vPrintString( pcTaskName );
    
    
    		/* Delay for a period. */
    		for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
    		{
    			/* This loop is just a very crude delay implementation.  There is
    			nothing to do in here.  Later exercises will replace this crude
    			loop with a proper delay/sleep function. */
    		}
    	}
    }
    /*-----------------------------------------------------------*/
    
    
    void vTask2( void *pvParameters )
    {
    const char *pcTaskName = "Task 2 is running\r\n";
    volatile uint32_t ul;
    
    
    	/* As per most tasks, this task is implemented in an infinite loop. */
    	for( ;; )
    	{
    		/* Print out the name of this task. */
    		vPrintString( pcTaskName );
    
    
    		/* Delay for a period. */
    		for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
    		{
    			/* This loop is just a very crude delay implementation.  There is
    			nothing to do in here.  Later exercises will replace this crude
    			loop with a proper delay/sleep function. */
    		}
    	}
    }
    As shown in code there are two task's of same priority running on PC. I don't understand timing of task's. Anybody help me
    Attached Images Attached Images Free rtos  code example 1-rted1-jpg 

  2. #2
    Registered User
    Join Date
    Oct 2019
    Posts
    46
    Shouldn't the code you are trying to trying to understand and make sense of be in the function below?

    Code:
    /* Start the scheduler to start the tasks executing. */
    vTaskStartScheduler();
    It looks to me like that should be the relevant code.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    80
    Quote Originally Posted by ghoul View Post
    Shouldn't the code you are trying to trying to understand and make sense of be in the function below?
    It looks to me like that should be the relevant code.
    No it's different. I am trying to figure out how long tasks are executing

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    > I am trying to figure out how scheduler works in the following code
    Either
    - read the design documentation of freeRTOS.
    - read the source code of freeRTOS.

    Attempting to reverse engineer the operation of the scheduler is like trying to learn how to cook from one bite of cake.

    > I am trying to figure out how scheduler works in the following code
    They're executing all the time. If you're not running task1, you're running task2.

    Since you gave them both the same priority, the scheduler would seem to round robin it's way through all the ready tasks.
    And the tasks will be perpetually either running or ready as you've written them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    80
    Quote Originally Posted by Salem View Post
    > I am trying to figure out how scheduler works in the following code
    Either
    - read the design documentation of freeRTOS.
    - read the source code of freeRTOS.

    Attempting to reverse engineer the operation of the scheduler is like trying to learn how to cook from one bite of cake.

    > I am trying to figure out how scheduler works in the following code
    They're executing all the time. If you're not running task1, you're running task2.

    Since you gave them both the same priority, the scheduler would seem to round robin it's way through all the ready tasks.
    And the tasks will be perpetually either running or ready as you've written them.
    When I set same priority for all task's then result of all task's should be same but when I ran my code I am not getting such result

    Code:
        
    /* FreeRTOS.org includes. */
    #include "FreeRTOS.h"
    #include "task.h"
    
    
    #include "supporting_functions.h"
    
    
    
    
    /* The task functions. */
    void vTask1( void *pvParameters );
    void vTask2( void *pvParameters );
    void vTask3(void* pvParameters);
    
    
    int main( void )
    {
    	/* Create one of the two tasks. */
    	xTaskCreate(	vTask1,		/* Pointer to the function that implements the task. */
    					"Task 1",	/* Text name for the task.  This is to facilitate debugging only. */
    					1000,		/* Stack depth - most small microcontrollers will use much less stack than this. */
    					NULL,		/* We are not using the task parameter. */
    					1,			/* This task will run at priority 1. */
    					NULL );		/* We are not using the task handle. */
    
    
    	/* Create the other task in exactly the same way. */
    	xTaskCreate( vTask2, "Task 2", 1000, NULL, 1, NULL );
    	xTaskCreate(vTask3, "Task 3", 1000, NULL, 1, NULL);
    
    
    	/* Start the scheduler to start the tasks executing. */
    	vTaskStartScheduler();	
    
    
    	for( ;; );
    	return 0;
    }
    
    
    
    
    void vTask1( void *pvParameters )
    {
    const char *pcTaskName = "Task 1 is running\r\n";
    
    
    	for( ;; )
    	{
    		/* Print out the name of this task. */
    		vPrintString( pcTaskName );
    		
    	}
    }
    
    
    
    
    void vTask2( void *pvParameters )
    {
    const char *pcTaskName = "Task 2 is running\r\n";
    
    
    	for( ;; )
    	{
    		/* Print out the name of this task. */
    		vPrintString( pcTaskName );
    		
    	}
    }
    
    
    void vTask3(void* pvParameters)
    {
    	const char* pcTaskName = "Task 3 is running\r\n";
    
    
    	for (;; )
    	{
    		/* Print out the name of this task. */
    		vPrintString(pcTaskName);
    
    
    	}
    }
    Task printing result

    Code:
    Task 1 is runningTask 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 3 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 1 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running
    Task 2 is running

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    Huh?
    It goes 1,2,3,1,2,3,1,2,3 and so on.

    What did you expect to see?

    You're not going to see exactly equal numbers of each task printed.

    For a start, it's a fair bet that vPrintString() has an internal semaphore to ensure that a complete message is printed.
    If tasks could be swapped mid string, you'd see things like
    TaTask 2 is runningsk 1 is running

    Also, sending strings over a slow serial port involves an awful lot of delay, which is somewhat dependent on how quickly your host machine can pull them off the serial line and print them in your console.

    Similarly, your clock interrupt which triggers potential context switches within the RTOS is not going to be exact to the nS.

    Consider two tasks which have no dependency on the outside world at all.
    Code:
    long long int count1 = 0, count2 = 0;
    void vTask1( void *pvParameters )
    {
        volatile uint32_t ul;
        /* As per most tasks, this task is implemented in an infinite loop. */
        for( ;; )
        {
            char buff[100];
            long long int c1 = count1, c2 = count2;
            long long int diff = c1 - c2;
            sprintf(buff,"T1=%lld T2=%lld\n", c1, c2, diff);
            vPrintString(buff);
     
            /* Delay for a period. */
            for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
            {
                /* This loop is just a very crude delay implementation.  There is
                nothing to do in here.  Later exercises will replace this crude
                loop with a proper delay/sleep function. */
            }
        }
    }
    
    void vTask2( void *pvParameters )
    {
        for( ;; )
        {
            count1++;
        }
    }
     
    void vTask3(void* pvParameters)
    {
        for (;; )
        {
            count2++;
        }
    }
    On a perfect machine, diff will always be zero.
    But in the real world - well, try it!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (FOSS) RTOS opinions/advice
    By Epy in forum Tech Board
    Replies: 3
    Last Post: 06-15-2016, 09:46 AM
  2. RTOS for uC
    By MartinR in forum C Programming
    Replies: 2
    Last Post: 12-14-2015, 08:36 AM
  3. how to install an rtos at naked hardware
    By KIBO in forum Tech Board
    Replies: 2
    Last Post: 03-23-2010, 10:28 AM
  4. RTOS, LUTs and interrupts
    By holychicken in forum C Programming
    Replies: 12
    Last Post: 07-23-2008, 10:43 AM
  5. modifying ping( ) in RTOS
    By chrispyone in forum C Programming
    Replies: 1
    Last Post: 06-30-2005, 12:41 PM

Tags for this Thread