Linux获取CPU/GPU的温度

        在 Linux 系统中,/sys/class/thermal/thermal_zone0/temp 文件中的温度值通常以 毫摄氏度(millidegree Celsius) 为单位存储。这意味着你需要将读取的值除以 1000 来转换为常见的摄氏度(°C)。

换算方法:

  1. 读取文件内容

    cat /sys/class/thermal/thermal_zone0/temp

    例如,输出可能是 53000

  2. 转换为摄氏度

    温度(°C)= 读取的值 / 1000

    例如:

    53000 / 1000 = 53.0°C

注意事项:

  1. 单位确认:绝大多数 Linux 系统使用毫摄氏度(如 thermal_zone),但极少数情况下可能是其他单位(如某些嵌入式设备)。如果不确定,可以查阅相关硬件文档。

  2. 多 thermal_zone:如果有多个传感器(如 CPU、GPU 等),可能会存在 thermal_zone1thermal_zone2 等,需分别读取。

  3. 动态更新:该文件的值会实时更新,可以通过 watch 命令动态监控:

    watch -n 1 'cat /sys/class/thermal/thermal_zone*/temp'

代码范例: 

void* monitor_thermalSensor_thread(void *data)
{

	FILE *fp = NULL;
	char buffer[10];
	int temp = 0;
	ALOGI("[%s]\n",__func__);

	while(1)
	{
		fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
		if(fp == NULL) 
        {
			sleep(1);
			continue;
		}

		memset(buffer, 0, sizeof(buffer));
		fgets(buffer, sizeof(buffer), fp);
		temp = atoi(buffer);
		printf("temperature: %d \n", temp);
		fclose(fp);

		if(temp > 120000) //120度
        {

			ALOGI("CPU temperature is too high. Reboot!!");
			sleep(6);
			am_property_set("ctl.start","reboot");
		}
		sleep(30);
	}
	ALOGI("======	monitor_thermalSensor_thread exit!!!	======\n");
	pthread_detach(pthread_self());
	pthread_exit(NULL);
}

void monitor_thermalSensor_temperature(void)
{
	pthread_t monitor_thermalSensorTemperature_thread;
	pthread_create(&monitor_thermalSensorTemperature_thread, NULL, monitor_thermalSensor_thread, NULL);
	return;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值