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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:30:32+00:00 2026-06-02T03:30:32+00:00

I have really strange behaviour while reading files from /proc If I read /proc/pid/stat

  • 0

I have really strange behaviour while reading files from /proc
If I read /proc/pid/stat lazily with prelude’s readFile – it works but not the way I want.
Switching to strict reading with Data.ByteString.readFile gives me an empty string.

I need strict reading here to be able to compare the results of two reads within short interval.

So using System.IO.readFile to read /proc/pid/stat simply does not work. It gives me the same result within 0.5 sec interval. I figure this is due to laziness and half closed handle or something …
Opening and closing the file handle explicitly works.

h <- openFile "/proc/pid/stat" ReadMode
st1 <- hGetLine h; hClose h

But why do the above if we have the bytestring strict reading. Right?

This is where I got stuck.

import qualified Data.ByteString as B
B.readFile "/proc/pid/stat" >>= print

This always returns an empty string. Also tested in GHCI.
Any suggestions. Thanks.

— UPDATE —

Thank you Daniel for suggestions.

This is what I actually need to do. This might help to show my dilemma in full and bring more general suggestions.

I need to calculate process statistics. Here is part of the code (just the CPU usage) as an example.

cpuUsage pid = do
  st1 <- readProc $ "/proc" </> pid </> "stat"
  threadDelay 500000 -- 0.5 sec
  st2 <- readProc $ "/proc" </> pid </> "stat"
  let sum1 = (read $ words st1 !! 13) +
             (read $ words st1 !! 14)
      sum2 = (read $ words st2 !! 13) +
             (read $ words st2 !! 14)
  return $ round $ fromIntegral (sum2 - sum1) * jiffy / delay * 100
  where
    jiffy = 0.01
    delay = 0.5
    readProc f = do
      h <- openFile f ReadMode
      c <- hGetLine h
      hClose h
      return c
  1. Prelude.readFile does not work due to the laziness
  2. Strict functions from ByteString don’t work. Thank you Daniel for the explanation.
  3. withFile would work (it closes the handle properly) if I stuffed the whole computation in it but then the interval will not be strictly 0.5 as computations take time.
  4. Opening and closing handles explicitly and using hGetContents does not work! For the same reason readFile doesn’t.

The only thing that work in this situation is explicitly opening and closing handles with hGetLine in above code snippet. But this is not good enough as some proc files are more then one line like /proc/meminfo.

So I need a function that would read the whole file strictly. Something like hGetContents but strict.

I was trying to do this:

readProc f = do
  h <- openFile f ReadMode
  c <- hGetContents h
  let c' = lines c
  hClose h
  return c'

Hoping that lines would trigger it to read the file in full. No luck. Still get an empty list.

Any help, suggestion is very appreciated.

  • 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-02T03:30:34+00:00Added an answer on June 2, 2026 at 3:30 am

    The ByteString code is

    readFile :: FilePath -> IO ByteString
    readFile f = bracket (openBinaryFile f ReadMode) hClose
        (\h -> hFileSize h >>= hGet h . fromIntegral)
    

    But /proc/whatever isn’t a real file, it’s generated on demand, when you stat them to get the file size, you get 0. So ByteString‘s readFile successfully reads 0 bytes.

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

Sidebar

Related Questions

I have a really strange behaviour with my tablet interface (Android > 3). When
I am experiencing some strange behaviour and I really don't have test date/ environments,
I encountered some really strange behaviour in Apache. I have a standard let PHP
I have come to know that from IE 5, there is a strange behaviour
I have a really strange behavior with highchart (via rails plugin): The graph display
I have a really strange issue. I am working on a Java SWING application
I have a kind of strange thing that I really nead for my text
I have noticed a strange piece of behaviour when using MessageDlg and attempting to
I have a really strange problem that I can't explain and fix, it is
I have a really strange issue relating to how I handle navigation in an

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.