C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 24th, 2012, 02:32 PM
dtvonly dtvonly is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 8 dtvonly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m
Reputation 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?

Reply With Quote
  #2  
Old August 24th, 2012, 02:45 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old August 24th, 2012, 06:23 PM
dtvonly dtvonly is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 8 dtvonly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m
Reputation Power: 0
Quote:
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?

Reply With Quote
  #4  
Old August 24th, 2012, 11:42 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Routine calling routine...

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap