refactoring fork() and EXEC_BACKEND

From: Neil Conway <neilc(at)samurai(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: refactoring fork() and EXEC_BACKEND
Date: 2005-03-04 05:30:00
Message-ID: 4227F258.9040709@samurai.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

While going through the usual motions needed to fork a child process of
the postmaster, it occurred to me that there's a fair bit of duplicated
code involved. There are also #ifdef for various situations (BeOS,
LINUX_PROFILE, and EXEC_BACKEND), which makes the code yet more ugly. I
think we could make this a lot cleaner.

I'd like to define an API like so:

pid_t fork_process(int proc_type);
pid_t fork_backend(Port *port);

If the process needs to add a lot of private information to the argv in
the case of EXEC_BACKEND, they could invoke a third variant:

#ifdef EXEC_BACKEND
pid_t forkexec_process(int proc_type, int argc, char **argv);
#endif

(Or possibly using varargs, if that is cleaner for most call-sites).
Hopefully most call sites could just use fork_process().

These functions would then take care of all the necessary
platform-specific judo:

- flush stdout, stderr
- invoke BeOS hooks as necessary
- save and restore profiling timer, if necessary
- if EXEC_BACKEND, use proc_type to lay out the argv for the new process
and then invoke internal_forkexec()
- otherwise, just invoke fork()
- return result to client

So, most call sites would be quite nice:

pid_t result = fork_process(PROC_TYPE_FOO);
if (result == -1) { /* fork failed, in parent */ }
else if (result == 0) { /* in child */ }
else { /* in parent, `result' is pid of child */ }

I'd also like to move the implementation of fork_process() and friends,
as well as internal_forkexec(), into a separate file -- I'd rather not
clutter up postmaster.c with it.

Comments?

-Neil

Browse pgsql-hackers by date

  From Date Subject
Next Message Vikram Kalsi 2005-03-04 05:57:15 Re: postgreSQL-8.0.1 compilation with icc-8.1 on Itanium-2 gives "error: asm statements not supported"
Previous Message Qu Tianlian 2005-03-04 01:18:01 答复: Novice Slony User (Was [HACKERS] hi all)