Wednesday 21 September 2016

Understading Process, Tasks and Threads in Operating Systems

In my experience, I have seen many beginners baffling around the role of Process and Task. Whether they are same or different ? Is different how, and if same, why it is needed to give two different name for similar entity?
In this article, I have tried to give simple explanation about these basic concepts of Operating Systems which would hopefully make you understand the importance of each entity at basic level.

Process: 

  • Always used in context to General Purpose Operating Systems (GPOS) ex: Linux or Windows. 
  • It an instance of program or application currently in execution
  • Process are generally large in number in any GPOS.
  • Whenever process is created, all necessary resources required for smooth execution of process is allocated by OS.
  • Every process believes that it can access dedicated address space of 4GB (2^32=4GB), for 32-bit processor but due to unavailability of sufficient physical address space, virtual memory concept is introduced and Memory Management Unit (MMU) comes into picture for virtual memory to physical memory translation.
  • There is a fair chance that process may modify/affect the address space assigned to other  to process so MMU must be involved which segregate the process address space by allocating different virtual address spaces to every process.
  • Due to this Virtual memory to physical memory translation, no process can know the actual physical address range of other chances (due to different offset values), process address range overlap chances becomes minimal and each of the processes can work smoothly.  
  • Ex: google chrome is an application, and flash plugin or any plugin we use in chrome can be considered as an independent process; even if one process crashes, other process is not affected!

Task:

  • This word is used mostly while discussing on Real Time Operating Systems
  • Task is considered similar to process in terms of its behaviour but certainly at the level lower to process but no where equivalent to thread.
  • Analogy could be given to a man , who stays as father in his home, but in office works as employee and in his friend circle, a helpful friend and sincere neighbour. Now in this example, even though the man is same but his role changes as per situation and place. Similar thing happens with process/task. In GPOS, it plays the role of process (demanding) and in RTOS, it plays a role of task (relatively less demanding).
  • A task is also an instance of application which is currently in execution just like a while loop in an application. 
  • Task is similar to process in terms of its conceptualisation but it differs in the way it behaves in RTOS
  • In RTOS, due to less number of parallel applications runing, number of task are significantly less than any GPOS, which makes the task management relatively easier for programmer so it does not need any virtual memory requirement.
  • Since virtual memory is not needed, no virtual to physical translation in needed which removes any need of MMU.
  • Removing the requirement of MMU lowers the system cost and also reduces the extra burden of translation, thus improves the response time of system and helping it be Real Time.

This discussion would be left without talking on threads, so lets have a very brief overview on thread 
Thread:
  • This entity is again used in GPOS 
  • This is considered as light weight process since it does not demand for independent data memory and heap memory as every process does. Thread do have its own stack memory.
  • Instead, it shares these memories which helps them for better inter thread communication
  • Thread cannot exist independently
  • There can be multiple threads in any process.

Live example:
Consider MS Office as one big application, which contains Ms-Word, Ms-Powerpoint, MS-Excel etc. as independent programs. These programs may be considered as independent process, which is created as if needed by user. Now when we write on MS-word, say, there are many things happening in background ex: auto-save, indentation, spell check, punctuation, etc. each of this work is governed by individual threads.

Hope this article would have given you some basic clarification of Process, Tasks and Threads.

Happy learning :-)