Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
Linux Magazine

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
Copyright Info
Terms of Use
Privacy Info
Masthead / Impressum
Your Account

Private Messages

News Archive
Submit News
User Articles
Web Links


The Web

Who's Online
There are currently, 230 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here



Current HOWTO: RTLinux HOWTO

RTLinux HOWTO: Writing RTLinux Programs Next Previous Contents

4. Writing RTLinux Programs

4.1 Introduction to writing modules

So what are modules? A Linux module is nothing but an object file, usually created with the -c flag argument to gcc. The module itself is created by compiling an ordinary C language file without the main() function. Instead there will be a pair of init_module/cleanup_module functions:

  • The init_module() which is called when the module is inserted into the kernel. It should return 0 on success and a negative value on failure.
  • The cleanup_module() which is called just before the module is removed.

Typically, init_module() either registers a handler for something with the kernel, or it replaces one of the kernel function with its own code (usually code to do something and then call the original function). The cleanup_module() function is supposed to undo whatever init_module() did, so the module can be unloaded safely.

For example, if you have written a C file called module.c (with init_module() and cleanup_module() replacing the main() function), the code can be converted into a module by typing :

        $ gcc -c {SOME-FLAGS} my_module.c

This command creates a module file named module.o, which can now be inserted into the kernel by using the 'insmod' command :

        $ insmod module.o

Similarly, for removing the module, you can use the 'rmmod' command :

        $ rmmod module

4.2 Creating RTLinux Threads

A realtime application is usually composed of several ``threads'' of execution. Threads are light-weight processes which share a common address space. In RTLinux, all threads share the Linux kernel address space. The advantage of using threads is that switching between threads is quite inexpensive when compared with context switch. We can have complete control over the execution of a thread by using different functions as will be shown in the examples following.

4.3 An example program

The best way to understand the working of a thread is to trace a realtime program. For example, the program shown below will execute once every second, and during each iteration it will print 'Hello World'.

The Program code (file - hello.c) :

#include <rtl.h>
#include <time.h>
#include <pthread.h>

pthread_t thread;

void * thread_code(void)
        pthread_make_periodic_np(pthread_self(), gethrtime(), 1000000000);

        while (1)
                pthread_wait_np ();
                rtl_printf("Hello World\n");

        return 0;

int init_module(void) 
   return pthread_create(&thread, NULL, thread_code, NULL);

void cleanup_module(void) 

So, let us start with the init_module(). The init_module() invokes pthread_create(). This is for creating a new thread that executes concurrently with the calling thread. This function must only be called from the Linux kernel thread (i.e., using init_module()).

        int  pthread_create(pthread_t  * thread,
                            pthread_attr_t * attr,
                            void * (*thread_code)(void *),
                            void * arg);

The new thread created is of type pthread_t, defined in the header pthread.h. This thread executes the function thread_code(), passing it arg as its argument. The attr argument specifies thread attributes to be applied to the new thread. If attr is NULL, default attributes are used.

So here, thread_code() is invoked with no argument. thread_code has three components - initialization, run-time and termination.

In the initialization phase, is the call to pthread_make_periodic_np().

        int pthread_make_periodic_np(pthread_t thread, 
                                     hrtime_t start_time, 
                                     hrtime_t period);

pthread_make_periodic_np marks the thread as ready for execution. The thread will start its execution at start_time and will run at intervals specified by period given in nanoseconds.

gethrtime returns the time in nanoseconds since the system bootup.

       hrtime_t gethrtime(void);

This time is never reset or adjusted. gethrtime always gives monotonically increasing values. hrtime_t is a 64-bit signed integer.

By calling the function pthread_make_periodic_np(), the thread tells the scheduler to periodically execute this thread at a frequency of 1 Hz. This marks the end of the initialization section for the thread.

The while() loop begins with a call to the function pthread_wait_np(), which suspends execution of the currently running realtime thread until the start of the next period. The thread was previously marked for execution using pthread_make_periodic_np. Once the thread is called again, it executes the rest of the contents inside the while loop, until it encounters another call to pthread_wait_np().

Because we haven't included any way to exit the loop, this thread will continue to execute forever at a rate of 1Hz. The only way to stop the program is by removing it from the kernel with the rmmod command. This invokes the cleanup_module(), which calls pthread_delete_np() to cancel the thread and deallocate its resources.

Next Previous Contents

The Linux Tutorial completely respects the rights of authors and artists to decide for themselves if and how their works can be used, independent of any existing licenses. This means if you are the author of any document presented on this site and do no wish it to be displayed as it is on this site or do not wish it to be displayed at all, please contact us and we will do our very best to accommodate you. If we are unable to accommodate you, we will, at your request, remove your document as quickly as possible.

If you are the author of any document presented on this site and would like a share of the advertising revenue, please contact us using the standard Feedback Form.




Security Code
Security Code
Type Security Code

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!

Amazon Wish List

Did You Know?
The Linux Tutorial welcomes your suggestions and ideas.


Tell a Friend About Us

Bookmark and Share

Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.16 Seconds