Using the Linux Clone() System Call

Simple Kernel threads for linux 2.0 in C and C++

jkthreads v1.2

A Hack By Jeff Koftinoff, jeffk@jdkoftinoff.com

Released under the Koftinoff Public License (KPL)

You may download jkthread-1.2.tar.gz now.

- WHY -

This is an experiment with the Linux 2.0 clone() call to implement usable kernel threads in a user program. These jkthreads have an API that has NOTHING to do with pthreads or Win32 threads or BeBox threads.

- NOTES -

It started as a hack. I spent three evenings writing this code. There are probably bugs in it. They are probably real subtle bugs, too.

This code is obsoleted by the LinuxThreads package with glibc2. You may want to look at this code if you are interested in the linux 2.0 'clone()' system call. This code will no longer be updated.

I used GCC 2.7.2, libc 5.3.12, Linux 2.0.0 etc.

The code here is not documented at all - Much of it is straightforward. It is for you to play with.

The C code implements simple threads, signal handlers to catch fallen children, semaphores using the ipc semget, and 'recursive' semaphores which allow the same thread to get a semaphore multiple times.

The C++ code implements a JKThread base class, a JKThreadWithMsgQ class which contains a messaging queue (implemented with semaphores, not ipc), and JKSemaphore and JKRecursiveSemaphore wrappers.

The test code in test1.cc uses printf() without a semaphore around it.

Please send me any comments/bugfixes on this code!

- FILES -

        jkcthread.c, jkcthread.h : The C language thread manager
        jkthread.cc, jkthread.h  : The C++ wrappers for threads
        test1.cc                 : Simple C++ test of threads, semaphores, and messaging
        Makefile                 : The Makefile
        COPYING                  :  The KPL
        README                   : This is it

- TODO -

DONE    * Semaphores held by Children who die (or are killed) should NOT
          be released!
                  
        * test test test
        
        * Write wrappers for other non-reentrant libc calls - like mmap()
        
        * Find a more memory efficient way to calculate a suitable address for
          errno based on getpid() or something else.
          
DONE    * The C++ JKThread class will try to kill the pid of the thread
          during destruction of the object, even it the thread has been
          manually killed.  Maybe we need a low level cleanup function for
          each thread that is running. This would be called from the
          signal handler of the parent.
                  
          Also keep in mind that if you delete a JKThread object without killing it
          first (or the JKThread goes out of scope first)
          the destructor of the JKThread class gets executed while the thread is running.
          This is a good way to confuse the thread. Of course at this point the
          thread is probably confused anyways.
          
        * Test children making new threads.
        
        * try TCP/IP networking code in a thread.
        
        * try re-entrant gethostbyname_r() etc..

        * Does the entire LIBC have to be re-compiled with -D_REENTRANT?
        
        * How to implement waiting for a semaphore for a maximum time?
        
        * What does CLONE_SIGHAND really do? I am not passing it to the clone() call.

e-mail: jeffk@jdkoftinoff.com

PGP/GNU PG Public Key