06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2. Fundamentals<br />

Lack of reentrance is common problem in badly written C programs. The<br />

reason is the use of static variables, i.e., variables that are allocated on a fixed<br />

place in memory instead of on the stack or on the heap.<br />

In most run-time systems, local variables in functions and blocks are allocated<br />

(pushed) on the stack, which means that they are deallocated (popped<br />

from the stack) when the PC leaves the scope. Data created dynamically,<br />

using operator new in Java or C++, are allocated on the heap which is sometimes<br />

called the free store. But also for heap allocation we usually have the<br />

reference to the object declared locally in some scope which means that it is<br />

put on the stack. This is a proper programming style which means that all<br />

data is pushed on the stack of the caller. If, on the other hand, we use static<br />

variables, two callers of one function will access the same local data (referred<br />

to by absolute addresses in the compiled code of the function).<br />

The real problem is when the one who wrote the function is not aware<br />

of concurrent programming. Example: A numerical equation solver was implemented<br />

in C, and then it was packaged together with other routines in a<br />

library. To optimize the code for the hardware at that time, the temporary<br />

variables of the algorithm were declared static. The function was then successfully<br />

used for years on an old-style UNIX machine, where each program<br />

only contains one point of execution (except for the signal handlers). Then,<br />

the library was compiled and linked with applications running on the newer so<br />

called multi-threaded UNIX dialects, such as Sun Solaris, HP-UX 10 and later,<br />

and later versions of IRIX from SGI. It was also used on the Microsoft Win32<br />

platform. In all these cases, when using the equation solver concurrently from<br />

different parts of the application, the results were sometimes wrong. In this<br />

case, the function was correct as a program, but not correct when used in a<br />

concurrent program where some interleavings resulted in undesired mixing of<br />

temporary data from different equations.<br />

Also in Java it is easy to declare static variables, but we should only do<br />

that for so called class variables needed for the structure of our program, and<br />

in that case we should treat them as other types of shared data (which we will<br />

return to). Note that when you write an ordinary function, you cannot know<br />

if it later will be used concurrently. In conclusion, one should learn concurrent<br />

programming before doing any type of professional programming.<br />

2.6 Models of concurrent execution<br />

The concurrency issues made our simple sequential execution model (from<br />

Section 2.1) more complex because we now have several sequences that interact<br />

with each other. With each sequence, there are:<br />

• A PC (the Program Counter) which according to the previous section<br />

refers to the next machine instruction to be executed.<br />

30 2012-08-29 16:05

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!