Threads
Threads

THREADS

Gabrelsoft

--

Thread, a lightweight task runner in an operating system is a basic unit to which an operating system allocates processor time. Every thread has scheduled priority and maintains a set of structures for the method used in executing tasks. A thread possesses a thread context, used in keeping information for executing tasks in a CPU. A situation where multiple thread run is called a Multi-threaded.

In this article, I will be sharing the advantages, and disadvantages of threading and also discussing Multi-threading.

There are two types of threads, which are user threads and kernel threads.

The User-Level Thread:

The implementation of the user thread is rightly on top of the Kernel thread, most times we encounter this one because it’s closest to us, because it’s implemented by us. Its operation is blocked and contains code for running and executing tasks in a thread context. The application starts with a single thread which later propagates into more.

However, the use of blocking calls made by the application developer may be blocked. A simple example of this problem is when the developer is performing a I/O operation, made to perform synchronously. When its initiated, a system call is made to the kernel-level thread and does not return the I/O operation that has been completed. In this case, it’s being blocked by the kernel and will not run. To avoid this simple pitfall, the program is to run asynchronously(non-blocking), for other threads to run.

The Kernel-Level Thread:

The kernel thread is often supported by the computer operating system directly. The kernel maintains a context of information that is reported down to registers, program counter, and thread-local-storage(if found), it’s relatively cheap to destroy since it’s gotten by default from the computer. Its scheduling is done on a thread basic. The Kernel performs scheduling and management in the Kernel area. It’s often slow to create and handle than the user threads,

also, the transfer of control from one thread to another within the same process requires a mode switch to the Kernel.

Since the computer is multitasking by default, and supports the kernel thread then we can see it as an advantage to kernel-level thread. One process must make a switch for another one to run, the switching of context process making itself unrunnable, with a state of waiting for an I/O or for synchronization operation to complete. To avoid unused threads the CPU is programmed to interrupt threads that exceed their time.

Multi-Threading Models

From the above, we understand thread can run singly, but when multiple threads run it’s known as multithreaded, which runs concurrently. In this later part, we will discuss Multi-thread Model.

Now we need to understand the Thread Model; having multiple threads running parallel in one process is similar to having multiple processes running in parallel in one computer. Threads share address space and other resources, this applies to memory, disk, and printers. The term multi-threading is also used to explain the situation of allowing various threads in the same process.

Multiple threads within the same application can run in parallel on multiple processors, it has three models similar to the Database entity relationship.

  • One-to-One relationship
  • Many-to-One relationship
  • Many-to-Many relationship

One-to-One Relationship

In this model, the kernel creates a thread to handle each user thread and places a limit on the number of threads created.

Example: Microsoft Windows XP and 95 implement this model

Many-to-One Relationship

In this model, user threads are mapped onto a single kernel thread in the operating system, it also possesses some thread library to help in its efficiency.

Many-to-Many Relationship

This model combines the best features of the one-to-one and many-to-one models.

  • Processes are split across multiple processors
  • Blocking the kernel system calls does not block the entire process
  • Users can create any number of the threads

--

--