0% found this document useful (0 votes)
64 views9 pages

Threads

Uploaded by

Md Majedul Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views9 pages

Threads

Uploaded by

Md Majedul Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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

html

Download to view thread in your system (Process threads view)


https://www.nirsoft.net/utils/process_threads_view.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.

Figure 4.1 - Single-threaded and multithreaded processes


4.1.1 Motivation

 Threads are very useful in modern programming whenever a process has


multiple tasks to perform independently of the others.
 This is particularly true when one of the tasks may block, and it is desired to
allow the other tasks to proceed without blocking.
 For example in a word processor, a background thread may check spelling and
grammar while a foreground thread processes user input ( keystrokes ), while
yet a third thread loads images from the hard drive, and a fourth does periodic
automatic backups of the file being edited.
 Another example is a web server - Multiple threads allow for multiple requests to
be satisfied simultaneously, without having to service requests sequentially or to
fork off separate processes for every incoming request. ( The latter is how this
sort of thing was done before the concept of threads was developed. A daemon
would listen at a port, fork off a child for every incoming request to be processed,
and then go back to listening to the port. )

Figure 4.2 - Multithreaded server architecture

4.1.2 Benefits

 There are four major categories of benefits to multi-threading:


1. Responsiveness - One thread may provide rapid response while other
threads are blocked or slowed down doing intensive calculations.
2. Resource sharing - By default threads share common code, data, and
other resources, which allows multiple tasks to be performed
simultaneously in a single address space.
3. Economy - Creating and managing threads ( and context switches between
them ) is much faster than performing the same tasks for processes.
4. Scalability, i.e. Utilization of multiprocessor architectures - A single
threaded process can only run on one CPU, no matter how many may be
available, whereas the execution of a multi-threaded application may be
split amongst available processors. ( Note that single threaded processes
can still benefit from multi-processor architectures when there are
multiple processes contending for the CPU, i.e. when the load average is
above some certain threshold. )

4.2 Multicore Programming

 A recent trend in computer architecture is to produce chips with multiple cores, or


CPUs on a single chip.
 A multi-threaded application running on a traditional single-core chip would have to
interleave the threads, as shown in Figure 4.3. On a multi-core chip, however, the
threads could be spread across the available cores, allowing true parallel processing, as
shown in Figure 4.4.

Figure 4.3 - Concurrent execution on a single-core system.

Figure 4.4 - Parallel execution on a multicore system

 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.

4.2.1 Programming Challenges ( New section, same content ? )

 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.

4.2.2 Types of Parallelism ( new )

In theory there are two different ways to parallelize the workload:

1. Data parallelism divides the data up amongst multiple cores ( threads ),


and performs the same task on each subset of the data. For example
dividing a large image up into pieces and performing the same digital
image processing on each piece on different cores.
2. Task parallelism divides the different tasks to be performed among the
different cores and performs them simultaneously.

In practice no program is ever divided up solely by one or the other of these, but instead by
some sort of hybrid combination.

4.3 Multithreading Models

 There are two types of threads to be managed in a modern system:

 User threads and


 kernel threads.

 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.

4.3.1 Many-To-One Model

 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.

Figure 4.5 - Many-to-one model

4.3.2 One-To-One Model

 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.

Figure 4.6 - One-to-one model


4.3.3 Many-To-Many Model

 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.

Figure 4.7 - Many-to-many model

 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

4.4 Thread Libraries

 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

1 Process is heavy weight or resource Thread is light weight, taking lesser


intensive. resources than a process.

2 Process switching needs interaction Thread switching does not need to


with operating system. interact with operating system.

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.

5 Multiple processes without using Multiple threaded processes use fewer


threads use more resources. resources.

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.

2 Implementation is by a thread library at Operating system supports


the user level. creation of Kernel threads.

3 User-level thread is generic and can run Kernel-level thread is specific to


on any operating system. the operating system.

4 Multi-threaded applications cannot take Kernel routines themselves can


advantage of multiprocessing. be multithreaded.

You might also like