Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: chrisf@brtph316.bnr.ca (Chris Fulmer)
Subject: Re: Threads better than forks?
Date: Fri, 15 Oct 1993 17:39:32 GMT	[thread overview]
Message-ID: <1993Oct15.173932.9580@brtph560.bnr.ca> (raw)
In-Reply-To: CEw81s.K6K@aaahq05.aaa.com

In article <CEw81s.K6K@aaahq05.aaa.com>, swh@aaahq05.aaa.com (Steven Wayne Hudson) writes:
> Also, what is the functional (NOT operational) difference between a thread 
> and fork?

Theoretically, I don't think that there's a computational difference
between the two -- ie for everything that you can do in one style, you
can theoretically do in the other.  But, the code may look a lot 
different.

With threads, for instance, you could write a server which had
a thread for each client, and those threads were responsible for
verifying client input, etc....  And, you could have another server 
thread for actually handling client requests.  The threads 
directly servicing the clients could pass the file descriptor
of the client to the "main" thread, which then sent a reply
message back to the client on the FD that was passed in.

If you did that with heavy-weight processes, the main guy
would have to send the message back to the service threads,
which then forwarded it back to the client.  Functionally
the same, but harder to program.

(Note that I'm not saying that this is the way that you'd
want to write a server process...)

The advantage of threading is that everything is implicitly
shared.  If your version of unix supports shared memory,
you can do the same things yourself, but you have to set
them up.  (You also couldn't share any of the per-process
things, like file descriptors or whatever...)

Because of the communication problem, when writing servers,
people rarely write ones which fork() for each new client.
Instead, they'll keep some context information on them, and
do the switching between contexts themselves.  This is 
especially true if there are inter-client dependancies.
With threads, it's easy -- just fork off a new thread.  If
you need some information, it's stored someplace global to
all the threads.

------------------------------------------------------
Chris Fulmer                    Bell-Northern Research
Sr. Software Designer            Research Triangle, NC
mail to woodstock!chris@mcnc.org (preferable) or...
        chrisf@bnr.ca            (not so preferable)
Oh yeah.  The usual disclaimers apply here.


  parent reply	other threads:[~1993-10-15 17:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-10-11 17:05 Threads better than forks? Thomas Georges
1993-10-11 18:25 ` Jon Mauney
1993-10-12  2:39   ` Thuan Q. Pham
1993-10-12 15:38     ` Lee Sailer
1993-10-12 18:04       ` zac
1993-10-12 18:53       ` Simon Patience
1993-10-14 15:37       ` Jari Tavi
1993-10-14 15:22     ` Steven Wayne Hudson
1993-10-14 18:57       ` Joshua Goldman
1993-10-15 17:39       ` Chris Fulmer [this message]
1993-10-14 15:23     ` FAQ request Steven Wayne Hudson
1993-10-13  9:11   ` Threads better than forks? Stuart John Cochran
1993-10-13 12:31     ` Simon Patience
1993-10-11 20:05 ` Brad Daniels
1993-10-12 19:19 ` Rich Salz
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox