Linux中应用层C语言的经典实现方法

简介: 大家好,今天给大家分享几个使用的代码实现。

第一:如何获取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;
}

运行结果:

410b5e885a2b4ddeaa691585b4384063.png

第二: 获取文件大小

   有时间我们需要获取某个文件的大小,比如如果需要发送文件里的内容,则需要知道文件的大小。

#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;
}

运行结果:

8f87b56cc2bb46798d2a0af82d791407.png

第三:获取时间戳

   系统时间戳很常用,比如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;
}

运行结果:

a184aa09d3cc4e31a5fcd1d4f760d26a.png

第四:获取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;
}

运行结果:

92807ebccc5240b181b5b66b78ce1010.png

第五:获取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;
}

运行结果:

63c7de0b54f74acdae55c03c1bcf7ebb.png

目录
相关文章
|
Ubuntu 物联网 Linux
从零安装一个Linux操作系统几种方法,以Ubuntu18.04为例
一切就绪后,我们就可以安装操作系统了。当系统通过优盘引导起来之后,我们就可以看到跟虚拟机中一样的安装向导了。之后,大家按照虚拟机中的顺序安装即可。 好了,今天主要介绍了Ubuntu Server版操作系统的安装过程,关于如何使用该操作系统,及操作系统更深层的原理,还请关注本号及相关圈子。
|
XML 缓存 Linux
在Linux环境下解决Visual Studio Code字体显示异常和字体替换方法。
解决Linux下VS Code字体显示异常,需要对Linux字体渲染机制有所理解,并对VS Code的配置选项进行合理设置。替换字体时则要通过系统字体配置或VS Code设置来完成。通过上述方法,可以有效地解决字体显示问题,从而提升代码编辑的视觉体验。
2180 21
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
2912 10
|
安全 网络协议 Linux
Linux网络应用层协议展示:HTTP与HTTPS
此外,必须注意,从HTTP迁移到HTTPS是一项重要且必要的任务,因为这不仅关乎用户信息的安全,也有利于你的网站评级和粉丝的信心。在网络世界中,信息的安全就是一切,选择HTTPS,让您的网站更加安全,使您的用户满意,也使您感到满意。
628 19
|
NoSQL Linux 编译器
GDB符号表概念和在Linux下获取符号表的方法
通过掌握这些关于GDB符号表的知识,你可以更好地管理和理解你的程序,希望这些知识可以帮助你更有效地进行调试工作。
801 16
|
Ubuntu Linux 图形学
推广与体验Ubuntu Linux的便捷方法
如果你的朋友或家人对尝试Linux感兴趣,但希望在安装之前先体验一下,你可以分享以下链接给他们:Ubuntu在线导览。通过这个链接,他们可以在任何地方轻松体验Ubuntu,无需安装即可深入了解这个流行的操作系统。
|
Linux Shell
Linux系统下快速批量创建和删除文件的方法
总的来说,使用shell脚本来批量处理文件是一种非常强大的工具,只要你愿意花时间学习和实践,你会发现它能大大提高你的工作效率。
964 19
|
Ubuntu Linux 网络安全
如何在Linux中更改主机名?修改主机名最新方法
本期教程将指导您如何在Linux系统中更改主机名。主机名是Linux系统的常用功能,用于识别服务器,帮助区分不同服务器,并与网络进程和其他应用程序协同工作。教程涵盖显示当前主机名的方法(通过`hostname`命令),以及在CentOS 7、Debian 9和Ubuntu 16.04及以上版本中更改主机名的步骤(使用`hostnamectl set-hostname`命令)。对于其他Linux版本,可编辑`/etc/hostname`文件实现更改。记得重启相关服务或服务器以使更改生效!
3462 12
|
网络协议 Ubuntu Linux
Wireguard in Linux的安装方法
本文介绍了如何在Ubuntu和Rocky Linux中安装配置WireGuard,并探讨了配置过程中可能出现的DNS泄露问题及解决方法,包括通过nmtui设置DNS及调整DNS优先级参数。