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.
- Clean modular architecture
- Direct kernel interaction
- Minimal heap allocation
- Predictable latency
- Real systems programming practice
π Full Notes (PDF)
Environment & Tools
- WSL Ubuntu 22.04
- g++ Compiler
- CMake Build System
- Git
- VS Code Remote WSL
- perf / valgrind profiling
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
- getcwd()
- chdir()
- opendir() / readdir()
- stat()
- open() / read() / write()
- fork()
- execvp()
- dup2()
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:
- fork β create child
- exec β replace child with program
- wait β parent synchronizes
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
- Minimize syscalls
- Use stack buffers
- Avoid heap allocations
- Prefer absolute paths
- Cache metadata
- Use modular command architecture
Linux POSIX System Calls C++ Shell Low Latency