Thread

From OSDev.wiki
Revision as of 17:21, 13 January 2018 by osdev>Shukantpal (Introduction; Advantages; Complexity of Threads;)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

In operating systems, threads are the basic unit of execution and concurrency of various tasks pending in the system. They are generally internal components of processes and share the same set of resources, i.e. file handles, shared-memory handles, POSIX signals, message-passing buffers and many more. But they can have specific sets of special resources in architectures like Windows NT, where the kernel can destroy some resources corresponding to a thread when it terminates.

Threads can see each other executing in a parallel fashion as they get selected by the scheduler, and then execute for a specific period of time. That means if the threads were designed to repeatedly print a specific number unique to each thread, you would see them being printed in a random fashion. But threads can concurrently execute in real-time on multi-processor systems only, because multiple CPUs are executing the tasks simultaneously as opposed to uni-processor systems where one CPU executes each task one-by-one.

Advantages

All modern operating systems implement threads with deep support. They export kernel calls for configuring how the kernel manages various threads. Threads have many advantages over traditional processes and are used in newer applications -

1. Concurrency - Threads allow multiple tasks to execute in a parallel fashion and speed up the various work of the program. For example, two worker threads can be parsing two different CSS stylesheets in a browser while loading a page. This increase the output and responsiveness of the program.

2. Better Resource Usage - When work is divided into separate processes, a huge overhead is incurred as compared to dividing the work between threads that live in the same address space under one process. It also reduces context switches and can provide better cache usage for the program.

3. User-level Management of Threads - When short-lived tasks arise in a process, threads can also be created in user-space which eliminates the need for a context switch the kernel. This greatly improves performance with the M:N threading model.

4. Simplicity - Creating a single program which effectively utilizes a thread API, is very simple compared with creating separate work-processes and corresponding programs, using inter-process communication to inter-link them.

Complexity of Threads

Multithreading in programs and the operating system itself can lead to the following undesirable conditions -

1. Race Conditions & Synchronization Overhead - When many threads are working on the same data structure in memory, then they must somehow serialize their changes with synchronization techniques. This greatly impacts performance when the number of competing threads is huge, especially when concurrency bugs cause deadlocks, livelocks or other race conditions. For example, if threads 'A' and 'B' require both the resources 'R1' and 'R2', and 'A' locks 'R1' first and 'B' locks 'R2' first. Now both are competing for the other resource, both of which are already locked. This results into a complete stoppage of execution and is called a deadlock.

2. Reliability - Multithreading is indeed complex compared to the single-threaded model. Also, if one thread throws a exception (not C++) which the system cannot handle then the whole process may be terminated.

TODO: Talk about threading models (1:1, 1:N, M:N), and kernel-mode and user-mode threads, blocking, schedulers and threading APIs