Skip to main content
Dilectus
CS Fundamentals · OS & concurrency

Processes vs Threads vs Coroutines: What Runs Where and Why

Learn the differences between processes, threads, and coroutines, how they share memory and CPU time, and when to use each in real systems.

AI-generated explainer by Dilectus · · 7 min read

When code needs to do more than one thing, the first question is: what should the unit of execution be? Processes, threads, and coroutines all help you overlap work, but they do it at very different levels of isolation, cost, and control.

If you mix them up, you get bugs like shared-memory races, blocked servers, or systems that are harder to scale than they need to be. The goal is to know what each one gives you, what it costs, and which problems it solves best.

The short version

Think of them as three layers:

  • Process: a running program with its own memory space.
  • Thread: a smaller execution path inside a process, sharing that process’s memory.
  • Coroutine: a task that can pause and resume cooperatively, usually inside a thread.

A useful mental model is:

  • processes are about isolation,
  • threads are about parallelism inside shared memory,
  • coroutines are about efficient waiting and structured concurrency.
+------------------- OS -------------------+
|                                         |
|  Process A             Process B        |
|  +-------------+       +-------------+  |
|  | Thread 1    |       | Thread 1    |  |
|  | Thread 2    |       | Thread 2    |  |
|  | Coroutine A  |       | Coroutine X |  |
|  | Coroutine B  |       | Coroutine Y |  |
|  +-------------+       +-------------+  |
|                                         |
+-----------------------------------------+

What a process gives you

A process is the operating system’s unit for isolation. It has its own virtual address space, file descriptors, environment, and security boundary, although some resources can be inherited or shared depending on how it is created.

Why processes matter

  1. Safety: a crash in one process usually does not crash another.
  2. Security: the OS can apply permissions and sandboxing per process.
  3. Fault isolation: if one worker leaks memory or deadlocks, you can restart it independently.
  4. Deployment flexibility: separate services are often separate processes.

Cost of a process

Processes are heavier than threads because the OS must manage separate address spaces and more metadata. Creating one is usually more expensive than creating a thread, and switching between processes can be costlier because the CPU may need to switch memory mappings and lose some cache locality.

That said, “heavy” is relative. On a modern system, a process is still cheap enough for many server designs, especially when you value isolation more than raw efficiency.

What a thread gives you

A thread is an execution path inside a process. Threads in the same process share code, heap memory, open files, and other resources, but each thread has its own stack and register state.

This shared-memory model is powerful and dangerous.

Why threads are useful

  • Lower overhead than processes for concurrent work inside one application.
  • Easy sharing of data without network calls or serialization.
  • Good for CPU-bound work when you have multiple cores.

The main problem: shared state

Because threads share memory, two threads can read and write the same data at the same time. That creates race conditions unless you protect shared state with locks, atomics, or other synchronization tools.

A simple example:

# Illustrative example
count = 0

# Two threads doing this at the same time can lose updates
count = count + 1

That count = count + 1 is not one atomic action; it is read, add, write. Without synchronization, one increment can overwrite another.

Thread trade-offs

AspectProcessThread
Memory isolationStrongWeak, shared memory
Creation costHigherLower
Context switch costHigherLower
Crash impactUsually containedCan corrupt shared state in same process
CommunicationIPC, serializationShared variables, queues, locks
Best forIsolation, services, sandboxingParallel work inside one app

What a coroutine gives you

A coroutine is a function that can suspend and later resume its execution. Unlike threads, coroutines usually do not run in parallel by themselves. They are scheduled cooperatively by an event loop or runtime.

That means a coroutine gives up control only at suspension points, such as waiting for network I/O, timers, or other async operations.

Why coroutines are useful

Coroutines are excellent when your program spends a lot of time waiting:

  1. A request arrives.
  2. The code starts an I/O operation.
  3. The coroutine pauses instead of blocking the thread.
  4. The thread handles other coroutines.
  5. When the I/O completes, the coroutine resumes.

This lets one thread manage many concurrent tasks with low overhead.

What coroutines are not

  • They are not a replacement for CPU parallelism.
  • They do not automatically make code thread-safe.
  • They can still block the whole thread if you call blocking APIs inside them.

For example, if a coroutine runs a blocking database call on the event-loop thread, you lose the main benefit of async design.

How they differ in practice

The cleanest way to compare them is by answering three questions: who schedules them, what they share, and what happens when they block.

QuestionProcessThreadCoroutine
Who schedules it?OSOSRuntime / event loop
What does it share?Little by defaultMemory of the processUsually the thread’s stack is not held across suspension
Can it run in parallel?Yes, with multiple processesYes, across coresNot by itself; depends on underlying threads/processes
What happens on blocking I/O?Only that process blocksThat thread blocksThe coroutine suspends; ideally the thread stays free
Typical useServices, workers, isolationCPU work, shared-memory concurrencyHigh-concurrency I/O, async servers

A practical server often uses all three:

  • multiple processes for isolation and scaling,
  • multiple threads inside each process for CPU work or blocking libraries,
  • many coroutines per thread for handling lots of network requests.

A realistic server flow

Imagine a web app that receives 10,000 concurrent requests, most of which wait on a database or cache.

  1. A load balancer sends traffic to several worker processes.
  2. Each process may have a small pool of threads.
  3. Each thread runs an event loop that drives many coroutines.
  4. A coroutine starts a database query and suspends.
  5. The thread immediately picks up another ready coroutine.
  6. When the query finishes, the original coroutine resumes and returns a response.

This design works because the expensive part is waiting, not computing. If each request took heavy CPU time, you would usually need more threads or more processes to use multiple cores effectively.

When to use it (and when not to)

Use processes when

  • you need fault isolation,
  • you want separate services or sandboxes,
  • you need to restart workers independently,
  • you are separating untrusted or memory-heavy components.

Avoid processes when

  • you need to share large in-memory state frequently,
  • serialization overhead dominates,
  • the tasks are tiny and short-lived.

Use threads when

  • you want parallelism inside one program,
  • you have CPU-bound work on multiple cores,
  • you need to call blocking libraries that are not async-friendly,
  • shared in-memory data is convenient and manageable.

Avoid threads when

  • shared state is complex and hard to protect,
  • you need tens of thousands of concurrent tasks,
  • lock contention becomes the bottleneck.

Use coroutines when

  • most of the time is spent waiting on network, disk, or timers,
  • you need very high concurrency with modest memory use,
  • your runtime and libraries support async end to end.

Avoid coroutines when

  • your workload is CPU-heavy and pure async will not help,
  • your ecosystem is mostly blocking and hard to adapt,
  • your team is not ready to manage async control flow carefully.

How this comes up in interviews

Interviewers usually probe whether you understand both the runtime model and the trade-offs.

Common questions

  • What is the difference between a process and a thread?
  • Why can threads be faster than processes?
  • Why are threads dangerous with shared memory?
  • What problem do coroutines solve?
  • When would you choose async I/O over threads?
  • Can coroutines run in parallel?

What a strong answer sounds like

A good answer usually has three parts:

  1. Definition: process = isolated memory space, thread = execution path inside a process, coroutine = suspendable task.
  2. Trade-off: processes isolate better, threads share memory and are cheaper, coroutines scale waiting-heavy workloads efficiently.
  3. Use case: choose based on whether the bottleneck is isolation, CPU parallelism, or I/O waiting.

A strong follow-up is to mention that coroutines often still run on threads underneath, so async and threading are not opposites; they are different layers.

A simple decision guide

If you are designing a system, ask this in order:

  1. Do I need isolation or separate failure domains?
    • Yes → processes.
  2. Do I need multiple cores for CPU-heavy work?
    • Yes → threads or multiple processes.
  3. Is the workload mostly waiting on I/O?
    • Yes → coroutines, often with a small thread pool.
  4. Do I need to share mutable memory?
    • Yes → threads, but add locks or other synchronization.
  5. Do I need many concurrent connections with low overhead?
    • Yes → coroutines.

In real systems, the answer is often a combination, not a single choice.

Key takeaways

  • Processes give you isolation; threads give you shared-memory concurrency; coroutines give you lightweight suspension and resumption.
  • Threads are cheaper than processes, but shared memory makes correctness harder.
  • Coroutines are best for high-concurrency I/O, not for raw CPU parallelism.
  • Blocking code inside an async coroutine can erase the benefit of coroutines.
  • Real systems often combine all three: processes for isolation, threads for parallelism, coroutines for scalable waiting.
  • In interviews, explain both the runtime model and the trade-offs, not just the definitions.

Frequently asked questions

Are coroutines the same as threads?
No. Threads are scheduled by the OS and can run in parallel on multiple cores. Coroutines are usually scheduled by a runtime or event loop and typically run cooperatively inside a thread.
Why not just use threads for everything?
Threads work well, but they become harder to manage at very high concurrency because of memory overhead, context switching, and shared-state bugs. Coroutines are often a better fit when most tasks spend time waiting on I/O.
Do coroutines make code non-blocking automatically?
Only if the operations they call are also non-blocking or async-aware. If a coroutine calls a blocking function, it can still block the underlying thread and hurt concurrency.
When should I choose processes over threads?
Choose processes when you care about isolation, independent restarts, security boundaries, or separate services. Threads are better when you need fast shared-memory communication inside one application.