#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Routine calling routine...


    Hi. Please see the code below:

    Code:
    void task1(void);
    void task2(void);
    
    void main()
    {
    	task1();
    
    	while(1);
    }		
    
    void task1()
    {
    	static unsigned int i=0;
    	while(1)
    	{
    		NOP();
    		i++;
    		if(i>254)
    			while(1);
    		task2();
    	}
    }
    
    void task2()
    {
    	static unsigned int j=0;
    	while(1)
    	{
    		NOP();
    		j++;
    		if(j>254)
    			while(1);
    		task1();
    	}
    }
    The problem: both i and j only got to 29 then the program ended.
    Why?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    What's to say?

    None of the loops do anything useful.

    It's just task1() task2() task1() task2() until you run out of stack and the whole thing just blows up.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    What's to say?

    None of the loops do anything useful.

    It's just task1() task2() task1() task2() until you run out of stack and the whole thing just blows up.
    The whole reason for what I am doing is to "simulate" what RTOS are doing. Most RTOS have while(1) in their task routines within which other routine(s) are called in this simular way. So if it is a stack issue, how is that resolved?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Well in an RTOS (or any OS for that matter), tasks are not responsible for switching between themselves.

    Here, spawn() creates a separate stack, and the named function is an entry point.
    The schedule() function is then called to start the first task, and thereafter periodically switch between tasks.
    Note: These are both imaginary functions - you can't just write this and expect it to work, without providing a lot of implementation.
    Code:
    void task1(void);
    void task2(void);
    
    void main()
    {
    	spawn(task1);
    	spawn(task2);
    	schedule();
    }		
    
    void task1()
    {
    	static unsigned int i=0;
    	while(1)
    	{
    		NOP();
    		i++;
    	}
    }
    
    void task2()
    {
    	static unsigned int j=0;
    	while(1)
    	{
    		NOP();
    		j++;
    	}
    }
    If you want a reasonable simulation without a lot of effort on your part, then look at threads.
    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

IMN logo majestic logo threadwatch logo seochat tools logo