Threads
What is a Thread?
• A thread refers to a single sequential flow of activities being executed
in a process; it is also known as the thread of execution or the thread
of control. Now, thread execution is possible within any OS's process.
Apart from that, a process can have several threads.
Introduction
• In traditional operating systems, each process has an address space
and a single thread of control.
• it is desirable to have multiple threads of control in the same address
space running.
• With threads we now add a new element i-e, ability for the parallel
entities to share an address space and all of its data among
themselves. This ability is essential for certain applications, which is
why having multiple processes (with their separate address spaces)
will not work.
Why threading?
• Why would anyone want to have a kind of process within a process?
The main reason for having threads is that in many applications, multiple
activities are going on at once.
they are easier (i.e., faster) to create and destroy than processes.
third reason for having threads is also a performance argument.
Finally, threads are useful on systems with multiple CPUs, where real
parallelism is possible.
Read Book Page 98, 99 and 100.
The tread model
• The process model is based on two independent concepts:
resource grouping
execution
Resource grouping
• One way of looking at a process is that it is a way to group related
resources together.
• A process has an address space containing program text and data, as
well as other resources.
• These resources may include open files, child processes, pending
alarms, signal handlers, accounting information, and more.
Execution
• The thread has a program counter that keeps track of which
instruction to execute next.
• It has registers, which hold its current working variables. It has a
stack, which contains the execution history, with one frame for each
procedure called but not yet returned from.
• Although a thread must execute in some process, the thread and its
process are different concepts and can be treated separately.
Processes are used to group resources together; threads are the
entities scheduled for execution on the CPU.
POSIX threads
• To make it possible to write portable threaded programs, IEEE has
defined a standard for threads in IEEE standard 1003.1c. The threads
package it defines is called Pthreads. Most UNIX systems support it.
The standard defines over 60 function calls.
• few of the major ones to give an idea of how it works are:
Threads implementation types
• There are two main places to implement threads: user space and the
kernel.
• User level thread implementation
• Kernal level thread implementation
• Hybrid thread implementation
Scheduler activations
• While kernel threads are better than user-level threads in some key
ways, they are also indisputably slower. As a consequence,
researchers have looked for ways to improve the situation without
giving up their good properties. an approach devised by Anderson et
al. (1992), called scheduler activations. Related work is discussed by
Edler et al. (1988) and Scott et al. (1990).
• The goals of the scheduler activation work are to mimic the
functionality of kernel threads, but with the better performance and
greater flexibility usually associated with threads packages
implemented in user space
POP UP threads
• a completely different approach is also possible, in which the arrival
of a message causes the system to create a new thread to handle the
message. Such a thread is called a pop-up thread.