Threads and Processes
An idea of how threads and processes can be related to each other is depicted in fig.
There are several similarities and differences between a thread and a process.
Threads are like mini processes. The various similarities between threads and processes are:
1. Like processes, each thread has its own program counter and stack to keep track of where it is.
2. Threads share CPU just as processes do.
3. Like processes, threads (within a process) also run sequentially.
4. Threads can also create child threads.
5. Threads also have almost the same states as processes - ready, running, blocked, terminated.
6. If one thread is blocked, the other thread in the same process can run, irn exactly the same way that when one process blocks, another process in the same machine can run.
The various differences between threads & processes are:
1. Each process has its own distinct address space in the main memory. On the other hand, all the threads in a process share the same address space.
2. Threads require less system resources than processes.
3. Unlike processes, threads are not independent of one another.
4. Processes may originate from different users, and may be hostile to one another whereas an individual task will multiple threads will always be owned by a single user.
5. New threads take less time for creation than new processes.
6. It takes less time to terminate a thread than to terminate a process.
7. It takes less time to switch between two threads within the same process than to switch between two processes.
8. Processes are more robust in comparison to threads.
9. Threads enhance efficiency in communication between different executing As threads within same process share memory and files, they can programs. communicate with each other without invoking the operating system's kernel.
Types of threads
Threads in a process are specified by the operating system or by the user. Based on this there are three types of threads : Kernel level thread, user level treads, and hybrid thread (see fig.)
1. Kernel level threads
· Threads of processes defined by operating system itself are called kernel threads .
· In these types of threads, kernel performs thread creation, scheduling and management in kernel space (see fig. next).
· Kernel threads are used for internal workings of the operating system, such as scheduling the user threads.
· Kernel threads are slower to create and manage as operating system manages them.
· If a thread performs a blocking system call, the kernel can schedule another threads in the application for execution.
· In multiprocessor environment, the kernel can schedule threads on different processors.
· The various operating system that support kernel level threads are windows NT, windows 2000, Salaris 2.
Advantages of kernel level threads
1. The operating system can schedule multiple threads for the same process on multiple processors.
2. The operating system is aware of the presence of threads in the process therefore, even if one thread of a process gets blocked, the operating system chooses the next one to run either from the same process or from the different process. Hence they are good for applications that frequently block.
Disadvantages of kernel level threads
1. Kernel level threads are slower and inefficient.
2. Switching between threads is time consuming as the kernel performs switching (via an interrupt).
3. Since kernel must manage and schedule threads as processes, it requires full threads control block (TCB) for each thread. As a result there is significant rhead and increase in kernel complexity
2 User level threads
· The threads of user application process are called user threads.
· They are implemented entirely in the user space of the main memory (see fig.).
· They are supported above the kernel and are implemented in user level libraries rather than via a system call.
· Here, user level library (containing functions to manipulate user threads) is used for thread creation, scheduling and management without any support from kernel. .
· As kernel's intervention is not required in thread creation and scheduling, user level threads are fast to create and manage.
Advantages of user level threads
1. User level threads are fast in creation as kernel intervention is not required.
2. There is fast switching among threads, as switching between user level threads can be done independent of the operating system.
3. They have better performance over kernel threads as they do not need to make system calls for threads creation.
4. Threads scheduling can be application specific.
Disadvantages of user level threads
1. When a user level thread executes a system call, not only that thread is blocked but also all of the threads within the process are blocked. This is because operating system is unaware of the presence of threads and only knows about the existence of a process actually constituting these threads.
2. Multithreaded applications using user level threads cannot take the advantage of multiprocessing as kernel assigns one process to only one processor as a time. This is again because of operating system is unaware of the presence of threads and it schedules processes not threads.
3 Hybrid Approach .
· In hybrid approach both kernel level threads and user level threads are implemented (see fig.) For example Solaris 2 (a version of UNIX).
No comments:
Post a Comment