Linux中应用层C语言的经典实现方法
2023-09-14 09:13:23 时间
大家好,今天给大家分享几个使用的代码实现。
目录
第一:如何获取CPU的温度
应用可以定时获取CPU的温度,比如程序异常崩溃时,我们可能需要分析多方面原因,CPU温度就是其中之一。
#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"
struct cpu_temperature
{
int integer_part;
int decimal_part;
};
typedef struct cpu_temperature cpu_temperature_t;
cpu_temperature_t get_cpu_temperature(const char *_cpu_temp_file)
{
FILE *fp = NULL;
cpu_temperature_t cpu_temperature = {0};
int temp = 0;
fp = fopen(_cpu_temp_file, "r");
if (NULL == fp)
{
printf("fopen file error\n");
return cpu_temperature;
}
fscanf(fp, "%d", &temp);
cpu_temperature.integer_part = temp / 1000;
cpu_temperature.decimal_part = temp % 1000 / 100;
fclose(fp);
return cpu_temperature;
}
int main(int arc, char *argv[])
{
cpu_temperature_t cpu_temperature = {0};
cpu_temperature = get_cpu_temperature(CPU_TEMP_FILE0);
printf("cpu_temperature = %d.%d ℃\n", cpu_temperature.integer_part, cpu_temperature.decimal_part);
return 0;
}
运行结果:
第二: 获取文件大小
有时间我们需要获取某个文件的大小,比如如果需要发送文件里的内容,则需要知道文件的大小。
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
long get_file_size(const char *_file_name)
{
FILE * fp = fopen(_file_name, "r");
if (NULL == fp)
{
printf("fopen error\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
long size = ftell(fp);
fclose(fp);
return size;
}
int main()
{
#define FILE_NAME "./get_file_size"
long file_size = get_file_size(FILE_NAME);
printf("file_size = %ld\n", file_size);
return 0;
}
运行结果:
第三:获取时间戳
系统时间戳很常用,比如log输出时,可以附带时间戳数据,方便分析。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
long long get_sys_time_ms(void)
{
long long time_ms = 0;
struct timeval sys_current_time;
gettimeofday(&sys_current_time, NULL);
time_ms = ((long long)sys_current_time.tv_sec*1000000 + sys_current_time.tv_usec) / 1000;
return time_ms;
}
int main(int arc, char *argv[])
{
long long cur_sys_time = get_sys_time_ms();
printf("cur_sys_time = %lld ms\n", cur_sys_time);
return 0;
}
运行结果:
第四:获取MAC地址
MAC地址,有时候会作为设备ID实用,作业设备唯一标识。
#include <stdio.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int get_netif_mac(const char *_ifr_name, uint8_t *_mac)
{
int32_t ret = -1;
struct ifreq m_ifreq;
int32_t sock = 0;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
printf("socket err\r\n");
goto err;
}
strcpy(m_ifreq.ifr_name, _ifr_name);
ret = ioctl(sock,SIOCGIFHWADDR, &m_ifreq);
if (ret < 0)
{
printf("ioctl err:%d\r\n",ret);
goto err;
}
snprintf((char *)_mac, 32, "%02x%02x%02x%02x%02x%02x", (uint8_t)m_ifreq.ifr_hwaddr.sa_data[0],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[1],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[2],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[3],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[4],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[5]);
return 0;
err:
return -1;
}
int main(int argc, char **argv)
{
char mac_str[32] = {0};
get_netif_mac("wlan1", mac_str);
printf("mac = %s\n", mac_str);
return 0;
}
运行结果:
第五:获取IP
有时候需要获取本机IP进行显示。
#include <stdio.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int get_local_ip(const char *_ifr_name, char *_ip)
{
int ret = -1;
int sockfd;
struct sockaddr_in sin;
struct ifreq ifr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd)
{
printf("socket error\n");
return ret;
}
strncpy(ifr.ifr_name, _ifr_name, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0)
{
printf("ioctl error\n");
close(sockfd);
return ret;
}
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
int ip_len = snprintf(_ip, 32, "%s", inet_ntoa(sin.sin_addr));
close(sockfd);
ret = ip_len;
return ret;
}
int main(int argc, char **argv)
{
char ip_str[32] = {0};
get_local_ip("wlan1", ip_str);
printf("ip = %s\n", ip_str);
return 0;
}
运行结果:
相关文章
- 【Linux驱动】linux内核模块简介
- [Linux] linux awk命令详解
- 【Linux】嵌入式开发,在Linux中使用C语言对Fork函数执行子函数及父函数,命令ps 及 ls 操作
- 【Linux】嵌入式开发,在Linux中使用C语言对标准I/O库文件的替换Copy
- Linux驱动编写的方法学习
- [Linux]linux下取消用户名和密码直接登录
- [内核]Linux workqueue
- linux常见错误解答汇总
- linux 服务器必备服务
- linux系统调用函数(C语言):open,close函数和文件描述符
- 【华为云技术分享】Linux内核发展史 (1)
- Linux linux echo命令的-n、-e两个参数
- linux题目整理(一)
- L75.linux命令每日一练 -- 第11章 Linux系统管理命令 -- lsof和uptime
- L60.linux命令每日一练 -- 第九章 Linux进程管理命令 -- top和nice
- L50.linux命令每日一练 -- 第八章 Linux磁盘与文件系统管理命令 -- tune2fs和parted
- L42.linux命令每日一练 -- 第七章 Linux用户管理及用户信息查询命令 -- groupdel和passwd
- L36.linux命令每日一练 -- 第五章 Linux信息显示与搜索文件命令 -- locate和updatedb
- L1.linux命令每日一练 -- 第一章 Linux命令行简介
- 嵌入式linux开发,Linux下访问PHY芯片寄存器,获取phyID号,获取phy的link状态
- 嵌入式Linux开发,Ubuntu下交叉编译内核报错:error while loading shared libraries: /lib/x86_64-linux-gnu/libc++.so:
- geo ftp环境变量 export source ~/.bashrc Linux中filezilla下载ftp文件 ftp.ncbi.nlm.nih.gov linux下载 ftp下载geo非原始数
- linux===给新手的 10 个有用 Linux 命令行技巧(转)