Skip to content

Instantly share code, notes, and snippets.

View antonioastorino's full-sized avatar

Antonio Astorino antonioastorino

View GitHub Profile
@antonioastorino
antonioastorino / using-flock.c
Last active June 1, 2025 13:09
Using `flock()` in C
/*
# flock in C
## Motivation
Try and search for "Flock in C" on YouTube. You won't find this stuff.
## Explanation
This tiny project is to show how to use a file to share data between 2 processes. For simplicity, a producer and a consumer are created within the same executable.
The producer uses `flock` with the "exclusive" flag. That is because the producer wants nobody else to access the file while it is being written. Also, the producer prefers to wait for readers to complete reading. Hence, the producer blocks until the readings are in progress.