Help: Threading App... Is this strange behaiviour???

Joel Sherrill <joel@OARcorp.com> joel.sherrill at OARcorp.com
Tue Aug 10 13:29:22 UTC 2004


Alex wrote:
> Hi all,
> I am beginning to write rtems threading apps. I first wrote a very simple app just to test the waiting for the child thread. See code below.
> When I execute the app I see the screen output two times (replicated)!
> I cant see why it is happening... Can you help me?

Hard to say exactly why it is happening but it looks like the program
is somehow interacting with the BSP exit code to resetting itself.  Try
putting an explicit call to exit() after the "END OF PROGRAM" puts().

> The output I get when I run the code below in a intel 686, rtems 4.6 is the following:
> 
> STARTING THE MAIN THREAD
> Creating a secondary thread of SCHED_RR with priority 1
> Main Thread: Waitting for the second thread to finish
> Secondary Thread: begining
>    -> “The execution pauses a little at this point, due to the thread cicle
> Secondary Thread: Finishing
> *** END OF PROGRAM ***
> 
> STARTING THE MAIN THREAD
> Creating a secondary thread of SCHED_RR with priority 1
> Main Thread: Waitting for the second thread to finish
> Secondary Thread: begining
>    -> “The execution pauses a little at this point, due to the thread cicle
> Secondary Thread: Finishing
> *** END OF PROGRAM ***
> 
> Why the output appears two times?
> 
> 
> 
> #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, &current );
>   }
> 
>   puts("\nSecondary Thread: finishing" );
>   pthread_exit( NULL );
>   return NULL;
> }
> 
> 
> #define CONFIGURE_INIT_TASK_ENTRY_POINT   POSIX_Init
> void POSIX_Init(void* args)
> {
>   pthread_t        Task_id;
>   pthread_t        Init_id;
>   int              status;
>   pthread_attr_t   attr;
>   struct sched_param  param;
> 
> 
>   puts( "\n\n STARTING THE MAIN THREAD" );
>   /* get id of this thread */
>   Init_id = pthread_self();
>   /*SET MAIN THREAD SCHEDULER*/
>   param.sched_priority = 1;
>   status = pthread_setschedparam( Init_id, SCHED_RR, &param );
> 
> 
>   /* Create a second thread as SCHED_RR */
>   puts( "\nCreating a secondary thread of SCHED_RR with priority 1" );
>   status = pthread_attr_init( &attr );
>   attr.schedpolicy = SCHED_RR;
>   attr.schedparam.sched_priority = 1;
>   status = pthread_create( &Task_id, &attr, Task_1, NULL );
> 
>   puts( "\nMain Thread: Waitting for the second thread to finish" );
>   status = pthread_join( Task_id, NULL );
> 
>   puts( "*** END OF PROGRAM ***" );
> }
> 
> 
> 
> #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>
> 
> 
> 
> ___________________________________________________________________________________________
> Sabia que o e-mail do IOL é o e-mail nacional mais usado pelos Portugueses?
> Estudo realizado pela Multidados, Empresa de consultoria e tratamento estatístico de dados.
> http://www.multidados.com/
> 


-- 
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