Thread scheduling problem

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Thread scheduling problem

vasantha.rajan-2
Hi Gary,

Thanks for your reply,but still i cant get the problem.

I will give two set of programs below.

program 1:

void cyg_user_start()
{
       
        cyg_thread_create(10, &counter_thread, 0 ,"counter_thread", stack, STACKSIZE,    
&handle, &thread);
        cyg_thread_resume(handle);
        printf("End of start");
}

//      The thread resumed
void counter_thread(cyg_addrword_t data)
{
        printf("inside counter thread\n");
        cyg_thread_delay(10);
       printf("delay completed\n");
}


program 2:

void cyg_user_start()
{
        cyg_semaphore_init(&sem,val);
        cyg_thread_create(10, &counter_thread, 0 ,"counter_thread", stack, STACKSIZE,    
&handle, &thread);
        cyg_thread_resume(handle);
        cyg_semaphore_wait(&sem);       // Initial value of val is 0
  printf("End of start");
}

//      The thread resumed
void counter_thread(cyg_addrword_t data)
{
        printf("inside counter thread\n");
        cyg_thread_delay(10);
         printf("delay completed\n");
}


when i run program 1:The thread is resumed after scheduler  starts and the
thread waits for the stipulated amount of time and again resumes and prints
the statement "delay completed".

when i run program 2:The thread is resumed  before cyg_user_start() ends
ie,before the scheduler starts(I guess...) and it waits infinitely thereonly
and the statement "delay completed" is not printed.(I am not posting the
semaphore... I think that wont be a problem...)

So my question is

1.What is the difference between the two codes??
2.Does scheduler comes into picture in both the codes??
3.In code 2 why does the program waits infinitely?(It means the scheduler
doesnt have control over that particular thread)


sorry for a lengthy mail....

Thanks

Regards
Vasanth


--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

Reply | Threaded
Open this post in threaded view
|

Re: Thread scheduling problem

Gary Thomas
On Tue, 2005-12-27 at 18:19 +0530, vasantha.rajan wrote:

> Hi Gary,
>
> Thanks for your reply,but still i cant get the problem.
>
> I will give two set of programs below.
>
> program 1:
>
> void cyg_user_start()
> {
>
> cyg_thread_create(10, &counter_thread, 0 ,"counter_thread", stack, STACKSIZE,    
> &handle, &thread);
> cyg_thread_resume(handle);
> printf("End of start");
> }
>
> //      The thread resumed
> void counter_thread(cyg_addrword_t data)
> {
> printf("inside counter thread\n");
> cyg_thread_delay(10);
>        printf("delay completed\n");
> }
>
>
> program 2:
>
> void cyg_user_start()
> {
> cyg_semaphore_init(&sem,val);
> cyg_thread_create(10, &counter_thread, 0 ,"counter_thread", stack, STACKSIZE,    
> &handle, &thread);
> cyg_thread_resume(handle);
> cyg_semaphore_wait(&sem);       // Initial value of val is 0
>   printf("End of start");
> }
>
> //      The thread resumed
> void counter_thread(cyg_addrword_t data)
> {
> printf("inside counter thread\n");
> cyg_thread_delay(10);
> printf("delay completed\n");
> }
>
>
> when i run program 1:The thread is resumed after scheduler  starts and the
> thread waits for the stipulated amount of time and again resumes and prints
> the statement "delay completed".
>
> when i run program 2:The thread is resumed  before cyg_user_start() ends
> ie,before the scheduler starts(I guess...) and it waits infinitely thereonly
> and the statement "delay completed" is not printed.(I am not posting the
> semaphore... I think that wont be a problem...)
>
> So my question is
>
> 1.What is the difference between the two codes??

You shouldn't call any function which may block (i.e.
cyg_semaphore_wait) until the scheduler (and interrupts) have been
started.

> 2.Does scheduler comes into picture in both the codes??
> 3.In code 2 why does the program waits infinitely?(It means the scheduler
> doesnt have control over that particular thread)

No, it simply means you violated the rules for what can be called
when.  The scheduler isn't a separate thread (or any other sort of
special context) - it's merely the choide of what happens after some
sort of scheduling event, e.g. when one thread is suspended or a
timer fires and makes a thread ready to run, etc.  If you call a
function like cyg_semaphore_wait() which will cause one thread to
block and hence pass control (via scheduling to another thread) before
the interrupts have been enabled (a side effect of calling
cyg_scheduler_start - which happens when cyg_user_start completes),
then yes the other thread will run, but since there are no interrupts,
the cyg_thread_delay will never complete.

--
------------------------------------------------------------
Gary Thomas                 |  Consulting for the
MLB Associates              |    Embedded world
------------------------------------------------------------


--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

Reply | Threaded
Open this post in threaded view
|

Re: Thread scheduling problem

Gary Thomas
Please keep your replies on the mailing list so that all may benefit.
Private email support is available only with a support contract.

On Tue, 2005-12-27 at 19:49 +0530, vasantha.rajan wrote:
> On Tuesday 27 December 2005 18:35, you wrote:
>
> Hi Gary ,
>
> Thanks for your reply.....
>
> 1.So, from your first answer, I had understood that no threads should be
> resumed before scheduler_start().
>

No - threads can be resumed anytime, including before the scheduler is
started.

> 2.And from your second answer, i had understood that Interrupts will be
> enabled only after scheduler starts,but still I cant get what is relation
> bewteen interrupts and thread scheduling (in context to the code what i have
> typed previous)

The scheduler and interrupts are not directly related - it's just that
unless interrupts are enabled, there won't be a system timer running and
your call to cyg_thread_delay() will never complete, thus the second
thread gets stuck.

>
> 3. I violate the rule by scheduling the Thread before scheduler_start(), but
> still does the scheduler have control over this particular thread or the
> thread is like an independent function.
>
> As we are new to eCos we are lagging in fundas..

Ah, but you have the sources, so everything you need is available to
you!

>
>
> Thanks
>  
> Regards
> Vasanth
>  On Tue, 2005-12-27 at 18:19 +0530, vasantha.rajan wrote:
> > > Hi Gary,
> > >
> > > Thanks for your reply,but still i cant get the problem.
> > >
> > > I will give two set of programs below.
> > >
> > > program 1:
> > >
> > > void cyg_user_start()
> > > {
> > >
> > > cyg_thread_create(10, &counter_thread, 0 ,"counter_thread", stack,
> > > STACKSIZE, &handle, &thread);
> > > cyg_thread_resume(handle);
> > > printf("End of start");
> > > }
> > >
> > > //      The thread resumed
> > > void counter_thread(cyg_addrword_t data)
> > > {
> > > printf("inside counter thread\n");
> > > cyg_thread_delay(10);
> > >        printf("delay completed\n");
> > > }
> > >
> > >
> > > program 2:
> > >
> > > void cyg_user_start()
> > > {
> > > cyg_semaphore_init(&sem,val);
> > > cyg_thread_create(10, &counter_thread, 0 ,"counter_thread", stack,
> > > STACKSIZE, &handle, &thread);
> > > cyg_thread_resume(handle);
> > > cyg_semaphore_wait(&sem);       // Initial value of val is 0
> > >   printf("End of start");
> > > }
> > >
> > > //      The thread resumed
> > > void counter_thread(cyg_addrword_t data)
> > > {
> > > printf("inside counter thread\n");
> > > cyg_thread_delay(10);
> > > printf("delay completed\n");
> > > }
> > >
> > >
> > > when i run program 1:The thread is resumed after scheduler  starts and
> > > the thread waits for the stipulated amount of time and again resumes and
> > > prints the statement "delay completed".
> > >
> > > when i run program 2:The thread is resumed  before cyg_user_start() ends
> > > ie,before the scheduler starts(I guess...) and it waits infinitely
> > > thereonly and the statement "delay completed" is not printed.(I am not
> > > posting the semaphore... I think that wont be a problem...)
> > >
> > > So my question is
> > >
> > > 1.What is the difference between the two codes??
> >
> > You shouldn't call any function which may block (i.e.
> > cyg_semaphore_wait) until the scheduler (and interrupts) have been
> > started.
> >
> > > 2.Does scheduler comes into picture in both the codes??
> > > 3.In code 2 why does the program waits infinitely?(It means the scheduler
> > > doesnt have control over that particular thread)
> >
> > No, it simply means you violated the rules for what can be called
> > when.  The scheduler isn't a separate thread (or any other sort of
> > special context) - it's merely the choide of what happens after some
> > sort of scheduling event, e.g. when one thread is suspended or a
> > timer fires and makes a thread ready to run, etc.  If you call a
> > function like cyg_semaphore_wait() which will cause one thread to
> > block and hence pass control (via scheduling to another thread) before
> > the interrupts have been enabled (a side effect of calling
> > cyg_scheduler_start - which happens when cyg_user_start completes),
> > then yes the other thread will run, but since there are no interrupts,
> > the cyg_thread_delay will never complete.
>
--
------------------------------------------------------------
Gary Thomas                 |  Consulting for the
MLB Associates              |    Embedded world
------------------------------------------------------------


--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss