Day 13— CS Fundamentals December — About Operating Systems — Threading

Yashvardhan Kukreja
4 min readDec 14, 2019

Threading: A very important topic even from a programmer’s perspective. I don’t think I need to emphasize the importance of learning the concept of threading for acing a software engineering interview.

So, without further adieu, I will get started with the article.

Let’s dive in!

Introduction — The rise of threads

So, we know that a process is basically a program under execution. Also, a process can create other processes which are child processes under it.

Now, the issue is that processes are kind of heavy in the sense that they take more time to spin up, they take more time to terminate, and they even don’t share memory space hence, inter-process communication involves some irritating latency.

So, Enter the “threads”

Processes are divided into segments called threads. Threads are extremely lightweighted.

  • They are super fast and light to spin up.
  • They can be terminated very quickly.
  • Even the context switching, in case of threads, is wayyyy quicker than in the case of processes.

“In computers, unlike real life, threads are very very different from strings ” — Me :P

Types of processes

Single-threaded processes

This is a process which is composed of and handled by only one thread. That’s it! Here, a single thread utilizes all the resources like code, data and files.

Multi-threaded processes

Here, the process is controlled by multiple threads. So, the resources are shared by all the threads.

For each of the threads, depending on the type of resources it needs, resources are shared.

For example,

if thread 1 requires only the code, then this thread will get access to the code of the process.

if thread 2 requires data associated, then this thread will get access to the data associated with the process.

if thread 3 requires files associated, then this thread will get access to the files associated with the process.

Now, the great thing about using multiple threads is that you can execute multiple tasks for the process at the same time.

Some nice benefits of using threads

  • Remote Procedure Calls: The fact that threads share the same memory address space makes the inter-thread communication very convenient and fast hence, making them suitable to make Remote Procedure Calls.
  • Responsiveness: Programs can continue running even if some parts (threads) of it get blocked hence, ensuring responsiveness to the user.
  • Resource Sharing: Allows applications to have several different threads of activities share resources within the same memory address space.
  • Utilization of multi-processor architecture: Threads can be made to run in parallel over multiple processors and hence, making execution fast as ever.

Types of Threads

  • User-level thread: As a programmer, whenever you create a thread through your code, you are creating a user-level thread because that thread is created in the user-space. Basically, a user-level thread is meant for executing user-space code.
  • Kernel-level thread: This is the one which is associated with only kernel code and is not involved with user-space code. The kernel threads are created and managed by the Operating System for tasks like memory management, process management, etc.

Now, the thing is, at the end of the day, every thread has to communicate with the kernel because after all, it is executing a piece of code only, right!

So, we know that kernel-level threads are directly associated with the OS and hence, kernel.

But what about user-level threads?

They are not supposed to and don’t communicate with the kernel directly.

Actually, they first communicate with the right kernel-level thread which in turn makes them communicate with the kernel.

So, we can say that user-level threads are mapped to kernel-level threads.

But they can be mapped to kernel-level threads in different ways. Let’s talk about that!

User-level to Kernel-level thread mapping models

  • One-to-One model: Here, every user-level thread is mapped and linked to a single-different kernel-level thread.
  • Many-to-One model: Here, many user-level threads map to one kernel-level thread.
  • Many-to-Many model: Here, the user-level threads are mapped to smaller or equal number of kernel-level threads.

That’s it!

Thanks for reaching till here!

I hope you understood the article and got a good idea about threads and threading practices in operating systems.

Stay tuned for another article which is going to come tomorrow associated with some other interesting CS fundamental.

Find me on

LinkedInhttps://www.linkedin.com/in/yashvardhan-kukreja-607b24142/

GitHubhttps://www.github.com/yashvardhan-kukreja

Email — yash.kukreja.98@gmail.com

Adios!

--

--

Yashvardhan Kukreja

Software Engineer @ Red Hat | Masters @ University of Waterloo | Contributing to Openshift Backend and cloud-native OSS