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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:43:48+00:00 2026-06-08T06:43:48+00:00

When I try to open /proc/net/tcp from a child POSIX thread in C++ it

  • 0

When I try to open /proc/net/tcp from a child POSIX thread in C++ it fails with a “No such file or directory” error. If I try to open it from the parent thread it succeeds every time, and the process of opening/closing it in the parent thread then makes it succeed about a third of the time in the child thread too. I can open /proc/uptime in the child thread 100% of the time without issue. Here’s some example code which can be compiled with “g++ -Wall test.cc -o test -pthread”:

#include <iostream>
#include <fstream>
#include <cstring>
#include <cerrno>
#include <pthread.h>

using namespace std;

void * open_test (void *)
{
    ifstream in;
    in.open("/proc/net/tcp");
    if (in.fail())
        cout << "Failed - " << strerror(errno) << endl;
    else
        cout << "Succeeded" << endl;
    in.close();

    return 0;
}

int main (int argc, char * argv[])
{
    open_test(NULL);

    pthread_t thread;
    pthread_create(&thread, NULL, open_test, NULL);
    pthread_exit(0);
}

I am running this on an Ubuntu 12.04 box with an Intel i5-2520M (2 cores * 2 virtual cores) on Linux kernel 3.2.0. Here is the output of me running the above code 6 times in a row:

mike@ung:/tmp$ ./test
Succeeded
Failed - No such file or directory
mike@ung:/tmp$ ./test
Succeeded
Succeeded
mike@ung:/tmp$ ./test
Succeeded
Failed - No such file or directory
mike@ung:/tmp$ ./test
Succeeded
Failed - No such file or directory
mike@ung:/tmp$ ./test
Succeeded
Succeeded
mike@ung:/tmp$ ./test
Succeeded
Failed - No such file or directory
mike@ung:/tmp$

It’s probably worth noting that I don’t have this problem if I use fork instead of posix threads. If I use fork, then the child process has no problems reading /proc/net/tcp

Just a couple of data points to throw in…. It looks like this is a regression in Linux as 2.6.35 seems to work 100% of the time. 3.2.0 pukes most of the time even on my slow old Pentium M based laptop.

  • 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-06-08T06:43:49+00:00Added an answer on June 8, 2026 at 6:43 am

    This behavior seems to be a kind of bug in the /proc virtual filesystem. If you add this code just before opening the file:

        system("ls -l /proc/net /proc/self/net/tcp");
    

    You’ll see that /proc/net is a symbolic link to /proc/self/net, and /proc/sec/net/tcp is properly listed for both calls to open_test, even when the spawned thread call fails.

    Edit: I just realized the above test is bogus, since the self would refer to the shell process of the system call, not this process. Using the following function instead also reveals the bug:

    void ls_command () {
        ostringstream cmd;
        cmd << "ls -l /proc/net "
            << "/proc/" << getpid()
            << "/net/tcp "
            << "/proc/" << syscall(SYS_gettid)
            << "/net/tcp";
        system(cmd.str().c_str());
    }
    

    You’ll see that the spawned thread will sometimes not be able to see the parents’ /net/tcp file. In fact it has disappeared, since this is the spawned shell’s process that is running the ls command.

    The workaround below allows the child thread to reliably access what would be its /proc/net/tcp.

    My theory is that it is some kind of race condition bug with correctly setting up the /proc/self entry for the thread as the proper blend of parent state and thread specific state. As a test and work around, I modifed the open_test code to use the “process identifier” associated with the thread, rather than trying to access the parent process’s (because /proc/self refers to the parent process id, not the thread’s).

    Edit: As the evidence indicates, the bug has to do with the parent process cleaning up its /proc/self/... state before the child thread has had a chance to read it. I still maintain this to be a bug, since the child thread is still technically part of the process. It’s getpid() is still the same before and after the main thread calls pthread_exit(). The /proc entry for the parent process should remain valid until all child threads are completed. Even though

    Edit2: Jonas argues this may not be a bug. As evidence of that, there is this from man proc:

           /proc/[pid]/fd
                  ...
                  In  a  multithreaded process, the contents of this directory are
                  not available if the main thread has already  terminated  (typi-
                  ally by calling pthread_exit(3)).

    But then consider this entry for /proc/self in the same man page entry:

           /proc/self
                  This directory refers to the process accessing  the  /proc  file
                  system,  and  is  identical  to the /proc directory named by the
                  process ID of the same process.

    If one is to believe this is not a bug because threads and processes are treated the same in Linux, then threads should have an expectation that /proc/self will work. The bug may easily be fixed by modifying /proc/self to change to use /proc/[gettid] value when the /proc/[getpid] version is no longer available, just as the workaround is doing below.

    void * open_test (void *)
    {
        ifstream in;
        string file = "/proc/net/tcp";
        in.open(file.c_str());
        if (in.fail()) {
            ostringstream ss;
            ss << "/proc/" << syscall(SYS_gettid) << "/net/tcp";
            cout << "Can't access " << file
                 << ", using " << ss.str() << " instead" << endl;
            file = ss.str();
            in.open(file.c_str());
        }
        if (in.fail())
            cout << "Failed - " << strerror(errno) << endl;
        else
            cout << "Succeeded" << endl;
        in.close();
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[EDIT] I try to open a file from a folder in my base directory.
I need to translate this piece of code from Perl to Lua open(FILE, '/proc/meminfo');
when I try to open a file for reading in my console application i
When I try to open a file in write mode with the following code:
When I try to open a project's CodeAnalysis page I get the error An
Whenever I try to open a file with ifstream, it compiles fine, but will
Whenever I try to open .pyc or .pyo files, I always get an error
I'm getting the following error when I try to open up a connection with
I need to open a html help file from within a legacy windows application
I wanted to try open Hello World from here . I already had Visual

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.