# What Is the Difference Between a Process and a Thread in Operating Systems?

> Understand the key difference between a process and a thread in operating systems. Learn how they differ in resource allocation and CPU scheduling to optimize your system knowledge.

- Repository: [CyC2018/CS-Notes](https://github.com/CyC2018/CS-Notes)
- Tags: deep-dive
- Published: 2026-02-24

---

**A process is the operating system's unit of resource allocation with isolated memory and resources, while a thread is the unit of CPU scheduling that executes within a process and shares its parent's resources.**

The CS-Notes repository by CyC2018 provides authoritative technical documentation on these fundamental operating system concepts. Understanding the **difference between a process and a thread in operating systems** is essential for designing concurrent applications, optimizing performance, and managing system resources effectively.

## Resource Allocation vs. CPU Scheduling

### What Is a Process?

According to `notes/计算机操作系统 - 进程管理.md` in the CS-Notes repository, a **process** is defined as the operating system's unit of **resource allocation**. When a program starts, the OS creates a process that owns its own **virtual address space**, file descriptors, environment variables, and other kernel-managed resources.

As stated in the repository, "进程是资源分配的基本单位" (the process is the basic unit of resource allocation). Processes are isolated from one another; one process cannot directly read or write the memory of another without explicit **inter-process communication (IPC)** mechanisms.

### What Is a Thread?

A **thread** (sometimes called a lightweight process) is the OS's unit of **CPU scheduling**. Threads exist **inside** a process and, as documented in the CS-Notes material, "线程不拥有资源" (threads do not own resources). Instead, they access the resources of their parent process—including its address space, open files, and heap.

Because threads share memory, they can communicate by simply reading and writing shared variables. This makes thread-level parallelism cheaper than process-level parallelism, though it introduces synchronization challenges such as race conditions and deadlocks.

## Key Differences Between Processes and Threads in Operating Systems

### Resource Ownership

The fundamental distinction lies in resource possession. A process owns resources; a thread does *not* own resources, it *accesses* those of its parent process. All threads within a process inherit access to the same memory pool and file handles, while processes maintain strict isolation.

### Context Switching Cost

**Process switching** requires saving and restoring the full CPU context and possibly changing memory maps. As documented in the CS-Notes process management notes, "进程切换时，涉及当前执行进程 CPU 环境的保存及新调度进程 CPU 环境的设置，而线程切换时只需保存和设置少量寄存器内容，开销很小" (during process switching, the system must save the CPU environment of the currently executing process and set up the CPU environment for the newly scheduled process, whereas thread switching only requires saving and setting a few registers, resulting in very low overhead).

### Creation Overhead

Forking a new process incurs memory allocation for a separate address space and full resource duplication. Creating a thread only allocates a stack and a thread-control block. The repository notes that "创建或撤销进程时，系统都要为之分配或回收资源 … 开销远大于创建或撤销线程时的开销" (when creating or destroying a process, the system must allocate or reclaim resources for it... the overhead is much greater than when creating or destroying a thread).

### Communication Mechanisms

Threads communicate via **shared memory**—directly reading and writing variables in the process's address space. Processes must use explicit **IPC** mechanisms such as pipes, sockets, or shared memory regions. The CS-Notes states: "线程间可以通过直接读写同一进程中的数据进行通信，但是进程通信需要借助 IPC" (threads can communicate by directly reading and writing data in the same process, but process communication requires IPC).

## Code Examples from the CS-Notes Repository

### Creating a Process with fork() (C)

The following example demonstrates process creation using the `fork()` system call, which duplicates the calling process and gives the child its own copy of the address space:

```c
#include <unistd.h>
#include <stdio.h>

int main() {
    pid_t pid = fork();               // OS creates a new process
    if (pid == 0) {
        // Child process – has its own address space
        printf("Child: PID = %d\n", getpid());
    } else {
        // Parent process
        printf("Parent: PID = %d, child PID = %d\n", getpid(), pid);
    }
    return 0;
}

```

The two processes run independently with isolated memory spaces.

### Creating a Thread with std::thread (C++)

This example illustrates thread creation within a single process. Both threads share the same process memory and can access global variables without extra IPC:

```cpp
#include <thread>
#include <iostream>

void worker(int id) {
    std::cout << "Thread " << id << " running in process " << getpid() << "\n";
}

int main() {
    std::thread t1(worker, 1);
    std::thread t2(worker, 2);
    t1.join();   // wait for threads to finish
    t2.join();
    return 0;
}

```

### Shared Memory Between Threads (Java)

The following Java example from `notes/Java 并发.md` demonstrates how threads share variables within a process:

```java
public class ThreadDemo {
    private static int counter = 0;          // shared variable

    public static void main(String[] args) throws InterruptedException {
        Runnable incr = () -> {
            for (int i = 0; i < 1_000_000; i++) counter++;
        };
        Thread t1 = new Thread(incr);
        Thread t2 = new Thread(incr);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("Final counter = " + counter);
    }
}

```

Both threads increment the same `counter` variable because they belong to the same process. Proper synchronization would be required in production code to prevent race conditions.

## Summary

- A **process** is the OS unit of **resource allocation** with isolated memory space, while a **thread** is the unit of **CPU scheduling** that shares process resources.
- Process creation and context switching incur significantly higher overhead than thread operations due to memory map changes and full context saves.
- Threads communicate through direct **shared memory** access, whereas processes require explicit **IPC** mechanisms like pipes or sockets.
- According to the CS-Notes repository, threads "不拥有资源" (do not own resources) but merely access those of their parent process.
- Modern concurrent systems use a hybrid approach: multiple processes for isolation, with multiple threads per process for efficiency.

## Frequently Asked Questions

### Is a thread the same as a lightweight process?

While threads are sometimes referred to as lightweight processes, they are distinct concepts. A thread is a unit of execution within a process, sharing the process's resources, whereas a lightweight process typically refers to the kernel-level abstraction that implements threads. In the CS-Notes context, threads are explicitly defined as separate from processes, emphasizing that they do not own resources independently.

### Can a thread exist without a process?

No. Threads are always contained within a process. If the parent process terminates, all its threads are automatically destroyed. The repository notes that threads exist "inside" a process and access its resources; they cannot function as independent entities without the resource container provided by a process.

### Why is process switching slower than thread switching?

Process switching requires saving and restoring the entire CPU context and updating memory management information (page tables), whereas thread switching only involves saving and restoring a few registers. As documented in `notes/计算机操作系统 - 进程管理.md`, the overhead for process switching is substantial because it involves changing the full CPU environment, while thread switching has "开销很小" (very small overhead).

### How do processes communicate compared to threads?

Threads communicate by directly reading and writing shared variables in the process's heap memory. Processes, being isolated with separate address spaces, must use inter-process communication mechanisms such as pipes, sockets, message queues, or explicitly mapped shared memory regions. The CS-Notes repository highlights this distinction: "线程间可以通过直接读写同一进程中的数据进行通信，但是进程通信需要借助 IPC".