Low-Latency Linux Command Line (CML) – Built with POSIX & C++

Overview

This project is a high-performance Linux command-line file explorer + mini shell implemented in modern C++ using raw POSIX system calls. The goal is deterministic, low-latency and memory-efficient execution without heavy abstractions.

πŸ“„ Full Notes (PDF)

Environment & Tools

Memory Model Used

Type Speed Allocation
Stack Very Fast Automatic
Heap Slower Manual

char buffer[4096]; // stack buffer β†’ fastest

Stack buffers avoid malloc/new overhead β†’ ideal for low latency tools.

Core System Calls Used

pwd Command – getcwd()


char buffer[PATH_MAX];
getcwd(buffer, sizeof(buffer));
std::cout << buffer;

Kernel copies current working directory directly into user buffer. Fast, deterministic, zero heap allocation.

cd Command – chdir()


chdir(path.c_str());

Changes process working directory inside kernel. Must use c_str() because POSIX expects C-style strings.

ls Command – Directory Traversal


DIR* dir = opendir(".");
while((entry = readdir(dir)) != nullptr) {
    cout << entry->d_name;
}
closedir(dir);

Directories are just special files containing entries (inode + name).

cat Command – open/read/write


int fd = open(path.c_str(), O_RDONLY);
char buffer[4096];

while((bytes = read(fd, buffer, 4096)) > 0)
    write(1, buffer, bytes);

close(fd);

Uses raw syscalls β†’ faster than std::cout/printf. 4KB buffer reduces syscall overhead.

cp Command – File Copy


while((bytes = read(src_fd, buffer, 4096)) > 0)
    write(dest_fd, buffer, bytes);

Classic read β†’ write loop. Kernel performs two copies: disk β†’ kernel β†’ user β†’ kernel β†’ disk.

fork + exec Pattern (Shell Core)


pid_t pid = fork();

if(pid == 0) {
    execvp(argv[0], argv.data());
}
else {
    waitpid(pid, nullptr, 0);
}

Shell workflow:

Output Redirection – dup2()


int fd = open("out.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(fd, 1);   // redirect stdout
execvp("ls", argv);

Anything written to FD 1 (stdout) now goes to file. This is how ls > out.txt works internally.

File Descriptor Model


0 β†’ stdin
1 β†’ stdout
2 β†’ stderr
3+ β†’ files/sockets/pipes

FD is just an integer index into kernel’s file descriptor table.

Design Principles Followed

Linux POSIX System Calls C++ Shell Low Latency