Download Why Threads? - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

CP/M wikipedia , lookup

Plan 9 from Bell Labs wikipedia , lookup

Unix security wikipedia , lookup

Burroughs MCP wikipedia , lookup

Distributed operating system wikipedia , lookup

Spring (operating system) wikipedia , lookup

Security-focused operating system wikipedia , lookup

DNIX wikipedia , lookup

Process management (computing) wikipedia , lookup

Thread (computing) wikipedia , lookup

Transcript
INTRODUCTION OF THREADS
By
Balambika.A
What is threads?
• 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.
Single-thread process
Multi-threaded
• 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.
• 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
Multithreaded process
Why Threads?
• A process with multiple threads make a great server
for example printer server.
• Because threads can share common data, they do not
need to use interprocess communication.
• Because of the very nature, threads can take
advantage of multiprocessors.
Threads are cheap in the sense that:
• They only need a stack and storage for registers therefore,
threads are cheap to create.
• Threads use very little resources of an operating system
in which they are working - threads do not need new
address space, global data, program code or operating
system resources.
• Context switching are fast when working with threads.
The reason is that we only have to save and/or restore PC,
SP and registers.
Two types of threads
• User-level thread
Thread management done by user-level threads
library.
• Kernel-Level Thread
Supported by the Kernel.
User-level threads
• User-level threads implement in user-level libraries,
rather than via systems calls, so thread switching
does not need to call operating system and to cause
interrupt to the kernel.
Three primary thread libraries:
• POSIX Pthreads
• Win32 threads
• Java threads
Advantages:
• 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.
Kernel-Level Threads
• In this method, the kernel knows about and manages the
threads. No runtime system is needed in this case.
• Instead of thread table in each process, the kernel has a thread
table that keeps track of all threads in the system.
• the kernel also maintains the traditional process table to keep
track of processes. Operating Systems kernel provides system
call to create and manage threads.
Example :
• Windows XP/2000
• Solaris
• Linux
Advantages:
• Because 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.
Multithreading Models
• There are two types of threads to be managed in a
modern system: User threads and kernel threads.
user-level threads are mapped to kernel ones.
• Many-to-One
• One-to-One
• Many-to-Many
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.
Many-to-one model
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.
One-to-one model