Networking Forums

Networking Forums > Computer Networking > Linux Networking > Are sockets thread safe

Reply
Thread Tools Display Modes

Are sockets thread safe

 
 
jainarunk@gmail.com
Guest
Posts: n/a

 
      06-12-2006, 08:38 PM
Hello Group,

I am working on Linux environments. The kernal version is 2.4.21 and
my question is;


Are sockets on Linux thread safe. For example if the socke it open and

one thread was
reading from the socket and had to relinquish CPU to make way for
another thread, And
then the other thread is trying to 'Write' to the same socket.


What will be the behavior.


If these libraries are not thread safe then what are my alternatives


1. onLinux
2. on Unix


Thanks for your help.


nagrik


ps: I have read the man pages for sockets and they don't say anything
about thread safety.

 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a

 
      06-12-2006, 08:59 PM

jainar...@gmail.com wrote:
> Hello Group,
>
> I am working on Linux environments. The kernal version is 2.4.21 and
> my question is;
>
>
> Are sockets on Linux thread safe. For example if the socke it open and
>
> one thread was
> reading from the socket and had to relinquish CPU to make way for
> another thread, And
> then the other thread is trying to 'Write' to the same socket.


Linux kernel structures are protected by locks so that one can not
screw them up by several threads doing calls on the same socket. You
should care to protect you logic data stream consistency when several
threads operate on the same stream.

 
Reply With Quote
 
Dan N
Guest
Posts: n/a

 
      06-13-2006, 01:29 AM
On Mon, 12 Jun 2006 13:38:08 -0700, jainarunk wrote:

> Are sockets on Linux thread safe.


Socket read and write are independent operations, it doesn't matter if
they're done in different threads. There are, however, some
idiosyncrasies when it comes to closing them.

Dan



 
Reply With Quote
 
jainarunk@gmail.com
Guest
Posts: n/a

 
      06-13-2006, 06:06 PM

Dan N wrote:
> On Mon, 12 Jun 2006 13:38:08 -0700, jainarunk wrote:
>
> > Are sockets on Linux thread safe.

>
> Socket read and write are independent operations, it doesn't matter if
> they're done in different threads. There are, however, some
> idiosyncrasies when it comes to closing them.
>
> Dan


Dan,

Thanks for your reply. But I am still confused. I understood that
read and write
operations are independent. But what will happen in the following
scenario;

one thread was
reading from the socket and had to relinquish CPU to make way for
another thread,

And then the other thread is trying to 'Write' to the same socket.

In this situation there may be some data in the socket buffer/pipeline
(if there is any),
which is not processed by the read operation because CPU exited the
first thread.

Will the data on socket get corrupted whenn the second thread tries to
write to the
same socket.

There are, however, some
> idiosyncrasies when it comes to closing them.
>


And what you mean with idiosyncrasies, in the event of closing them.

If you could please clarify this.


Thanks

nagrik

 
Reply With Quote
 
jainarunk@gmail.com
Guest
Posts: n/a

 
      06-13-2006, 10:41 PM

Dan N wrote:
> On Mon, 12 Jun 2006 13:38:08 -0700, jainarunk wrote:
>
> > Are sockets on Linux thread safe.

>
> Socket read and write are independent operations, it doesn't matter if
> they're done in different threads. There are, however, some
> idiosyncrasies when it comes to closing them.
>
> Dan


Dan,

I am also confused about two threads writing to same socket at the
concurrently.

Thread 1: write "Hello World"

Thread 2: write "Hello World"

Can the end result on the other side of the socket be

Hel Hello lo Wor World ld.

Similaly if two threads are reading from the same socket the string
specified above

The other end of the socket input is

"Networking code is difficult."

Can the result be as follows

Thread 1: Network
Thread 2: ing co
Thread 1: de is
Thread 2: diff
Thread 1: ic
Thread 2: ult.

And of course the question still remains when two threads are trying to
do simultanious
read and write. What will happen to data at both ends.

Thanks.

nagrik

 
Reply With Quote
 
Phil Frisbie, Jr.
Guest
Posts: n/a

 
      06-13-2006, 11:19 PM
(E-Mail Removed) wrote:

> Dan N wrote:
>
>>On Mon, 12 Jun 2006 13:38:08 -0700, jainarunk wrote:
>>
>>
>>>Are sockets on Linux thread safe.

>>
>>Socket read and write are independent operations, it doesn't matter if
>>they're done in different threads. There are, however, some
>>idiosyncrasies when it comes to closing them.
>>
>>Dan

>
>
> Dan,
>
> I am also confused about two threads writing to same socket at the
> concurrently.
>
> Thread 1: write "Hello World"
>
> Thread 2: write "Hello World"
>
> Can the end result on the other side of the socket be
>
> Hel Hello lo Wor World ld.


If the socket is TCP, then yes, but if the socket is UDP then no.

> Similaly if two threads are reading from the same socket the string
> specified above
>
> The other end of the socket input is
>
> "Networking code is difficult."
>
> Can the result be as follows
>
> Thread 1: Network
> Thread 2: ing co
> Thread 1: de is
> Thread 2: diff
> Thread 1: ic
> Thread 2: ult.


Again, with TCP is is possible, but with UDP it is not.

> And of course the question still remains when two threads are trying to
> do simultanious
> read and write. What will happen to data at both ends.


In most cases it makes no sense to have multiple threads read or writing to a
socket, unless the socket is UDP and you are processing separate messages.

However, as has already been explained, it is possible, and actually common to
have one thread reading from a socket and another writing to the same socket
with no problems.

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com
 
Reply With Quote
 
Rick Jones
Guest
Posts: n/a

 
      06-14-2006, 12:08 AM
(E-Mail Removed) <(E-Mail Removed)> wrote:
> Thanks for your reply. But I am still confused. I understood that
> read and write operations are independent. But what will happen in
> the following scenario;


> one thread was reading from the socket and had to relinquish CPU to
> make way for another thread,


> And then the other thread is trying to 'Write' to the same socket.


> In this situation there may be some data in the socket
> buffer/pipeline (if there is any), which is not processed by the
> read operation because CPU exited the first thread.


> Will the data on socket get corrupted whenn the second thread tries
> to write to the same socket.


A socket can be considered "full-duplex" in this regard. Inbound and
outbound socket buffers are separate.

rick jones
--
oxymoron n, commuter in a gas-guzzling luxury SUV with an American flag
these opinions are mine, all mine; HP might not want them anyway...
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
 
Reply With Quote
 
Dan N
Guest
Posts: n/a

 
      06-14-2006, 02:19 AM
On Tue, 13 Jun 2006 15:41:45 -0700, (E-Mail Removed) wrote:


> Thread 1: write "Hello World"
>
> Thread 2: write "Hello World"
>
> Can the end result on the other side of the socket be
>
> Hel Hello lo Wor World ld.


I'm not sure, but I would expect that it wouldn't. I would think that
each thread would finish it's write, but I wouldn't count on it.

>
> Similaly if two threads are reading from the same socket the string
> specified above
>
> The other end of the socket input is
>
> "Networking code is difficult."
>
> Can the result be as follows
>
> Thread 1: Network
> Thread 2: ing co
> Thread 1: de is
> Thread 2: diff
> Thread 1: ic
> Thread 2: ult.


I think that each thread would finish its read before the next thread
started, but, again, I wouldn't count on it.

As someone said in one of the other replies, it doesn't make sense to read
from different threads.

If you want to share data between threads, you're probably better off
using some other method to do so rather than reading from the same socket.

Dan



 
Reply With Quote
 
jainarunk@gmail.com
Guest
Posts: n/a

 
      06-14-2006, 05:05 PM

Phil Frisbie, Jr. wrote:
> (E-Mail Removed) wrote:
>
> > Dan N wrote:
> >
> >>On Mon, 12 Jun 2006 13:38:08 -0700, jainarunk wrote:
> >>
> >>
> >>>Are sockets on Linux thread safe.
> >>
> >>Socket read and write are independent operations, it doesn't matter if
> >>they're done in different threads. There are, however, some
> >>idiosyncrasies when it comes to closing them.
> >>
> >>Dan

> >
> >
> > Dan,
> >
> > I am also confused about two threads writing to same socket at the
> > concurrently.
> >
> > Thread 1: write "Hello World"
> >
> > Thread 2: write "Hello World"
> >
> > Can the end result on the other side of the socket be
> >
> > Hel Hello lo Wor World ld.

>
> If the socket is TCP, then yes, but if the socket is UDP then no.
>
> > Similaly if two threads are reading from the same socket the string
> > specified above
> >
> > The other end of the socket input is
> >
> > "Networking code is difficult."
> >
> > Can the result be as follows
> >
> > Thread 1: Network
> > Thread 2: ing co
> > Thread 1: de is
> > Thread 2: diff
> > Thread 1: ic
> > Thread 2: ult.

>
> Again, with TCP is is possible, but with UDP it is not.
>
> > And of course the question still remains when two threads are trying to
> > do simultanious
> > read and write. What will happen to data at both ends.

>
> In most cases it makes no sense to have multiple threads read or writing to a
> socket, unless the socket is UDP and you are processing separate messages.
>


Phil,

The need arose, because I am writing a client for a web server, where
each thread in
the client wraps its own request for a paraticular web page from the
server and writes
to the same web server on the other end of the same socket. And of
course each
thread would be interested to read the web page for it sent the
request. I hope it
explains.

Thanks for your reply.

> However, as has already been explained, it is possible, and actually common to
> have one thread reading from a socket and another writing to the same socket
> with no problems.
>
> --
> Phil Frisbie, Jr.
> Hawk Software
> http://www.hawksoft.com


 
Reply With Quote
 
Phil Frisbie, Jr.
Guest
Posts: n/a

 
      06-14-2006, 06:28 PM
(E-Mail Removed) wrote:

> Phil,
>
> The need arose, because I am writing a client for a web server, where
> each thread in
> the client wraps its own request for a paraticular web page from the
> server and writes
> to the same web server on the other end of the same socket.


Why use the same socket? You are making your client GREATLY more complex, just
to save a few sockets from being created? The complexity to keep track of each
response and only let the correct thread read that response will likely lead to
an error prone client that will be very hard to debug.

> And of
> course each
> thread would be interested to read the web page for it sent the
> request. I hope it
> explains.


You have explained WHAT you want to do, but not WHY you have chosen to do it
this way. Perhaps if you explain your needs we can suggest a better way to do it.

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does select thread safe in kernel 2.4.x Readon Shaw Linux Networking 1 09-07-2006 07:20 AM
Not a Plusnet thread ;o) Andy M Jenkins Broadband 0 11-30-2004 07:10 AM
Red Hat thread sync question Ramki Linux Networking 5 11-23-2004 11:04 PM
This thread is not the only thing thats wireless...... political commentator Wireless Internet 0 10-26-2004 09:53 PM
Yet another Losing Connections thread David Windows Networking 1 02-27-2004 08:40 PM



1 2 3 4 5 6 7 8 9 10 11