0% found this document useful (0 votes)
5 views

Multi Threaded Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Multi Threaded Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Thread

• A thread is a basic unit of CPU


utilization;
• it comprises a thread ID, a program
counter, a register set, and a stack.
• It shares with other threads belonging
to the same process its code section, data
section, and other operating-system
resources, such as open files and signals.
Multithread

• Most modern applications are


multithreaded
• Multiple tasks with the application can
be implemented by separate threads
• Update display
• Fetch data
• Spell checking
• Answer a network request
• Process creation is heavy-weight while
thread creation is light-weight
• Can simplify code, increase efficiency
• Kernels are generally multithreaded
User-level threads

• User-level threads implement in user-level libraries, rather than via systems calls,
• Thread switching does not need to call operating system and to cause interrupt to
the kernel.
• The kernel knows nothing about user-level threads and manages them as if they
were single-threaded processes.
Advantages of User level thread

• User-level threads does not require modification to operating systems.


• Simple Representation: Each thread is represented simply by a PC, registers, stack
and a small control block, all stored in the user process address space.
• Simple Management :This simply means that creating a thread, switching between
threads and synchronization between threads can all be done without intervention of
the kernel.
• Fast and Efficient: Thread switching is not much more expensive than a procedure
call
Disadvantages of User level thread

• There is a lack of coordination between threads and operating system kernel.


• Process as whole gets one time slice irrespective of whether process has one thread
or 1000 threads within.
• User-level threads requires non-blocking systems call
• Otherwise, entire process will block in the kernel, even if there are runnable threads
left in the processes.
Kernel-Level Threads

• The kernel level threads are managed by the kernel.


• No runtime system is needed in this case.
• The kernel has a thread table that keeps track of all threads in the system.
• In addition, the kernel also maintains the traditional process table to keep track of
processes.
• Operating Systems kernel provides system call to create and manage threads.
Advantages of Kernel level threads

• Kernel has full knowledge of all threads


• Scheduler may decide to give more time to a process having large number of threads
than process having small number of threads.
• Kernel-level threads are especially good for applications that frequently block.
Disadvantages of Kernel level threads

• The kernel-level threads are slow and inefficient.


• For instance, threads operations are hundreds of times slower than that of user-level
threads.
• Since kernel must manage and schedule threads as well as processes.
• It require a full thread control block (TCB) for each thread to maintain information
about threads.
• As a result, there is significant overhead and increased in kernel complexity.
Advantages of Threads over Multiple Processes

• Context Switching
• Threads are very inexpensive to create and destroy
• Threads require space to store, the PC, the SP, and the general-purpose
registers,
• Threads do not require space to share memory information, Information
about open files of I/O devices in use, etc.
• it is much faster to switch between threads.• it is easier for a context switch
using threads.
• Sharing
• Treads allow the sharing of a lot resources that cannot be shared in process,
• For example, sharing code section, data section, Operating System resources
like open file etc
Disadvantages of Threads over Multiprocesses

• Blocking
• If the kernel is single threaded, a system call of one thread will block the
whole process and
• CPU may be idle during the blocking period.
• Security
• an extensive sharing among threads there is a potential problem of security.
• It is possible that one thread overwrites the stack of another thread
Benefits

• Responsiveness
• Multithreading is an interactive application, may allow a program to continue
running even if part of it is blocked, or is performing a lengthy operation.
• Resource Sharing
• The benefit of sharing code and data is that it allows. an application to have
several different threads of activity within the same address space.
• Economy
• in Solaris, creating a process is about thirty times slower than is creating
thread and context switching is about five times slower.
• Scalability
• Multithreading can be greatly increased in a multiprocessor architecture,
where threads may be running in parallel on different processors
Multithreaded server architecture

• Each Thread performs a specific task, such as managing devices or interrupt


handling.
• Solaris creates a set of threads in the kernel for interrupt handling.
• Linux uses a kernel thread for managing the amount of free memory.
Concurrent execution on a Single-Core system
Parallel Execution on multi-core system
THREADS - Many-to-One Model

• Many user-level threads mapped to


single kernel thread.
• Only one thread can access the kernel
at a time,
• Hence, multiple threads are unable to
run in parallel on multicore systems.
• The entire process will block if a
thread makes a blocking system call.
THREADS – one-to-one Model

• Each user-level thread maps to kernel thread.


• It provides more concurrency than the many-to-one model by allowing another thread to
run when a thread makes a blocking system call.
• It also allows multiple threads to run in parallel on multiprocessors.• The only drawback
to this model is that creating a user thread requires creating the corresponding kernel
thread.
• Because the overhead of creating kernel threads can burden the performance of an
application
THREADS – 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
• developers can create as many user
threads as necessary, and the
corresponding kernel threads can run
in parallel on a multiprocessor.
• Also, when a thread performs a
blocking system call, the kernel can
schedule another thread for execution.
Two-level model.

• One variation on the many-to-many.


model still multiplexes many user level
threads to a smaller or equal number
of kernel threads.
• But also allows a user-level thread to
be bound to a kernel thread.
• This variation is sometimes referred
to as the two-level model

You might also like