RTLinux Application Development Tutorial

 in
Here's how to get started developing the code for your next high-altitude atmospheric research project, or anything else where a hard real-time task needs to communicate with other software.
The User-Space Code

This section is very simple. The user-space code consists of a small C application that interacts with a couple of files, doing reads and writes. These reads and writes to the FIFOs direct the real-time threads, controlling which one is in charge of ``hello'' and ``world'', eventually triggering a shutdown.

Listing 4. A Normal User-Space Application

Listing 4 shows a normal user-space application. A few header files are needed, including our common definitions with the real-time code, along with other normal I/O headers. We declare a few variables and then open the real-time FIFOs. They are treated as normal files, so it's a simple matter of using normal open() calls on the /dev/rtf* entries. If you compare the FIFO numbers with the real-time code, you will see that /dev/rtf0 is the control FIFO, while 3 and 4 are the two channels to the real-time threads:

     msg.task = 0;
     msg.command = HELLO;
     write(ctl, &msg, sizeof(msg));
     msg.task = 1;
     msg.command = WORLD;
     write(ctl, &msg, sizeof(msg));
     hello_thread = 0;

If you refer back to the real-time code, we started the real-time threads in a stalled state, so that they don't actually do anything. The first thing we need to do is write commands to the control FIFO to start them up. To do this, we fill out a structure for thread 0, with a command value of HELLO, so that it will be told to write ``hello''. Then the same is done for thread 1, telling it to print ``world''. For future reference, we remember that thread 0 is in charge of ``hello''.

Listing 5. The Main Loop

Listing 5 shows the main loop, and there aren't any real surprises. As there are two files we are watching, we set up a normal file descriptor set for select() to work with. The real-time code is running every half-second; here we sample more often, just to keep things simple. In a real environment where the intervals are tighter (tens of milliseconds or less), the data might be timestamped or transported in a different way.

Here, however, it's simpler to just poll for data. Once we read it, the loop dumps the string, and the FIFO it came from, to stdout. Just to demonstrate interactive FIFO handling, the code talks to the control FIFO every 20 iterations, telling the real-time threads to switch roles:

     msg.command = STOP;
     msg.task = 0;
     write(ctl, &msg, sizeof(msg));
     msg.task = 1;
     write(ctl, &msg, sizeof(msg));
     close(fd0);
     close(fd1);
     close(ctl);
     return 0;
}

Once the main routine completes, there is one more step needed to shut down cleanly. The real-time threads need to be turned off. If we failed to do this and just exited, nothing bad would happen, other than the real-time code would continuously overwrite the FIFO buffers. Instead, we send a stop command to the control FIFO so that the threads stop working. As we will see in the demonstration, the module still will be loaded, but it will no longer be causing any significant resource utilization.

Running the Example

Running the example is very simple and is no different from any of the other example programs that come with RTLinux. As mentioned, we assume that at this point the normal RTLinux modules are loaded into the kernel. First, you need to load the real-time code:

insmod thread_mod.o

Now the real-time code is up and running. In the real-time kernel, the threads already are configured as periodic and are executing, just not generating anything useful. Now, start the user-space application:

./thread_app
FIFO 1: hello
FIFO 2: world
FIFO 1: hello
...
FIFO 1: world
FIFO 2: hello
This will continue until the user-space code completes 1,000 reads from the file descriptor set. Note that the FIFO outputs will reverse as the code directs the control thread to reverse the roles. This may not happen completely in sync, as the real-time kernel is under no obligation to handle user-space code if it doesn't have time. This means that the code to switch roles might only get through the first command, but not the one directed at the second thread. This does introduce potential complexity, but delaying user code in deference to real-time operation is rarely a bad thing, if ever.

Conclusion

That concludes a simple introduction to RTLinux, its concepts, API and a short example. It is a lot to digest at once but should serve only as a starting point. For more information on RTLinux, RTLinuxPro and other FSMLabs products, check out http://www.fsmlabs.com/.

Matt Sherer works for FSMLabs, Inc., developers of RTLinux. He lives in Socorro, New Mexico, where he tries to maintain a balance between writing, coding and enjoying the Land of Enchantment. Matt can be reached at sherer@fsmlabs.com.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

doubt in rtlinux coding part

vsagar's picture

IN RTLINUX 2.4.8-rtl while devloping the module it contains sin,cos,pow,exp functions are used, eventhough we included math.h libray,im getting unresolved symbols for cos,sin,pow,exp funtions in exicution.what is problem,
and in reltime fifos are unable commnicate the linux process and rtl process, even i hav use rtf_create call.

i will wait for ur valuable solutions.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState