📄 kernel_proc.c
字号:
#include <stdio.h>#include <string.h>float version_info(){ FILE* fp; char buffer[1024]; size_t bytes_read; char* match; char *version_info;//Read the entire contents of /proc/version the buffer. fp= fopen("/proc/version","r"); bytes_read= fread(buffer, 1, sizeof(buffer), fp); fclose(fp);//Bail if read failed or if buffer isn’t big enough. if (bytes_read== 0 || bytes_read== sizeof(buffer)) return 0;//NUL-terminate the text. buffer[bytes_read] = '\0';//Locate the line that starts with “cpuMHz”. match = buffer;//strstr(buffer, “cpuMHz”); //if (match == NULL) // return 0;//Parse the line to extract the clock speed. sscanf(match,"Linux version %s",version_info); printf("The kernel version is %s\n",version_info); return 0;}float cpu_type(){ FILE* fp; char buffer[1024]; size_t bytes_read; char* match; char *cpu_type; //Read the entire contents of /proc/cpuinfointo the buffer. fp= fopen("/proc/cpuinfo","r"); bytes_read= fread(buffer, 1, sizeof(buffer), fp); fclose(fp);//Bail if read failed or if buffer isn’t big enough. if (bytes_read== 0 || bytes_read== sizeof(buffer)) return 0;//NUL-terminate the text. buffer[bytes_read] = '\0';//Locate the line that starts with “cpuMHz”. match =strstr(buffer,"vendor_id"); if (match == NULL) return 0;//Parse the line to extract the clock speed. sscanf(match,"vendor_id :%s",cpu_type); printf("The cpu type is %s\n",cpu_type); return 0;}float cpu_model(){ FILE* fp; char buffer[1024]; size_t bytes_read; char* match; char *cpu_model; //Read the entire contents of /proc/cpuinfointo the buffer. fp= fopen("/proc/cpuinfo","r"); bytes_read= fread(buffer, 1, sizeof(buffer), fp); fclose(fp);//Bail if read failed or if buffer isn’t big enough. if (bytes_read== 0 || bytes_read== sizeof(buffer)) return 0;//NUL-terminate the text. buffer[bytes_read] = '\0';//Locate the line that starts with “cpuMHz”. match =strstr(buffer,"model name"); if (match == NULL) return 0;//Parse the line to extract the clock speed. sscanf(match,"model name :%s",cpu_model); printf("The cpu model name is %s\n",cpu_model); //printf("%s",cpu_model+=(strlen(cpu_model)+1)); printf("sizeof(cpu_model) is %d\n",strlen(cpu_model)); return 0;}float time_since_boot(){ int days,hours,minutes; float seconds; FILE* fp; char buffer[1024]; size_t bytes_read; float time; //Read the entire contents of /proc/cpuinfointo the buffer. fp= fopen("/proc/uptime","r"); bytes_read= fread(buffer, 1, sizeof(buffer), fp); fclose(fp);//Bail if read failed or if buffer isn’t big enough. if (bytes_read== 0 || bytes_read== sizeof(buffer)) return 0;//NUL-terminate the text. buffer[bytes_read] = '\0';//Locate the line that starts with “cpuMHz”. sscanf(buffer,"%f",&time); days=time/(60*60*24); hours=(time-days*60*60*24)/(60*60); minutes=(time-days*60*60*24-hours*60*60)/60; seconds=(time-days*60*60*24-hours*60*60-minutes*60); printf("Time since booted is %d:%d:%d:%d\n",days,hours,minutes,(int)seconds); return 0;}main(){ version_info(); cpu_type(); cpu_model(); time_since_boot();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -