Skip to content

alejaalvar/monitorasaurus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Monitorasaurus

A lightweight, terminal-based process monitor written in C. Inspired by top and htop, Monitorasaurus periodically clears the screen and displays the most CPU-intensive processes running on the system, refreshing at a configurable interval until you stop it.

How It Works

Monitorasaurus uses a POSIX unnamed semaphore initialized to 0 as a precise, interruptible timer. On each iteration of the main loop, it calls sem_timedwait() with an absolute future timestamp. Since sem_post() is never called, the wait always expires — acting as a sleep that can be cleanly interrupted by a SIGINT (Ctrl+C). When the timeout fires, the screen is cleared and the output of ps augx --sort=-pcpu | head -n 20 is read via popen() and printed to stdout, with long lines truncated to the configured display width.

A SIGINT handler registered with sigaction() sets a volatile sig_atomic_t flag to false, which causes the main loop to exit cleanly without requiring sem_post().

Features

  • Real-time display of top CPU-consuming processes
  • Configurable refresh interval, refresh count, and output width
  • Clean Ctrl+C exit via sigaction-based signal handling
  • Verbose mode for debugging/logging to stderr
  • No dynamic memory allocation — zero memory leaks

Requirements

  • Linux (POSIX semaphores, ps, popen)
  • GCC

Building

make

To clean build artifacts:

make clean

To clean and rebuild from scratch:

make clean all

Usage

./monitorasaurus [-i #] [-c #] [-w #] [-v] [-h]
Option Description Default
-i # Refresh interval in seconds 2
-c # Number of refreshes before exiting (0 = run until Ctrl+C) 0
-w # Maximum width of each output line in characters 100
-v Enable verbose logging to stderr off
-h Print help and exit

Examples

Run with default settings (refresh every 2 seconds, run indefinitely):

./monitorasaurus

Refresh every 5 seconds, stop after 10 refreshes:

./monitorasaurus -i 5 -c 10

Narrow output to 80 columns with verbose logging:

./monitorasaurus -w 80 -v

Example Output

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
alejalva   12345 98.2  1.4 123456 56789 pts/1    R+   10:01   0:42 ./desplodocus_mt 200 10
alejalva   12346 45.1  0.8  98765 32100 pts/2    S+   10:00   0:18 ./desplodocus_mt 100 5
root         512  2.3  0.1   9876  4321 ?        Ss   09:55   0:01 /usr/sbin/sshd
...

Press Ctrl+C to exit at any time.

Project Structure

Lab4/
├── monitorasaurus.c   # Main source file
├── monitorasaurus.h   # Macros and constants (CMD, CLEAR_SCREEN, etc.)
├── Makefile           # Build rules
└── README.md

License

This project is licensed under the MIT License. See LICENSE for details.

About

A real-time system monitoring utility, written in C.

Topics

Resources

License

Stars

Watchers

Forks

Contributors