Re: difference between thread and process
neuneudr@yahoo.fr wrote On 02/26/07 13:39,:
Hi,
I'd like to understand what's the difference between a
thread and a process and how this is related to Java's
threading mechanism.
All the threads within a process share the same address
space, but different processes have different address spaces.
That last part is not 100% true, because things like shared
libraries and shared memory blur the line a bit. Still, it
is the single most important difference between a process
and a thread.
There are some other things threads share that processes
do not; the list tends to vary somewhat between operating
systems. For example, all the threads in a process share
the same "credentials" -- user name, access privileges, and
so on -- but different processes have different credentials.
In some operating systems, the "working directory" and the
"locale" and the "environment" and the set of "file descriptors"
are attributes of an entire process, and thus are shared by
all a process' threads.
For example it used to be that, under Linux, every Java
thread would appear as a single process (using the 'ps'
command that reports a snapshot of the current
processes).
That was an artifact of the way Linux implemented threads
in its early days. I believe modern Linuces have changed to
an implementation that is more nearly POSIX-conforming.
One thing that I particularly don't understand is the
following: the Thread class's stop() method is
deprecated. And, sure enough, I've been able to
write quite some amount of multi-threaded Java
code without ever needing to brutally 'stop' threads.
Why does Windows provides a TerminateProcess()
and Unix a kill() facility to terminate/kill processes?
Killing an entire process and stopping all of its threads
is simpler than killing just one thread and leaving the others
running. Even so, "simpler" does not always imply "safe:" if
a process is part of a multi-process framework or service, the
service as a whole may be damaged if one of its processes dies
unexpectedly.
Is it bad programming style to call TerminateProcess()
and kill() ? If not, would it be bad to have in Java
a "correct" way to kill a Thread ? (not stop(), which is
broken, but something else?)
What makes these processes so fundamentally different
than Java threads that it is possible to terminate/kill
them?
When the process dies, all of its internal resources are
destroyed and reclaimed by the O/S. It no longer matters that
thread T1 holds a lock that T2,T3,...,Tn are waiting for; they
are all dead and will wait no more. If the data structures in
the process' memory are in an inconsistent state at the moment
the process dies, nobody cares: All the memory will be cleared
and reused for other purposes, and it will not matter what data
it used to hold.
When a single thread stops abruptly, it may still be holding
locks, it may have left some data structures in inconsistent
states, it may have just finished opening a file but not yet
stored the file descriptor anywhere ... All these things are
of consequence because the other threads may try to acquire
the still-locked locks, manipulate the damaged data structures,
or get into trouble by losing track of how many files are open.
They all share the environment of the deceased thread, and thus
are vulnerable to each other.
It is much easier to spray insecticide on an entire nest
of hornets than to poison the queen and leave the others alone.
Both actions, though, have their hazards.
--
Eric.Sosman@sun.com