Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4339938
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:16:57+00:00 2026-05-21T11:16:57+00:00

Before voting to close, please read, I know that there are similar questions (:

  • 0

Before voting to close, please read, I know that there are similar questions (:

Here’s my situation – I have an application, that is multithreaded. So, lets say I have 10 threads. All of them read from the same file descriptor (it’s actually a socket ). And in a very rare situation, when a critical error occurs, the socket should be shutdown by one of the threads. The thing is, that any of these thread can do this. If the closing of the socket has failed, _Exit( FAILURE ) is executed (I know, that this sounds like an awful design or problem in the code, but it actually isn’t, as this is caused by a non-opensource 3rd party lib, that has a bug).

And here’s the problem situation – it’s possible all of them to try to shutdown the socket in the same time. And one closes it, but the others cannot close it (shutdown returns -1, as the socket is already closed) and the bad _Exit( FAILURE ) is executed and that ruins everything.

Obviously, I need an additional check – if the socket is already closed (it’s possible all threads to have failed shutting down the socket for some reason, and then at least one must execute _Exit, that’s why checking the return code of shutdown is not enough).

Well, I found this question and it looks like that’s exactly what I’m trying to do.
But I know, that any kind of system calls take time (of course ) and it’s OS dependent when exactly the socket will be closed.

And here’s the question – how can I make difference if a socket is already closed or it cannot be closed for some reason? Will the fcntl ensure me, that if one thread has closed the socket and at the same time if other thread try to shutdown the socket, it will fail and then, if I make this check ( with fcntl ), this will work for me?

I also saw the other answers like: "you can use select or poll", but they are still system calls and I don’t know if they will be the better choice. I also don’t know how exactly to use them, but it’s not a big deal, I guess.

Thanks!


I can also check the errno set by shutdown, but what does "connected" mean? And what is the difference between "connected" and "not a valid descriptor"

ENOTCONN
    The specified socket is not connected.

Also, what bothers me is, that the FD, I’m trying to close, could be invalid, as I take it from /proc/net/tcp mapped with proc/PID/fd and I don’t know if all files will look like the way, they look on my OS (the OS will be for sure RHEL4 or RHEL5, if that matters)

Doh! It’s damn long, but I can’t explain it shorter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:16:58+00:00Added an answer on May 21, 2026 at 11:16 am

    I assume you’re saying it’s possible for your application to reasonably continue after shutting down the socket?

    It seems that a better approach would be to have a mediator thread that gets notified of socket close requests from any of the worker threads, notifies the other threads that the socket is now dead, and takes care of closing the socket itself. This eliminates the worries about the reason for failure because it’s all handled in a single thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I start, I know there is this post and it doesn't answer my
Before you answer this I have never developed anything popular enough to attain high
Before I jump headlong into C#... I've always felt that C, or maybe C++,
Before, I have found the Cost in the execution plan to be a good
Before my app was on Heroku and used mysql gem. When I migrate that
Before copy on write (COW) , when it says that the parent and child
Before I do this I figured I would ask if it was the best
Before moving on to use SVN, I used to manage my project by simply
Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of
Before I upgraded to Firefox 3 I used to constantly use the View Source

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.