473,383 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

*NIX sockets question

I've started with socket programming and follow question arose:

If, in the server, I get a client/socket connection with function:

Expand|Select|Wrap|Line Numbers
  1. client_sockfd = accept( server_sockfd, (struct sockaddr *) &client_address, &client_len );
Then, is it a problem if I access later on this client_sockfd from two separate threads? Can I close() it from any of them?

I'm thinking of creating a thread to read() the socket and another separate one to write(), since I have asynchronous communication in both ways. Do I need to dup() client_sockfd, or just using the same absolute number for the separate threaded read() and write() is fine?
Nov 19 '08 #1
8 1699
weaknessforcats
9,208 Expert Mod 8TB
Unless the documentation specifically says that this is thread safe, I would use the socket only from the creating thread.
Nov 19 '08 #2
Unless the documentation specifically says that this is thread safe, I would use the socket only from the creating thread.
Aw... that was painful to read.

Seriously, there is no way I can use it from a separate thread? Currently I'm passing the fd number as argument of a new thread. It works. But don't know if me and my app will end up in developer's hell for that.

What about dup()? Would it help here?
Nov 19 '08 #3
donbock
2,426 Expert 2GB
Seriously, there is no way I can use it from a separate thread? Currently I'm passing the fd number as argument of a new thread. It works. But don't know if me and my app will end up in developer's hell for that.
Functions that aren't thread-safe can indeed be used from multiple threads and they will often appear to work fine. The problem is there are certain timing windows where execution in one thread damages another. The program fails sporadically; and the failure mode may be subtle or catastrophic.

If you're lucky you'll see some of these failures while you're testing . If you're not lucky, the failures won't start until you're demonstrating your code to somebody important (boss, teacher, customer,...).

Debugging one of these problems is typically very difficult. After all, there is no flaw in your program; the problem lies in a library function that you don't have the source code for.
Nov 19 '08 #4
Functions that aren't thread-safe can indeed be used from multiple threads and they will often appear to work fine. The problem is there are certain timing windows where execution in one thread damages another. The program fails sporadically; and the failure mode may be subtle or catastrophic.

If you're lucky you'll see some of these failures while you're testing . If you're not lucky, the failures won't start until you're demonstrating your code to somebody important (boss, teacher, customer,...).

Debugging one of these problems is typically very difficult. After all, there is no flaw in your program; the problem lies in a library function that you don't have the source code for.
I'm aware of the problems of not mutexing things and so on and so forth, but what I'm worried here is about the specific issue:

"Is it possible to work on the same file descriptor (for a socket, if that's relevant) from several threads at the same time?"

If not, why not? How can I overcome this?

My problem is that I have to implement bidirectional communication, without no end being a master. I.e., each of the sides can start sending at any time, so I can't afford having a thread stuck at read(), since it won't be able to write() until read() is done (and read() will remain blocked most of the time).

I'm also aware that I can fcntl() the read so that it does not block on call. But that would mean busy-checking the input, and I have 23 threads more in my app demanding execution time.
Nov 19 '08 #5
donbock
2,426 Expert 2GB
I'm aware of the problems of not mutexing things and so on and so forth, but what I'm worried here is about the specific issue:

"Is it possible to work on the same file descriptor (for a socket, if that's relevant) from several threads at the same time?"

If not, why not? How can I overcome this?
Our point is that you're using socket library functions that you didn't write. If their documentation doesn't explicitly say they're thread-safe then you have no way of knowing if they are safe to use in multiple threads. They might be intrinsically thread-safe or they might not be (for instance, they might use static variables). On top of that, you can't make them thread-safe (by wrapping them with mutexes) unless you understand in what way they aren't thread-safe.

Go ahead and use them in multiple threads if you insist; but you can't ever be sure a failure isn't waiting for a good time to occur.
Nov 20 '08 #6
Our point is that you're using socket library functions that you didn't write. If their documentation doesn't explicitly say they're thread-safe then you have no way of knowing if they are safe to use in multiple threads. They might be intrinsically thread-safe or they might not be (for instance, they might use static variables). On top of that, you can't make them thread-safe (by wrapping them with mutexes) unless you understand in what way they aren't thread-safe.
I'm using read(), write() and close(), which are syscalls. I don't know if in the background they're using socket library functions in my case. Are they?

Go ahead and use them in multiple threads if you insist; but you can't ever be sure a failure isn't waiting for a good time to occur.
I'm just defending my point as I see it. My concern is producing quality code, and for discussing something and getting at the top of an issue I have to follow some reasoning line. I can't tell my management "I'm not doing it like this because some guru suggested it might be wrong", and there is no evidence on why; specially if the change requested by the guru has a significant impact on overall performance. Give me a good reason and I'll change it.

Again, my feel is that it is more about how *NIX implements file descriptors rather than questioning the thread-safeness of read() and write(). You also think so?
Nov 20 '08 #7
donbock
2,426 Expert 2GB
My concern is a general one: I have no specific experience with sockets that would lead me to believe the related functions are or aren't thread-safe. I am reacting so strongly because I was burned badly many years ago when I learned the hard way that several functions in our vendor's Standard C Library weren't reentrant. (This was before the term "thread" gained currency.) It took me forever to track down the causes of the intermittent failures. Now I'm careful to check library documentation for claims of thread-safety. Another place to check is the POSIX standard -- I don't remember if it requires certain functions to be thread-safe, but it might. I'm perfectly willing to trust a vendor's claim for thread-safety; but I'm unwilling to assume thread-safety if they don't mention it.

My bad experience was with a cross-compiler we used to develop a foreground/background scheduler in an embedded system; where the library vendor may not have ever expected to face reentrant execution. Your question is about a *nix operating system that explicitly supports threads and processes. Perhaps my reflexive paranoa is uncalled for. You'll have to decide.
Nov 20 '08 #8
Well, you weren't that paranoid. I found out documentation on my OS literally states: "A given socket can be read or written by only one task at a time".

So my will of having separate threads reading and writting is not possible. I'll have to think of something else.

The reason for my question was to take as much advantadge as possible of the OS services: If something is already solved by the OS there is no need to reinvent it, and sometimes it's not easy to find out the solution that best fits the available environment in a way that you don't need to reinvent the wheel.

Sockets appears to be one of this cases in which you have a thousand ways to solve the same problem, with the risk of implementing something with little performance at the expense of high processing requirements.

By now I'll try to implement it using select() syscall instead of my multiple thread approach. I've seen examples using select() to read from several open sockets in the same task, but not using select() to read and write, as I plan to do (so, I don't like it, for being a solution with no references). But I see no other choice (which I suspect is wrong and there might be around other things, but I don't find them).
Nov 21 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
3
by: derSchweiz | last post by:
Hi, Experimenting with sockets, and I think I got it half working. I have the following code: Private Sub socket_receive(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
0
by: richard.charts | last post by:
Well, unfortunately, I couldn't get any answer on the msdn forums, so I'll give it a shot here. Real question at the bottom....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.