DEV Community

Ben Santora
Ben Santora

Posted on

Create a Linux System Info Utility in C

This Linux project creates a simple utility written in C that retrieves and displays system-level information, including the system name, memory usage, and processor details. It serves as an educational tool for understanding how to interact with Linux system calls and files like /proc/cpuinfo. The utility demonstrates the use of system calls such as uname, sysinfo, and gethostname, along with reading system data from Linux-specific files. It is ideal for beginners learning Linux system programming and provides a foundation for extending functionality, such as adding network statistics or disk usage information. Each component of the utility highlights a different aspect of Linux APIs, from retrieving kernel and architecture details with uname to gathering memory and uptime statistics using sysinfo, and processor details via /proc/cpuinfo.

Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <sys/sysinfo.h>

int main() {
    struct utsname sys_info;
    struct sysinfo mem_info;
    char hostname[256];

    // Get system name, release, version, and architecture
    if (uname(&sys_info) == 0) {
        printf("System Information:\n");
        printf("  System Name: %s\n", sys_info.sysname);
        printf("  Node Name: %s\n", sys_info.nodename);
        printf("  Release: %s\n", sys_info.release);
        printf("  Version: %s\n", sys_info.version);
        printf("  Machine: %s\n", sys_info.machine);
    } else {
        perror("uname");
    }

    // Get hostname
    if (gethostname(hostname, sizeof(hostname)) == 0) {
        printf("  Hostname: %s\n", hostname);
    } else {
        perror("gethostname");
    }

    // Get uptime and memory information
    if (sysinfo(&mem_info) == 0) {
        printf("Memory and Uptime Information:\n");
        printf("  Uptime: %ld seconds\n", mem_info.uptime);
        printf("  Total RAM: %ld MB\n", mem_info.totalram / 1024 / 1024);
        printf("  Free RAM: %ld MB\n", mem_info.freeram / 1024 / 1024);
        printf("  Shared RAM: %ld MB\n", mem_info.sharedram / 1024 / 1024);
        printf("  Buffered RAM: %ld MB\n", mem_info.bufferram / 1024 / 1024);
        printf("  Total Swap: %ld MB\n", mem_info.totalswap / 1024 / 1024);
        printf("  Free Swap: %ld MB\n", mem_info.freeswap / 1024 / 1024);
        printf("  Process Count: %d\n", mem_info.procs);
    } else {
        perror("sysinfo");
    }

    // Get processor type
    FILE *cpu_info = fopen("/proc/cpuinfo", "r");
    if (cpu_info) {
        char line[256];
        printf("Processor Information:\n");
        while (fgets(line, sizeof(line), cpu_info)) {
            if (strncmp(line, "model name", 10) == 0) {
                printf("  %s", line + 13); // Skip "model name : "
                break;
            }
        }
        fclose(cpu_info);
    } else {
        perror("/proc/cpuinfo");
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Copy the code and save as a .c file
For ex: sys_info.c

Compile and Run:

gcc sys_info.c -o sys_info
./sysinfo
Enter fullscreen mode Exit fullscreen mode

end

There are some 36 million lines of C code in the Linux kernel. Learning a bit of C allows you to take a look 'under the hood' at some of the low-level elements and processes of the Linux operating system.

Stay tuned for more articles on learning how to use the Linux OS.

Ben Santora - December 2024

Top comments (0)