Thread 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 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 Single and Multithreaded Processes 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 Thread Example: Multithreaded Server Architecture Single-Threaded Example • Imagine the following C program main() { ComputePI(“pi.txt”); PrintClassList(“clist.text”); } • What is the behavior here? 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 CPU1 CPU2 Time CPU1 CPU2 CPU1 CPU2 Memory Footprint of Two-Thread Example • If we stopped this program and examined it with a debugger, we would see Stack 1 – Two sets of CPU registers Heap Global Data Code Address Space – Two sets of Stacks Stack 2 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 Benefits • Responsiveness • Resource Sharing • Economy • Utilization of MP Architectures 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 User Threads • Thread management done by user-level threads library • Three primary thread libraries: – POSIX Pthreads – Win32 threads – Java threads Kernel Threads • Supported by the Kernel • Examples – Windows XP/2000 – Solaris – Linux – Tru64 UNIX – Mac OS X Many-to-One (User-Level Threads) • Many user-level threads mapped to single kernel thread 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 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 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 One-to-One (Kernel-Level Threads) • Each user-level thread maps to kernel thread 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 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 Solaris combines both ULT and KLT 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 Pthreads • Example: Design a multithreaded program that performs the following summation in a separate thread N sum = i i=0 Pthreads #include <pthread.h> #include <stdio.h> 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); } 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); } 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 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 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 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 1. Signal is generated by particular event 2. Signal is delivered to a process 3. 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 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 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) 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 Operating System Examples • Windows XP Threads • Linux Thread Windows XP Threads Linux Threads 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) 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)