How mdasar2927/linux-file-io-systems-locking works
Purpose and Entry Points
This is a small Linux/POSIX C systems-programming exercise with two standalone command-line programs:
• filecopy.c: copies one file to another.
• lock.c: demonstrates advisory file locking and lock inspection.
Each source file defines its own main; there is no shared library, build system, service, or application server.
File Copy CLI
filecopy.c accepts exactly <source_file> <destination_file>. It validates arguments, opens the source read-only, and creates/truncates the destination with owner read/write permissions.
Core boundary: filesystem paths supplied by the caller become POSIX file descriptors via open.
Paths:
• filecopy.c
Copy Data Path
filecopy.c performs buffered sequential copying:
1. read obtains up to 1024 bytes from the source descriptor.
2. write sends that byte count to the destination descriptor.
3. The loop ends at EOF; descriptors are closed afterward.
Failures opening, reading, or writing are reported with perror and terminate the process. Destination truncation occurs before the copy loop, so a later read/write failure can leave a partial destination file.
Paths:
• filecopy.c
Locking CLI
lock.c accepts <filename>, opens it write-only, and operates on its descriptor using Linux flock.
It first acquires a shared advisory lock (LOCK_SH), waits, then attempts a non-blocking upgrade to an exclusive lock (LOCK_EX | LOCK_NB). It later unlocks with LOCK_UN and closes the descriptor.
Paths:
• lock.c
Lock State and Contention Boundary
The locking flow is intentionally process-coordination oriented rather than data mutation oriented. Lock acquisition depends on other processes holding compatible flock locks on the same underlying file. Failed exclusive upgrade releases the shared lock before exiting.
The program opens the target without O_CREAT; the file must already exist and be writable by the executing user.
Paths:
• lock.c
External Runtime and Observability
Both programs target a Linux/POSIX userspace and rely on libc plus kernel file-descriptor APIs (open, read, write, close, flock). lock.c additionally invokes the external Linux lslocks command through system("lslocks") after each lock-state transition, exposing kernel-visible lock state to stdout.
Paths:
• lock.c
• filecopy.c
Components 12
File Copy CLI
- filecopy main · Validates source and destination paths.
filecopy.c - Open source and destination · Creates or truncates the destination before copying.
filecopy.c - Buffered copy loop · Reads up to 1024 bytes and writes each chunk.
filecopy.c - Close descriptors
filecopy.c
Locking CLI
- lock main · Accepts one existing writable target file.
lock.c - Open target · Opens write-only without creating the file.
lock.c - Advisory lock flow · Shared lock, nonblocking exclusive upgrade, then unlock.
lock.c - lslocks inspection · Prints kernel-visible lock state after transitions.
lock.c
Other components
- Command-line caller
- Linux filesystem · Maps paths to underlying files and descriptors.
- libc and Linux kernel APIs · Provides open, read, write, close, and flock.
- Other locking processes · Compatible or conflicting flock holders determine acquisition.
Connections 16
- Command-line caller → filecopy main: source and destination arguments
- Command-line caller → lock main: filename argument
- filecopy main → Open source and destination: validated paths
- Open source and destination → Linux filesystem: open source; create/truncate destination
- Open source and destination → Buffered copy loop: file descriptors
- Buffered copy loop → Linux filesystem: sequential file data
- Buffered copy loop → Close descriptors: EOF or failure
- lock main → Open target: target path
- Open target → Linux filesystem: open existing writable file
- Open target → Advisory lock flow: target descriptor
- Advisory lock flow → Linux filesystem: advisory flock state
- Other locking processes → Advisory lock flow: lock contention
- Advisory lock flow → lslocks inspection: after each state transition
- lslocks inspection → libc and Linux kernel APIs: system("lslocks")
- filecopy main → libc and Linux kernel APIs: POSIX descriptor calls
- lock main → libc and Linux kernel APIs: flock and descriptor calls