Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2007

Tip: Looking for answers? Try searching our database.

difference between thread and process

Thread view: 
neuneudr@yahoo.fr - 26 Feb 2007 18:39 GMT
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.

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).

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?

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?
Eric Sosman - 26 Feb 2007 20:33 GMT
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
[quoted text clipped - 4 lines]
> 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
[quoted text clipped - 4 lines]
> 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.

Signature

Eric.Sosman@sun.com

neuneudr@yahoo.fr - 27 Feb 2007 15:00 GMT
Re,

apparently my first reply message got lost.

So I re-sent this message to thank you for
your quick and detailed answer :)

Thanks again,

 Driss


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.