All of lore.kernel.org
 help / color / mirror / Atom feed
* [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, &param1);

            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, &param2);

            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

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.