Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.

Similar presentations


Presentation on theme: "Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control."— Presentation transcript:

1 Thread

2 A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control within a program It shares with other threads belonging to the same process its code section, data section, and other OS resources, such as open files and signals A traditional (or heavyweight) process has a single thread of control If a process has multiple threads of control, it can perform more than one task at a time. Threads are a way for a program to split itself into two or more simultaneously running tasks. That is the real excitement surrounding threads

3 Single and Multithreaded Processes

4 Thread Examples A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling and grammar checking in the background

5 Thread Example: Multithreaded Server Architecture

6 Single-Threaded Example Imagine the following C program main() { ComputePI( “ pi.txt ” ); PrintClassList( “ clist.text ” ); } What is the behavior here?

7 Use of Threads Version of program with Threads main() { CreateThread(ComputePI( “ pi.txt ” )); CreateThread(PrintClassList( “ clist.text ” )); } What does “ CreateThread ” do? – Start independent thread running given procedure What is the behavior here? – This should behave as if there are two separate CPUs CPU1CPU2CPU1CPU2 Time CPU1CPU2

8 Memory Footprint of Two-Thread Example If we stopped this program and examined it with a debugger, we would see – Two sets of CPU registers – Two sets of Stacks Code Global Data Heap Stack 1 Stack 2 Address Space

9 Per Thread State Each Thread has a Thread Control Block (TCB) – Execution State: CPU registers, program counter, pointer to stack – Scheduling info: State (more later), priority, CPU time – Accounting Info – Various Pointers (for implementing scheduling queues) – Pointer to enclosing process (PCB) – Etc

10 Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures

11 Multithreading Models Support for threads may be provided either at the user level, for user threads (supported above the kernel and managed without kernel support), or by the kernel, for kernel threads (supported and managed directly by the OS) Three common ways of establishing relationship between user and kernel threads – Many-to-One – One-to-One – Many-to-Many

12 User Threads Thread management done by user-level threads library Three primary thread libraries: – POSIX Pthreads – Win32 threads – Java threads

13 Kernel Threads Supported by the Kernel Examples –Windows XP/2000 –Solaris –Linux –Tru64 UNIX –Mac OS X

14 Many-to-One (User-Level Threads) Many user-level threads mapped to single kernel thread

15 Many-to-One (User-Level Threads) Basically, the kernel is not aware of the existence of threads. Thread switching does not require kernel mode privileges and scheduling is application specific. Thread management is done by the thread library in user space, so it is efficient Just as a uniprocessor provides the illusion of parallelism by multiplexing multiple processes on a single CPU, user-level threads packages provide the illusion of parallelism by multiplexing multiple user threads on a single kernel thread

16 Many-to-One (User-Level Threads) Since there is only one kernel thread, if a user thread executes a blocking system call, the entire process blocks, since no other user thread can execute until the kernel thread (which is blocked in the system call) becomes available Multithreaded programs will run no faster on multiprocessors than they run on uniprocessors. The single kernel thread acts as a bottleneck, preventing optimal use of the multiprocessor

17 Many-to-One (User-Level Threads) Advantages – Thread switching does not involve kernel  no mode switching – Scheduling can be application specific  choose best algorithm – ULTs can run on any OS  only needs a thread library Disadvantages – Most system calls are blocking and the kernel blocks processes  all threads within the process will be blocked – Kernel can only assign processes to processors  threads within same process cannot run simultaneously on processors

18 One-to-One (Kernel-Level Threads) Each user-level thread maps to kernel thread

19 One-to-One (Kernel-Level Threads) Because each kernel thread is actually a different kernel-schedulable entity, multiple threads can run concurrently on different processors – Can achieve significant speedups when migrated from uniprocessors to multiprocessors Unlike the many-to-one model, threads blocking in the kernel do not impede process progress under the one-to-one model. When one user thread and its kernel thread block, the other user threads can continue to execute since their kernel threads are unaffected The only drawback is that creating a user thread requires creating the corresponding kernel thread – Overhead of creating kernel threads can burden the performance of the application

20 Many-to-Many Model Allows many user-level threads to be mapped to many kernel threads Idea is to combine the best of both approaches

21 Solaris combines both ULT and KLT

22 Thread Libraries A thread library provides the programmer with an API for creating and managing threads Two primary ways of implementing a thread library – Provide the library entirely in user space with no kernel support. All code and data structures for the library exist in user space. Invoking a function in the library results in a local function in user space and not a system call – Implement kernel-level library supported directly by the OS. In this case, code and data structures for the library exist in kernel space. Invoking a function results in a system call to the kernel – Three main thread libraries in use today: (1) POSIX Pthreads, (2) Win32, and (3) Java

23 Pthreads Example: Design a multithreaded program that performs the following summation in a separate thread N sum =  i i = 0

24 Pthreads #include int sum; //this data is shared by the thread(s) void *runner(void *param); // the thread main(int argc, char* argv[]) { pthread_t tid; // the thread identifier pthread_create(&tid,NULL,runner,argv[1]); // create thread pthread_join(tid,NULL); // now wait for the thread to exit printf("sum = %d\n",sum); }

25 Pthreads void *runner(void *param) { int upper = atoi(param); int i; sum = 0; if (upper > 0) { for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); }

26 Threading Issues Semantics of fork() and exec() system calls Thread cancellation of target thread –Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations

27 Thread Cancellation Terminating a thread before it has finished Two general approaches: –Asynchronous cancellation terminates the target thread immediately –Deferred cancellation allows the target thread to periodically check if it should be cancelled

28 Semantics of fork() and exec() If one thread in a program calls fork(), does the new process duplicate only the calling thread or all threads? – Some UNIX systems provide two versions of fork() – One duplicates all threads – The other duplicates only the thread that invoked fork() The exec() system call typically works in the same way as described before. That is, if a thread invokes exec(), the program specified in the parameter to exec() will replace the entire process – including all threads

29 Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals  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

30 Thread Pools 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

31 Thread Specific Data 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)

32 Scheduler Activations Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library This communication allows an application to maintain the correct number kernel threads

33 Operating System Examples Windows XP Threads Linux Thread

34 Windows XP Threads

35 Linux Threads

36 Windows XP Threads Implements the one-to-one mapping, kernel-level Each thread contains –A thread id –Register set –Separate user and kernel stacks –Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: –ETHREAD (executive thread block) –KTHREAD (kernel thread block) –TEB (thread environment block)

37 Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process)


Download ppt "Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control."

Similar presentations


Ads by Google