|
What are threads?
The concept of process embodies two separate and potentially independent concepts: one relating to resource ownership and one relating to the execution. This distinction has led to the development in some operating systems, of a construct known as thread.
In most operating systems resource ownership and scheduling/execution are indeed the essence of a process. But these characteristics are independent and could be treated independently by the operating system. To distinguish the two characteristics, the unit of dispatching is usually referred to as a ‘thread’, or ‘lightweight process’, while the unit of resource ownership is usually still referred to as a process or task.
Multithreading
Multithreading refers to the ability of an operating system to support multiple threads of execution within a single process. A Java run-time environment is an example of a system of one process with multiple threads.
In a multithreaded environment, a process is defined as the unit of resource allocation and a unit of protection. The following are associated with processes:
- A virtual address space that holds the process image.
- Protected access to processors, other processes (for Inter-process Communication), files, and i/o resources (devices and channels).
Within a process there may be one or more threads, each with the following:
- A thread execution state (Running, Ready etc.)
- A saved thread context when not running.
- An execution stack.
- Some per thread static storage for local variables.
- Access to the memory and resources of its process, shared with all other threads in that process.
While the process is running, processor registers are controlled by that process, and the contents of these registers are saved when the process is not running. In a multithreaded environment, there is still a single process control block and user address space associated with the process but now there are separate stacks for each thread containing register values, priority and other thread related state information.
Thread Advantages
- It takes far less time to create a new thread in an existing process than to create brand-new process.
- It takes less time to terminate a thread than a process.
- It takes less time to switch between two threads within the same process.
- Threads enhance efficiency in communication between different executing programs. In most operating systems, communication between independent processes requires the intervention of the kernel to provide protection and the mechanisms needed for communication. However, because threads within the same process share memory and files, they can communicate with each other without invoking the kernel.
Thread Functionality
Like processes, threads have execution states and may synchronize with one another.
Thread states
The key states for a thread are Running, Ready and Blocked. In particular if a process is swapped out, all of its threads are necessarily swapped out because they all share the address space of the process.
There are four basic thread operations associated with a change in thread state.
- Spawn
Typically, when a new process is spawned, a thread for that process is also spawned. Subsequently a thread within a process may spawn another thread within the same process, providing an instruction pointer and arguments for the new thread. The new thread is provided with its own register context and stack space and placed on the ready queue.
- Block
When a thread needs to wait for an event, it will block (saving its user registers, program counter, and stack pointers).
- Unblock
When the event for which a thread is blocked occurs, the thread is moved to the ready queue.
- Finish
When a thread completes, its register context and stacks are de-allocated.
Thread Synchronization:
All of the threads of a process share the same address space and other resources, such as open files. Any alteration of a resource by one thread affects the environment of the other threads in the same process. It is therefore necessary to synchronize the activities of the various threads so that they do not interfere with each other or corrupt data structures. For Eg, if two threads each try to add an element to a doubly linked list, one element may be lost or the list may end up malformed. Generally, the techniques used in the synchronization of threads are, in general, the same as for the synchronization of processes.
|