You are on page 1of 20

Operating System Concepts – 9th Edition 4.

1 Silberschatz, Galvin and Gagne ©2013


Threads

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©2013
What is Thread?
 A thread is the smallest unit of processing that can be performed in an OS. In most
modern operating systems, a thread exists within a process - that is, a single
process may contain multiple threads.
 A thread is a single sequence stream within in a process. Because threads have
some of the properties of processes, they are sometimes called lightweight
processes.
 In a process, threads allow multiple executions of streams. In many respect,
threads are popular way to improve application through parallelism.
 A thread is a flow of execution through the process code, with its own program
counter that keeps track of which instruction to execute next, system registers
which hold its current working variables, and a stack which contains the execution
history.
 A thread shares with its peer threads few information like code segment, data
segment and open files. When one thread alters a code segment memory item, all
other threads see that. (Threads within the same process share the resources of
the process)

Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
What is Thread?
 Threads provide a way to improve application performance through
parallelism.
 Threads represent a software approach to improving performance of
operating system by reducing the overhead thread is equivalent to a
classical process.
 Each thread belongs to exactly one process and no thread can exist outside
a process. 
 The following figure shows the working of a single-threaded and a
multithreaded process.

Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Difference between Process and Thread

Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©2013
ADVANTAGES OF THREAD

 Responsiveness – may allow continued execution if part of process is


blocked, especially important for user interfaces
 Resource Sharing – threads share resources of process, easier than
shared memory or message passing
 Economy – cheaper than process creation, thread switching lower
overhead than context switching
 Scalability – process can take advantage of multiprocessor
architectures

Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
 Concurrent execution on single-core system:

 Parallelism on a multi-core system:

Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Types of Thread
 Threads are implemented in following two ways :
User threads - management done by user-level threads library
 Three primary thread libraries:
 POSIX Pthreads
 Windows threads
 Java threads
Kernel threads - Operating System managed threads acting on kernel, an operating
system core.
 Examples – virtually all general purpose operating systems, including:
 Windows
 Solaris
 Linux
 Tru64 UNIX
 Mac OS X

Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
User Level Threads
 In this case, the thread management kernel is not aware of the existence of
threads. The thread library contains code for creating and destroying
threads, for passing message and data between threads, for scheduling
thread execution and for saving and restoring thread contexts. The
application starts with a single thread.

Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
User Level Threads
Advantages
Thread switching does not require Kernel mode privileges.
User level thread can run on any operating system.
Scheduling can be application specific in the user level thread.
 The process maintains a thread table to keep information about threads
 User level threads are fast to create and manage(Switching between threads
happens much faster).

Disadvantages
In a typical operating system, most system calls are blocking.
(in user space is that when a thread does a blocking system call (e.g. IO
operation) the operating system will take the CPU from the process instead of
staring another thread in the same process).

Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Kernel Level Threads
 In this case, thread management is done by the Kernel. There is no thread
management code in the application area. Kernel threads are supported
directly by the operating system. Any application can be programmed to be
multithreaded. All of the threads within an application are supported within a
single process.
 The Kernel maintains context information for the process as a whole and for
individuals threads within the process. Scheduling by the Kernel is done on
a thread basis. The Kernel performs thread creation, scheduling and
management in Kernel space. Kernel threads are generally slower to create
and manage than the user threads.

Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
Kernel Level Threads
Advantages
Kernel can simultaneously schedule multiple threads from the same process
on multiple processes.
If one thread in a process is blocked, the Kernel can schedule another thread
of the same process.
 OS controls the switching between threads
Disadvantages
Kernel threads are generally slower to create and manage than the user
threads.
 Switching between threads takes more time than that switching between
threads implemented in the user space

Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
Difference between User-Level & Kernel-Level Thread

Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Multithreading Models

 Many-to-One

 One-to-One

 Many-to-Many

Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
Many-to-One

 Many user-level threads mapped to


single kernel thread
 One thread blocking causes all to block
 Multiple threads may not run in parallel
on multicore system because only one
may be in kernel at a time
 Few systems currently use this model
 Examples:
 Solaris Green Threads
 GNU Portable Threads

Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
One-to-One
 Each user-level thread maps to kernel thread
 Creating a user-level thread creates a kernel thread
 More concurrency than many-to-one
 Number of threads per process sometimes
restricted due to overhead
 Examples
 Windows
 Linux
 Solaris 9 and later

Operating System Concepts – 9th Edition 4.17 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model
 Allows many user level threads to be
mapped to many kernel threads
 Allows the operating system to create
a sufficient number of kernel threads
 Solaris prior to version 9
 Windows with the ThreadFiber
package

Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
Exercises (1)
 True or false
 Processes usually share the resources of other processes ( )
 Threads in the same process share the process resources ( )
 if implemented in user mode, if a thread makes a blocking system call,
the next thread in the process will become running. ( )
 The threads has a state like processes ( )
 Each thread has its own stack and register values ( )
 Thread switching is more expensive than process switching ( )

Operating System Concepts – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
Exercises (2)
•Q2) Threads can either be managed in user mode or in kernel mode. Fill the following table with
kernel mode/user mode/both according to the given feature

Feature Kernel mode/user mode/ both


The operating system has no idea
about threads
Switching between threads is slower
When a thread blocks, another
process is scheduled
The OS controls switching between
threads

Q3) State for each of the following requirements whether we should manage threads
in user mode or in kernel mode
• We need to have faster context switching between threads within the same
process ( )
• The operating system should not be involved in context switching between
threads ( )
• When a thread does a blocking system call and the process quantum is not
finished, another thread of the same process is scheduled. ( )

Operating System Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013

You might also like