A process is a program in execution,
whereas a thread is a path of execution within
a process. Processes are generally used to execute large, ‘heavyweight’ jobs
such as running different applications, while threads are used to carry out
much smaller or ‘lightweight’ jobs such as auto saving a document in a program,
downloading files, etc. Whenever we double-click an executable file such as
Paint, for instance, the CPU starts a process and the process starts its primary
thread.
Each process runs in a separate address space in the CPU. But threads, on the other hand, may share address space with other threads within the same process. This sharing of space facilitates communication between them. Therefore, Switching between threads is much simpler and faster than switching between processes
Threads also have a great degree of control over other threads of the same process. Processes only have control over child processes.
To understand the actual differences between processes and threads, let us consider a real-life example. Suppose you are driving from city A to city B and you have had only three hours of sleep the night before after a terribly hectic day. Imagine yourself as the CPU and driving to city B as a process. While driving you may have to do a number of smaller tasks such as checking the map on the GPS, making sure that you don’t exceed speed limits, making sure you don’t fall asleep (trust me, it happens!), etc. These little tasks are actually threads of your process because collectively they serve to help in its execution. If any thread fails to execute properly, the entire process may be disrupted. (For instance, imagine what would happen if you actually fell asleep!)
So you gather people from a nearby village, which can be thought of as a thread to your new process, and shove the tree out of the way. Then you go back to your car and continue with your journey.
Fig 1.1- A thread within a process
Each process runs in a separate address space in the CPU. But threads, on the other hand, may share address space with other threads within the same process. This sharing of space facilitates communication between them. Therefore, Switching between threads is much simpler and faster than switching between processes
Threads also have a great degree of control over other threads of the same process. Processes only have control over child processes.
Also, any
changes made to the main thread may affect the behavior of other threads within
the same process. However, changes made to the parent processes do not affect
the child processes.
Considering
the similarities, a thread can do anything that a process can do. They are both
able to reproduce (Processes create child processes and threads create more
threads), and they both have ID, base priority, execution time and exit status
as attributes.
Fig 1.2- Life cycle of a process
To understand the actual differences between processes and threads, let us consider a real-life example. Suppose you are driving from city A to city B and you have had only three hours of sleep the night before after a terribly hectic day. Imagine yourself as the CPU and driving to city B as a process. While driving you may have to do a number of smaller tasks such as checking the map on the GPS, making sure that you don’t exceed speed limits, making sure you don’t fall asleep (trust me, it happens!), etc. These little tasks are actually threads of your process because collectively they serve to help in its execution. If any thread fails to execute properly, the entire process may be disrupted. (For instance, imagine what would happen if you actually fell asleep!)
Now let’s
assume that there had been a storm the night before. After travelling some
distance you see that a toppled over tree is blocking the road ahead. You
cannot move any further unless the tree is removed. Therefore, your current
process has to be stopped. Now you’ve got a brand new process
in your hands- getting the tree out of the
way.
So you gather people from a nearby village, which can be thought of as a thread to your new process, and shove the tree out of the way. Then you go back to your car and continue with your journey.
Fig 1.3- Check out my drawing skills!!
Let us take
another example. Suppose you are cooking pasta in your kitchen. Once again,
imagine that you are the CPU and cooking pasta
is your process. Your threads this time will be rolling and cutting the pasta dough, preparing the sauce,
grating cheese, etc. All of a sudden from the corner of your eye you
notice something moving. You turn towards that direction and to your utter
horror you see a huge rat! Now you cannot cook with the knowledge that there is
a huge, nasty rat at large in your kitchen. So you stop doing whatever you were
doing and set about accomplishing a new task, or a new process-
Getting rid of the nasty little rodent. So you
cut a little slice of cheese, take a stick and wait for the rat in ambush. All of these are your threads.
Finally when the rat does show up, you try to hit it and miss terribly, but you
do succeed in scaring it away from your kitchen. Satisfied, you peacefully get
back to your cooking.
Considering threads in windows and
linux
In Linux,
there is no distinction between processes and threads; instead it simply uses
the term task. On the contrary, in
windows there are considerable differences between these two as has been
mentioned above.
The limited distinction
between threads and processes in Linux exists because the entire context of the
process is not present in the main process data structure. Instead the context
is contained in independent data structures. The process data structure simply
contains pointers to these separate structures. In windows, on the other hand,
all the data vital to the process are contained exclusively within the process
itself.
In Linux,
the scheduler represents a higher priority of execution by a lower number,
whereas in Windows it is represented by a higher number.
References
-Modern Operating Systems by Andrew S. Tanenbaum and Albert S. Woodhull
-Operating System Principles by Abraham Silberschatz, Peter Baer Galvin and Greg Gagne
-http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process
-www.wikipedia.com
-http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process
-http://rossano.pro.br/fatec/cursos/soi/Linux.pdf
-http://avellano.fis.usal.es/~lalonso/amp_inf/windows.pdf
-http://codesam.blogspot.com/2011/03/introduction-to-thread-in-java-part-2.html
References
-Modern Operating Systems by Andrew S. Tanenbaum and Albert S. Woodhull
-Operating System Principles by Abraham Silberschatz, Peter Baer Galvin and Greg Gagne
-http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process
-www.wikipedia.com
-http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process
-http://rossano.pro.br/fatec/cursos/soi/Linux.pdf
-http://avellano.fis.usal.es/~lalonso/amp_inf/windows.pdf
-http://codesam.blogspot.com/2011/03/introduction-to-thread-in-java-part-2.html
No comments:
Post a Comment