<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Indeed. A quick look at 'stanford.c' reveals that there are plenty
of globals.<br>
You have two options:<br>
- add a semaphore that protects the global variables and serialize
access to<br>
them, e.g., by taking the semaphore before calling 'stanford()'
and releasing<br>
it after return. This assumes that it is OK to share the global
state among<br>
multiple tasks.<br>
- modify 'stanford.c' to make it reentrant. Move all global
variables to a 'struct'<br>
and pass a pointer to this 'context information' to 'stanford()'.
Each task has<br>
it's own copy/instance of such a context. You probably also need
to add a routine<br>
for initializing a context.<br>
<br>
Note: IIRC rtems/newlib's 'rand()' is thread-safe, i.e., every
thread has it's own<br>
'rand-state'. Otherwise you'd have to think about that one, too.<br>
<br>
HTH<br>
-- Till<br>
<br>
On 10/18/2011 08:53 PM, Gedare Bloom wrote:
<blockquote
cite="mid:CAC82fA2BBvX0GbeT5aQ1x2=2EQffCAzNefstdHFyZOGEKqSy2w@mail.gmail.com"
type="cite">
<p>Hi,</p>
<p>The most likely answer is that global variables in the
stanford.c file are shared and not protected e.g. by semaphores.</p>
<div class="gmail_quote">On Oct 18, 2011 5:31 PM, "Constantine
"chicky" Giotopoulos" <<a
moz-do-not-send="true"
href="mailto:kotsosgiotopoulos@gmail.com">kotsosgiotopoulos@gmail.com</a>>
wrote:<br type="attribution">
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
<div class="gmail_quote">
<div class="gmail_quote">Hello everyone.<br>
<br>
I am using RTEMS to create a number of tasks and run a set
of functions performing mostly mathematical operations.
These are included in a file named "stanford.c" ( <a
moz-do-not-send="true"
href="http://classes.engineering.wustl.edu/cse465/docs/BCCExamples/stanford.c"
target="_blank">http://classes.engineering.wustl.edu/cse465/docs/BCCExamples/stanford.c</a>
). The problem is that when I enable preemption, for some
reason the functions do not output the expected results.</div>
<div class="gmail_quote"> </div>
<div class="gmail_quote">I initially create 100 tasks using
the following lines of code:</div>
<div class="gmail_quote">----------------------------------------------------------------------------------------------------<br>
rtems_task Init( rtems_task_argument ignored )<br>
{<br>
rtems_status_code status; rtems_id id;<br>
int i;<br>
<br>
for (i=0;i<100;i++)<br>
{<br>
srand(i*3); <br>
<br>
status = rtems_task_create(<br>
rtems_build_name( 'T', 'A', '2', ' ' ), rand()%254+1,<br>
RTEMS_MINIMUM_STACK_SIZE, RTEMS_DEFAULT_MODES,<br>
RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
&id<br>
);<br>
assert( !status );<br>
<br>
status = rtems_task_start( id, task2, id%1000 );<br>
assert( !status );<br>
}<br>
<br>
status = rtems_task_delete( RTEMS_SELF );<br>
<br>
exit( 0 );<br>
} <br>
-----------------------------------------------------------------------------------------------------------------------------<br>
Then I state the functionality of the tasks.Each task
executes the functions included in the "stanford.c" 5
times using a "for" loop. Each task is triggered with a
"wake_after" directive that is set randomly before each
execution and the priority is changed also randomly after
each execution.The point in that is to get various tasks
triggering at a random manner and taking over the CPU in a
(somewwhat) random way:<br>
---------------------------------------------------------------------------------------------------------------------------<br>
rtems_task task2( rtems_task_argument id_arg )<br>
{<br>
rtems_status_code status;<br>
int i;<br>
<br>
for (i=0;i<5;i++)<br>
{<br>
srand(id_arg*i);<br>
rtems_task_wake_after(rand()%1000);<br>
printf("Task2-stanford with id=%d is executing.
Iteration:%d\n", id_arg, i);<br>
stanford(); <br>
printf("Task2-stanford with id=%d iteration:%d has
finished.\n\n\n", id_arg, i);<br>
<br>
rtems_task_set_priority(RTEMS_SELF, rand()%254+1,
1);<br>
}<br>
rtems_task_delete( RTEMS_SELF );<br>
}<br>
<br>
My configuration options are:<br>
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER<br>
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER<br>
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE<br>
#define CONFIGURE_MICROSECONDS_PER_TICK 100<br>
#define CONFIGURE_MAXIMUM_TASKS 105<br>
#define CONFIGURE_INIT<br>
#include <rtems/confdefs.h><br>
--------------------------------------------------------------------------------------------------------------------------</div>
<div class="gmail_quote">I have attached the code that is
being executed (test.c-makefile)<br>
<br>
If I enable non-preemption, then the execution output is
as expected, no error messages or anything out of the
ordinary, for example:<br>
------------------------------------------------------------------------------------------------------------------------<br>
Task2-stanford with id=698 is executing. Iteration:0 <br>
Starting <br>
Perm Towers Queens Intmm Mm Puzzle Quick
Bubble Tree FFT <br>
0 0 0 0 0 0
0 0 0 0 <br>
<br>
Nonfloating point composite is 0 <br>
<br>
Floating point composite is 0 <br>
Task2-stanford with id=698 iteration:0 has finished. <br>
<br>
<br>
Task2-stanford with id=714 is executing. Iteration:0 <br>
Starting <br>
Perm Towers Queens Intmm Mm Puzzle Quick
Bubble Tree FFT <br>
0 0 0 0 0 0
0 0 0 0 <br>
<br>
Nonfloating point composite is 0 <br>
<br>
Floating point composite is 0 <br>
Task2-stanford with id=714 iteration:0 has finished. <br>
<br>
<br>
Task2-stanford with id=730 is executing. Iteration:0 <br>
Starting <br>
Perm Towers Queens Intmm Mm Puzzle Quick
Bubble Tree FFT <br>
0 0 0 0 0 0
0 0 0 0 <br>
<br>
Nonfloating point composite is 0 <br>
<br>
Floating point composite is 0 <br>
Task2-stanford with id=730 iteration:0 has finished.<br>
<br>
However, when I enable preemption, tasks start
interrupting each other (depending on their
priority). Especially when it comes to functions that use
recursion, I am presented with errors. These do not occur
everytime these functions are executed, but occasionally.
These errors occur mostly when a task is interrupted by
another task(s) of higher priority. The error messages are
somewhat like that:<br>
" Perm Towers Error in Towers: nothing to pop <br>
Error in Towers: nothing to pop <br>
Error in Towers: disc size error <br>
Error in Towers: disc size error <br>
Error in Towers: nothing to pop <br>
Error in Towers: nothing to pop <br>
Error in Towers: disc size error"<br>
<br>
"Task2-stanford with id=714 is executing. Iteration:2 <br>
Starting <br>
Task2-stanford with id=745 is executing. Iteration:3 <br>
Starting <br>
Perm Towers Queens Intmm Mm Puzzle Quick
Bubble Tree FFT <br>
0 0 0 0 0 0
0 0 0 0 <br>
<br>
Nonfloating point composite is 0 <br>
<br>
Floating point composite is 0 <br>
Task2-stanford with id=745 iteration:3 has finished. <br>
<br>
<br>
<strong> Perm Error in Perm. <br>
</strong> Towers Queens Intmm Mm Puzzle Quick
Bubble Tree FFT <br>
0 0 0 0 0 0
0 0 0 0 <br>
<br>
Nonfloating point composite is 0 <br>
<br>
Floating point composite is 0 <br>
Task2-stanford with id=714 iteration:2 has finished. <br>
<br>
<br>
<b> Perm Error in Perm. </b><br>
Task2-stanford with id=714 is executing. Iteration:3 <br>
Starting <br>
Perm Towers Queens Intmm Mm Puzzle Quick
Bubble Tree FFT <br>
0 0 0 0 0 0
0 0 0 0 <br>
<br>
Nonfloating point composite is 0 <br>
<br>
Floating point composite is 0 <br>
Task2-stanford with id=714 iteration:3 has finished."<br>
-------------------------------------------------------------------------------------------------------------------------------------------------<br>
<br>
My question is, why are these errors produced when I
enable preemption and are not produced when I disable
preemption? Does the fact that this concerns only the
recursive functions have to do anything with the problem?
Could for some reason, when there is a context switch, the
data in the registers of the task being interrupted are
not stored correctly in the memory? Or that they are not
being correctly transfered back to the registers when it's
their turn to be executed again? <br>
<br>
Or could it be a memory issue? I have tried initializing
the tasks with (RTEMS_MINIMUM_STACK_SIZE * 2) but the
errors keep coming. I also tried to allocate more memory
to the task stack by using the
"CONFIGURE_EXTRA_TASK_STACKS", but when I checked if with
the "rtems_stack_checker_report_usage" directive, for some
reason it didn't have an impact at the available memory
being used by the tasks.<br>
<br>
What I need, is for the program to execute with preemption
enabled without presenting these errors.<br>
What am I missing out? Are there any options I could
utilize, or debugging with gdb is the way to continue (I'd
like to avoid that if possible)?<br>
<br>
Thank you in advance.<br>
</div>
<br>
</div>
<br>
<br>
_______________________________________________<br>
rtems-users mailing list<br>
<a moz-do-not-send="true" href="mailto:rtems-users@rtems.org">rtems-users@rtems.org</a><br>
<a moz-do-not-send="true"
href="http://www.rtems.org/mailman/listinfo/rtems-users"
target="_blank">http://www.rtems.org/mailman/listinfo/rtems-users</a><br>
<br>
</blockquote>
</div>
</blockquote>
<br>
</body>
</html>