Threads
Threads
html
Threads
References:
1. Abraham Silberschatz, Greg Gagne, and Peter Baer Galvin, "Operating System Concepts,
Ninth Edition ", Chapter 4
4.1 Overview
A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and
a set of registers, ( and a thread ID. )
Traditional ( heavyweight ) processes have a single thread of control - There is one
program counter, and one sequence of instructions that can be carried out at any given
time.
As shown in Figure 4.1, multi-threaded applications have multiple threads within a
single process, each having their own program counter, stack and set of registers, but
sharing common code, data, and certain structures such as open files.
4.1.2 Benefits
For operating systems, multi-core chips require new scheduling algorithms to make
better use of the multiple cores available.
As multi-threading becomes more pervasive and more important ( thousands instead
of tens of threads ), CPUs have been developed to support more simultaneous threads
per core in hardware.
For application programmers, there are five areas where multi-core chips
present new challenges:
1. Identifying tasks - Examining applications to find activities that can be
performed concurrently.
2. Balance - Finding tasks to run concurrently that provide equal value. I.e.
don't waste a thread on trivial tasks.
3. Data splitting - To prevent the threads from interfering with one another.
4. Data dependency - If one task is dependent upon the results of another,
then the tasks need to be synchronized to assure access in the proper
order.
5. Testing and debugging - Inherently more difficult in parallel processing
situations, as the race conditions become much more complex and difficult
to identify.
In practice no program is ever divided up solely by one or the other of these, but instead by
some sort of hybrid combination.
User threads are supported above the kernel, without kernel support. These are the
threads that application programmers would put into their programs.
Kernel threads are supported within the kernel of the OS itself. All modern OSes
support kernel level threads, allowing the kernel to perform multiple simultaneous
tasks and/or to service multiple kernel system calls simultaneously.
In a specific implementation, the user threads must be mapped to kernel threads, using
one of the following strategies.
In the many-to-one model, many user-level threads are all mapped onto a single
kernel thread.
Thread management is handled by the thread library in user space, which is very
efficient.
However, if a blocking system call is made, then the entire process blocks, even if
the other user threads would otherwise be able to continue.
Because a single kernel thread can operate only on a single CPU, the many-to-one
model does not allow individual processes to be split across multiple CPUs.
Green threads for Solaris and GNU Portable Threads implement the many-to-one
model in the past, but few systems continue to do so today.
The one-to-one model creates a separate kernel thread to handle each user
thread.
One-to-one model overcomes the problems listed above involving blocking
system calls and the splitting of processes across multiple CPUs.
However the overhead of managing the one-to-one model is more significant,
involving more overhead and slowing down the system.
Most implementations of this model place a limit on how many threads can be
created.
Linux and Windows from 95 to XP implement the one-to-one model for threads.
The many-to-many model multiplexes any number of user threads onto an equal
or smaller number of kernel threads, combining the best features of the one-to-
one and many-to-one models.
Users have no restrictions on the number of threads created.
Blocking kernel system calls do not block the entire process.
Processes can be split across multiple processors.
Individual processes may be allocated variable numbers of kernel threads,
depending on the number of CPUs present and other factors.
One popular variation of the many-to-many model is the two-tier model, which
allows either many-to-many or one-to-one operation.
IRIX, HP-UX, and Tru64 UNIX use the two-tier model, as did Solaris prior to
Solaris 9.
Figure 4.8 - Two-level model
Thread libraries provide programmers with an API for creating and managing threads.
Thread libraries may be implemented either in user space or in kernel space. The
former involves API functions implemented solely within user space, with no kernel
support. The latter involves system calls, and requires a kernel with thread library
support.
There are three main thread libraries in use today:
1. POSIX Pthreads - may be provided as either a user or kernel library, as an
extension to the POSIX standard.
2. Win32 threads - provided as a kernel-level library on Windows systems.
3. Java threads - Since Java generally runs on a Java Virtual Machine, the
implementation of threads is based upon whatever OS and hardware the JVM is
running on, i.e. either Pthreads or Win32 threads depending on the system.
Difference between Process and Thread
S.N. Process Thread
3 In multiple processing environments, All threads can share same set of open
each process executes the same code files, child processes.
but has its own memory and file
resources.
4 If one process is blocked, then no While one thread is blocked and waiting,
other process can execute until the a second thread in the same task can
first process is unblocked. run.
6 In multiple processes each process One thread can read, write or change
operates independently of the others. another thread's data.
Difference between User-Level & Kernel-Level Thread
S.N. User-Level Threads Kernel-Level Thread
1 User-level threads are faster to create and Kernel-level threads are slower
manage. to create and manage.