Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

select: Invalid Argument

49 views
Skip to first unread message

Neel

unread,
Oct 27, 2008, 10:51:14 PM10/27/08
to
Hi,
I 'm facing one minor problem but it is crashing my program.
I have two threads in a program in Infinite loop.

On one thread I just have select(NULL,NULL,NULL,NULL, time); to make
it sleep for "time" amount of time.
In the second thread 'm receiving that on socket but before that I
have select(sockfd+1, &fds, NULL,NULL, time);
now this gives me an error "select: Invalid Argument"

my code is


if(select(sock+1, &fds, NULL, NULL, &tv) == -1)
{
perror("select");
exit(0);
}

if (FD_ISSET(sock, &fds)){
printf("YES...");
}


In nutshell:
Thread 1:
while(1){
sleep for some time

create Thread and make it execute function THREAD-2
}

Thread 2:
while(1){
select on socket for some time

check if data received

yes? take action

no? continue

}

When I was not checking the return value of select on socket, my
thread was running infinitely give NO CHANCE to other parent thread to
execute. I dont understand what's wrong with this code.

Can anyone pls help me with this?

Thanks in advance

Ian Collins

unread,
Oct 27, 2008, 10:57:49 PM10/27/08
to
Neel wrote:
> Hi,
> I 'm facing one minor problem but it is crashing my program.
> I have two threads in a program in Infinite loop.
>
> On one thread I just have select(NULL,NULL,NULL,NULL, time); to make
> it sleep for "time" amount of time.
> In the second thread 'm receiving that on socket but before that I
> have select(sockfd+1, &fds, NULL,NULL, time);
> now this gives me an error "select: Invalid Argument"

<snip>

> In nutshell:
> Thread 1:
> while(1){
> sleep for some time
>
> create Thread and make it execute function THREAD-2
> }
>
> Thread 2:
> while(1){
> select on socket for some time
>
> check if data received
>
> yes? take action
>
> no? continue
>
> }
>

Don't use select for your sleep, use nanosleep.

Use non-blocking mode for the data socket with select, or blocking mode
without select.

--
Ian Collins

David Schwartz

unread,
Oct 28, 2008, 6:57:38 PM10/28/08
to
On Oct 27, 7:51 pm, Neel <a.k.v...@gmail.com> wrote:

> Can anyone pls help me with this?

Odds are there is a bug in code you haven't posted.

DS

0 new messages