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

  • Home
  • SEARCH
  • 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 7546361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:10:42+00:00 2026-05-30T09:10:42+00:00

I am working on a multithreaded program. It’s able to close all the threads

  • 0

I am working on a multithreaded program. It’s able to close all the threads properly, however, it segfaults at the end. Through commenting out certain parts of my code, I found that it is within this area of the code that deals with opening/closing a file:

char *pid_fname;
FILE *file;

sprintf(pid_fname, "%s%d%s", "/proc/", pid, "/stat");
file = fopen(pid_fname, "r");

/* code */

fclose(file);

I tried debugging in gdb, however I only get this after printing out ‘where’ after the segfault:

#0  0x2f312f63 in ?? ()
#1  0x74617473 in ?? ()
#2  0xbfaee700 in ?? ()
#3  0xbfaee77c in ?? ()
#4  0x006a7810 in ?? ()
#5  0x00000000 in ?? ()

Can anyone give me some pointers on where to go from here?

  • 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-30T09:10:43+00:00Added an answer on May 30, 2026 at 9:10 am

    You have no backing storage for the file name. You’ve created a pointer but have allocated no space. That means it’s almost certainly pointing somewhere where you don’t want to write to 🙂

    Assuming you know the maximum range of a process ID (say 5 digits for example), the simplest fix is something like (and changing the arguments slightly since /proc/ and /stat are fixed strings):

    char pid_fname[sizeof("/proc/99999/stat")];
    sprintf(pid_fname, "/proc/%d/stat", pid);
    

    Otherwise, you’ll need to dynamically allocate enough space based on the actual pid value, and code defensively if you run out of memory.

    Since systems tend to have a fixed range for process IDs, I’d opt for the fixed size buffer. If you really want to protect from bugs whilst still not worrying about dynamic allocation, you could use something like:

    char pid_fname[sizeof("/proc/99999/stat")];
    if ((pid < 0) || (pid > 99999)) {
        fprintf (stderr, "WTH? What sort of PID was that (%d)?\n", pid);
        exit (1);
    }
    sprintf(pid_fname, "/proc/%d/stat", pid);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a multithreaded program using C++ and Boost. I am using
I am working on a multithreaded application (C#), and 2 threads are updating a
I'm working on a multithreaded program in Java that uses a shared array to
I'm working on a multithreaded multi-platform Qt-based program that uses libssh2. The program was
I am working on a multithreaded game in java. I have several worker threads
i'm working with a multi-threaded program (using pthreads) that currently create a background thread
I'm working on a multithreaded C++ application that is corrupting the heap. The usual
I'm working on a multithreaded application, and I want to debug it using GDB.
I have very little experience working with sockets and multithreaded programming so to learn
I'm working on a multi-threaded Silverlight application. The application has two threads: Main/UI and

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.