Subscribe by Email

Thursday, May 9, 2013

What is a thread? What is meant by multi-threading?

About Thread

- A thread is a smaller instance of a process i.e., a sequence of instructions and an operating system scheduler can manage it independently. 
- A thread is sometimes also called as the light weight process. 
- The way these threads and processes are implemented is different for different operating systems. 
- But in majority of the cases threads are contained within a process. 
- The same process can have more than one thread. 
- These threads have to share the resources including memory while different processes may not share these. 
- In simple words, we can say that the instructions or code and the context or the values of the process are shared by its constituting threads. 

In this article we focus on threads and multi-threading.

- Multi-threading is a task of multiprocessor systems.
- But even single processor systems can do it by time division multiplexing just like the multitasking. 
- In TDM context switch occurs between the many threads. 
- This happens many times and so it seems to the user that a number of processors are being executed concurrently.
- However, in multiprocessor systems concurrency can be truly achieved since every processor processes one thread and so many threads are executed simultaneously. 
- Both multiprocessor and time threading is supported by most of the modern operating systems with help from the process scheduler. 
- The threads can only be manipulated through a system and this all is facilitated by the kernel of the operating system. 
- This is why some implementations have been named as the kernel thread. 
- An example of kernel thread is the LWP or the lightweight process and it shares same state as well as info. 
- Some programs even use user space threads when threading with the help of signals and timers etc. 
- These programs perform a kind of ad hoc time slicing. 
- Some may take threads and processes to be the same but there is a considerable difference between the two:
  1. Processes are independent whereas the threads are a subset of the processes.
  2. More state information is contained in threads whereas the same process state, resources and memory are shared by all the threads contained in that process.
  3. Address spaces of different processes are different whereas the same address space is also shared by the threads.
  4. IPC or the inter–process communication is the only medium for the processes to communicate with each other.
  5. The threads within the same process are switched with context switch which is faster than that of the processes.

Features of Multi-threading

- Multi-threading is now among the widespread programming models. 
- The major characteristic feature of this model is that multiple threads can execute within the same process context. 
- Even though the resources of the process are shared by the threads, they execute independently. 
- The most widespread application of this model is in parallel computing.
- Full advantage of this technology can be taken only when it is applied to a multiprocessor system or a distributed system. 
- This is so because the program threads have a natural tendency to heed to the true concurrent execution. 
- But in these cases, necessary precautions must be taken for avoiding the race conditions and other undesirable behavior. 
- For the correct manipulation of data thread synchronization is also important. - Mutually exclusive operations are another requirement of the threads for preventing the simultaneous modification of the common data. 
- If these primitives are used carelessly, then it can lead the system to a deadlock. 
- Another feature of multi-threading is that it is always responsive to I/P. 
- This can be contrasted with the single threaded applications where if one block occurs, the whole program freezes.

No comments:

Facebook activity