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 383281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:16:03+00:00 2026-05-12T15:16:03+00:00

Everyone knows the classic model of a process listening for connections on a socket

  • 0

Everyone knows the classic model of a process listening for connections on a socket and forking a new process to handle each new connection. Normal practice is for the parent process to immediately call close on the newly created socket, decrementing the handle count so that only the child has a handle to the new socket.

I’ve read that the only difference between a process and a thread in Linux is that threads share the same memory. In this case I’m assuming spawning a new thread to handle a new connection also duplicates file descriptors and would also require the ‘parent’ thread to close it’s copy of the socket?

  • 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-12T15:16:04+00:00Added an answer on May 12, 2026 at 3:16 pm

    No. Threads share the same memory, so they share the same variables. If you close socket in parent thread, it will be also closed in child thread.

    EDIT:

    • man fork: The child inherits copies of the parent’s set of open file descriptors.

    • man pthreads: threads share a range of other attributes (i.e., these attributes are process-wide rather than per-thread): […] open file descriptors

    And some code:

    #include <cstring>
    #include <iostream>
    using namespace std;
    
    #include <errno.h>
    #include <fcntl.h>
    #include <pthread.h>
    #include <unistd.h>
    
    // global variable
    int fd = -1;
    
    void * threadProc(void * param) {
        cout << "thread: begin" << endl;
        sleep(2);
        int rc = close(fd);
        if (rc == -1) {
            int errsv = errno;
            cout << "thread: close() failed: " << strerror(errsv) << endl;
        }
        else {
            cout << "thread: file is closed" << endl;
        }
        cout << "thread: end" << endl;
    }
    
    int main() {
        int rc = open("/etc/passwd", O_RDONLY);
        fd = rc;
    
        pthread_t threadId;
        rc = pthread_create(&threadId, NULL, &threadProc, NULL);
    
        sleep(1);
    
        rc = close(fd);
        if (rc == -1) {
            int errsv = errno;
            cout << "main: close() failed: " << strerror(errsv) << endl;
            return 0;
        }
        else {
            cout << "main: file is closed" << endl;
        }
    
        sleep(2);
    }
    

    Output is:

    thread: begin
    main: file is closed
    thread: close() failed: Bad file descriptor
    thread: end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK...I am hoping this is a classic problem that everyone knows the answer to
Not everyone knows that php can handle variables that have first characters as numbers,
I am new to Ruby as probably everyone here knows by now :) I
Everyone knows that: www.example.com/city/new-york is much better than: www.example.com/city?id=43567 Fine, meaningful and human-readable. But
Everyone knows two of the ways to create an empty array: Array.new and []
Everyone knows this using (var db = new DatabaseEntites()) { var row = db.SomeTable.SingleOrDefault(r
Everyone knows the = sign. SELECT * FROM mytable WHERE column1 = column2; However,
as everyone knows Windows does paths with backslashes where Unix does paths with forward
I think everyone knows this site http://pinterest.com/ and I don't want to create site
It seems everyone knows about the dumb little but in PhoneGap that doesn't allow

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.