~/learn

Samples

Small, self-contained C/C++ examples demonstrating individual systems programming concepts.

Small, self-contained examples — each one focuses on a single concept and is ready to compile and run.

Using Makefile

In this sample, we will compile a program having two modules with Makefile.

Opening a file

This sample demonstrates the usage of functions open and close.

Reading a file

This sample demonstrates how to read a text file using read() function.

Copy a file

This sample demonstrates how to implement a basic copy program using read and write functions.

Redirect the output

Redirect standard output and standard error into a file using dup/dup2, while keeping the ability to write to the console when needed.

Creating holes

This sample demonstrates the ability to create a sparse file containing both data segments and holes using lseek function.

Detect holes

The sample demonstrates how to detect data and hole segments in the given file.

Create a process

The sample demonstrates how to create a process using fork() function.

Create a thread

The sample demonstrates how to create a thread using pthread_create() function and wait for its completion.

Bounded buffer

A producer–consumer (bounded buffer) solution in C++ implemented two ways: with semaphores and with condition variables.

Dining philosophers

A C++ solution to the dining philosophers problem using semaphores, with a deadlock-prone naive version and an odd/even fix.

TCP server

Create a TCP server with the BSD socket API — binding to a port, listening, accepting clients one at a time, and reading a message from each.

TCP client

The sample demonstrates how to create a TCP client using the BSD socket API — resolving an address, connecting to a server, and sending a message.

Using select

The sample demonstrates how to use select() to monitor one or multiple file descriptors at the same time.

Using poll

The sample demonstrates how to use poll() to monitor multiple file descriptors — watching both stdin and a named pipe (FIFO) for incoming data.

Using epoll

The sample demonstrates how to use epoll to monitor multiple file descriptors — watching both stdin and a named pipe (FIFO) for incoming data.