Skip to content
Sahithyan's S3
1
Sahithyan's S3 — Operating Systems

Thread

A thread is the basic unit of CPU utilization, consisting of a program counter, a stack, and a set of registers.

Threads are sometimes called lightweight processes. They have some of the properties of processes but are more efficient to create and manage.

Multiple threads inside the same process allow parallel or concurrent execution of tasks.

Threads are created using clone() syscall in Linux.

A traditional process has a single thread of control. A multi-threaded process has multiple threads of control within the same address space.

Each thread includes:

  • Thread ID
  • Program counter
  • Register set
  • Stack

Threads within the same process share:

  • Code section
  • Data section
  • OS resources such as open files and signals
  • Responsiveness
    Allows applications to remain responsive to user input while performing intensive operations.
  • Resource sharing
    More efficient than IPC.
  • Economy
    Thread management has overhead compared to process management.
  • Scalability
    Multithreaded applications can take advantage of multiprocessor architectures more effectively.

Threads can be implemented in user space or kernel space.

Managed by user-level library; no kernel intervention is required. Kernel doesn’t know about these. Fast creation and switching. Entire process blocks if one thread makes a blocking syscall. Mapped to 1 or more kernel thread.

Managed by OS kernel. True parallelism on multicore. One blocked thread doesn’t affect its siblings. More overhead.

Aka. LWP. A kernel-level thread, sharing the address space with a user-level thread. A process can be mapped to multiple LWPs. Kernel schedules LWPs, while user threads are mapped onto LWPs by a thread library.

Technically, a thread. Naming confusion is because of historical reasons.

Defines how user threads map to kernel threads.

Many user threads map to a single kernel thread. No parallelism. Not used in modern systems.

Example: GNU portable threads.

Each user thread map to a kernel thread. True parallelism. Higher overhead. Number of threads per process may be limited due to overhead.

Used in Linux and Windows.

Many user threads map to many kernel threads. OS decides number of kernel threads. Flexible. Rarely implemented in real-life systems.

Similar to many-tomany, but allows binding specific user threads to specific kernel threads.

Provide APIs to create/manage threads.

Manages user-level threads. All thread operations happen in user space. Fast, but a blocking system call can block all threads.

Examples: pthreads.

Manages kernel-level threads. OS manages threads directly.

Examples: pthreads, windows threads, java threads.