threadsthreads operating systems. threadsthreads a thread, or thread of execution, is the sequence...
Post on 22-Dec-2015
229 Views
Preview:
TRANSCRIPT
ThreadsThreadsThreadsThreadsA Thread, or thread of execution, is the sequenceof instructions being executed.
A process may have one or more threads.
Each thread has its own * program counter * set of registers * execution stack
But, all threads within a process share the same address space the same files, the same signals, and the same child processes!
operatingsystems
code data files
registers stack
thread
single thread process
code data files
registers
stack
thread
registers
stack
thread
multiple thread process
pc
pc pc
Why Threads?Why Threads?Why Threads?Why Threads?
It allows an application to do multiple things at once * Real parallelism with multiple cpu’s * overlap I/O, user interaction with processing * share the same address space
Threads are cheaper than processes because they haveno resources associated with them. Context switchesare cheaper.
operatingsystems
Benefits of using threads
Responsiveness: One thread can run while another is blocked *
Resource sharing
Economy: On a Solaris (SUN) O/S Thread creation is 30 times faster than process creation
Context switching for a thread is 5 times faster than process context switching
Exploitation of multi-processor architectures
* Assuming that threads are implemented in the kernel
operatingsystems
Implementing ThreadsImplementing ThreadsImplementing ThreadsImplementing Threads
There are two approaches to implementing threads,and the differences are quite controversial.
Support for threads can be provided either at theuser level or by the kernel.
Virtually all contemporary operating systems supportkernel threads.
operatingsystems
Implementing ThreadsImplementing ThreadsImplementing ThreadsImplementing ThreadsImplementing Threads in User Space
userspace
kernelspace
process table
thread table
run-time system
process
threads
thread table
run-time system
process
threads
A run-time systemprovides the interface to manage threads
Each process has it’sown thread table
No trap to kernel spaceis needed, no contextswitch is needed
operatingsystems
-- many to One Model --
Implementing ThreadsImplementing ThreadsImplementing ThreadsImplementing ThreadsImplementing Threads in User Space
userspace
kernelspace
process table
thread table
run-time system
process
threads
thread table
run-time system
process
threads
operatingsystems
Problems:
1. A blocking system call issued in one thread will block all threads in the process.
2. If a page fault occurs in one thread, all threads are blocked while the system pages.
3. Once a thread starts running there is no way to interrupt it. It must give up the cpu voluntarily.
Implementing ThreadsImplementing ThreadsImplementing ThreadsImplementing ThreadsImplementing Threads in the Kernel
userspace
kernelspace
process table
process
threads
process
threads
No run time system. Usekernel calls to manage threads
Thread table is kept in thekernel
Kernel schedules threads,no blocking issues
But … system calls are much more expensive thanusing a run-time system.
thread table
operatingsystems
-- one to one model --
Implementing ThreadsImplementing ThreadsImplementing ThreadsImplementing ThreadsThere is a hybrid approach to thread support used by Linux.
userspace
kernelspace
process table
process
threads
process
threads
The kernel has a small setof re-usable threads that itmanages.
Threads in user space aremapped onto one of the available kernel threads.
thread table
thread table
run-time system
thread table
run-time system
operatingsystems
Thread Libraries
Thread libraries provide the programming interfacefor creating and managing threads.
Thread libraries may be implemented completely inuser space, or in kernel space.
Thread Libraries
There are three main thread libraries in use today
* POSIX pthreads * win32 threads * Java threads
Thread Issues
When a thread issues a fork( ) system call, does the newprocess contain just one thread, or all of the threads ofthe original process?
top related