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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:05:02+00:00 2026-05-20T11:05:02+00:00

I’m making a load balancer (a very simple one). It looks at how long

  • 0

I’m making a load balancer (a very simple one). It looks at how long the user has been idle, and the load on the system to determine if a process can run, and it goes through processes in a round-robin fashion.

All of the data needed to control the processes are stored in a text file.
The file might look like this:

 PID=4390 IDLE=0.000000 BUSY=2.000000 USER=2.000000
 PID=4397 IDLE=3.000000 BUSY=1.500000 USER=4.000000
 PID=4405 IDLE=0.000000 BUSY=2.000000 USER=2.000000
 PID=4412 IDLE=0.000000 BUSY=2.000000 USER=2.000000
 PID=4420 IDLE=3.000000 BUSY=1.500000 USER=4.000000

This is a university assignment, however parsing the text file isn’t supposed to be a big part of it, which means I can use whatever way is the quickest for me to implement.

Entries in this file will be added and removed as processes finish or are added under control.

Any ideas on how to parse this?

Thanks.

  • 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-20T11:05:03+00:00Added an answer on May 20, 2026 at 11:05 am

    Here is a code that will parse your file, and also account for the fact that your file might be unavailable (that is, fopen might fail), or being written while you read it (that is, fscanf might fail). Note that infinite loop, which you might not want to use (that’s more pseudo-code than actual code to be copy-pasted in your project, I didn’t try to run it). Note also that it might be quite slow given the duration of the sleep there: you might want to use a more advanced approach, that’s more sort of a hack.

    int pid;
    float idle, busy, user;
    
    FILE* fid;
    fpos_t pos;
    int pos_init = 0;
    
    while (1)
    {
      // try to open the file
      if ((fid = fopen("myfile.txt","rw+")) == NULL)
      {
         sleep(1); // sleep for a little while, and try again
         continue; 
      }
    
      // reset position in file (if initialized)
      if (pos_init)
         fsetpos (pFile,&pos);
    
      // read as many line as you can
      while (!feof(fid))
      {
         if (fscanf(fid,"PID=%d IDLE=%f BUSY=%f USER=%f",&pid, &idle, &busy, &user))
         {
            // found a line that does match this pattern: try again later, the file might be currently written
            break;
         }
    
         // add here your code processing data         
    
         fgetpos (pFile,&pos); // remember current position
         pos_init = 1; // position has been initialized
      }
    
      fclose(fid);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.