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
_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help