HELP: Threads: strange behaiviour (???)
Joel Sherrill <joel@OARcorp.com>
joel.sherrill at OARcorp.com
Thu Aug 26 13:25:06 UTC 2004
Alex wrote:
> Hi,
>
> I have a rtems program that launchs 2 extra threads. Each thread shares the same execution function.
> Each thread, main included, uses the SCHED_RR with poriority 1.
> The objective is to run the threads at the same time as possible sharing the time slice. In other words, I
> want to execute each thread in each quantum, so I choosed the SCHED_RR.
>
> What I get is the first extra thread running till the end and then the second extra thread begins execution.
> Why both extra threads didn't start execution "at the same time?"
>
> The output I get is:
>
> ...
> WAITING FOR SECONDARY THREADS
>
> secondary thread: beginning
>
> "some moments after..."
>
> secondary thread: finishing
>
> secondary thread: beginning
>
> "some moments after..."
>
> secondary thread: finishing
> ...
>
>
>
> The output I expected was:
>
> ...
> secondary thread: beginning
> secondary thread: beginning
>
> "some moments after..."
>
> secondary thread: finishing
> secondary thread: finishing
> ...
>
> Do you agree with me?
Maybe yes, maybe now. :)
I think your secondary thread could be running to completion in less
than a timeslice. Some things to try:
+ Why status += in Task_1?
+ Change the for in Task_1 to something that actually ensures it is
hanging around for at least 1 or 2 clock ticks. Watch "current"
until it changes more than a timeslice.
--joel
> This is source code:
>
> #define CONFIGURE_INIT
> #include <rtems.h>
> #include <signal.h>
> #include <rtems/posix/timer.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>
> #include <rtems/error.h>
> #include <bsp.h>
> #include <pthread.h>
> #ifndef _POSIX_THREADS
> #error "rtems is supposed to have pthreads"
> #endif
>
> #include <assert.h>
> #include <string.h>
> #include <time.h>
> #include <unistd.h>
> #include <time.h>
> #include <sched.h>
>
>
> void *Task_1(void *argument)
> {
> int i=0;
> int status;
> struct timespec current;
>
> puts("\nSecondary Thread: beginning" );
> for (i=0;i<100000000;i++)
> {
> status += clock_gettime( CLOCK_REALTIME, ¤t );
> }
>
> puts("\nSecondary Thread: finishing" );
> pthread_exit( NULL );
> return NULL;
> }
>
>
> #define CONFIGURE_INIT_TASK_ENTRY_POINT POSIX_Init
> void POSIX_Init(void* args)
> {
> const int MAX_THREADS =10;
> const int n_number_of_counts =10;
>
> int i=0;
> pthread_t Task_ids[MAX_THREADS];
> pthread_t Init_id;
> int status;
> pthread_attr_t attr1;
> pthread_attr_t attr2;
> struct sched_param param;
>
>
> puts("\n\nSTARTING THE MAIN PROGRAM");
> Init_id = pthread_self();
> param.sched_priority = 1;
> status = pthread_setschedparam(Init_id, SCHED_RR, ¶m);
>
> puts("\nCREATING THE SECONDARY THREADS");
>
> status = pthread_attr_init(&attr1);
> if(status!=0)
> {
> puts("\n Error in pthread_attr_init1()");
> exit(1);
> }
> attr1.schedpolicy = SCHED_RR;
> attr1.schedparam.sched_priority = 1;
>
> status = pthread_attr_init(&attr2);
> if(status!=0)
> {
> puts("\n Error in pthread_attr_init1()");
> exit(1);
> }
> attr2.schedpolicy = SCHED_RR;
> attr2.schedparam.sched_priority = 1;
>
>
> status = pthread_create(&(Task_ids[0]),&attr1,Task_1,NULL);
> if(status!=0)
> {
> puts("\n Error in pthread_create1()");
> exit(1);
> }
>
> status = pthread_create(&(Task_ids[1]),&attr2,Task_1,NULL);
> if(status!=0)
> {
> puts("\n Error in pthread_create2()");
> exit(1);
> }
>
> puts("\nAFTER THREADS CREATION...\n");
>
> puts( "\nWAITING FOR SECONDARY THREADS..." );
> status = pthread_join( Task_ids[0], NULL );
> if(status!=0)
> {
> puts("\n Error in pthread_join0()");
> exit(1);
> }
>
> status = pthread_join( Task_ids[1], NULL );
> if(status!=0)
> {
> puts("\n Error in pthread_join1()");
> exit(1);
> }
>
> puts("\nEND OF PROGRAM ***");
> exit(1);
> }
>
>
> #define CONFIGURE_MICROSECONDS_PER_TICK 1000 /*one milisecond per tick*/
> #define CONFIGURE_TICKS_PER_TIMESLICE 1 /*each time slice runs for one milisecond*/
> #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
> #define CONFIGURE_POSIX_INIT_THREAD_TABLE
> #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
> #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
> #define CONFIGURE_MAXIMUM_TASKS 20
> #define CONFIGURE_MAXIMUM_POSIX_THREADS 20
> #define CONFIGURE_MAXIMUM_SEMAPHORES 20
> #define CONFIGURE_MAXIMUM_MESSAGE_QUEUES 20
> #define CONFIGURE_MAXIMUM_POSIX_TIMERS 20
> #define CONFIGURE_MAXIMUM_TIMERS 20
> #define CONFIGURE_MAXIMUM_POSIX_QUEUED_SIGNALS 20
> #define CONFIGURE_EXTRA_TASK_STACKS (3 * RTEMS_MINIMUM_STACK_SIZE)
> #include <confdefs.h>
>
>
>
>
>
> ________________________________________________________________________________
> Não recebe as fotografias que lhe tentam enviar? A sua caixa de correio é muito pequena?
> Com o Correio IOL pode enviar e receber ficheiros com 7MB.
> Crie já a sua nova conta de e-mail: http://www.iol.pt/rodape_correio1
>
--
Joel Sherrill, Ph.D. Director of Research & Development
joel at OARcorp.com On-Line Applications Research
Ask me about RTEMS: a free RTOS Huntsville AL 35805
Support Available (256) 722-9985
More information about the users
mailing list