🔧 Programmierung 🕛 vor 1 Jahr 3 Min Lesezeit
0

Create a Linux System Process Monitor in C

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




This Process Monitor uses a C program designed to list all running processes on a Linux system. It displays the Process ID (PID), memory usage, and CPU usage for each process, updating this information every 5 seconds.






CODE
#include <stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<unistd.h>
#include
<dirent.h>
#include
<sys/types.h>
#include
<sys/stat.h>

void display_process_info(long pid, long uptime);
void list_processes(long uptime);

// Function to get system uptime
long get_system_uptime() {
FILE *file = fopen("/proc/uptime", "r");
if (!file) {
perror("fopen");
return -1;
}
long uptime;
fscanf(file, "%ld", &uptime);
fclose(file);
return uptime;
}

int main() {
while (1) {
long uptime = get_system_uptime();
if (uptime != -1) {
list_processes(uptime);
sleep(5); // Update every 5 seconds
}
}
return 0;
}

void list_processes(long uptime) {
DIR *dir;
struct dirent *entry;

dir = opendir("/proc");
if (dir == NULL) {
perror("opendir");
return;
}

printf("PID\tMemory (KB)\tCPU (%%)\n");
printf("------------------------------\n");

while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char *endptr;
long pid = strtol(entry->d_name, &endptr, 10);
if (*endptr == '\0') {
// It's a valid PID
display_process_info(pid, uptime);
}
}
}

closedir(dir);
}

void display_process_info(long pid, long uptime) {
char path[256];
snprintf(path, sizeof(path), "/proc/%ld/stat", pid);
FILE *file = fopen(path, "r");
if (!file) {
perror("fopen");
return;
}

// Read the contents of /proc/[pid]/stat
char buffer[1024];
long utime, stime, starttime;
if (fgets(buffer, sizeof(buffer), file) != NULL) {
sscanf(buffer, "%*d %*s %*c %*d %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %ld %ld %*d %*d %*d %*d %*u %*u %*d %*u %*d %*u %*u %*u %ld", &utime, &stime, &starttime);
}
fclose(file);

snprintf(path, sizeof(path), "/proc/%ld/statm", pid);
file = fopen(path, "r");
if (!file) {
perror("fopen");
return;
}

// Read memory usage from /proc/[pid]/statm
long mem_usage;
if (fscanf(file, "%ld", &mem_usage) == 1) {
mem_usage = mem_usage * getpagesize() / 1024; // Convert to KB
}
fclose(file);

// Calculate CPU usage
long total_time = utime + stime;
long seconds = uptime - (starttime / sysconf(_SC_CLK_TCK));
double cpu_usage = 100.0 * ((double) total_time / sysconf(_SC_CLK_TCK) / (double) seconds);

printf("%ld\t%ld\t\t%.2f\n", pid, mem_usage, cpu_usage);
}









Features



Lists all running processes.



Displays details such as PID, memory usage (in KB), and CPU usage (as a percentage).



Updates process information periodically to reflect real-time system activity.






Usage



Compilation

To compile the program, use the following command:




CODE
gcc process_monitor.c -o process_monitor






Execution

To run the program, execute:




CODE
sudo ./process_monitor









Example Output



The program outputs a list of running processes with their respective memory and CPU usage:






PID Memory (KB) CPU (%)



1 2616 0.00

5 2800 0.00

12 2624 0.00

...






How It Works



The program reads process information from the /proc directory.



It fetches memory usage data from /proc/[pid]/statm.



CPU usage is calculated based on the process's CPU time and the system uptime, read from /proc/[pid]/stat and /proc/uptime, respectively.



The information is updated every 5 seconds to provide a real-time view.






Considerations



Use on any PC running the Linux OS. Or use WSL with Windows. Note: Running the program in Windows Subsystem for Linux (WSL) should work similarly to a native Linux environment, though CPU usage will likely be very minimal if there are few active processes.

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
6 Quellen
Create a Coloring Page with ChatGPT | OpenAI Academy
1 Quelle
New Gemini Update Is SCARY GOOD!
1 Quelle
OpenAI Just Revealed Something More Dangerous Than AGI
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Create a Linux System Process Monitor in C

Thematisch verwandte Begriffe: Create, Linux, System, Process · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...