[Home]Start With LXRT

Last edit: Peter Favrholdt on October 14, 2006 22:40 (5090 days, 5 hours and 20 minutes ago) (diff)
Rtai.Dk | RecentChanges | Preferences | DIAPM RTAI

I'm sure the following part is not perfect, but I hope it will help beginners to do their first steps. If there are faults, please feel free to correct them!

The following steps show how to write a simple main() skeleton.

In your main program you will need a few includes, for instance:

#include        <stdio.h>
#include        <errno.h>
#include        <pthread.h>
#include        <unistd.h>
#include        <sys/types.h>
#include        <sys/mman.h>
#include        <pthread.h>
#include        <signal.h>
#include        <rtai_lxrt_user.h>
#include        <rtai_bits.h>

Note: you should not include rtai.h (which is used only for kernel modules).

Then let's go to main():

int main( int siArgC, char *pcArgV[] )
  printf( "\nmain is running!\n" );

First let's initialize the RTAI scheduler. This is valid for the whole system:

  /* Init RTAI Scheduler */

Now we have to start the timer and the scheduler:

  /* Start of the timer (and the scheduler)
     "0" as parameter because we use the oneshot mode and so 
     the amount of the period is not relevant */
  start_rt_timer( 0 );

Linux must not perform paging - it whould break the realtime!

  /* No paging is allowed! */
  mlockall( MCL_CURRENT | MCL_FUTURE );

Now we have to get a buddy for main(): By the function "rt_task_init_schmod()" the buddy task for the current posix thread is created and for the posix thread the scheduler is switched to work in FIFO mode. Due this it has a higher priority than all linux tasks and the scheduling is done in FIFO (not round robin) mode. The priority within the posix threads is also defined.

Due to the generation of the buddy the posix thread is made a LXRT task. The pointer to the RT_TASK structure "pstMainBuddy" has to be defined as: "static RT_TASK *pstMainBuddy;".

  /* Create the buddy of the main task */
  pstMainBuddy = rt_task_init_schmod( nam2num( "MAINB" ),   /* Name           */
                                      MAIN_PRIORITY,        /* Priority       */
                                      0,                    /* Stack Size     */
                                      0,                    /* max_msg_size   */
                                      SCHED_FIFO,           /* Policy         */
                                      0 );                  /* cpus_allowed   */

  if( pstMainBuddy == NULL)
    fprintf( stderr, "%s: Error creating the main buddy!\n", pcArgV[0] );
    exit( 1 );

Now you can implement your own stuff (creating other tasks and so on)

After this, if main() comes to its end some day ... ;-)

  if( pstMainBuddy )
    rt_task_delete( pstMainBuddy );
  sleep( 1 );
  printf( "Program %s will be finished!\n", pcArgV[0] );
  sleep( 2 );
  return( 0 );
} /* End of main() */   

To compile the above use save the source in any file say sample.c

> gcc $CFLAGS $LDFLAGS sample.c -o sample

where $CFLAGS can be obtained by running

> rtai-config --lxrt-cflags


by running

> rtai-config --lxrt-ldflags

Once the program is compiled. The best way to run the program is to use rtai-load. To use rtai-load we have to define a file .runinfo in the same directory where the sample executable exists.

To see the format of .runinfo see the /usr/share/doc/rtai-doc/ (for debian)

Edit text of this page | View other revisions | Download Rtai.dk