Problem of thread creation with Xenomai Posix
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.
Quoted 152 lines · Fabien MAHOT
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_FIFOconfiguration). 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_createto 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_createT1
M : afterpthread_createT1
M : beforepthread_createT2
M : afterpthread_createT2
T1 : start
T1 : finish
T2 : start
T2 : finishResult in Xenomai environment :
M : before
pthread_createT1
M : afterpthread_createT1
M : beforepthread_createT2
T1 : start
T1 : finish
M : afterpthread_createT2
T2 : start
T2 : finish
Quoted 7 lines
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.
Signature · 1 line
Gilles Chanteperdrix
Quoted 10 lines · Fabien MAHOT
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_FIFOconfiguration). The aim is that the main thread stays in execution during the creation of all the other threads.
Quoted 8 lines
However, in my program, when the main thread calls the posix function
pthread_createto 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
Signature · 4 lines
Johan Borkhuis Dutch Space BV email: [email protected] Newtonweg 1 phone: 071-5245788 Leiden fax: 071-5245499 The Netherlands
Quoted 20 lines · Johan Borkhuis
Quoted 10 lines · Fabien MAHOT
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_FIFOconfiguration). The aim is that the main thread stays in execution during the creation of all the other threads.Quoted 9 lines
However, in my program, when the main thread calls the posix function
pthread_createto 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.
Quoted 6 lines
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_shadowafter 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).
Signature · 1 line
Gilles Chanteperdrix
Quoted 2 lines · Gilles Chanteperdrix
When using Xenomai posix skin,
pthread_createcreates real-time tasks as well.
Yes, that is what I would expect, as the Xenomai Posix skin is used :-)
Quoted 3 lines
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
Signature · 4 lines
Johan Borkhuis Dutch Space BV email: [email protected] Newtonweg 1 phone: 071-5245788 Leiden fax: 071-5245499 The Netherlands
Quoted 7 lines · Johan Borkhuis
Quoted 2 lines · Gilles Chanteperdrix
When using Xenomai posix skin,
pthread_createcreates real-time tasks as well.Yes, that is what I would expect, as the Xenomai Posix skin is used :-)
Quoted 7 lines
Quoted 3 lines
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.
Signature · 1 line
Gilles Chanteperdrix