Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 2.3: Threads and multithreading models

Why one process may contain many threads, how concurrency differs from parallelism, how user threads map to kernel threads, and how to create threads with Pthreads.

Module 2 1.5 hours Theory CO2
← Before
2.2 Operations on processes
fork, exec, wait, exit
You are here
2.3 Threads and multithreading models
Module 2 · Process, Scheduling, Threads and Deadlocks
Next →
2.4 CPU scheduling I
Criteria, FCFS, SJF, Priority

Learning objectives

By the end of this session you should be able to:

  • Define a thread and list what threads share and what each owns privately.
  • State four benefits of multithreading and distinguish concurrency from parallelism.
  • Apply Amdahl's law to estimate speed-up on multicore systems.
  • Compare many-to-one, one-to-one, many-to-many and two-level models.
  • Write a Pthreads program using pthread_create and pthread_join.

Lecture notes

1. What is a thread?

A thread is a basic unit of CPU utilisation: it has a thread ID, a program counter, a register set and a stack. It shares with the other threads of the same process the code section, data section, heap, open files and signals. A traditional process has one thread; a multithreaded process has several and can do several things at once — a web browser downloads images in one thread while rendering text in another.

Single-threaded process

code · data · files
registers · stack
1 thread

Multithreaded process

code · data · files (shared)
regs+stack | regs+stack | regs+stack
3 threads

2. Benefits

Responsiveness

A long operation in one thread doesn't freeze the UI thread.

Resource sharing

Threads share memory automatically — no IPC set-up needed.

Economy

Creating and switching threads is much cheaper than processes (no new address space).

Scalability

Threads of one process can run in parallel on different cores.

3. Multicore programming: concurrency vs parallelism

Concurrency means more than one task is making progress (possible on one core by interleaving). Parallelism means more than one task executes at the same instant (needs multiple cores).

Single core (concurrent): T1T2T3T1T2…

Two cores (parallel):
core 0: T1T3T1
core 1: T2T4T2

Amdahl's law — the limit of parallel speed-up

If a fraction S of a program must run serially, the speed-up on N cores is at most

\[ \text{speedup} \le \frac{1}{S + \frac{1-S}{N}} \]

Example: 25% serial (S = 0.25), N = 2 → 1/(0.25 + 0.375) = 1.6×; N = 4 → 1/(0.25 + 0.1875) = 2.29×; N → ∞ → at most 1/0.25 = 4×. The serial part dominates as cores grow.

4. User threads and kernel threads

  • User threads are managed by a thread library in user space, without kernel support. Fast to create and switch, but the kernel sees only one process.
  • Kernel threads are supported and scheduled directly by the OS (Linux, Windows, macOS all do this).

A multithreading model describes how user threads are mapped to kernel threads.

Many user threads → one kernel thread. Thread management is done by a user-level library, so it is efficient.

U1U2U3
↓ ↓ ↓
K

Problem: if one thread makes a blocking system call, the whole process blocks; and only one thread can be in the kernel at a time, so no parallelism on multicore. Example: early Solaris Green Threads, GNU Portable Threads.

Each user thread → its own kernel thread.

U1U2U3
↓ ↓ ↓
K1K2K3

More concurrency: a blocking call blocks only that thread; threads run in parallel on multiple cores. Cost: creating a user thread creates a kernel thread, so systems may limit the number. Used by Linux and Windows (this is what you get with Pthreads on Linux).

Many user threads multiplexed onto a smaller or equal number of kernel threads.

U1U2U3U4
↘ ↓ ↓ ↙
K1K2

Developers create as many user threads as they like, and the kernel threads still run in parallel; a blocking call lets another kernel thread be scheduled. Harder to implement — few systems use it today (Go's goroutine scheduler works this way in user space).

A variation of many-to-many that also allows a user thread to be bound to a kernel thread (older IRIX, HP-UX, Solaris 8).

5. Thread libraries — Pthreads

A thread library provides the API for creating and managing threads. The three main ones are POSIX Pthreads, Windows threads and Java threads. Pthreads is a specification (IEEE 1003.1c); Linux implements it with one-to-one kernel threads.

sum_thread.c — a worker thread computes 1 + 2 + … + N
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

long sum;                              /* shared by all threads (data section) */

void *runner(void *param)              /* the thread starts here */
{
    long upper = atol(param);
    sum = 0;
    for (long i = 1; i <= upper; i++)
        sum += i;
    pthread_exit(0);
}

int main(int argc, char *argv[])
{
    pthread_t tid;                     /* thread identifier  */
    pthread_attr_t attr;               /* thread attributes  */

    if (argc != 2) { fprintf(stderr, "usage: %s <n>\n", argv[0]); return 1; }
    pthread_attr_init(&attr);
    pthread_create(&tid, &attr, runner, argv[1]);  /* create */
    pthread_join(tid, NULL);                       /* wait   */
    printf("sum = %ld\n", sum);
    return 0;
}
Terminal
$ gcc sum_thread.c -o sum_thread -pthread
$ ./sum_thread 10
sum = 55

6. Threading issues (brief)

  • fork() in a multithreaded program — on Linux/POSIX the child gets only the calling thread; if it calls exec() immediately, that is all it needs.
  • Thread cancellation — asynchronous (immediately) or deferred (the target checks at safe points).
  • Thread pools — create threads once and reuse them for incoming requests, bounding the number of threads.
  • Shared data — because threads share globals like sum, two threads updating it at once can race. Synchronization is Module 3.

VideoBytes

Threads and their benefits

Video coming soon

Threads and their benefits

Recorded by Dr. Krithika L.B. Slot m2-threads

Multithreading models and Pthreads

Video coming soon

Multithreading models and Pthreads

Recorded by Dr. Krithika L.B. Slot m2-thread-models

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
Which of these are shared between threads of a process: stack, heap, global variables, registers, open files?

Shared: heap, global variables, open files. Private: stack and registers (and the program counter).

Why does a blocking system call block the whole process in the many-to-one model?

All user threads are mapped to a single kernel thread; when it blocks in the kernel, there is no other kernel thread to run the remaining user threads.

An application is 40% serial. What is the maximum speed-up on 8 cores?

1 / (0.4 + 0.6/8) = 1 / 0.475 ≈ 2.1×.

Can a single-core machine have concurrency? Parallelism?

Concurrency yes (interleaving threads); parallelism no — it needs several cores executing at the same instant.

What does pthread_join do?

It blocks the caller until the specified thread terminates — the thread analogue of wait().

Key takeaways

  • Threads share code, data, heap and files; each has its own registers, PC and stack.
  • Benefits: responsiveness, sharing, economy, scalability; Amdahl's law limits speed-up.
  • Models: many-to-one (no parallelism), one-to-one (Linux/Windows), many-to-many, two-level.
  • Pthreads: pthread_create starts a thread, pthread_join waits for it.

Where this leads

Processes and threads all compete for the CPU. Which one should run next, and for how long? That is CPU scheduling.

Continue to 2.4 — Scheduling criteria and non-preemptive algorithms →