Real-Time Applications with RTLinux

RTLinux gives you a hard real-time, low-level environment when you need it--and all the power and flexibility of Linux when you don't.
Listing 2. Using the rtlinux_sigaction Function
.
.
.
#include <rtlinux_signal.h>
#define IRQ 7
void my_handler(int);
struct rtlinux_sigaction sig, oldsig;
float old_time=0.0;
float new_time=0.0;
float omega=10.0;
        /* spin speed */
int main(void)
{
     old_time = <sampleclock>;
     /* capture IRQ 7 and execute my_handler
        each time that IRQ 7 arrives: */
     sig.sa_handler = my_handler;
     sig.sa_flags = RTLINUX_SA_PERIODIC;
     rtlinux_sigaction( irq, & sig, & oldsig )
                                                      
    /* the main part of our program: we wish to
       plot information as long as the rotor is
       still spinning */
    while(omega>1.0){
        sleep(1);
        printf("Omega = %.1f\n",omega); 
              /* to stdout */
        plot(old_time,omega);
             /* via fancy plotting package*/
    }
    /* We are no longer spinning,
       let's clean up after ourselves... */
    /* free the irq: */
    sig.sa_handler = RTLINUX_SIG_IGN;
    rtlinux_sigaction( IRQ, & sig, & oldsig );
    /* exit gracefully */
    return 0;
}
void my_handler(int argument)
{
     /* calculate spin speed here */
new_time= <sampleclock>;
     omega = 1.0/(new_time - old_time);
     old_time = new_time;
}

Function

rtlinux_sigaction(), identifies the function my_handler() as the function that we wish to execute each time that IRQ 7 is triggered. Note that ``RTLINUX_SA_PERIODIC'' tells rtlinux_sigaction() to reset itself and wait for the next signal over and over again--otherwise the signal handler would be executed exactly once. Then the while() loop in our program both prints out and plots the latest spin speed. Finally, when the spin speed drops to below 1Hz, the program begins the shutdown process, which involves the deregistering my_handler() as our signal handler.

The job of my_handler() is straightforward: calculate the spin speed. The accuracy of this calculation should be quite high because each time that IRQ 7 is triggered, the handler is called as quickly as the underlying hardware allows.

Regardless of which scheme we use to implement Task 2, the most important thing to note is the amazing versatility and elegance that the RTLinux programming environment provides.

Conclusion

The design compromises that make Linux such a powerful general-purpose OS render it less than ideal for hard and even soft real-time applications. By decoupling real-time and non-real-time processes, RTLinux harnesses the best of both worlds: on the one hand, it offers a high-performance, strictly deterministic real-time application environment, while on the other, it offers the rich programming environment, large application and user base, and powerful networking power of Linux. Most importantly, all improvements made to Linux by its huge development community become instantly available to RTLinux users.

RTLinux is open-source software distributed under the GPL. Further information is available at the FSMLabs web site (http://www.fsmlabs.com/downloads.html) and the software is freely available for download from the FSMLabs ftp server (ftp://ftp.fsmlabs.com/pub/rtlinux/v3/). There are working versions for x86 (uni-processor and SMP), Alpha, PowerPC (uni-processor only), and PC-104 (via miniRTL). Both source and RPM packages are available.

______________________

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix