* [Xenomai-help] Problem with API Posix
@ 2007-05-04 8:08 Perrine Martignoni
2007-05-04 8:55 ` Gilles Chanteperdrix
0 siblings, 1 reply; 2+ messages in thread
From: Perrine Martignoni @ 2007-05-04 8:08 UTC (permalink / raw)
To: xenomai
[-- Attachment #1: Type: text/plain, Size: 3294 bytes --]
Hello,
I'm writing some simple examples of real time code with API Posix of Xenomai
and I think I'm doing something wrong.
I would like to have two threads with different priority and see the two
threads switching.
Here is my code :
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <limits.h>
pthread_t task1, task2;
pthread_mutex_t mutex;
void routine1 (void * cookie);
void routine2 (void * cookie);
int tab[200];
void affich(int i)
{
static int cpt = 0;
pthread_mutex_lock(&mutex);
tab[cpt] = i;
cpt ++;
if(cpt == 150){
cpt = 0;
}
pthread_mutex_unlock(&mutex);
}
void routine1 (void * cookie){
for(;;){
affich(1);
sleep(1);
}
}
void routine2 (void * cookie)
{
for(;;){
affich(2);
sleep(2);
}
}
void cleanup_upon_sig(int sig __attribute__((unused)))
{
pthread_mutex_destroy(&mutex);
pthread_exit(&task1);
pthread_exit(&task2);
exit(0);
}
int main(int argc, char **argv)
{
sigset_t mask;
pthread_attr_t attr1, attr2;
struct sched_param param1, param2;
int i = 0;
mlockall(MCL_CURRENT | MCL_FUTURE);
pthread_mutex_init(&mutex,NULL);
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
signal(SIGINT, cleanup_upon_sig);
sigaddset(&mask, SIGTERM);
signal(SIGTERM, cleanup_upon_sig);
sigaddset(&mask, SIGHUP);
signal(SIGHUP, cleanup_upon_sig);
pthread_attr_init(&attr1);
pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_JOINABLE);
pthread_attr_setinheritsched(&attr1, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr1, SCHED_FIFO);
pthread_attr_setschedparam(&attr1, ¶m1);
param1.sched_priority =20;
pthread_attr_setstacksize(&attr1, PTHREAD_STACK_MIN);
pthread_attr_init(&attr2);
pthread_attr_setdetachstate(&attr2, PTHREAD_CREATE_JOINABLE);
pthread_attr_setinheritsched(&attr2, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr2, SCHED_FIFO);
pthread_attr_setschedparam(&attr2, ¶m2);
param2.sched_priority = 99;
pthread_attr_setstacksize(&attr2, PTHREAD_STACK_MIN);
pthread_create (&task1,&attr1,(void *)routine1,NULL);
pthread_create (&task2,&attr2,(void *)routine2,NULL);
sleep(10);
for(i = 0; i < 150 ; i++){
printf("Task %d\n",tab[i]);
}
return 0;
}
And here is the result :
Task 2
Task 2
Task 2
Task 2
Task 2
Task 1 don't run when Task 2 is sleeping.
I know that sleep() is not real time but it is the reason of this problem?
Thank you
[-- Attachment #2: Type: text/html, Size: 19233 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Xenomai-help] Problem with API Posix
2007-05-04 8:08 [Xenomai-help] Problem with API Posix Perrine Martignoni
@ 2007-05-04 8:55 ` Gilles Chanteperdrix
0 siblings, 0 replies; 2+ messages in thread
From: Gilles Chanteperdrix @ 2007-05-04 8:55 UTC (permalink / raw)
To: Perrine Martignoni; +Cc: xenomai
Perrine Martignoni wrote:
> void cleanup_upon_sig(int sig __attribute__((unused)))
>
> {
>
> pthread_mutex_destroy(&mutex);
>
> pthread_exit(&task1);
>
> pthread_exit(&task2);
>
> exit(0);
>
> }
This will not work:
- pthread_exit, can not be used to terminate another thread than the
current thread, what you are looking for is pthread_cancel and pthread_join
- pthread_mutex_destroy will not work if a thread has currently locked
the mutex, so you can call pthread_mutex_destroy only after having
canceled and joined task1 and task2
- doing the cleanups in the signal handler leads to a risk of deadlock,
especially if the signal handler is able to run on any thread.
> sigemptyset(&mask);
>
> sigaddset(&mask, SIGINT);
>
> signal(SIGINT, cleanup_upon_sig);
>
> sigaddset(&mask, SIGTERM);
>
> signal(SIGTERM, cleanup_upon_sig);
>
> sigaddset(&mask, SIGHUP);
>
> signal(SIGHUP, cleanup_upon_sig);
What is this mask for ? You set it up but do not use it. If you want to
ensure that the newly created threads are created with these signals
masked, you should call pthread_sigmask before any call to
pthread_create to mask these signals, and unmask them after having
created the new threads. That is because when calling pthread_create,
the newly created thread signal masked is copied from the parent thread.
>
>
>
> pthread_attr_init(&attr1);
>
> pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_JOINABLE);
>
> pthread_attr_setinheritsched(&attr1, PTHREAD_EXPLICIT_SCHED);
>
> pthread_attr_setschedpolicy(&attr1, SCHED_FIFO);
>
> pthread_attr_setschedparam(&attr1, ¶m1);
This is incorrect. param1 is not initialized and you are using it, but
since you do not check the return value, you do not know if this call
works or fails.
>
> param1.sched_priority =20;
> (...)
> pthread_create (&task1,&attr1,(void *)routine1,NULL);
How do you know if task1 is running ? You do not check pthread_create
return value.
Why your example does not work is not obvious at first sight, but the
general advice we can give you is:
- please read the documentation at :
http://www.xenomai.org/documentation/branches/v2.3.x/html/api/group__posix.html
and
http://www.unix.org/single_unix_specification
- please check the return value of the services you use, when they have one;
- please have a look at the programs included in xenomai distribution.
--
Gilles Chanteperdrix
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-05-04 8:55 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-04 8:08 [Xenomai-help] Problem with API Posix Perrine Martignoni
2007-05-04 8:55 ` 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.