Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
* Threads better than forks?
@ 1993-10-11 17:05 Thomas Georges
  1993-10-11 18:25 ` Jon Mauney
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Thomas Georges @ 1993-10-11 17:05 UTC (permalink / raw)


I just finished the DCE applications programming 3 day class and was 
discussion some issues with a coworker.  He asked the question, "What's the
advantage of using threads over fork/exec with signals?"

Anyone care to post some advantages?  His thought was that the non-threded
kernels would have lots of overhead that could be avoided by using
fork and exec.

fyi - I explained the differences (e.g. one process vs. many, etc) already.
I think what he was curious about was why threads are better.

Thanks.

Tom Georges
Harris Corporation
Melbourne, Florida



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  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-13  9:11   ` Threads better than forks? Stuart John Cochran
  1993-10-11 20:05 ` Brad Daniels
  1993-10-12 19:19 ` Rich Salz
  2 siblings, 2 replies; 15+ messages in thread
From: Jon Mauney @ 1993-10-11 18:25 UTC (permalink / raw)


tgeorges@nyx.cs.du.edu (Thomas Georges) writes:

>I just finished the DCE applications programming 3 day class and was 
>discussion some issues with a coworker.  He asked the question, "What's the
>advantage of using threads over fork/exec with signals?"

>Anyone care to post some advantages?  His thought was that the non-threded
>kernels would have lots of overhead that could be avoided by using
>fork and exec.

The two most obvious reasons are efficiency and convenience.

a) Fork is usually a fairly heavyweight operation.  Creating a thread
  should be fairly fast, making it feasible to create a new thread
  as needed.  Also, scheduling of threads within a process doesn't
  require a context switch, and there takes much less time than
  switching between two processes.

2) Because threads share an address space, data communication among threads
  is simpler.  Two communicate between processes you must go to
  some trouble to set up an IPC mechanism.  with threads, the data
  is simply there.
-- 
Jon Mauney, Mauney Computer Consulting
mauney@csc.ncsu.edu   (919) 828-8053

"She wrote a long letter, on a short piece of paper."


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  1993-10-11 17:05 Threads better than forks? Thomas Georges
  1993-10-11 18:25 ` Jon Mauney
@ 1993-10-11 20:05 ` Brad Daniels
  1993-10-12 19:19 ` Rich Salz
  2 siblings, 0 replies; 15+ messages in thread
From: Brad Daniels @ 1993-10-11 20:05 UTC (permalink / raw)


In article <1993Oct11.170510.13690@mnemosyne.cs.du.edu>,
Thomas Georges <tgeorges@nyx.cs.du.edu> wrote:
>I just finished the DCE applications programming 3 day class and was 
>discussion some issues with a coworker.  He asked the question, "What's the
>advantage of using threads over fork/exec with signals?"

Apart from system overhead issues, there is the fact that threads share a
common data space.  This means you can pass information from one thread
to another without copying.  While you can achieve similar effects with
shared memory and multiple processes, managing the situation is more
complex and expensive.

Also, in the case of RPC communications, it is feasible to start up
as many threads as you want to simplify your algorithm.  For example,
in our package, we may use RPC to communicate with as many as 10 or
20 different data servers, and we may have several different scan rates
for querying each server.  Rather than deal with juggling servers,
finding lowest common denominator scan rates per server, deciding how
many communication processes to start, and deciding how to divvy up
shared memory among the logically independent communication
operations, we decided to simply start one thread per data source and
scan rate.  We have one connection per data source, and all the different
scan rates' threads use that one connection, so we also minimize the number
of network connections.

- Brad
-- 
----------------------------------------------------------------------
+ Brad Daniels			| "Let others praise ancient times;  +
+ Biles and Associates		|  I am glad I was born in these."   +
+ These are my views, not B&A's	|	  - Ovid(43 B.C - 17 A.D)    +


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  1993-10-11 18:25 ` Jon Mauney
@ 1993-10-12  2:39   ` Thuan Q. Pham
  1993-10-12 15:38     ` Lee Sailer
                       ` (2 more replies)
  1993-10-13  9:11   ` Threads better than forks? Stuart John Cochran
  1 sibling, 3 replies; 15+ messages in thread
From: Thuan Q. Pham @ 1993-10-12  2:39 UTC (permalink / raw)


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.

>     .... Also, scheduling of threads within a process doesn't
>     require a context switch, and there takes much less time than
>     switching between two processes.

Well, whether context switching is required for thread scheduling depends
on what kind of threads we're talking about.  If it's kernel thread,
switching between threads DOES require a context switch.  If it's
user-level thread, then the user-thread run-time library schedules the
threads, and the kernel doesn't know about it.  To the OS kernel, it's only
one process.  It's the runtime library that implements handles user-level
threads. 
  And since the kernel doesn't know about user-level threads, a context
switch means all the user threads in the same process get swapped out (or
in).  Also for the same reason, user-level threads within the same process
cannot make use of multiple processors at once.

/Thuan



-------------------------------------------------------------------
Thuan Q. Pham
Hewlett-Packard Laboratories              (415) 857-6818
1501 Page Mill Road                       pham@hpl.hp.com
Palo Alto, Ca 94303



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  1993-10-12  2:39   ` Thuan Q. Pham
@ 1993-10-12 15:38     ` Lee Sailer
  1993-10-12 18:04       ` zac
                         ` (2 more replies)
  1993-10-14 15:22     ` Steven Wayne Hudson
  1993-10-14 15:23     ` FAQ request Steven Wayne Hudson
  2 siblings, 3 replies; 15+ messages in thread
From: Lee Sailer @ 1993-10-12 15:38 UTC (permalink / raw)


Another difference between threads (tasks) and processes that I'm not quite
sure about has to do with scheduling.  As I understand it, all a process's
tasks share that one process's timeslice.  On the other hand, forked
process's each get a timeslice of their own.

  If this is true, than a function devided into 5 processes might execute
faster than the same function divided into 5 threads within one process,
since the 5 process solution would get five times the cpu time.

  lee


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  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
  2 siblings, 0 replies; 15+ messages in thread
From: zac @ 1993-10-12 18:04 UTC (permalink / raw)


In <29ej1h$n33@hpuerca.atl.hp.com>, sailer@atl.hp.com (Lee Sailer) writes:
>Another difference between threads (tasks) and processes that I'm not quite
>sure about has to do with scheduling.  As I understand it, all a process's
>tasks share that one process's timeslice.  On the other hand, forked
>process's each get a timeslice of their own.
>
>  If this is true, than a function devided into 5 processes might execute
>faster than the same function divided into 5 threads within one process,
>since the 5 process solution would get five times the cpu time.
>
>  lee


This is true only for user-level threads.  User-level thread libraries are 
cooperatively multitasked within a process' time-slice - the kernel doesn't
know anything about them.  A process only has a single thread of execution 
to work with.  With kernel-level threads OTOH, the thread is the unit of
scheduling.  In other words the scheduler doesn't even deal with processes
but just with threads.  In this case a process starting 5 threads would get
the same average cpu time as 5 single-threaded processes (assuming all the
other variables like priority and i/o are kept equal).


Zac 

     I don't speak for IBM.  Heck, I don't even WORK for IBM.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  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
  2 siblings, 0 replies; 15+ messages in thread
From: Simon Patience @ 1993-10-12 18:53 UTC (permalink / raw)


In article <29ej1h$n33@hpuerca.atl.hp.com>, sailer@atl.hp.com (Lee Sailer) writes:
|> Another difference between threads (tasks) and processes that I'm not quite
|> sure about has to do with scheduling.  As I understand it, all a process's
|> tasks share that one process's timeslice.  On the other hand, forked
|> process's each get a timeslice of their own.

You are wrong, at least in OSF/1 and Mach. A thread is the schedulable entity
and not the task/process. Thus 5 processes would get as much time as 5 threads 
as they would be scheduled in the same way. There is a small difference in that 
switching between 2 threads in the same process/task would not have to change 
address spaces whereas switching between two processes would so 5 threads may 
get even more time than 5 processes due to lower system overhead.

Simon.

-- 
  Simon Patience				Phone: (617) 621-7376
  Open Software Foundation			FAX:   (617) 621-8696
  1 Cambridge Center				Email: sp@osf.org
  Cambridge, MA 02142				       uunet!osf!sp


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  1993-10-11 17:05 Threads better than forks? Thomas Georges
  1993-10-11 18:25 ` Jon Mauney
  1993-10-11 20:05 ` Brad Daniels
@ 1993-10-12 19:19 ` Rich Salz
  2 siblings, 0 replies; 15+ messages in thread
From: Rich Salz @ 1993-10-12 19:19 UTC (permalink / raw)


In <1993Oct11.170510.13690@mnemosyne.cs.du.edu> tgeorges@nyx.cs.du.edu (Thomas Georges) writes:
> "What's the advantage of using threads over fork/exec with signals?"

There is really only one true difference:  all threads within a process
share that process's state (e.g., global variables, open files, etc).  The
other differences, e.g., "forks are more expensive" and "scheduling is
different" are not architectural differences.  These, and other differences
mentioned on the net, vary among operating systems, and hardware.

Okay, given global shared state what can you do with it?  Here's a simple
use.  A client knows that there are 10 servers out there that can solve
its problem.  It spawns ten threads, each one doing an RPC.  The first
one that comes back sends a cancel message to the other nine.  Suppose you
want to keep 10 "calculators" busy each doing a partitioned part of, say,
a Mandelbrot calculation.  Each thread just pulls another chunk of the
problem out of a pool.  (Call it tuple-space and you just invented inverted
Linda. :-)

Correctly programming with threads is difficult.  There's a book on Modula-3
that has a great chapter on how to use threads; it was written by Andrew
Birrell and is a slight revision of a wonderful DEC technical paper.  The
book is worth it for just this chapter.
	/r$


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  1993-10-11 18:25 ` Jon Mauney
  1993-10-12  2:39   ` Thuan Q. Pham
@ 1993-10-13  9:11   ` Stuart John Cochran
  1993-10-13 12:31     ` Simon Patience
  1 sibling, 1 reply; 15+ messages in thread
From: Stuart John Cochran @ 1993-10-13  9:11 UTC (permalink / raw)


Thomas Georges writes:
> [...] the DCE applications programming 3 day class [...]

Is DCE training provided by the OSF or OSF licensees? Our training unit has
failed to find any such courses here in the UK. 
---
Stuart Cochran, BT Laboratories, sjc@zoo.bt.co.uk




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  1993-10-13  9:11   ` Threads better than forks? Stuart John Cochran
@ 1993-10-13 12:31     ` Simon Patience
  0 siblings, 0 replies; 15+ messages in thread
From: Simon Patience @ 1993-10-13 12:31 UTC (permalink / raw)


In article <1993Oct13.091100.16567@zoo.bt.co.uk>, sjc@zoo.bt.co.uk (Stuart John Cochran) writes:
|> Thomas Georges writes:
|> > [...] the DCE applications programming 3 day class [...]
|> 
|> Is DCE training provided by the OSF or OSF licensees? Our training unit has
|> failed to find any such courses here in the UK. 

I don't know if there has been a training course run in the UK yet. Many are done
either in the OSF offices in Brussels or Grenoble or on customer sites if the
numbers are large enough. You should call the OSF office in Brussels on 
+32 27 72 88 88 if you want to know the upcoming course schedules in Europe.

Simon.

-- 
  Simon Patience				Phone: (617) 621-7376
  Open Software Foundation			FAX:   (617) 621-8696
  1 Cambridge Center				Email: sp@osf.org
  Cambridge, MA 02142				       uunet!osf!sp


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  1993-10-12  2:39   ` Thuan Q. Pham
  1993-10-12 15:38     ` Lee Sailer
@ 1993-10-14 15:22     ` Steven Wayne Hudson
  1993-10-14 18:57       ` Joshua Goldman
  1993-10-15 17:39       ` Chris Fulmer
  1993-10-14 15:23     ` FAQ request Steven Wayne Hudson
  2 siblings, 2 replies; 15+ messages in thread
From: Steven Wayne Hudson @ 1993-10-14 15:22 UTC (permalink / raw)


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?

Also, what is the functional (NOT operational) difference between a thread and fork?

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



^ permalink raw reply	[flat|nested] 15+ messages in thread

* FAQ request
  1993-10-12  2:39   ` Thuan Q. Pham
  1993-10-12 15:38     ` Lee Sailer
  1993-10-14 15:22     ` Steven Wayne Hudson
@ 1993-10-14 15:23     ` Steven Wayne Hudson
  2 siblings, 0 replies; 15+ messages in thread
From: Steven Wayne Hudson @ 1993-10-14 15:23 UTC (permalink / raw)


Could someone post or email this group's FAQ?  I would greatly
appreciate it.

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



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  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
  2 siblings, 0 replies; 15+ messages in thread
From: Jari Tavi @ 1993-10-14 15:37 UTC (permalink / raw)



>Another difference between threads (tasks) and processes that I'm not quite
>sure about has to do with scheduling.  As I understand it, all a process's
>tasks share that one process's timeslice.  On the other hand, forked
>process's each get a timeslice of their own.
>
>  If this is true, than a function devided into 5 processes might execute
>faster than the same function divided into 5 threads within one process,
>since the 5 process solution would get five times the cpu time.

	This is not true. In OSF/1 and other thread oriented OS's 
	thread is minimum unit of execution - each thread can be
	scheduled individually and can execute parallel in different
	processor. So in fact thread based solution can reach much higher
	level of parallelism as it can use both processes and threads to
	increase the level of concurrent execution.

	There are different implementations in other operating systems 
	than OSF/1 though. Some of them may not be real multi threaded.




			-jari
--
-------------------------------------------------------------------------------
Full-Name:      Jari Tavi			
Organization:   DEC Finland                     Dept:           DS/OSG
Tel:            +358-0-434 5252	                Fax:            +358-0-452 1392 
E-mail:         jpt@rixrax.enet.dec.com
Address:        Digital Equipment Corporation Oy
                PL 16
                02201 Espoo
                FINLAND
-------------------------------------------------------------------------------
* All opinions are my personal, and do not necessarily represent opinions of
* my employer! 


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  1993-10-14 15:22     ` Steven Wayne Hudson
@ 1993-10-14 18:57       ` Joshua Goldman
  1993-10-15 17:39       ` Chris Fulmer
  1 sibling, 0 replies; 15+ messages in thread
From: Joshua Goldman @ 1993-10-14 18:57 UTC (permalink / raw)


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


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Threads better than forks?
  1993-10-14 15:22     ` Steven Wayne Hudson
  1993-10-14 18:57       ` Joshua Goldman
@ 1993-10-15 17:39       ` Chris Fulmer
  1 sibling, 0 replies; 15+ messages in thread
From: Chris Fulmer @ 1993-10-15 17:39 UTC (permalink / raw)


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.


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~1993-10-15 17:39 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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