Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: josh@meth.osf.org (Joshua Goldman)
Subject: Re: Threads better than forks?
Date: 14 Oct 1993 18:57:30 GMT	[thread overview]
Message-ID: <29k7eq$l7o@paperboy.osf.org> (raw)
In-Reply-To: CEw81s.K6K@aaahq05.aaa.com

In article <CEw81s.K6K@aaahq05.aaa.com>, swh@aaahq05.aaa.com (Steven Wayne Hudson) writes:
> In article 93Oct11183950@hpltp.hpl.hp.com, thuan@hpltp.hpl.hp.com (Thuan Q. Pham) writes:
> > In article <mauney.750363918@adm> mauney@adm.csc.ncsu.edu (Jon Mauney) writes:
> > 
> > >   a) Fork is usually a fairly heavyweight operation.  Creating a thread
> > >     should be fairly fast
> > 
> > Yes, forking a process is much more expensive than spawning thread because
> > of the overhead.  To create a process, the system must set up the address
> > space and other system resources (memory pages, file descriptors, etc. etc..)
> > 
> > In a multithreaded process (Mach calls it "task"), threads within the same
> > process share all of the process's resources, so creating a thread is a
> > cheap operation: setting up the thread running context (program counter,
> > registers set, etc...).  It inherits the rest from the parent process.
> > And since threads share the process's resources, communication between them
> > can be done very naturally and efficiently with shared memory.
> > 
> O.K.  So how do you create a thread?

If you using the pthread package (pthread for POSIX threads).  There are a 
whole set of pthread calls: pthread_create, pthread_join, pthread_exit, ...

If you have access to it, there's a good chapter on pthreads in the
OSF/1 Application Programmer's Guide.  I don't know if DEC includes it
with the Alpha documentation.  OSF only publishes the reference documents 
and the User's Guide through Prentice Hall.
> 
> Also, what is the functional (NOT operational) difference between a thread and fork?

As the above description states, the thread
inherits much of its state from the parent process.  Basically, this means
that they share the same address space.  This has its ups and downs.

Ups:  less expensive to create because you don't have to initialize 
entire address space and easy to communicate back and forth because all
you have to do is write into a global address (if more than one thread is
writing to the same address, you may have simultaneous update issues).

Downs: Any interface that remembers the context of a call may have problems 
with threads.  If you don't one thread can make a call and another can get 
the first thread's context.  You need to use thread_safe versions of these
calls.  For example the random() call returns a fixed  series of random numbers
depending on the seed set in initstate().  If two threads are both calling 
random, they will each get only some of the numbers in the series.  The other
thread will get the missing numbers.  The random_r() allows you to specify a
pointer to a structure that gives each thread its own random series.  Obviously,
in this particular case, you might not care that you were getting an unpredictable
set of random numbers, but there are other state-sensitive calls that would
provide you with some bugs that are very tricky to find.

> 
> ---
> Steve Hudson                     ///
> swh@aaahq05.aaa.com             (0 0)         
> -----------------------------ooO-(_)-Ooo---------------------------------
> 

-- 
Although I did write some of the OSF/1 documentation (the Internationalization
chapter and worked on many of the Programmer's Ref pages), I did not work
on the pthreads chapter.

Joshua Goldman josh@osf.org 617-621-8857

Opinions are not those of:

Open Software Foundation
11 Cambridge Center
Cambridge, MA 02142


  reply	other threads:[~1993-10-14 18:57 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 [this message]
1993-10-15 17:39       ` Chris Fulmer
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