Professional Documents
Culture Documents
OS Module2.1 Threads
OS Module2.1 Threads
OS Module2.1 Threads
Chapter 4: Threads
● Overview
● Multithreading Models
● Thread Libraries
● Threading Issues
Objectives
● To discuss the APIs for the Pthreads, Win32, and Java thread
libraries.
The primary difference is that threads within the same process run in a
shared memory space, while processes run in separate memory spaces.
Threads
Thread is a single sequence stream within a process.
Threads have same properties as of the process so they are called as light weight
processes.
Threads are executed one after another but gives the illusion as if they are
executing in parallel.
Threads are not independent of each other as they share the code, data, OS
resources etc.
Threads
Similarity between Threads and Processes –
•Threads are designed to assist each other, processes may or may not do it.
Single and Multithreaded Processes
Need of Thread
•It takes far less time to create a new thread in an existing process than to
create a new process.
•Threads can share the common data, they do not need to use Inter-
Process communication.
● Resource Sharing
By default, threads share the memory (and resources) of the process to
which they belong. Thus, an application is allowed to have several
different threads of activity within the same address-space.
● Economy
Allocating memory and resources for process-creation is costly. Thus, it is
more economical to create and context-switch threads.
● Scalability
In a multiprocessor architecture, threads may be running in parallel on
different processors. Thus, parallelism will be increased.
Types of Threads
In the operating system, there are two types of threads.
2.User-level thread.
User-level thread
• The operating system does not recognize the user-level thread.
• The kernel level thread does not know nothing about the user level thread.
• There is a thread control block and process control block in the system for
each thread and process in the kernel-level thread.
• The kernel knows about all the threads and manages them.
• The kernel-level thread offers a system call to create and manage the
threads from user-space.
2.The scheduler may decide to spend more CPU time in the process of
threads being large numerical.
3.The kernel-level thread is good for those applications that block the
frequency.
◦ Dividing activities
◦ Balance
◦ Data splitting
◦ Data dependency
● Examples
● Windows XP/2000
● Solaris
● Linux
● Tru64 UNIX
● Mac OS X
Difference between User-Level & Kernel-Level Thread
S.N. User-Level Threads Kernel-Level Thread
Multithreading allows the application to divide its task into individual threads.
In multi-threads, the same process or task can be done by the number of threads,
or we can say that there is more than one thread to perform the task in
multithreading. With the use of multithreading, multitasking can be achieved.
Many operating systems support kernel thread and user thread in a combined
way. Example of such system is Solaris.
1. Many-to-one model
2. One-to-one model
3. Many-to-many model.
Many to Many Model
In this model, we have multiple user threads multiplex to same or lesser number
of kernel level threads.
Number of kernel level threads are specific to the machine, advantage of this
model is if a user thread is blocked we can schedule others user thread to other
kernel thread.
Example:
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
Many to One Model
Many-to-one model maps many user level threads to one Kernel-level thread.
Only one thread can access the Kernel at a time, so multiple threads are
unable to run in parallel on multiprocessors.
Examples:
Solaris Green Threads
GNU Portable Threads
One to One Model
It also allows another thread to run when a thread makes a blocking system call.
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
Two-level Model
● Similar to M:M, except that it allows a user thread to be bound to
kernel thread
● Examples
● IRIX
● HP-UX
● Tru64 UNIX
● Solaris 8 and earlier
Thread Libraries
● Thread library provides programmer with API for creating and managing
threads.
• Library entirely in user space. All code and data structures for the library
exist in a local function call in user space and not in a system call.
• Kernel-level library supported by the OS. In this case the code and data
structures for the library exist in kernel space.
Thread Libraries
The main thread libraries which are used are
•JAVA thread − The JAVA thread API allows threads to be created and
managed directly as JAVA programs.
● Signal handling
● Thread pools
● Thread-specific data
Semantics of fork() and exec()
● One that duplication all threads- The child thread does not call exec()
after Forking.
● Options:
● Deliver the signal to the thread to which the signal applies.
● Deliver the signal to every thread in the process.
● Deliver the signal to certain threads in the process.
● Assign a specific thread to receive all signals for the process.
Thread Pools
● Create a number of threads in a pool where they await work.
● Advantages:
● Usually slightly faster to service a request with an existing thread
than create a new thread.
● Useful when you do not have control over the thread creation process
(i.e., when using a thread pool).