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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:06:53+00:00 2026-06-13T00:06:53+00:00

I have a function which loops through the directories in the proc file system.

  • 0

I have a function which loops through the directories in the proc file system. This function then greps a process name to find its PID and returns this PID to the calling function.

The function seems to work fine but fails in one or two cases while opening some directory(corresponding to a process).
This is what I am doing.

 dr = readdir(dp); 

Loop through dr
Check dr type for directory and process name
compare the process name with a string.

 Return PID in case of a match 
 dr = readdir(dp);
 end loop 



main() {
   DIR *d;
   struct dirent *e;

   e=malloc(sizeof(struct dirent));
   d=opendir("/proc");

   while ((e = readdir(d)) != NULL) {
      printf("%d %s\n", e->d_type, e->d_name);
   }

   closedir(d);
}
  • 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-13T00:06:54+00:00Added an answer on June 13, 2026 at 12:06 am

    Presumably the problem is that directories are disappearing before you get to check out the files inside. This would mean that a process that was running when you go the directory listing is no longer running when you go to read its process information. This is normal and something you’ll have to handle (ideally silently) in your application.

    Also, the code snippet you provided definitely does not do what you described above it. Presumably you edited it for simplicity, but in doing so you removed any clues as to what you might be doing wrong.

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

Sidebar

Related Questions

i have a function which retrieves values from a webservice , then loops through
I have a jQuery function which loops through a set of <article> -elements: $(article:first).fadeTo(animationTime,1,function()
I have a function which loops through rows in a table so that only
I have a function here that loops through two wrappers and then loops through
I have a function which looks something like this, it returns a noncopyable class
Suppose I have a function which looks like this: template <class In, class In2>
I have an application on Vista which loops through all processes and finds associated
I have this code below which works fine but seems unnecessary as it loops
I have a for loop which loops through an array and want to match
I have a function that loops through different types of listings pulled from MySQL

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.