ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
select() is staying blocked after close() is called, surely this isn't normal? The program connects to my local webserver and waits for input, about 1 second after waiting, threadfunction calls close(), select never returns. Well that's what I get when the program is run from a shell, if it's run in gdb, select returns -1 and errno is 9 (bad file descriptor).
Is this normal behaviour? ...I can't see how it is. Can somebody please compile my code and report the behaviour. I'm feeling my system is screwed up
The way the program is written there is no guarantee which will execute first, the statements after pthread_create() or the code in the thread function.
Its possible the debugger is just intrusive enough to change the outcome.
If the thread function closes the socket before the select call then a socket error should occur.
On the other hand if the select call is first then it should block.
Even if I used a syncronisation object, I couldn't be sure which was called first. sleep() should suffice though, the main thread should be running while threadfunction is sleeping. The weird thing is, when I stop the process (ctrl z) during sleep(1), I can see select has been called, looking at thread 1's backtrace, yet when the process runs through it's as if close was called first.
Anywayz, the issue with the debugger wasn't my original problem. My original problem was, select is not returning after close is called.
Quote:
On the other hand if the select call is first then it should block.
You're kidding me? But it doesn't make any sense to keep an i/o function blocked when the file or socket has been closed - it will never return.
btw, program still blocks when select is replaced by recv.
I know it seems like a good idea to have select() break when one of the file descriptors its waiting on is no longer valid, but actually thats not how it works. Select simply waits for the data read or write events. When the socket is closed this is not a data read event so select() does not break.
I always use non-blocking sockets so send() does not block, and supply a timeout with select() to test for received data. Its the user's responsibility to test for errors and handle them, select() is not designed to do that for the user.
It's fixed! I'm using shutdown() instead. I read somewhere that close() only closes the connection when the descriptor's reference count is equal to 0, I suspect this is the problem?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.