1. boot: Rtai 2. username/password: rtai 3. startx

--> Notice: after log off, data erased --> Bring your own USB stick if want to save files

IDEA BEHIND RTAI: [credits: Harco Kuppens, www.cs.ru.nl/lab/rtai/]

A real-time scheduler replaces the original scheduler, and

- intercepts the timer interrupt and external device interrupts, - runs any real-time code associated with these, and - runs any normal Linux processes in the time left over.

Installing RTAI requires patching the (patches available for certain versions)

RTAI Linux tasks are "kernel modules," meaning they run as part of the privileged Linux kernel, similar to device drivers.

- rtai (core module) - rtai_sched (real time scheduler module: occurs when tasks perform certain system calls and on timer handler activation - each 8254 interrupt. Given a priority level, the first initialized task will be the first elected and will run to completion unless a task with a higher priority is elected or it terminates or the task calls a blocking system function. Priorities: 0 (highest) to 0x3fffFfff (lowest). Linux is given priority 0x7fffFfff. - rtai_fifos: IPC - rtai_shm: allows sharing memory among different real time tasks and Linux processes - lxrt: implements services to make available any of the RTAI schedulers functions to Linux processes - rtai_pqueue: kernel-safe message queues - rtai_pthread: provides hard real-time threads, where each thread is a RTAI task - rtai_utils

--> see them in /usr/realtime/modules

Kernel modules, like device drivers, execute in a primitive environment, without direct access to many user-level Linux facilities like terminal or file I/O.

Any reasonably large real-time application will want these features, and these applications typically are split between real-time and non-real-time parts, with the non-real-time parts using all the nice Linux features. Communication between the two parts can be accomplished numerous ways, using shared memory, first-in, first-out (FIFO) queues and other communication pathways we will describe later. This split complicates RT Linux programming, in comparison with other real- time operating systems in which everything runs in real-time. On the other hand, the simpler environment of kernel modules makes RT Linux quite fast.

WARNING: Errors may crash the system (running in privileged mode) 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)

--> use "sudo" to run a program with administrator privileges

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 kernel object '.ko' 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.

--> SAMPLE KERNEL MODULE

/* hello.c */ #include #include MODULE_LICENSE("GPL"); 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; }

--> note printk()

--> COMPILING AND RUNNING A KERNEL MODULE

In the newer linux versions, kernel modules are built using the kernel build system 'kbuild', which uses a so-called 'Kbuild' makefile to make a module

1. mkdir example cd example 2. wget http://www.cs.ru.nl/lab/rtai/exercises/1.Helloworld/hello.c 3. try useful shell commands pwd uname -r

4. nano Kbuild in the Kbuild file, place instructions about how to compile: - where are Linux kernel source tree [/lib/modules/...] - what target [modules:] - what object files are needed [.c -> .o] - which compiler to use [$(MAKE)]

--> standard format

# Kbuild file

KDIR ?= /lib/modules/`uname -r`/build modules: make -C $(KDIR) M=`pwd` modules obj-m := hello.o

--> Notice that after "modules:" there is new line and the next line must start with a TAB

5. make -f Kbuild 6. sudo insmod hello.ko 7. sudo rmmod hello.ko 8. dmesg

9. tail -f /var/log/kern.log

10. sudo insmod /usr/realtime/modules/rtai_hal.ko sudo insmod /usr/realtime/modules/rtai_sched.ko

11. lsmod lsmod | grep rtai

12. cd /usr/realtime/testsuite cd kern/latency sudo ./run