Subscribe by Email


Saturday, April 20, 2013

Explain the concepts of threads and processes in operating system?


Threads and processes are an important part of the operating systems that have features of multi–tasking and parallel programming. These come under the sole concept of ‘scheduling’. Let us try to understand these concepts with the help of an analogy.

- Consider the process to be a house and threads are its occupants. 
- Then, process is like a container having many attributes. 
- These attributes can be compared to that of a house such as number of rooms, floor space and so on. 
- Despite having so many attributes, this house is a passive thing which means it can’t perform anything on its own. 
- The active elements in this situation are the occupants of the home i.e., the threads. 
- The various attributes of the house are actually used by them. 
- Since you too live in a house you must have got an idea how it actually works and behaves. 
- You do whatever you like in the house if only you are there. 
- What if another person starts living with you? You just can’t do anything you want to do. 
- You cannot use the washroom without making sure that the other person is not there. 
- This can be related to multi – threading. 
- Just as a part of estate is occupied by the house, an amount of memory is occupied by the process. 
- Just as the occupants are allowed to freely access anything in the house, similarly the occupied memory is utilized by the threads that are a part of that process i.e., the access to memory is common. 
- If one process allocates some memory, it can be accessed by all other threads also. 
- If such a thing is happening, it has to be made sure that from all the threads, the access to the memory is synchronized. 
- If it cannot be synchronized, then it becomes clear that the memory has been allocated specifically to a thread. 
- But in actual, things are a lot more complicated because at some point of time everything has to be shared. 
- If one thread wants to use some resource that is already under use by some other thread, than that thread has to follow the concept of mutual exclusion. 
An object known as the mutex is used by the thread for achieving exclusive access to that resource. 
- Mutex can be compared to a door lock. 
- Once a thread locks this, no other thread can use that resource until the mutex is again unlocked by that thread. 
- Mutex is one resource that a thread uses. 
- Now, suppose there are many threads waiting to use the resource when mutex is unlocked, the question that arises now is that who will be next one to use the resource. 
- This problem can be solved by either deciding on the basis of length of wait or on basis of priority. 
- Suppose there is a location that can be accessed by more than one threads simultaneously.
- You want to have only a limited number of threads using that memory location at any given point of time. 
- This problem cannot be solved by mutex but with another resource called semaphore. 
- Semaphore with a count of 1 is the resource that can only be used by one thread at a time. 
- In semaphore of greater count more threads can access it simultaneously.  
- It just depends up on how you characterize or set the lock.


No comments:

Facebook activity