A process may need to do several things at once, such as a word processor that checks spelling while the user types. Threads make this possible within one process.
Thread Concept
A thread is the basic unit of CPU utilisation. It has its own thread ID, program counter, register set and stack, but it shares with the other threads of the same process the code, data, open files and other resources. A traditional process has a single thread of control. A multithreaded process has several threads that run concurrently, and on several cores they can run in parallel. Threads are often called lightweight processes, since creating and switching them costs much less than for processes.
Definition: Thread. The smallest unit of execution within a process, with its own program counter, registers and stack, sharing the code, data and resources of the process with its other threads.
Benefits of Threads
Responsiveness: an interactive program can continue to respond while one thread does a long task. Resource sharing: threads share the memory and resources of their process, with no special mechanism needed. Economy: creating a thread and switching between threads are much cheaper than for processes. Scalability: a multithreaded program can use several CPU cores at once. A web server, for instance, creates a thread for each client request instead of a new process.
Types of Threads
User-level threads are managed by a thread library in user space, without kernel support. They are fast to create and switch, but if one thread makes a blocking system call, the whole process blocks, and they cannot run in parallel on several cores. Kernel-level threads are managed by the OS kernel. They can run in parallel and one can block without stopping the others, but they are slower to create and manage. Thread libraries include POSIX Pthreads, Windows threads and Java threads.
Multithreading Models
The models map user threads to kernel threads. Many-to-one: many user threads on one kernel thread; simple, but no parallelism, and one blocking call blocks all. One-to-one: each user thread has its own kernel thread; true parallelism, but many kernel threads add cost. It is used by Linux and Windows. Many-to-many: many user threads are multiplexed onto a smaller or equal number of kernel threads, combining the advantages of both.
Table 2.3: User-level and kernel-level threads
| Feature | User-level | Kernel-level |
|---|---|---|
| Managed by | Thread library | OS kernel |
| Creation and switch | Fast | Slower |
| Blocking call | Blocks the whole process | Blocks only that thread |
| Parallelism on cores | No | Yes |
Example 2.3: Process or thread?
Problem: A web server receives 1000 requests a second. Compare creating a process per request with creating a thread per request.
Solution:
- A new process needs its own address space, copied or set up, which is costly.
- A new thread shares the server's memory and needs only a stack and registers.
- Threads can also share caches and data directly.
Result: Threads are far cheaper and faster, so servers use a thread (or a pool of threads) per request.
#include <stdio.h>
#include <pthread.h>
int total = 0;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
void *work(void *arg) {
int id = *(int *)arg;
pthread_mutex_lock(&m);
total += id;
pthread_mutex_unlock(&m);
return NULL;
}
int main(void) {
pthread_t t[4];
int id[4] = {1, 2, 3, 4};
for (int i = 0; i < 4; i++)
pthread_create(&t[i], NULL, work, &id[i]);
for (int i = 0; i < 4; i++) pthread_join(t[i], NULL);
printf("four threads shared one variable: total = %d\n", total);
return 0;
}
Output
four threads shared one variable: total = 10
Common mistakes
- Thinking that threads of one process have separate memory. They share code and data.
- Assuming user-level threads run in parallel on several cores.
- Sharing data between threads without synchronisation, which causes race conditions.
Key takeaways
- A thread has its own registers and stack, and shares the process's code and data.
- Benefits: responsiveness, resource sharing, economy and scalability.
- User-level threads are fast but block together; kernel threads run in parallel.
- Models: many-to-one, one-to-one and many-to-many.
Practice questions
- What does a thread share with other threads of its process?
- Give two benefits of multithreading.
- What is the one-to-one model?
Answers to the practice questions
- The code, data, open files and other resources.
- Responsiveness and economy (also resource sharing, scalability).
- Each user thread is mapped to its own kernel thread.
Sources
- Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit II
