Understanding Threads and Processes: Scheduling, Models, and Multicore Basics
This article demystifies threads and processes, explaining OS task scheduling, the differences between them, various threading models (one-to-one, many-to-one, many-to-many), their relationship with multicore CPUs, and how to view thread and process information on Windows.
What Is a Thread
A thread is the smallest unit of execution within a program. The article begins by acknowledging the abstract nature of threads and the difficulty of explaining them clearly, aiming to make the concept more understandable.
Task Scheduling
Most operating systems (e.g., Windows, Linux) use pre‑emptive round‑robin scheduling. Each task runs for a short time slice, then is paused so the next task can run. The running state is called "running", and a paused task is in the "ready" state waiting for its next time slice. This rapid switching creates the illusion of concurrent execution.
Process
A process is an independent unit that the OS allocates resources to. It consists of a program, its data, and a Process Control Block (PCB) that stores descriptive and control information. Processes are dynamic, concurrent, independent, and structured.
Dynamic: a process exists only while its program is executing.
Concurrent: multiple processes can run at the same time.
Independent: the OS schedules each process separately.
Structured: composed of program code, data, and PCB.
Thread
Early operating systems had no concept of threads; the process was the smallest executable unit. As CPUs became faster, the overhead of switching between processes grew too large, leading to the invention of threads. A thread is a single sequential flow of control, the smallest unit the processor schedules. Threads share the memory space of their parent process.
Differences Between Process and Thread
Key distinctions:
A thread is the smallest unit of program execution; a process is the smallest unit of resource allocation.
A process contains one or more threads; threads are different execution paths within the same process.
Threads share a process's memory and resources; they are invisible to other processes.
Context switching between threads is much faster than between processes.
Multithreading and Multicore
In a multicore processor, each core corresponds to a kernel thread. Kernel threads are managed directly by the OS kernel and are scheduled onto physical cores. Modern CPUs often use hyper‑threading, presenting two logical threads per core (e.g., a dual‑core CPU appears as four threads).
Thread Models
Three common models map user‑level threads to kernel threads:
One‑to‑One Model
Each user thread has a dedicated kernel thread. This provides true parallelism on multicore systems but can be limited by the maximum number of kernel threads and incurs higher context‑switch overhead.
Many‑to‑One Model
Multiple user threads are multiplexed onto a single kernel thread. Thread switching is fast because it occurs in user space, and the number of user threads is virtually unlimited. However, if one thread blocks, all threads block, and true parallelism on multiple CPUs is not achieved.
Many‑to‑Many Model
This hybrid model maps many user threads to many kernel threads, combining the advantages of the previous two. It avoids global blocking, allows many user threads, and can improve performance on multiprocessor systems, though gains are lower than the one‑to‑one model.
Viewing Processes and Threads
On Windows, the Task Manager (opened via Ctrl+Alt+Del or right‑clicking the taskbar) shows the number of processes and threads for each application. The "Processes" tab lists each process; the "Details" tab can be customized to display a "Threads" column.
The "Performance" tab shows CPU and memory usage, and the number of logical processors (e.g., a dual‑core CPU with hyper‑threading appears as four logical processors).
Thread Lifecycle
When the number of threads is less than the number of processors, threads run truly concurrently on separate cores. If there are more threads than cores, the OS schedules them using time‑slice pre‑emptive scheduling, creating the illusion of concurrency.
Threads have five states similar to early processes: Created, Ready, Running, Blocked (waiting), and Exited. The article provides a diagram of the thread lifecycle.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
