Skip site navigation (1) Skip section navigation (2)

Re: Using Threads?

From: Bruce Guenter <bruceg(at)em(dot)ca>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Using Threads?
Date: 2000-12-04 23:57:29
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-hackers
On Mon, Dec 04, 2000 at 02:30:31PM -0800, Dan Lyke wrote:
> Adam Haberlach writes:
> > Typically (on a well-written OS, at least), the spawning of a thread
> > is much cheaper then the creation of a new process (via fork()).
> This would be well worth testing on some representative sample
> systems.

Using the following program for timing process creation and cleanup:

main() {
  int i;
  int pid;
  for (i=0; i<100000; ++i) {
    if(pid==-1) exit(1);
    if(!pid) _exit(0);

And using the following program for timing thread creation and cleanup:

#include <pthread.h>

threadfn() { pthread_exit(0); }

main() {
  int i;
  pthread_t thread;
  for (i=0; i<100000; ++i) {
    if (pthread_create(&thread, 0, threadfn, 0)) exit(1);
    if (pthread_join(thread, 0)) exit(1);

On a relatively unloaded 500MHz PIII running Linux 2.2, the fork test
program took a minimum of 16.71 seconds to run (167us per
fork/exit/wait), and the thread test program took a minimum of 12.10
seconds to run (121us per pthread_create/exit/join).  I use the minimums
because those would be the runs where the tasks were least interfered
with by other tasks.  This amounts to a roughly 25% speed improvement
for threads over processes, for the null-process case.

If I add the following lines before the for loop:
  char* m;
The cost for doing the fork balloons to 240us, whereas the cost for
doing the thread is constant.  So, the cost of marking the pages as COW
is quite significant (using those numbers, 73us/MB).

So, forking a process with lots of data is expensive.  However, most of
the PostgreSQL data is in a SysV IPC shared memory segment, which
shouldn't affect the fork numbers.
Bruce Guenter <bruceg(at)em(dot)ca>             

In response to


pgsql-hackers by date

Next:From: Lamar OwenDate: 2000-12-05 01:18:44
Subject: Re: Using Threads?
Previous:From: MatthewDate: 2000-12-04 23:48:36
Subject: RE: Using Threads?

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group