Originally Published: Wednesday, 15 August 2001 Author: Subhasish Ghosh
Published to: develop_articles/Development Articles Page: 4/4 - [Printable]

Understanding Re-entrant Kernels

Undertanding the details of the Linux kernel may seem beyond the reach of many people, but at Linux.com we don't believe in "beyond the reach". In this article Ghosh begins his exploration down into the depths of the machine with a look at how a multi-threaded program can help illustrate re-entrancy, and vica versa.

Code Walkthrough  << Page 4 of 4  

An Explanation

At the top, we have declared a prototype for the function that the thread will call when we create it. This is: void *thread_function (void *arg); as required by pthread_create, it takes a pointer to void as its only argument and returns a pointer to void. In main we declare some variables. And then we call pthread_create to cause our new thread to start running.

pthread_t a_thread;
void *thread_result;
res = pthread_create (&a_thread, NULL, thread_function, (void *)message);

Here we pass the address of the a_thread object that refers to the instance of the newly created thread and that we can use to refer to the thread afterwards. We pass the default thread attribute, as NULL, and the final two parameters are the function to call and a parameter to pass to it.

If the pthread_create call succeeds we now have two threads running. The original thread (which is main) continues and executes the code section lying after the pthread_create and a new thread starts executing in the function thread_function. The original thread checks that the new thread has started and then calls pthread_join. pthread_join is the thread equivalent of wait that processes use to collect child processes. The function is defined in pthread.h and is declared below:

int pthread_join (pthread_t th, void **thread_return);

Here we have:

res = pthread_join (a_thread, &thread_result);

Here we pass the identifier of the thread that we are waiting for to join and a pointer to a result. This function will wait until the thread terminates before it returns. It then prints the return value from the thread, the contents of the variable and then exits.

In thread_function something else happens. The new thread starts executing in thread_function, prints out its arguments, sleeps for a short period, updates the global variables and then exits returning a string to the main thread. The new thread writes to the same array, message, to which the original thread has access. Thus, this multithreaded program illustrates re-entrancy, though in a very controlled and mild fashion.

So, this brings us to the end of the world of "Re-entrant Kernels" at least for the time being. But as we have had seen earlier, creating re-entrant routines in Linux can be a whole lot of fun.

Further Reading

Besides the Online resources on Unix/Linux Kernels, if one wants to study about the Unix/Linux Kernels seriously in more detail, I am sure these following books would be immensely helpful.
  1. Books on UNIX Kernel:
    • The Design of the Unix Operating System; By: M.J.Bach
    • The Magic Garden Explained : The Internals of the Unix System V Release 4; By: B. Goodheart and J. Cox
    • The Design and Implementation of the 4.4 BSD Operating System; By: McKusick, Karels & Quarterman
    • Unix Internals: The New Frontiers; By: U. Vahalia
  2. Books on Linux Kernel:
    • Linux Kernel Internals; By: Bohme
    • The Linux Kernel Book; By: Dumas & Mevel
    • Linux Core Kernel Commentary; By: S. Maxwell
    • Linux Device Drivers; By: A. Rubini

About the Author: My name is Subhasish Ghosh. I am 20 years old, currently a computer-systems engineering student in India. I am a Microsoft Certified Professional (MCP), MCSD, MCP certified on NT 4.0, recently completed Red Hat Linux Certified Engineer (RHCE) Training & cleared Brainbench.com "Linux General Administration" certification exam. I have been working with Linux for a long time now, have had programmed using C, C++, VC++, VB, COM, DCOM, MFC, ATL 3.0, PERL, Python and Linux Kernel programming.

Latest News: Currently busy playing with POSIX threads, riding my new bike at top speed and broke-off with my girlfriend. E-mail: subhasish_ghosh@linuxmail.org

Code Walkthrough  << Page 4 of 4