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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:11:40+00:00 2026-05-19T12:11:40+00:00

Is it safe to use lseek(fd,0) and then read(fd,buf) for /proc/stat file instead of

  • 0
  1. Is it safe to use lseek(fd,0) and then read(fd,buf) for /proc/stat file instead of reopening it to get updated contents of this file next time?
  2. And what does the mmap() call after opening this file really do (see below)?

The problem I am encountering is that top reports way too low CPU usage (10% vs 100% for software interrupts). The strace tool indicates that top does not reopen this file but instead lseeks to beginning and reads it once again. And somehow the contents which are read from this file next time does not match with what I would get when I run cat for /proc/stat file alone.

Also If I run top and cat /proc/stat in a loop at the same time then top starts to report correct CPU-Usage.

One more difference I spot is that top uses mmap() call right after opening the /proc/stat file, while cat does not do that. I am not sure if this also could be related with my problem (because filesdes=-1 here):

mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7b37649000

I am using Ubuntu 10.04.1 Desktop edition with 2.6.32-27-server image. The CPU is Intel Q6600.

  • 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-19T12:11:40+00:00Added an answer on May 19, 2026 at 12:11 pm

    It’s very interesting what you ask… I start checking in my machine but I don’t see differences between cat /proc/stat and execute top. Anyway, I’m at work and I’m not totally ‘free’ to make tests.

    The way you describe to ‘refresh’ the opened file to read new data is correct… in case of a [f|l]seek() call to the end and then to the beginning of the file will update the EOF and new data will be read.

    I don’t think that the mmap() call will cause the problem you mentioned, it could make the read faster, but nothing else (I’m not 100% sure).

    I would suggest you to make a small app in C that open /proc/stat, read it, seek it and read it again to see how it’s updated, also if you have some stress test to do could be useful.

    Now, answering your real questions:

    1. Yes, AFAIK it’s sure because you will be ‘waiting’ for new data on the file and it should be better than open and close the file all the time.

    2. It maps a file to the process address space, here are some info and examples:

    http://www.gnu.org/s/libc/manual/html_node/Memory_002dmapped-I_002fO.html
    http://www.linuxquestions.org/questions/programming-9/mmap-tutorial-c-c-511265/

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

Sidebar

Related Questions

In ASP.NET or WCF is it safe to use ServiceSecurityContext.Current.WindowsIdentity.Name to get the current
I use safe navigation operator to avoid NPEs like this myInstance?.field1 myInstance?.field2 myInstance?.field3 To
is this password check algorithm safe to use in low security environment like local
I have unescaped data from users. So is it safe to use like this:
Is it safe to use something like this to ensure that a directory isn't
I want to know if it is safe to use this: @company = Company.find(params[:company_ids])
I just wonder if this line of code is safe to use to avoid
is it safe to use cast (int) instead of escaping? class opinion { function
Is it safe to use File Last Modified (e.g. NTFS) when detecting if a
Is it safe to use php_include file for mysql login information or is is

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.