Minor improvements in backup and recovery:
[PostgreSQL.git] / src / backend / postmaster / fork_process.c
blobe83d017e8b80d68d4707078575e14fc605ad2535
1 /*
2 * fork_process.c
3 * A simple wrapper on top of fork(). This does not handle the
4 * EXEC_BACKEND case; it might be extended to do so, but it would be
5 * considerably more complex.
7 * Copyright (c) 1996-2007, PostgreSQL Global Development Group
9 * IDENTIFICATION
10 * $PostgreSQL$
12 #include "postgres.h"
13 #include "postmaster/fork_process.h"
15 #include <time.h>
16 #include <sys/time.h>
17 #include <unistd.h>
19 #ifndef WIN32
21 * Wrapper for fork(). Return values are the same as those for fork():
22 * -1 if the fork failed, 0 in the child process, and the PID of the
23 * child in the parent process.
25 pid_t
26 fork_process(void)
28 pid_t result;
30 #ifdef LINUX_PROFILE
31 struct itimerval prof_itimer;
32 #endif
35 * Flush stdio channels just before fork, to avoid double-output problems.
36 * Ideally we'd use fflush(NULL) here, but there are still a few non-ANSI
37 * stdio libraries out there (like SunOS 4.1.x) that coredump if we do.
38 * Presently stdout and stderr are the only stdio output channels used by
39 * the postmaster, so fflush'ing them should be sufficient.
41 fflush(stdout);
42 fflush(stderr);
44 #ifdef LINUX_PROFILE
47 * Linux's fork() resets the profiling timer in the child process. If we
48 * want to profile child processes then we need to save and restore the
49 * timer setting. This is a waste of time if not profiling, however, so
50 * only do it if commanded by specific -DLINUX_PROFILE switch.
52 getitimer(ITIMER_PROF, &prof_itimer);
53 #endif
55 result = fork();
56 if (result == 0)
58 /* fork succeeded, in child */
59 #ifdef LINUX_PROFILE
60 setitimer(ITIMER_PROF, &prof_itimer, NULL);
61 #endif
65 return result;
68 #endif /* ! WIN32 */