210 likes | 531 Views
How to write a RTLinux program. Real-Time System Project g916704 徐 瑩 g916303 余坤庭 g916703 吳建達. How to write a RTLinux program. 0.Introduction RTLinux 1.How To Setup RTLinux Environment 2.Write a RTLinux program. Introduction RTLinux. Introduction RTLinux.
E N D
How to write a RTLinux program Real-Time System Project g916704 徐 瑩 g916303 余坤庭 g916703 吳建達
How to write a RTLinux program 0.Introduction RTLinux 1.How To Setup RTLinux Environment 2.Write a RTLinux program
Introduction RTLinux 1.RTLinux is a hard realtime operation system. 2.Virtual Machine Layer between RTLinux Kernel and standard Linux Kernel. (Intercept all hardware interrupts) 3. Hardware interrupt RTLinux Kernel(idle) software interruptsLinux kernel(running)
Introduction RTLinux 4.RTLinux owns fixed-priority scheduler, and assigns the lowest priority to the standard Linux kernel. 5.All tasks in RTLinux: (1)direct access to hardware (2)do not use virtual memory 6.RTLinux manages to convert the existing Linux kernel into a hard realtime environment without hindering futrue Linux development.
How To Setup RTLinux Environment 1.How To Setup RTLinux Environment (C:\rtlinux-3.1\doc\html\Installation\Installation.html)
DownLoad and UnPack RTLinux • Download rtlinux source • Example: ftp://debella.ikk.sztaki.hu/mirrors/ftp.fsmlabs.com/pub/rtlinux/v3/ rtlinux-3.1.tar.gz • Unpack rtlinux-3.1.tar.gz • tar zxvf rtlinux-3.1.tar.gz • mv rtlinux-3.1 /usr/src/ • Check the Linux kernel version • kernel_patch-2.4.4(uname -a)
Download and Unpack Linux Kernel • Download kernel source • http://www.kernel.org • Example: ftp://ftp.kernel.org/pub/linux/kernel/v2.4/linux-2.4.4.tar.bz2 • Unpack kernel source • bunzip2 linux-2.4.4.tar.bz2 • tar xvf linux-2.4.4.tar • mv linux /usr/src/
Patch and Build Kernel • Patch the kernel • cd /usr/src/linux • patch –p1 < /usr/src/rtlinux-3.1/kernel_patch-2.4.4 • Build the new kernel • make config / menuconfig / xconfig • make dep • make bzImage • make modules • make modules_install • cp arch/i386/boot/bzImage /boot/rtzImage-2.4.4
Modify Boot Loader Configuration • Lilo: /etc/lilo.conf • Image=/boot/vmlinuz-2.4.18-0.13(old) label=linux(old) Initrd=/boot/initrd-2.4.18-0.13.img Read-only Root=/dev/hda2 • Image=/boot/rtzImage-2.4.4(new) label=rtlinux(new) Initrd=/boot/initrd-2.4.18-0.13.img Read-only Root=/dev/hda2 Execute /sbin/lilo !!!Restart computer by typing:/sbin/shutdown –r now
Compile RTLinux • Compile RTLinux • cd /usr/src/rtlinux-3.1 • make config / menuconfig / xconfig • make • make devices • make install Congratulations!
How to write a RTLinux program 2.Write a RTLinux program (C:~\rtlinux-3.1\examples\hello.c)
Examples – Hello.c #include <rtl.h> #include <time.h> #include <pthread.h> pthread_t thread; void * start_routine(void *arg){ struct sched_param p; p . sched_priority = 1; pthread_setschedparam (pthread_self(), SCHED_FIFO, &p); pthread_make_periodic_np (pthread_self(), gethrtime(), 500000000); while (1) { pthread_wait_np(); rtl_printf("I'm here; my arg is %x\n", (unsigned) arg); } return 0;}
Hello.c(con’t) int init_module(void) { return pthread_create (&thread, NULL, start_routine, 0); } void cleanup_module(void) { pthread_cancel (thread); pthread_join (thread, NULL); }
Write a RTLinux program 1.Creating RTLinux Threads 2.Cancel a Thread 3.Time Facilities 4.Scheduling Threads
1.Creating RTLinux Threads (1)A realtime application is usually composed of several ``threads'' of execution. (2)To create a new realtime thread, we use the pthread_create() function. This function must only be called from the Linux kernel thread (i.e., using init_module()): (3)#include <pthread.h> int pthread_create(pthread_t * thread, pthread_attr_t * attr, void *(*start_routine)(void *), void * arg);
2.Cancel a Thread • To cancel a thread : • pthread_cancel(pthread_t thread); pthread_join(pthread_t thread, void**thread_return) (resource deallocated) • pthread_delete_np(pthread_t thread) 1.You should join the thread in cleanup_module with pthread_join() for its resources to be deallocated. 2.You must make sure the thread is cancelled before calling pthread_join() from cleanup_module(). Otherwise, Linux will hang waiting for the thread to finish. 3.If unsure, use pthread_delete_np(3) instead of pthread_cancel()/pthread_join().
Compiling and Executing ``Hello World'' In order to execute our program, we must first do the following: • Compile the source code and create a module. We can normally accomplish this by using the Linux GCC compiler directly from the command line. To simplify things, however, we'll create a Makefile. Then we'll only need to type ``make'' to compile our code. • Locate and copy the rtl.mk file. The rtl.mk file is an include file which contains all the flags needed to compile our code. For simplicity, we'll copy it from the RTLinux source tree and place it alongside of our hello.c file. • Insert the module into the running RTLinux kernel. The resulting object binary must be ``plugged in'' to the kernel, where it will be executed by RTLinux.
cp /usr/include/rtlinux/rtl.mk . make -f rtl.mk hello.o <方法一> rtlinux start hello rtlinux stop hello rtlinux staus hello <方法二> modprobe -a rtl rtl_time rtl_sched rtl_posixio rtl_fifo insmod hello.o rmmod hello