You are on page 1of 35

1

SPRING 2011 - 102

Why Threads : Issues with Processes What are threads? Threading models Threading issues

SPRING 2011 - 102

The fork() system call is expensive

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.

SPRING 2011 - 102

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.

SPRING 2011 - 102

Thread

f1

f2

A traditional process can be viewed as:

An address space with a single thread!

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

Process Address Space


main() { thread(t1,f1); thread(t2,f2); }
f1() { }

main

t1 t2
PC

PC

PC

f2() { }
SPRING 2011 - 102

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

SPRING 2011 - 102

10

SPRING 2011 - 102

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

SPRING 2011 - 102

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

SPRING 2011 - 102

13

SPRING 2011 - 102

14

SPRING 2011 - 102

15

Each thread has


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

Resources State Static data Program Stack Stack

SPRING 2011 - 102

17

SPRING 2011 - 102

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

SPRING 2011 - 102

22

Three most popular threading models


User-level threads Kernel-level threads

Combination of user- and kernel-level threads

SPRING 2011 - 102

23

User-level threads perform threading operations in user space


Threads are created by runtime libraries.

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

Thread management done by kernel and Kernel is aware of threads

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

user thread kernel thread;

per

User level Threads

Kernel level Threads

SPRING 2011 - 102

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

User level Threads

Kernel level Threads

Multiple user threads are multiplexed over a smaller or equal number of kernel threads
SPRING 2011 - 102

28

SPRING 2011 - 102

29

Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations

SPRING 2011 - 102

30

Does fork() duplicate only the calling thread or all threads?

SPRING 2011 - 102

31

Terminating a thread before it has finished

Two general approaches:


Asynchronous cancellation terminates the target thread immediately. Prematurely terminating a thread can cause subtle errors in processes because multiple threads share the same address space Deferred cancellation allows the target thread to periodically check if it should be cancelled Some thread implementations allow a thread to determine when it can be terminated to prevent process from entering inconsistent state

SPRING 2011 - 102

32

Signals are used in UNIX systems to notify a process that a particular event has occurred

A signal handler is used to process signals


1.
2. 3.

Signal is generated by particular event


Signal is delivered to a process Signal is handled

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

SPRING 2011 - 102

34

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 Allows the number of threads in the application(s) to be bound to the size of the pool

SPRING 2011 - 102

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)

SPRING 2011 - 102

You might also like