parallel-processing
file-handling
multi-threading
concurrency
process-synchronization

Processing single file from multiple processes

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Having multiple processes work on the same file sounds simple, but it quickly becomes a concurrency problem. Without coordination, processes can overwrite each other’s changes, read partial state, or produce output that looks valid but is semantically corrupt. The right strategy depends on whether processes are reading, writing, appending, or partitioning the file by offset.

Understand the Access Pattern First

There is no single safe answer for “multiple processes on one file.” You need to classify the workload:

  • Many readers, no writers.
  • One writer, many readers.
  • Many appenders.
  • Many processes updating different parts of the file.

Read-only sharing is usually safe. Concurrent writes are where problems start. Even when writes target “different” areas, metadata changes, buffering, and flush timing can still create hazards.

Use File Locks for Coordinated Mutation

If processes must write to the same file, explicit locking is usually the first tool to consider. On Unix-like systems, advisory locks are common.

python
1import fcntl
2
3with open("shared.txt", "a+") as f:
4    fcntl.flock(f.fileno(), fcntl.LOCK_EX)
5    f.write("safe line\n")
6    f.flush()
7    fcntl.flock(f.fileno(), fcntl.LOCK_UN)

This works only if every participating process agrees to honor the lock. Advisory locking is a contract between cooperating processes, not a magic global protection layer.

That point is easy to miss. A lock only helps when every process uses the same locking discipline. One uncooperative writer can still corrupt the file.

For read access, shared locks may be appropriate:

python
1import fcntl
2
3with open("shared.txt", "r") as f:
4    fcntl.flock(f.fileno(), fcntl.LOCK_SH)
5    data = f.read()
6    fcntl.flock(f.fileno(), fcntl.LOCK_UN)

Prefer Append-Only or Partitioned Designs

If you can redesign the file interaction, append-only workflows are much easier to reason about than in-place edits. Each process appends a record, and later stages compact or merge results.

Safer still is partitioned output:

  • Give each process its own temp file.
  • Merge after all workers finish.

Example shell-oriented pattern:

bash
1worker1 > output.part1
2worker2 > output.part2
3worker3 > output.part3
4cat output.part1 output.part2 output.part3 > output.txt

This avoids almost all cross-process file contention. In many systems, the correct answer is “do not share one writable file directly.”

That is often the engineering answer worth emphasizing: the best concurrency fix is sometimes an architecture change, not a better lock.

Coordinate Work with a Queue, Not the File Itself

If multiple processes are consuming tasks from one dataset, keep the file immutable and coordinate through a process-safe queue or database table instead of using the file as both storage and synchronization mechanism.

Python example with multiprocessing queue:

python
1from multiprocessing import Process, Queue
2
3def worker(queue: Queue):
4    while not queue.empty():
5        item = queue.get()
6        print(f"processing {item}")
7
8if __name__ == "__main__":
9    q = Queue()
10    for n in range(10):
11        q.put(n)
12
13    procs = [Process(target=worker, args=(q,)) for _ in range(3)]
14    for p in procs:
15        p.start()
16    for p in procs:
17        p.join()

This pattern separates concurrency control from file mutation, which is usually a cleaner architecture.

Beware Partial Writes and Buffering

Even when writes look small, buffering can delay data flush and create interleaving you did not expect. Always think about:

  • File open mode.
  • Buffer flush behavior.
  • Atomicity guarantees of the filesystem.
  • Whether record boundaries matter.

If a write must be durable before the next process acts, flush and sync explicitly.

python
with open("shared.txt", "a") as f:
    f.write("event=complete\n")
    f.flush()

For stronger durability guarantees, platform-specific syncing may also be needed, but that comes with performance cost.

When You Need Random Access Updates

If several processes update different byte ranges of a large binary file, you need a stronger coordination model. At minimum:

  • Assign non-overlapping ranges explicitly.
  • Lock per region if the platform supports it.
  • Validate offsets and lengths carefully.

At that point, a database, memory-mapped file, or purpose-built storage format may be more appropriate than ad hoc file mutation.

Testing Concurrent File Work Correctly

Concurrency bugs often hide in low-contention local tests. To surface them:

  • Run the workload many times.
  • Increase process count.
  • Introduce random delays between read and write.
  • Validate file contents after every run.

A system that “usually works” under low contention is often already broken.

Common Pitfalls

  • Assuming separate processes will naturally serialize file access.
  • Relying on advisory locks when some writers do not honor them.
  • Using one shared writable file when per-process files and merge would be simpler.
  • Ignoring buffering and thinking write() means “instantly visible everywhere.”
  • Treating the file as both work queue and output store.

Summary

  • Multiple processes can safely share one file only when access patterns are clearly defined and coordinated.
  • Advisory locking is useful, but only among cooperating writers.
  • Append-only and per-process temp-file designs are often safer than shared in-place writes.
  • Queues and databases are usually better synchronization tools than a mutable file.
  • Test under real contention, not just one light local run.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.