Lesson 4 Thread Management 1 Threads Process The

  • Slides: 21
Download presentation
Lesson 4: Thread Management 1

Lesson 4: Thread Management 1

Threads • Process – The unit of resource ownership is referred to as a

Threads • Process – The unit of resource ownership is referred to as a process or task. – An entity corresponding to a user job or application that owns resources • Multithreading – The ability of an OS to support multiple, concurrent paths of execution within a single process.

Threads • Thread: – The unit of dispatching is referred to as a thread

Threads • Thread: – The unit of dispatching is referred to as a thread or lightweight process (Lightweight: Need less resources, i-e memory) – Process creation is heavy-weight while thread creation is lightweight – A dispatchable unit of work that executes sequentially and is interruptible – Thread share resources of process with other threads – It comprises a thread ID, a program, counter, a register set, and a stack. – Multiple tasks with the application can be implemented by separate threads.

Threads • Example of common applications: – A web browser might have one thread

Threads • Example of common applications: – A web browser might have one thread display images or text while another thread retrieves data from the network. – A word processor may have a thread for displaying graphics, another thread performing spelling and grammar checking in the background. • Example of web server – If the server will create a separate process that listens for client requests, it is resource costly, server creates a new thread to service the request.

Threads

Threads

Threads • Each Thread has: – An execution state (Running, Ready, etc. ) –

Threads • Each Thread has: – An execution state (Running, Ready, etc. ) – An execution stack – Some per-thread storage for local variables – Access to the memory and resources of its process • All threads of a process share this.

Threads

Threads

Single Vs. Multithreaded Models

Single Vs. Multithreaded Models

Uni-processor Multithreading • The key states for a thread are: § Spawn: § When

Uni-processor Multithreading • The key states for a thread are: § Spawn: § When a new process is spawned, a thread for that process is also spawned. § A thread within a process may spawn another thread within the same process q The new thread is provided with its own register context and stack space and placed on the ready queue.

Uni-processor Multithreading • The key states for a thread are: § Block: § When

Uni-processor Multithreading • The key states for a thread are: § Block: § When a thread needs to wait for an event, it will block (saving its user registers, program counter, and stack pointers). § Unblock: § When the event for which a thread is blocked occurs, the thread is moved to the Ready queue. § Finish: § When a thread completes, its register context and stacks are deallocated.

Uni-processor Multithreading

Uni-processor Multithreading

Uni-processor Multithreading

Uni-processor Multithreading

Advantages of Threads • Responsiveness: – Program continue running even if part of it

Advantages of Threads • Responsiveness: – Program continue running even if part of it is blocked, increasing response • Resource Sharing – Threads share the memory and the resources of the process to which they belong by default.

Advantages of Threads • Economy: – Easy creation and context switch – Resource sharing

Advantages of Threads • Economy: – Easy creation and context switch – Resource sharing speedup process by about 30 times. • But also increase overhead of switching decrease speed of system by about 5 times. • Scalability – Ability to enhance the system – The benefits of multithreading can be even greater in a multiprocessor architecture, where threads may be running in parallel on different processing cores.

Multicore/Multiprocessor Programming • Multicore systems place multiple computing cores on a single chip. •

Multicore/Multiprocessor Programming • Multicore systems place multiple computing cores on a single chip. • Each core appears as a separate processor.

Multithreading • Achieves concurrency without the overhead of using multiple processes – Threads within

Multithreading • Achieves concurrency without the overhead of using multiple processes – Threads within the same process can exchange information through their common address space and have access to the shared resources of the process – Threads in different processes can exchange information through shared memory that has been set up between the two processes

Thread Synchronization • All threads of a process share the same address space and

Thread Synchronization • All threads of a process share the same address space and other resources, any alteration of a resource by one thread affects the other threads in the same process. • Threads within the same process can exchange information through their common address space and have access to the shared resources of the process. • Threads in different processes can exchange information through shared memory that has been set up between the two processes.

User-Level Threads (ULTs) • All thread management is done by the application • The

User-Level Threads (ULTs) • All thread management is done by the application • The kernel is not aware of the existence of threads

Kernel Threads § Thread management is done by the kernel § No thread management

Kernel Threads § Thread management is done by the kernel § No thread management is done by the application § The kernel can simultaneously schedule multiple threads from the same process on multiple processors § If one thread in a process is blocked, the kernel can schedule another thread of the same process

Relationship between ULT & KLT Many-to-One Many to Many One to One

Relationship between ULT & KLT Many-to-One Many to Many One to One

Relationship B/W Threads & Processes

Relationship B/W Threads & Processes