* [Xenomai-help] Problem of thread creation with Xenomai Posix
@ 2008-02-14 13:32 Fabien MAHOT
2008-02-14 13:46 ` Gilles Chanteperdrix
2008-02-14 13:48 ` Johan Borkhuis
0 siblings, 2 replies; 6+ messages in thread
From: Fabien MAHOT @ 2008-02-14 13:32 UTC (permalink / raw)
To: xenomai
Hi,
I need your help to understand why my application doesn't work correctly.
I use the Xenomai 2.3.4 kernel with its POSIX skin to develop a real time
application. In this program, I create several threads thanks to a main
thread which has the highest priority (SCHED_FIFO configuration). The aim
is that the main thread stays in execution during the creation of all the
other threads.
However, in my program, when the main thread calls the posix function
pthread_create to create a new thread, it's preempted by a thread created
previously. In spite of the main thread has the highest priority.
To illustrate my problem, I wrote a test program. I execute this
application in my xenomai environment and in a linux environment (linux
2.6.20 kernel configured preemptive) :
My test program : The main function creates Main Thread. After Main Thread
creates T1 thread and next T2 thread. Main priority : 30, T1 priority :
25, T2 priority : 20.
#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
void console(char * chaine)
{
write(2, chaine, strlen(chaine));
fflush(stderr);
}
int func(volatile int* i)
{
return (*i)++;
}
void* thread1(void * context) {
volatile int i;
volatile int result;
console("T1 : start\n");
while (i < 10000) {
result = func(&i);
}
console("T1 : finish\n");
return NULL;
}
void* thread2(void * context) {
volatile int i;
volatile int result;
console("T2 : start\n");
while (i < 10000) {
result = func(&i);
}
console("T2 : finish\n");
return NULL;
}
void* thread_main(void *arg) {
pthread_attr_t attr;
pthread_t p1;
pthread_t p2;
struct sched_param sch;
struct sched_param sch1;
int result;
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
sch.sched_priority = 25;
pthread_attr_setschedparam(&attr, &sch);
console("M : before pthread_create T1\n");
pthread_create(&p1, &attr, thread1, NULL);
console("M : after pthread_create T1\n");
sch.sched_priority = 20;
pthread_attr_setschedparam(&attr, &sch);
console("M : before pthread_create T2\n");
result = pthread_create(&p2, &attr, thread2, NULL);
if (result != 0)
{
console("creation of thread 2 impossible\n");
}
console("M : after pthread_create T2\n");
pthread_attr_destroy(&attr);
while (1) {
sleep(500);
}
return NULL;
}
int main(int argc, char** argv) {
pthread_attr_t attr;
pthread_t p;
struct sched_param sch;
mlockall(MCL_CURRENT|MCL_FUTURE);
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
sch.sched_priority = 30;
pthread_attr_setschedparam(&attr, &sch);
pthread_create(&p, &attr, thread_main, NULL);
pthread_attr_destroy(&attr);
while (1) {
sleep(500);
}
return 0;
}
Result in Linux environment :
M : before pthread_create T1
M : after pthread_create T1
M : before pthread_create T2
M : after pthread_create T2
T1 : start
T1 : finish
T2 : start
T2 : finish
Result in Xenomai environment :
M : before pthread_create T1
M : after pthread_create T1
M : before pthread_create T2
T1 : start
T1 : finish
M : after pthread_create T2
T2 : start
T2 : finish
In the Linux environment, the thread creation is correct, Main thread is
not preempted by T1.
But in the Xenomai environment, we can see that Main thread is preempted
by T1 thread during the T2 creation.
I would like to know why I ve got this problem with Xenomai. If you ve got
an idea, please , give me your opinion.
Thanks a lot.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Xenomai-help] Problem of thread creation with Xenomai Posix
2008-02-14 13:32 [Xenomai-help] Problem of thread creation with Xenomai Posix Fabien MAHOT
@ 2008-02-14 13:46 ` Gilles Chanteperdrix
2008-02-14 13:48 ` Johan Borkhuis
1 sibling, 0 replies; 6+ messages in thread
From: Gilles Chanteperdrix @ 2008-02-14 13:46 UTC (permalink / raw)
To: Fabien MAHOT; +Cc: xenomai
On Thu, Feb 14, 2008 at 2:32 PM, Fabien MAHOT
<fabien.mahot@domain.hid> wrote:
> Hi,
>
> I need your help to understand why my application doesn't work correctly.
>
> I use the Xenomai 2.3.4 kernel with its POSIX skin to develop a real time
> application. In this program, I create several threads thanks to a main
> thread which has the highest priority (SCHED_FIFO configuration). The aim
> is that the main thread stays in execution during the creation of all the
> other threads.
>
> However, in my program, when the main thread calls the posix function
> pthread_create to create a new thread, it's preempted by a thread created
> previously. In spite of the main thread has the highest priority.
>
> To illustrate my problem, I wrote a test program. I execute this
> application in my xenomai environment and in a linux environment (linux
> 2.6.20 kernel configured preemptive) :
>
> My test program : The main function creates Main Thread. After Main Thread
> creates T1 thread and next T2 thread. Main priority : 30, T1 priority :
> 25, T2 priority : 20.
>
>
> #include <sys/mman.h>
> #include <pthread.h>
> #include <unistd.h>
> #include <sys/time.h>
> #include <string.h>
> #include <stdio.h>
>
> void console(char * chaine)
> {
> write(2, chaine, strlen(chaine));
> fflush(stderr);
> }
>
> int func(volatile int* i)
> {
> return (*i)++;
> }
>
> void* thread1(void * context) {
> volatile int i;
> volatile int result;
>
> console("T1 : start\n");
> while (i < 10000) {
> result = func(&i);
> }
> console("T1 : finish\n");
>
> return NULL;
> }
>
> void* thread2(void * context) {
> volatile int i;
> volatile int result;
>
> console("T2 : start\n");
> while (i < 10000) {
> result = func(&i);
> }
> console("T2 : finish\n");
>
> return NULL;
> }
>
> void* thread_main(void *arg) {
> pthread_attr_t attr;
> pthread_t p1;
> pthread_t p2;
> struct sched_param sch;
> struct sched_param sch1;
> int result;
>
> pthread_attr_init(&attr);
> pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
> pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
>
> sch.sched_priority = 25;
> pthread_attr_setschedparam(&attr, &sch);
>
> console("M : before pthread_create T1\n");
> pthread_create(&p1, &attr, thread1, NULL);
> console("M : after pthread_create T1\n");
>
> sch.sched_priority = 20;
> pthread_attr_setschedparam(&attr, &sch);
>
> console("M : before pthread_create T2\n");
> result = pthread_create(&p2, &attr, thread2, NULL);
> if (result != 0)
> {
> console("creation of thread 2 impossible\n");
> }
> console("M : after pthread_create T2\n");
>
> pthread_attr_destroy(&attr);
>
> while (1) {
> sleep(500);
> }
> return NULL;
> }
>
> int main(int argc, char** argv) {
> pthread_attr_t attr;
> pthread_t p;
> struct sched_param sch;
>
> mlockall(MCL_CURRENT|MCL_FUTURE);
>
> pthread_attr_init(&attr);
> pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
> pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
>
> sch.sched_priority = 30;
> pthread_attr_setschedparam(&attr, &sch);
> pthread_create(&p, &attr, thread_main, NULL);
> pthread_attr_destroy(&attr);
>
> while (1) {
> sleep(500);
> }
>
> return 0;
> }
>
>
>
> Result in Linux environment :
>
> M : before pthread_create T1
> M : after pthread_create T1
> M : before pthread_create T2
> M : after pthread_create T2
> T1 : start
> T1 : finish
> T2 : start
> T2 : finish
>
> Result in Xenomai environment :
>
> M : before pthread_create T1
> M : after pthread_create T1
> M : before pthread_create T2
> T1 : start
> T1 : finish
> M : after pthread_create T2
> T2 : start
> T2 : finish
>
> In the Linux environment, the thread creation is correct, Main thread is
> not preempted by T1.
> But in the Xenomai environment, we can see that Main thread is preempted
> by T1 thread during the T2 creation.
>
> I would like to know why I ve got this problem with Xenomai. If you ve got
> an idea, please , give me your opinion.
pthread_create suspends the caller for a short time, I do not think
that it contradicts the posix standard. Besides, there are other calls
in your example that may suspend their caller: write and fflush.
--
Gilles Chanteperdrix
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Xenomai-help] Problem of thread creation with Xenomai Posix
2008-02-14 13:32 [Xenomai-help] Problem of thread creation with Xenomai Posix Fabien MAHOT
2008-02-14 13:46 ` Gilles Chanteperdrix
@ 2008-02-14 13:48 ` Johan Borkhuis
2008-02-14 13:51 ` Gilles Chanteperdrix
1 sibling, 1 reply; 6+ messages in thread
From: Johan Borkhuis @ 2008-02-14 13:48 UTC (permalink / raw)
To: Fabien MAHOT; +Cc: xenomai
Fabien MAHOT wrote:
> Hi,
>
> I need your help to understand why my application doesn't work correctly.
>
> I use the Xenomai 2.3.4 kernel with its POSIX skin to develop a real time
> application. In this program, I create several threads thanks to a main
> thread which has the highest priority (SCHED_FIFO configuration). The aim
> is that the main thread stays in execution during the creation of all the
> other threads.
>
> However, in my program, when the main thread calls the posix function
> pthread_create to create a new thread, it's preempted by a thread created
> previously. In spite of the main thread has the highest priority.
>
> I would like to know why I ve got this problem with Xenomai. If you ve got
> an idea, please , give me your opinion.
>
>
This is quite easy to explain: the task you create is a real time task.
This task will always run before your main task, which is a Linux task.
If you add a call to rt_task_shadow after the mlockall your main task
will also become a Xenomai task, and you will see the same behavior as
with Linux.
Kind regards,
Johan Borkhuis
--
Johan Borkhuis Dutch Space BV
email: j.borkhuis@domain.hid Newtonweg 1
phone: 071-5245788 Leiden
fax: 071-5245499 The Netherlands
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Xenomai-help] Problem of thread creation with Xenomai Posix
2008-02-14 13:48 ` Johan Borkhuis
@ 2008-02-14 13:51 ` Gilles Chanteperdrix
2008-02-14 14:55 ` Johan Borkhuis
0 siblings, 1 reply; 6+ messages in thread
From: Gilles Chanteperdrix @ 2008-02-14 13:51 UTC (permalink / raw)
To: Johan Borkhuis; +Cc: xenomai
On Thu, Feb 14, 2008 at 2:48 PM, Johan Borkhuis
<j.borkhuis@domain.hid> wrote:
> Fabien MAHOT wrote:
> > Hi,
> >
> > I need your help to understand why my application doesn't work correctly.
> >
> > I use the Xenomai 2.3.4 kernel with its POSIX skin to develop a real time
> > application. In this program, I create several threads thanks to a main
> > thread which has the highest priority (SCHED_FIFO configuration). The aim
> > is that the main thread stays in execution during the creation of all the
> > other threads.
> >
> > However, in my program, when the main thread calls the posix function
> > pthread_create to create a new thread, it's preempted by a thread created
> > previously. In spite of the main thread has the highest priority.
> >
>
> > I would like to know why I ve got this problem with Xenomai. If you ve got
> > an idea, please , give me your opinion.
> >
> >
> This is quite easy to explain: the task you create is a real time task.
> This task will always run before your main task, which is a Linux task.
>
> If you add a call to rt_task_shadow after the mlockall your main task
> will also become a Xenomai task, and you will see the same behavior as
> with Linux.
When using Xenomai posix skin, pthread_create creates real-time tasks
as well. And the main thread is automatically shadowed (by library
init code).
--
Gilles Chanteperdrix
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Xenomai-help] Problem of thread creation with Xenomai Posix
2008-02-14 13:51 ` Gilles Chanteperdrix
@ 2008-02-14 14:55 ` Johan Borkhuis
2008-02-14 15:10 ` Gilles Chanteperdrix
0 siblings, 1 reply; 6+ messages in thread
From: Johan Borkhuis @ 2008-02-14 14:55 UTC (permalink / raw)
To: Gilles Chanteperdrix; +Cc: xenomai
Gilles Chanteperdrix wrote:
> When using Xenomai posix skin, pthread_create creates real-time tasks
> as well.
Yes, that is what I would expect, as the Xenomai Posix skin is used :-)
> And the main thread is automatically shadowed (by library
> init code).
>
I did not know this. Is this true for all skins? I did see problems
(like the one described here) when I started an application that created
a RT-thread without first shadowing the main thread.
Kind regards,
Johan Borkhuis
--
Johan Borkhuis Dutch Space BV
email: j.borkhuis@domain.hid Newtonweg 1
phone: 071-5245788 Leiden
fax: 071-5245499 The Netherlands
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Xenomai-help] Problem of thread creation with Xenomai Posix
2008-02-14 14:55 ` Johan Borkhuis
@ 2008-02-14 15:10 ` Gilles Chanteperdrix
0 siblings, 0 replies; 6+ messages in thread
From: Gilles Chanteperdrix @ 2008-02-14 15:10 UTC (permalink / raw)
To: Johan Borkhuis; +Cc: xenomai
On Thu, Feb 14, 2008 at 3:55 PM, Johan Borkhuis
<j.borkhuis@domain.hid> wrote:
> Gilles Chanteperdrix wrote:
> > When using Xenomai posix skin, pthread_create creates real-time tasks
> > as well.
>
> Yes, that is what I would expect, as the Xenomai Posix skin is used :-)
>
>
> > And the main thread is automatically shadowed (by library
> > init code).
> >
>
> I did not know this. Is this true for all skins? I did see problems
> (like the one described here) when I started an application that created
> a RT-thread without first shadowing the main thread.
Posix, psos+, and uitron do it. Other skins do not.
--
Gilles Chanteperdrix
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-02-14 15:10 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-02-14 13:32 [Xenomai-help] Problem of thread creation with Xenomai Posix Fabien MAHOT
2008-02-14 13:46 ` Gilles Chanteperdrix
2008-02-14 13:48 ` Johan Borkhuis
2008-02-14 13:51 ` Gilles Chanteperdrix
2008-02-14 14:55 ` Johan Borkhuis
2008-02-14 15:10 ` Gilles Chanteperdrix
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.