All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Fabien MAHOT" <fabien.mahot@domain.hid>
To: xenomai@xenomai.org
Subject: [Xenomai-help] Application crash
Date: Mon, 17 Mar 2008 15:43:09 +0100 (CET)	[thread overview]
Message-ID: <16201.81.252.86.91.1205764989.squirrel@domain.hid> (raw)

Hello,

I ve got a problem with my application. I use the Xenomai 2.3.4 kernel
(disable priority coupling option -> deactivate).
I wrote a test program to show you the problem.

There are T1 thread with the highest priority and T2 thread.
T1 thread sleeps during 5ms and uses the console function to display its
current state.
T2 uses the console function to display its current state.
To use the console function, the threads need lockConsole mutex.

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <semaphore.h>

// Console
pthread_mutex_t lockConsole;
unsigned char bufferConsole[2048];
// Start of tasks
pthread_cond_t  start_signal;
pthread_mutex_t main_start_lock;

void console(char * chaine,...)
{
    if (pthread_mutex_lock(&lockConsole)!=0)
    {
       printf("Error:pthread_mutex_lock\n");
    }
    pthread_t p0;
    va_list ArgConsole;
    va_start(ArgConsole, chaine);
    vsprintf((char *)bufferConsole,chaine,ArgConsole);
    if (write(2, (char*)bufferConsole, strlen((char *)bufferConsole)) == -1)
    {
       printf("Error:write\n");
    }
    pthread_mutex_unlock(&lockConsole);
}

int func(volatile int* i)
{
	return (*i)++;
}

/************************ tasks ****************************/
void* thread1(void * cookie) {
    volatile int i=0;
    volatile int result;

    pthread_mutex_lock(&main_start_lock);
    pthread_cond_wait(&start_signal, &main_start_lock);
    pthread_mutex_unlock(&main_start_lock);

    while (i < 10) {
        usleep(5000);
        result = func(&i);
        console ("T1 : %d \n",result);
    }

    return NULL;
}

void* thread2(void * cookie) {
    volatile int i=0;
    volatile int result;

    pthread_mutex_lock(&main_start_lock);
    pthread_cond_wait(&start_signal, &main_start_lock);
    pthread_mutex_unlock(&main_start_lock);

    while (i < 200) {
        result = func(&i);
        console ("T2 : %d \n",result);
    }

    return NULL;
}

/************************** main *****************************/
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    struct sched_param sch;
    pthread_t p1;
    pthread_t p2;

    mlockall(MCL_CURRENT|MCL_FUTURE);

    pthread_mutexattr_init(&attr_proto);
    pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT);
    pthread_mutex_init(&lockConsole, &attr_proto);

    pthread_cond_init(&start_signal, NULL);
    pthread_mutex_init(&main_start_lock, NULL);

    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
    pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

    // creation of task 1
    sch.sched_priority = 80;
    pthread_attr_setschedparam(&attr, &sch);
    pthread_create(&p1, &attr, thread1, NULL);

    // creation of task 2
    sch.sched_priority = 75;
    pthread_attr_setschedparam(&attr, &sch);
    pthread_create(&p2, &attr, thread2, NULL);

    // Start of tasks
    pthread_mutex_lock(&main_start_lock);
    pthread_cond_broadcast(&start_signal);
    pthread_mutex_unlock(&main_start_lock);

    pthread_attr_destroy(&attr);

    while (1){
        sleep(500);
    }

    return 0;
}



Result in Xenomai environment(Xenomai 2.3.4 kernel) :
T2 : 0
T2 : 1
T2 : 2
T2 : 3
T2 : 4
T2 : 5
T2 : 6
T2 : 7
T2 : 8
T2 : 9

And the application crashes without error message.
When the application crashes, I think that T1 thread runs in the console
function and it will be put in hold (mutex or write). Thanks to an other
test program, I saw that there is a wait in the write function

So, there is a preemption possibility.

I try different configurations :
- When I delete the console call in T1, my application doesn't crash.
- When I use a clock thread which sleeps during 5ms and wakes up T1 thanks
to a semaphore, my application doesn't crash. (T1 doesn't call usleep)

So there is something strange. I think there is a problem when a thread
changes of domain (Xenomai to linux) after time out function use (when you
use these functions, is there a signal emission when the time out is
finished? maybe, this signal crashes my application...)

I would like to know why I ve got this problem. If you ve got an idea,
please , give me your opinion.

Thanks a lot.










             reply	other threads:[~2008-03-17 14:43 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-17 14:43 Fabien MAHOT [this message]
2008-03-17 18:23 ` [Xenomai-help] Application crash Gilles Chanteperdrix
2008-03-18 17:18   ` Fabien MAHOT
2008-03-19  8:42 ` Gilles Chanteperdrix
2008-03-19 11:55   ` Fabien MAHOT
2008-03-19 13:13     ` Gilles Chanteperdrix
2008-03-19 14:00       ` Fabien MAHOT
2008-03-19 15:33       ` Fabien MAHOT
2008-03-24 13:11         ` Gilles Chanteperdrix
2008-03-24 17:27           ` Gilles Chanteperdrix
2008-03-24 22:15             ` Gilles Chanteperdrix
2008-03-25 17:04               ` Fabien MAHOT
2008-03-25 17:37                 ` Gilles Chanteperdrix
2008-03-26  9:14                   ` Fabien MAHOT
2008-03-26  9:36                     ` Gilles Chanteperdrix
2008-03-26 10:06                       ` Fabien MAHOT
2008-03-26 19:58                         ` Gilles Chanteperdrix
2008-03-27  9:51                           ` Fabien MAHOT
2008-04-03  8:59                           ` Fabien MAHOT
2008-04-09  9:00                           ` [Xenomai-help] Posix timers increase Fabien MAHOT
2008-04-09 11:29                             ` Fabien MAHOT
2008-04-09 14:39                               ` Gilles Chanteperdrix
2008-04-09 14:45                                 ` Breno Carneiro Pinheiro
2008-03-26 10:35                       ` [Xenomai-help] Application crash Fabien MAHOT

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=16201.81.252.86.91.1205764989.squirrel@domain.hid \
    --to=fabien.mahot@domain.hid \
    --cc=xenomai@xenomai.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.