NOTE

Program, Process, and Thread

[toc] 1. What Are They? A program is a collection of instructions stored on the hard disk; it is a static entity. A process is a program loaded into memory with its instructions executed by the CPU; it is a dynamic entity. A thread is one of multiple

Operating Systems / LinuxCreated Updated 2 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Are They?

A program is a collection of instructions stored on the hard disk; it is a static entity. A process is a program loaded into memory with its instructions executed by the CPU; it is a dynamic entity. A thread is one of multiple execution flows within a process.

Example: Take a video player. After installation it lies on the hard disk, at which point it is a set of instructions + data. When a video is played, this program is first loaded into memory, and the CPU reads and executes the instructions. A video has both pictures and sound, and these need to proceed at the same time, so there are two threads: one responsible for sound and one responsible for pictures.

2. Development History of Processes, Threads, and Coroutines

2.1. Single-Process Operating System

  • Processes are executed sequentially, one after another.
  • Problems
    • One process must finish before the next process can execute.
    • If a process blocks, CPU resources are wasted.

2.2. Multi-Process/Multi-Thread Operating System

  • When one process blocks, switch to the next process for execution.
  • Problems
    • CPU switching cost. The more processes/threads there are, the higher the cost. During peak periods, perhaps 60% is used to execute programs and 40% for switching.
    • High resource usage. A process occupies 4G of virtual memory, while a thread occupies 4MB of memory.

2.3. Coroutines

  • N:1

    • Advantage: coroutine switching is completed in a user-space thread without entering kernel space, so this switching is very lightweight and fast.
    • Disadvantage: once a coroutine blocks and causes the thread to block, the other coroutines in this process cannot execute at all, so there is no concurrency capability.
  • 1:1

    • Advantage: solves the N:1 problem.
    • Disadvantage: the cost of creating, deleting, and switching coroutines is all completed by the CPU.
  • M:N

    • Advantage: solves the disadvantages of N:1 and 1:1.
    • Disadvantage: complex to implement.

3. Process State Transitions

  • Ready: waiting to be scheduled.
  • Running.
  • Waiting: waiting for resources.

4. Process vs Thread

Process Thread
Definition Dynamic entity of a program; a program has at least one process One execution path in a process; a process has at least one thread
Basic unit Basic unit of resource allocation Basic unit of system scheduling
Address space Has an independent address space; one crash does not make all of them crash Shares the process address space; if one crashes, the others also crash
Overhead Larger Smaller
Communication IPC Shared memory

4.1. Which Switches Faster, a Process or a Thread, and Why?

Thread switching is faster than process switching because threads share an address space, while processes have independent address spaces. Process switching therefore also incurs the overhead of switching the virtual address space.

5. Thread vs Coroutine

Threads are scheduled by the CPU and are preemptive; coroutines are scheduled in user space and are cooperative.

6. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub