嵌入式Linux下获取CPU温度方法
1 前言
嵌入式设备中,对于长时间工作的设备,特别是工控产品,一般会增加一个风扇为cpu主动散热。而风扇一般会选用可调速的,这样程序可以根据cpu温度实时调整风扇速度,以达到高温时能及时降低cpu温度,低温时节约功耗的目的。
2 获取cpu温度
cpu原厂提供的linux内核通常已经带有cpu温度检测驱动,并将温度信息映射到用户文件系统下,用户只需从该虚拟文件读取温度信息即可。cpu温度虚拟文件位于“/sys/devices/virtual/thermal”
或者“/sys/class/thermal”
下,命名为“thermal_zoneX”
,X表示cpu核温度检测节点,如果是多核cpu通常会有多个温度检测节点;使用其中一个即可,因为温度通常差异不大。
rk3399 ubuntu16 下cup核温度节点
root@firefly:/home# ls /sys/devices/virtual/thermal/
cooling_device0 cooling_device1 cooling_device2 thermal_zone0 thermal_zone1
root@firefly:/home# ls /sys/class/thermal/
cooling_device0 cooling_device1 cooling_device2 thermal_zone0 thermal_zone1
2.1 通过shell获取
root@firefly:/home# cat /sys/class/thermal/thermal_zone0/temp
42222
root@firefly:/home# cat /sys/class/thermal/thermal_zone1/temp
41666
其中温度单位是0.001℃,即是温度节点0“thermal_zone0”
当前温度为42.222℃。
2.2 通过watch实时刷新
通过watch命令,每2秒读取一次cpu温度输出到终端。
root@firefly:/home# watch -n 2 -d echo cpu temperature:[$(cat /sys/class/thermal/thermal_zone0/temp)]
Every 2.0s: echo cpu temperature:[42777] Thu Jul 30 16:02:05 2020
cpu temperature:[42777]
2.3 通过C语言读取
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>#define CPU_TEMP_FILE0 "/sys/devices/virtual/thermal/thermal_zone0/temp"
#define CPU_TEMP_FILE1 "/sys/devices/virtual/thermal/thermal_zone0/temp"int main(int arc, char *argv[])
{FILE *fp = NULL;int temp = 0;fp = fopen (CPU_TEMP_FILE0, "r");if (fp < 0){printf("open file failed,%s\n", strerror(errno));}for (;;){fscanf(fp, "%d", &temp);printf("cpu temperature: [%d.%d C]\n", temp/1000, temp%1000/100);sleep(2);}fclose(fp);return 0;
}
编译执行
root@firefly:/home# gcc cpu_temp.c -o cpu_temp
root@firefly:/home# ./cpu_temp
cpu temperature: [42.7 C]
cpu temperature: [42.7 C]
cpu temperature: [42.7 C]
嵌入式Linux下获取CPU温度方法相关推荐
- Python在Linux下获取CPU温度、使用率、内存使用率、硬盘使用率
方法一: psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于 ...
- Linux下获取cpu温度
大致strace跟踪了下sensors命令,发现是读取类似/sys/class/hwmon/hwmon1/temp1_input中的值,再结合google下的如下几篇文章: https://www.k ...
- Linux系统获取CPU温度
Linux系统获取CPU温度 摘自:https://jingyan.baidu.com/article/cbf0e500407d072eab289343.html 各位好,本篇将简单介绍如何在不同系列 ...
- linux 温度控制软件,linux下的cpu温度监控软件 lm-sensors
linux下的cpu温度监控软件 lm-sensors 发布时间:2008-09-05 00:27:46来源:红联作者:PCHCO 现在购买主机板时都会有厂商提供的监控软体可以使用,而最常使用到到功能 ...
- Ubuntu16.04安装(个鬼鬼)linux下的cpu温度监控软件 lm-sensors,设置cpupower
感觉好惨,毕设一波三折换了两回题目--进度条显示为∞.还能怎么办,继续做啊--现在是先做这个比较稳的简单的把实验先搞完.准备工作一安装im-sensors 安装linux下的cpu温度监控软件 lm- ...
- linux sockaddr in,Linux下获取sockaddr的方法
from http://blog.chinaunix.net/uid-11765716-id-373941.html 2011.06 在Unix下,为使不同格式的地址能够被传入到套接字函数,地址被强制 ...
- linux下监测cpu温度,linux下CPU温度监测
1.安装相关软件 sudo apt-get install lm-sensors sensors-applet [root@localhost ~]# sensors No sensors found ...
- linux下的cpu温度监控软件 lm-sensors
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 原贴:h ...
- linux shell下获取cpu温度
cpu0:cat /sys/class/thermal/thermal_zone0/temp cpu1:cat /sys/class/thermal/thermal_zone1/temp 就会获取到c ...
- linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下获取占用 ...
最新文章
- 教程 | 单级式目标检测方法概述:YOLO与SSD
- termux安装python3失败_boot空间不足导致安装python3失败
- MySQL测试环境遇到 mmap(xxx bytes) failed; errno 12解决方法
- Thrift在windows下的使用
- python 优化加速方案
- android 投影仪,不要购买投影仪, 安卓手机投屏很简单, 每个手机都可以
- 第七期 | 90天带学《机器学习》西瓜书+带打天池和达观杯AI大赛
- jq中查找上级_云计算自动化运维之linux-工作中常用命令总结(上)
- 最小二乘法原理及极值点判定
- mysql stragg_如何在MySQL中將子查詢行的結果顯示為一列?
- mysql的相关技术说明_MySQL 系统架构 说明
- webpack4配置(1)-打包一个js文件
- 在editor模式下遍历unity3d builtsetting中的场景
- 爬虫-----自定义框架
- MySQL 6.子查询
- 给想进投行的学弟学妹的忠告和建议
- 惊爆:Alexa 全球排名网站即将关闭
- 赋值运算和赋值表达式
- BERT模型系列大全解读
- 2022年制冷与空调设备运行操作最新解析及制冷与空调设备运行操作作业考试题库