Threads in Operating System

Sakshi Sawant
Dev Genius
Published in
2 min readNov 27, 2021

--

Kernel level and user-level threads

https://unsplash.com/photos/XJXWbfSo2f0

A thread is also called a lightweight process, it is the flow of execution through the process code.
Threads are used to improve the performance of an application by running them in parallel. Thread switching does not require to interact with the operating system, unlike a process that requires to interact with the operating system. A thread has its own program counter, system registers, and stack but they share common code, data, and file as shown in the figure below,

https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/4_Threads.html

If a thread is blocked or is waiting for some I/O operation, another thread in the same task can run without any interruption.

Threads are implemented in two ways,
1. User Level Threads: In user-level threads, threads are managed by the application and the kernel is not aware of the thread. The application begins with a single thread and then starts running in that thread. These threads can run on any application and these are fast to create and manage.

2. Kernel Level Threads: In these threads are managed by the kernel and these threads are directly supported by the operating system. The kernel is able to schedule multiple threads simultaneously from the same process on multiple processes. Kernel threads are slower to create and manage than user threads.

When multiple threads execute at the same time it is called multithreading. Multiple threads in the same application can run parallel on multiple processors and when there is a blocking system call it need not block the entire process.
There are three different types of multithreading models,

  1. Many to Many Model: In this the user level threads multiplexes to the kernel level thread of smaller or equal numbers. If a user thread is blocked we can schedule the other user threads to the other kernel thread.
  2. Many to One Model: Here, many user-level threads maps to one kernel-level thread, and thread management is done at the user space. The entire process gets blocked when a thread makes a blocking system call. In this model, the multiple threads cannot run in parallel on multiprocessors.
  3. One to One Model: Here, one user-level thread can use only one kernel-level thread at a time. This model has more concurrency than the many-to-one model. This model, to create a user thread it requires a corresponding kernel thread.

For more information on threads in the operating system refer to this site.

--

--