[Rtai] Kernel crash when rt_thread_init is called with same name (in user-space)

Shahbaz Yousefi shabbyx at gmail.com
Wed Mar 7 15:10:25 CET 2012


Using magma downloaded on January 9th, with Linux kernel 2.6.35.9.

First let me give a small example where this behavior happens, then I will
explain more about it:

*main.c:*

#include <unistd.h>
#include <sys/wait.h>
#include <rtai_lxrt.h>

void *function(void *arg)
{
    int i;
    for (i = 0; i < 1000; ++i)
        /* This is what causes crash: */
        if (rt_thread_init(nam2num("THREAD")+i, 100, 0, SCHED_FIFO, 0xf))
            break;
    if (i != 1000)
    {
        mlockall(MCL_CURRENT | MCL_FUTURE);
        rt_make_hard_real_time();
        rt_sleep(nano2count(10000));
        rt_make_soft_real_time();
        rt_task_delete(NULL);
    }
    return NULL;
}

int main(void)
{
    int num_times = 20;
    int children_each = 20;
    int i;
    rt_set_oneshot_mode();
    start_rt_timer(nano2count(100000));
    for (i = 0; i < num_times; ++i)
    {
        if (!fork())
        {
            pthread_t *tasks = malloc(children_each*sizeof(*tasks));
            int j;
            for (j = 0; j < children_each; ++j)
                tasks[j] = rt_thread_create(function, NULL, 10000);
            for (j = 0; j < children_each; ++j)
                rt_thread_join(tasks[j]);
            free(tasks);
            return EXIT_SUCCESS;
        }
    }
    for (i = 0; i < num_times; ++i)
        wait(NULL);
    stop_rt_timer();
    return EXIT_SUCCESS;
}

*Makefile:*

RTAI_OPTIONS := $(shell rtai-config --lxrt-cflags)
RTAI_LINK := $(shell rtai-config --lxrt-ldflags)

test: main.c
        gcc -o $@ $< $(RTAI_OPTIONS) $(RTAI_LINK)

What the program does is to spawn some processes, each of which having some
real-time threads. Each of the threads searches for a possible name to
acquire. If I assign different names to each thread (for example through
functions's arg), there is no problem.

However, if each thread tries acquiring a thread with whatever name it can
find available, the kernel crashes. If the threads are created with a
delay, there is again no problem.

This shows that when rt_thread_init is called by two different threads
asking to create a thread with the same name, the kernel crashes.

My questions are:
- *Is this a bug? If so, can you please resolve it?*
- *What alternative do I have to finding an unused name for each thread?*
  * Although in the documentation the behavior of rt_get_name given NULL is
not defined, from the source code it seems like it would give a free name.
This doesn't solve the problem however, because from the time you get a
free name to the time you call rt_thread_init, another thread may acquire
the same name and again: the kernel crash.

The reason I am doing this is the following. I have many applications that
spawn rtai threads (in user-space). They don't need to know their names and
all they need is to call rt_thread_init with a name that is available.
Since they are in different applications, they do not know what names are
already taken by other applications and therefore must try with different
names until they find a free one. The alternative would be to take a linux
shared semaphore which introduces it's own complexities of which
application would initialize it and who's going to clean it up.

My suggestion is to add a lock around the code in rtai_lxrt function that
handles LXRT_TASK_INIT. This way, if two threads call rt_thread_init with
the same name at the same time, one of them would succeed and one would
fail.

Thank you,
Shahbaz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.rtai.org/pipermail/rtai/attachments/20120307/227c740b/attachment.html>


More information about the Rtai mailing list