Threads Overview Benefits, User and Kernel Threads. Multithreading models One to One, Many To One, Many To Many Threading issues fork() and exec(). Thread Cancellation. Signal Handling. Thread Pools. Pthreads Win32API threads Linux Threads Textbook Silberschatz, Chapter 5
Threads Overview A traditional (or heavyweight) process has a single thread of control. lightweight process (LWP) is a basic unit of CPU utilization It comprises a thread ID a program counter a register set a stack. It shares with other threads belonging to the same process its code section data section other operating-system resources such as open files and signals.
Advantages of Threads over Multiple Processes Responsiveness Resource sharing Economy Utilization of multiprocessor architectures Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation thereby increasing responsiveness to the user. Example: A web browser might have one thread to display images or text while another thread retrieves data from the network. The 3rd thread handles the key input from the keyboard. Example: a user clicks a button that results in the performance of a time-consuming operation A single-threaded application would be unresponsive to the user until the operation had completed. In contrast, if the time-consuming operation is performed in a separate thread, the application remains responsive to the user.
Resource Sharing Processes can only share resources through techniques such as shared memory and message passing. Such techniques must be explicitly arranged by the programmer. However, threads share the memory and the resources of the process to which they belong by default. The benefit of sharing code and data is that it allows an application to have several different threads of activity within the same address space.
Resource sharing example main() { pthread_t thread1, thread2; Record_structure Record; (The “Record” is shared) pthread_create(&thread1, NULL, Write_to_Database, DB1, Record); pthread_create(&thread2, NULL, Write_to_Database, DB2, Record); . . . Both threads work parallely pthread_join( thread1, NULL); pthread_join( thread2, NULL); exit(0); } int Write_to_Database(DB, Record){ Open_Database(DB); Write_Record(... Loooong ...);
Economy Allocating memory and resources for process creation is costly. Threads are very inexpensive to create and destroy, and they are inexpensive to represent. they require space to store PC SP General-purpose registers they do not require space to share memory information Information about open files of I/O devices in use Code Data With so little context, it is much faster to switch between threads.
Economy Example In certain situations, a single application may be required to perform several similar tasks. a web server accepts client requests for web pages, images, sound, and so forth. A busy web server may have several (perhaps thousands of) clients concurrently accessing it. If the web server ran as a traditional single-threaded process it would be able to service only one client at a time and a client might have to wait a very long time for its request to be serviced.
Economy Example, Continue One solution is to have the server run as a single process that accepts requests. When the server receives a request, it creates a separate process to service that request. In fact, this process-creation method was in common use before threads became popular. Process creation is time consuming and resource intensive, however. It is generally more efficient to use one process that contains multiple threads. the server will create a separate thread that listens for client requests. When a request is made, rather than creating another process, the server creates a new thread to service the request and resume listening for additional requests.
Utilization of multiprocessor architectures The benefits of multithreading can be greatly increased in a multiprocessor architecture, where each thread may be running in parallel on a different processor.
Disadvantages of Threads over Multiprocesses Blocking -- The major disadvantage is that if the kernel is single threaded, a system call of one thread will block the whole process and CPU may be idle during the blocking period. Security -- Since there is, an extensive sharing among threads there is a potential problem of security. It is quite possible that one thread over writes the stack of another thread or damaged shared data Why the whole process will be blocked?
User level and Kernel level Threads Support for threads may be provided at either the user level, for user threads or by the kernel, for kernel threads. User Level Threads Thread management done by user-level threads library. Kernel Level Threads Kernel supports and manages the threads. Win32 Kernel threads (95,98,NT,2000,XP) Solaris Linux Tru64 UNIX Mac OS X Three primary user level thread libraries: POSIX Pthreads -Unix Win32 Pthreads Java threads
User level threads User threads are supported above the kernel and are implemented by a thread library at the user level. The library provides support for Thread creation Scheduling Management The kernel is unaware of user-level threads all thread creation and scheduling is done in user space without the need for kernel intervention. A consequence of this is that no kernel resources need to be allocated per thread, and switching between threads can be done without changing address space. Therefore, user-level threads are generally fast to create and manage
User level threads advantages User-level threads do not require modification to operating systems. Simple Representation: Each thread is represented simply by a PC registers Stack and a small control block all stored in the user process address space. Simple Management: This simply means that creating a thread switching between threads synchronization between threads can all be done without intervention of the kernel. Fast and Efficient: Thread switching is not much more expensive than a procedure call.
User level threads disadvantages There is a lack of coordination between threads and operating system kernel. Therefore, process as whole gets one time slice irrespect of whether process has one thread or 1000 threads within. It is up to each thread to relinquish control to other threads. User-level threads require non-blocking systems call a multithreaded kernel. Otherwise, if the kernel is single-threaded if a user-level thread performs a blocking system call then the entire process will blocked in the kernel, even if there are runnable threads left in the processes. if one thread causes a page fault, the process blocks.
Kernel level threads In this method, the kernel knows about and manages the threads. No runtime system is needed in this case. Instead of thread table in each process, the kernel has a thread table that keeps track of all threads in the system. In addition, the kernel also maintains the traditional process table to keep track of processes. Operating Systems kernel provides system call to create and manage threads. The Kernel LWPs can be viewed as “virtual CPUs” to which the scheduler of a threads library schedules user-level threads.
Kernel level threads advantages Because kernel has full knowledge of all threads, Scheduler may decide to give more time to a process having large number of threads than process having small number of threads. Kernel-level threads are especially good for applications that frequently block. Since the kernel is managing the threads, if a thread performs a blocking system call, the kernel can schedule another thread in the application for execution. Also, in a multiprocessor environment, the kernel can schedule threads on different processors.
Kernel level threads disadvantages Kernel threads are supported directly by the operating system: The kernel performs thread creation, scheduling, and management in kernel space. Because thread management is done by the operating system, kernel threads are generally slower to create and manage than are user threads. Since kernel must manage and schedule threads as well as processes. It requires a full thread control block (TCB – similar to PCB for processes) for each thread to maintain information about threads. As a result there is significant overhead and increased complexity in kernel.
User and Kernel level threads conclusion
Multithreading Models. Many-To-One. One-to-One Many-to-Many The many-to-one model maps many user-level threads to one kernel thread. Examples: Solaris Green Threads GNU Portable Threads Thread management is done in user space, so it is efficient but the entire process will block if a thread makes a blocking system call. Also, because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors. Many-to-One As the thread management is done in user space: Advantages: Efficient thread management. Disadvantages: Bad resource management.
Multithreading Models. One to One. maps each user thread to a kernel thread. provides more concurrency than the many-to-one model by allowing another thread to run when a thread makes a blocking system call it also allows multiple threads to run in parallel on multiprocessors. Examples (many modern systems) Windows NT /2000/XP… Linux Solaris 9 and later Disadvantages: Each user thread creating a user thread requires creating the corresponding kernel thread: which makes the system slow at thread creation time. The system resources may exhaust when many threads are created. Advantages: Good resource management. High concurrency. One-to-One The programmer should be careful to not exceed the allowed number of threads for the particular system.
Multithreading Models. Many-to-Many The many-to-many model multiplexes many user-level threads to a smaller or equal number of kernel threads. Allows the operating system to create a sufficient number of kernel threads Solaris 2, IRIX, HP-UX, Tru64 UNIX, Windows NT/2000/XP with the ThreadFiber package W7 - Hybrid (N:M user-space / kernel space threads mapping Advantages : Developers can create as many user threads as necessary in the application. The corresponding kernel threads can run in parallel on a multiprocessor. Also, when a thread performs a blocking system call, the kernel can schedule another thread for execution. Many-to-Many Disadvantages : Very complex to implement If kernel threads amount is not enough then the performance will be low.
Multithreading Models. Two-level. If kernel threads’ amount is not enough then some threads could be mapped directly to the kernel threads to have high performance only for those threads.
Threaded server It was a good solution to use threads instead of processes. Isn’t it ?
Thread pool