written 6.9 years ago by | modified 2.9 years ago by |
Subject: Operating System
Topic: PROCESS MANAGEMENT
Difficulty: Hard
written 6.9 years ago by | modified 2.9 years ago by |
Subject: Operating System
Topic: PROCESS MANAGEMENT
Difficulty: Hard
written 6.7 years ago by |
Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the programming running in the computer.
Each user request for a program or system service (and here a user can also be another program) is kept track of as a thread with a separate identity. As programs work on behalf of the initial request for that thread and are interrupted by other requests, the status of work on behalf of that thread is kept track of until the work is completed.
As each thread has its own independent resource for process execution, multiple processes can be executed parallel-y by increasing number of threads.
Thread is an execution unit which consists of its own program counter, a stack, and a set of registers.
Threads are implemented in following two ways −
User Level Threads − User managed threads. To know more
Kernel Level Threads − Operating System managed threads acting on kernel, an operating system core.
Some operating system provide a combined user level thread and Kernel level thread facility. Solaris is a good example of this combined approach. In a combined system, multiple threads within the same application can run in parallel on multiple processors and a blocking system call need not block the entire process. Multi-threading models are three types
Many to many relationship-
With such model, many user-level threads can be multiplexed by a smaller or equal number of kernel threads. Also, with such model implementation in the system, developers can run as many user threads as necessary with a corresponding kernel threads that can run in parallel on a multiprocessor. However, the overhead required in creating kernel threading is great compared to the one-to-one relationship.
Many to one relationship-
Where many user-level threads are mapped to a single kernel thread. In such model the thread is managed by thread library in the user space which makes such model efficient. However; the entire threads can be blocked if a thread makes a blocking system call. Also; with multiprocessors system; threads can access the kernel with only a single thread at a time and as such; multiple threads are unable to take advantage on multiprocessor and run in parallel on multiprocessors. With such model, developer will be able to create as many threads as required; however the true concurrency is not implemented since the kernel can schedule only one thread at a time.
One to one relationship-
Where each user thread is mapped to the kernel thread. Such mechanism provides concurrency operation than the many-to-many model where other threads are allowed to run even when one of these threads makes a blocking system call. Also, such model allows multiple threads to run in parallel on multiprocessors. Since such model creates a corresponding kernel thread with every creating of user thread, an overhead of creating kernel threads can affect the performance of an application and as such; this implementation can be restricted with the number of threads that can be supported by the system. Also, with such model, developer can have a greater concurrency applications; however a limited number of threads within an application can be implemented since a limited threads can be created by the system.
Multithreading is a type of execution model that allows multiple threads to exist within the context of a process such that they execute independently but share their process resources. A thread maintains a list of information relevant to its execution including the priority schedule, exception handlers, a set of CPU registers, and stack state in the address space of its hosting process.
Threading can be useful in a single-processor system by allowing the main execution thread to be responsive to user input, while the additional worker thread can execute long-running tasks that do not need user intervention in the background. Threading in a multiprocessor system results in true concurrent execution of threads across multiple processors and is therefore faster. However, it requires more careful programming to avoid non-intuitive behavior such as racing conditions, deadlocks, etc.
Operating systems use threading in two ways:
Pre-emptive multithreading, in which the context switch is controlled by the operating system. Context switching might be performed at an inappropriate time, Hence, a high priority thread could be indirectly pre-empted by a low priority thread.
Cooperative multithreading, in which context switching is controlled by the thread. This could lead to problems, such as deadlocks, if a thread is blocked waiting for a resource to become free.