[previous]
[index]
[next]
The Basics of Real-Time Linux
RT Linux Tasks are Kernel Modules, not Linux Programs
Kernel Modules are Dynamically Loaded
- Kernel modules are dynamically loaded using the 'insmod'
insert module program, and
- unloaded (stopped), using the 'rmmod' "remove module"
program.
- These programs are only available to the root user
(administrator), although there are ways to avoid giving real-time
programmers true root access to the system.
C is the Preferred Language
- Linux is written in the C programming language (with some assembly
language), as are RTAI and RTL, so C is the preferred language for
writing RT Linux programs.
- Other
languages (e.g., C++) can be used, but there are caveats that make C
the much-preferred language for RT Linux development.
- The examples in
this tutorial are all written in C.
- C programs are normally compiled into full executable programs,
but kernel modules are compiled into object code, with final linking
suppressed.
- Instead of a full-blown executable, your code will be
a loadable object '.o' "dot-oh" file,
- possibly
the result of linking together several other '.o' files if your
project is split into numerous
files for convenience or clarity.
- In C, a program's "entry point" where execution begins is a function
called 'main()'.
- For a kernel module, this
entry point is called 'init_module()'.
- 'insmod' looks for this symbol
when loading the code.
- A program's "exit point" is a function called 'cleanup_module()'. This
will be called when 'rmmod' removes the kernel module.
Here is the minimal C code that illustrates this:
/* simple.c */
#define __KERNEL__
#define MODULE
#include <linux/kernel.h>
#include <linux/module.h>
int init_module(void)
{
printk("hello, world!\n"); /* printk = kernel printf, to the console */
return 0;
}
void cleanup_module(void)
{
printk("goodbye, world!\n");
return;
}
/* end of simple.c */
The Mechanics of Compiling and Running
- The mechanics of compiling C code vary depending upon which compiler
you use.
- The Free Software Foundation's Gnu C compiler 'gcc' is installed with most Linux
distributions.
- With 'gcc', to compile this
example you would do:
gcc -c simple.c
- The '-c' "dash-c" means don't compile to
a full-blown executable, just leave it as the loadable object file 'simple.o'.
- To run this, you would use the 'insmod' program:
insmod simple.o
- To stop this, you would use the 'rmmod' program, passing the name of
the module without the '.o' suffix:
rmmod simple
- Note that the output of the 'printk()' calls in simple.c appears in the
main console, which may not be where you ran the
program, especially if you're running in the windowing environment, X
Windows.
- You can get to the main console window with Control-Alt-F1 on
most Linuxes, where the other function keys toggle between various
other consoles, and X Windows.
Next: Example 1, Pure Periodic Scheduling of a Single Task
Back: Start of Tutorial