leisurehound
Posts: 2
Joined: Sun Jun 18, 2017 9:57 pm

fseek() and /sys/class/thermal/thermal_zone0/temp

Sun Jun 18, 2017 10:10 pm

I have written some code to monitor the core Pi temperature at a fixed time interval. Its implemented pretty straightforwardly, essentially fopen(), fgets(), fseek(0, SEEK_SET), fgets(), fseek(0, SEEK_SET), fgets(),...fclose() but I'm finding that I'm not getting an updated temperature value, which I can clearly see is different by 'cat /sys/class/thermal/thermal_zone0/temp'

If I instead fopen(), fgets, fclose(),.... it gets updated values.

Should this operate fundamentally differently because its a /sys file?

TIA

Tim

Martin Frezman
Posts: 1021
Joined: Mon Oct 31, 2016 10:05 am

Re: fseek() and /sys/class/thermal/thermal_zone0/temp

Mon Jun 19, 2017 4:12 am

Yeah, it isn't like a normal file that gets appended to. It is just a single line.

You will need to close it and re-open it each time.
If this post appears in the wrong forums category, my apologies.

User avatar
DougieLawson
Posts: 30501
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: fseek() and /sys/class/thermal/thermal_zone0/temp

Mon Jun 19, 2017 7:43 am

leisurehound wrote: 'cat /sys/class/thermal/thermal_zone0/temp'
cat does an open, read & close.

What do you hope to achieve by reading that file apart from increasing the temperature by the simple process of reading that file? It doesn't tell you anything useful.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

jahboater
Posts: 1947
Joined: Wed Feb 04, 2015 6:38 pm

Re: fseek() and /sys/class/thermal/thermal_zone0/temp

Mon Jun 19, 2017 8:17 am

You need a sleep() in the loop as well ...

leisurehound
Posts: 2
Joined: Sun Jun 18, 2017 9:57 pm

Re: fseek() and /sys/class/thermal/thermal_zone0/temp

Mon Jun 19, 2017 3:56 pm

DougieLawson wrote:
leisurehound wrote:
What do you hope to achieve by reading that file apart from increasing the temperature by the simple process of reading that file? It doesn't tell you anything useful.
I'm running the Pi as a CAN query device in a race car. I'm confident its up to the task compute power wise, what I'm not confident of if it can coexist in the environment of a race car in an enclosure. It raises 15C on the bench without the enclosure. Monitoring the core temp at least gives me an indication if I need more heat sinks or cooling.

Since the app both queries, processes responses and broadcasts CAN frames back out, I can't block the select on the socket for too long as I punt and move on to the next query when the ECU decides its too busy to respond. The ECU can only process queries synchronously, so I have to wait to process responses before sending the next query until I simply give up after a timeout elapses. I've thus ended up with a naive loop that blocks for ~500usec on select but is otherwise checking for frames or sending the next query repeatedly, consuming 1 core ~100%.

I haven't done any serious C programming for quite a while and explored some multi threaded strategies but with the ECU being synchronous with its responses, the process is inherently single threaded.

I was hoping I could hold open a handle to the file and simply seek back to read again later, but have since learned that the 'file' is memory backed not actually a file.

JosAH
Posts: 55
Joined: Sat Nov 12, 2016 2:11 pm
Location: Voorschoten

Re: fseek() and /sys/class/thermal/thermal_zone0/temp

Mon Jun 19, 2017 4:04 pm

It is probably opened as a buffered stream. That's why you get the sane answer over and over again.

Kind regards,

Jos

User avatar
DougieLawson
Posts: 30501
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: fseek() and /sys/class/thermal/thermal_zone0/temp

Mon Jun 19, 2017 4:09 pm

jahboater wrote:You need a sleep() in the loop as well ...
No you don't. How are you going to get the processor to 85° using a loop with a pause? Burn that thing hot, run four copies of the program.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.


Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 10 guests