Write a c program to report behavior of Linux kernel including kernel version, CPU type and model. (CPU information)
To report the behavior of the Linux kernel, including the kernel version and CPU type and model, you can read information from the /proc filesystem. Below is a C program that retrieves and displays this information.
C program to shows linux kernel version and CPU information like CPU type and model
#include <stdio.h>
#include <stdlib.h>
int main() {
// Variables to hold the information
char kernel_version[256];
char cpu_info[256];
FILE *fp;
// Get kernel version
fp = popen("uname -r", "r");
if (fp == NULL) {
perror("Failed to run uname command");
exit(EXIT_FAILURE);
}
fgets(kernel_version, sizeof(kernel_version), fp);
pclose(fp);
// Get CPU information
fp = fopen("/proc/cpuinfo", "r");
if (fp == NULL) {
perror("Failed to open /proc/cpuinfo");
exit(EXIT_FAILURE);
}
// Read CPU model and type
while (fgets(cpu_info, sizeof(cpu_info), fp) != NULL) {
if (strncmp(cpu_info, "model name", 10) == 0 ||
strncmp(cpu_info, "cpu family", 10) == 0) {
printf("%s", cpu_info);
}
}
fclose(fp);
// Print kernel version
printf("Kernel Version: %s", kernel_version);
return 0;
}