Subject: Re: select call implementation and threads
To: malleswararao venkatanaga <bobbyavn@yahoo.com>
From: Bill Studenmund <wrstuden@netbsd.org>
List: tech-userlevel
Date: 05/27/2003 13:37:51
On Sun, 25 May 2003, malleswararao venkatanaga wrote:

> --- Andrey Petrov <petrov@netbsd.org> wrote:
> > On Sat, May 24, 2003 at 04:26:11AM -0700,
> > malleswararao venkatanaga wrote:
> > > Here is the code i tested with.
> > > /* Recv.c This is the code that receives messages
> > */
> > > #include <stdio.h>
> > > #include<fcntl.h>
> > > #include<errno.h>
> > > #include<sys/types.h>
> > > #include<sys/select.h>
> > > #include<sys/socket.h>
> > > #include<netinet/in.h>
> > > #include <pthread.h>
> > >
> > > void * stub_test (void *p );
> > > pthread_t  threads[3];
> > > int main()
> > > {
> > >   int rc;
> > >   stub_test2();
> > >   rc = pthread_create(&threads[0], NULL ,
> > stub_test
> > > ,(void *) &threads[0]);
> >
> > Well, well, well. Try to reverse 2 previous lines.
> >
> Hi Andrey,
> I've reversed the above 2 lines. But i still get
> non-deterministic results, one of the threads gets
> blocked and never comes out of the select call though
> it has messages to read.
>
> Probably there is something to do with the scheduling
> of the threads as the thread which is receiving and
> processing the messages is never yielding the other
> thread is never getting a chance to run through its
> steps and break out of the select call.
> Surprisingly the thread which gets blocked in the
> select call should have actually tried to yield to
> the others which is not happening.

No, it actually is yielding. 1.6 doesn't have kernel-assisted pthreads. To
the kernel there is only one thread. So the fact that one or the other
runs means that there is some yielding going on.

I think the key problem is that you're trying to have two simultaneous
select calls going on. Since to the kernel there is only one thread, there
can only be one select going on at once.

For what you want to have happen to work, the userland thread package has
to help. It has to consolidate both selects into one.

Take care,

Bill