written 2.5 years ago by |
THREADS
➤ A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. Allow a single application to do many things at once n Simpler programming model n Less waiting: Simpler programming model, Less waiting. Threads are faster to create or destroy, No separate address space
➤ Threads are a popular way to improve application performance through parallelism. In traditional operating systems the basic unit of CPU utilization is a process. Each process has its own program counter, its own register states, its own stack, and its own address space
➤ On the other hand, in operating systems with threads facility, the basic unit of CPU utilization is a thread. In these operating systems, a process consists of an address space and one or more threads of control.
➤ Each thread of a process has its own program counter, its own register states, and its own stack. But all the threads of a process share the same address space. Hence they also share the same global variables.
➤ In addition, all threads of a process also share the same set of operating system resources, such as open files, child processes, semaphores, signals, accounting information, and so on. Due to the sharing of address space, there is no protection between the threads of a process.
➤ However, this is not a problem. Protection between processes is needed because different processes may belong to different users. But a process (and hence all its threads) is always owned by a single user.
➤Threads provide better responsiveness, resource sharing, economy and better utilization of multiprocessor architectures.
Motivation for Using Threads:
- The main motivations for using a multithread process instead of multiple single-threaded processes for performing some computation activities are as follows:
- The overheads involved in creating a ne wprocess are in general considerably greater than those of creating a new thread within a process.
- Switching between threads sharing the same address space is considerably cheaper than switching between processes that have their own address spaces.
Threads allows parallelism to be combined with sequential execution and blocking system calls [Tanenbaum 1995 ].Parallelism improves performance and blocking system calls make programming easier.
Resource sharing can be achieved more efficiently and naturally between threads of a process than between processes because all threads of a process share the same address space.
Models for Organizing Threads
Depending on an application's needs, the threads of a process of the application can be organized in different ways. Three commonly used ways to organize the threads of a process are as follows:
Dispatcher-Worker Model: In this model, the process consists of single dispatcher threads and multiples worker threads. The dispatcher the request to one of the free worker threads for further processing of the request. Each worker threads work on a different client request. Therefore multiple client requests can be processed in parallel
Team Model: In this models, all threads behave as equals in the sense that there is no dispatcher-worker relationship for processing clients requests. Each threads gets and processes clients request on its own. This model is often used for implementing specialized threads within a process. That is, each thread of the process is specialized in servicing a specific type of request. Therefore, multiples types of requests can be simultaneously handled by the process