Professional Documents
Culture Documents
Chapter 4 102
Chapter 4 102
Chapter 4 102
Why Threads : Issues with Processes What are threads? Threading models Threading issues
Copying the entire parent process as child process. It may not always be required to copy entire parent process to child process as the child is going to call exec()system call immediately in most of the cases.
IPC is required to pass information between a parent and its child processes.
A thread is a lightweight process which executes within the address space of a process. A thread is an execution context that is independently scheduled, but shares single addresses space with other threads of the same process.
Thread
f1
f2
Process Terminated
SPRING 2011 - 102
Suppose we want that f1 and f2 should be executed by separate threads, while main function is executed concurrently by another thread. Multi threading refers to the ability of an OS to support multiple threads of execution with in a single process. A single program made up of a number of different concurrent activities; sometimes called multitasking. Multithreading works similar to multiprogramming. The CPU switches rapidly back and forth among threads providing the illusion that threads are running in parallel.
SPRING 2011 - 102
main
t1 t2
PC
PC
PC
f2() { }
SPRING 2011 - 102
A thread is a lightweight process which executes within the address space of a process.
A thread can be scheduled to run on a CPU as an independent unit and terminate. Multiple threads can run simultaneously. Threads may be managed by the operating system or by a user application Examples: Win32 threads, C-threads, Pthreads
10
11
MS-DOS supports a single user process and a single thread. Some UNIX, support multiple user processes but only support one thread per process
12
Java run-time environment is a single process with multiple threads Multiple processes and threads are found in Windows, Solaris, and many modern versions of UNIX
13
14
15
An execution state (running, ready, etc.) & An execution stack Saved thread context when not running Some per-thread static storage for local variables Access to the memory and resources of its process (all threads of a process share this)
They reside in the same address space and have access to the same data. When one thread alters an item of data in memory, other threads see the results if and when they access that item.
If one thread opens a file with read privileges, other threads in the same process can also read from that file.
SPRING 2011 - 102
16
Address Space
T1
T2
State
17
18
A thread can be in states similar to a process (new, ready, running, blocked, terminated) A thread can create another thread
Multiple threads can operate within the same address space No automatic protection mechanism is in place for threadsthey are meant to help each other
SPRING 2011 - 102
19
Responsiveness. Multi-threaded servers (e.g., browsers) can allow interaction with user while a thread is formulating response to a previous user query (e.g., rendering a web page) Resource sharing. Process resources (code, data, etc.) are shared by all threads. OS resources (PCB, etc.) are also shared by all threads. Economy. Take less time to create, schedule, and terminate a thread. Solaris 2: Thread Creation is thirty times faster than Process Creation and Thread Switching is five times faster than process switching. Performance. In multi-processor and multi-threaded architectures (each thread may run on a different processor in parallel.
SPRING 2011 - 102
20
Processors were originally developed with only one core. The core is the part of the processor that actually performs the reading and executing of instructions. A multi-core processor is a single component with two or more independent actual processors (called "cores"). Multithread programming make more efficient use of multiple cores. It allows separate concurrent tasks to run parallel on individual cores.
SPRING 2011 - 102
21
22
23
By default an application begins with a single thread within a process managed by Kernel. Later the application may spawn a new thread within the same process by invoking spawn utility in the thread library. Thread switching is fast and CPU is not interrupted during thread switching (No mode switch). Kernel is scheduling the processes while user level library will be scheduling the thread When ever ULT makes a system call, Kernel takes it as the system call has been made by the process, so all the threads of the process are blocked.
SPRING 2011 - 102
24
When ever a KLT makes a system call, only that thread is blocked and the Kernel can schedule another thread of same process. So all the threads of the process are NOT blocked
SPRING 2011 - 102
25
Many user threads per kernel thread; i.e. Kernel sees just one thread. Advantages Thread management is done in user space, so it is efficient. Disadvantages When a thread within a process makes a system call the entire process blocks. Because only one thread can access kernel at a time, no parallelism on multiprocessors is possible.
Operating system maps all threads in a multithreaded process to single execution context
SPRING 2011 - 102
26
Advantage Process does not block when a thread makes a system call True concurrency achieved :Multiple threads can run in parallel on multiprocessors Disadvantages: Overhead for creating a kernel thread per user thread Overhead due to context switching and reduced portability due to OS-specific APIs
One
per
27
Advantage Application can create as many user threads as needed. Kernel threads run in parallel on multiprocessors When a thread blocks, another thread can still run
Multiple user threads are multiplexed over a smaller or equal number of kernel threads
SPRING 2011 - 102
28
29
Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations
30
31
32
Signals are used in UNIX systems to notify a process that a particular event has occurred
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
SPRING 2011 - 102
33
34
Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool
35
Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool)