RTLinux Application Development Tutorial
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 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 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 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:
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: helloThis 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.
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 firstname.lastname@example.org.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- Paranoid Penguin - Building a Secure Squid Web Proxy, Part IV
- SUSE LLC's SUSE Manager
- Google's SwiftShader Released
- Managing Linux Using Puppet
- My +1 Sword of Productivity
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Non-Linux FOSS: Caffeine!
- SuperTuxKart 0.9.2 Released
- Parsing an RSS News Feed with a Bash Script
- Doing for User Space What We Did for Kernel Space
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide