lkml.org 
[lkml]   [1999]   [Nov]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRE: select() ?
Date

You need to carefully manage the lifetimes of shared objects, which you are
not doing. Do not 'close' a socket while another thread may still be using
it. If the socket is a TCP connection, you can shutdown the socket (with
'shutdown') to stop any I/O on it and ensure that other threads get errors
when they try to use it. Then as sure as you know no thread could still be
using the socket, you can 'close' it.

But calling 'close' on a socket that is in use by another thread risks many
race conditions. Don't ever do it.

Think of 'socket' like 'malloc' and 'close' like 'free'. Would you 'free'
an object while another thread was trying to use it?

DS

> My program had 3 threads:
>
> 1. while(1) {
> sk = accept( ... ) ;
> insert sk into a socket queue
> ...
> }
>
> 2. ...
> select()
> for ( every in socket queue )
> if ( FD_SET( sk, ... ) )
> read( sk, buf, len )
> ...
>
> 3. if ( some condition match on a socket )
> close( sk )
>
> Now I accept a new socket, such as sk = 19,
> In thread 3, I detect a condition, and close(19), then I connect server
> again, accept() return 19 (Now 19 is a free file slot).
>
> But in thread2, the select() return, and read(19) return -1, errno is
> "Bad file number".
> It seem that after select(), thread 2 only know 19 is closed, but don't
> know
> a new 19 is open. ( I connect server about 1min after close a previous
> socket,
> so select() return 1 min after 19 is closed ).
>
> Can any one help me !
>
>
>
>
>
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.rutgers.edu
> Please read the FAQ at http://www.tux.org/lkml/
>


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 13:54    [W:0.064 / U:0.036 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site