Operating Systems - Threads

What is a thread?

In the article Operating Systems - The Process Concept I wrote about processes that I assumed to have only one thread of control. A process with only one thread in execution is called a heavy-weight process. In this article I will introduce you to multithreaded processes.

Figure: Single-threaded vs multithreaded process


The figure above illustrates two processes. Threads always exists in a process. All of the threads in a process share the same memory space. They share the same code, the same data and files. But since they are executing on its own, they threads have their own registers and their own stack. Therefore, the system doesn't schedule a whole process, it's a thread that is being scheduled. Multithreaded processes can do many things at the same time, and this becomes very powerful since we have multicore processors today. One process can use more than one cores by splitting the process into threads, and let the threads be scheduled on different cores.


Why tho?

If we would only have one core to work with, we could make that core work faster by increasing the clocks frequency. And that's exactly what people did like 15 years ago. The clock frequency determines how fast the processor can shift the transistors from zeros to ones. The problem that arises here is that the greater frequency you get, the greater is the risk for your system to become unstable. Zeros can start to be interpreted as ones, and ones can be interpreted as zeros, and everything crashes. Another problem is that it generates more energy, which means that it generates more heat which can lead the system to overheat. And that's the reason why multicore processors came into play, so we have more separated working pieces in our processors, so they can work on lower clock frequency and do more work at the same time.

Multiple threads of execution means that we can do many things simultaneously in process.




Picture a loading website, imagine if nothing would happen when you click on buttons while it's loading. By splitting the work into multiple threads, the website can load and respond at the same time. At least give us the illusion that it's doing it simultaneously, in reality it's switching between those threads rapidly.



Creating and managing processes becomes easier. Context switching becomes more effective, time is being saved by switching threads from the same process than switching threads from different processes.


Resource sharing

All threads are in the same memory space, better utilization of resources.



A lot more effective.


Types of threads

There's two main types of threads, namely User threads and Kernel threads. User threads execute in user-mode, and they are invisible to the kernel. Kernel threads are part of the kernel, they do not necessarily execute all code in kernel-mode though. User threads are mapped to the kernel threads. The mapping can be done in several ways:


Many to one

In the many to one model, we have many user threads that are mapped onto a single kernel thread.

Figure: Many-to-one model


One to one

In the one to one model, each user thread is mapped onto it's own kernel thread.

Figure: One-to-one model


Many to many

In the many to many model, some user threads are mapped onto some kernel threads.

Figure: Many-to-many model

Enjoyed this article? Give the teacher an apple.




authors profile photo

Articles with similar tags