473,473 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using connection after fork

I'm looking at a database-using program (PostgreSQL/libpq and MySQL) which
does the following: A daemon process opens a database connection, forks
children on request, and those children access the database using that
inherited connection. After one request, the child dies. It seems to me
that this connection sharing cannot work because the processes would get
mixed up, but the author claims that he has tested this successfully. I've
been trying to figure out the semantics from the libc documentation, but I
can't find a definitive answer. Does anyone know what will happen in this
situation, and why it actually appears to work?

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
3 2067
Peter Eisentraut <pe*****@gmx.net> writes:
I'm looking at a database-using program (PostgreSQL/libpq and MySQL) which
does the following: A daemon process opens a database connection, forks
children on request, and those children access the database using that
inherited connection. After one request, the child dies. It seems to me
that this connection sharing cannot work because the processes would get
mixed up,
I agree, unless there is some interlock that prevents a new child from
being spawned before the last previous one died. It would seem to work
under light load where that happened anyway.
but the author claims that he has tested this successfully. I've
been trying to figure out the semantics from the libc documentation, but I
can't find a definitive answer. Does anyone know what will happen in this
situation, and why it actually appears to work?


I'll bet lunch that he simply has not stress-tested it hard enough to
have multiple children actually using the connection at the same time.
I can positively guarantee that it won't work if you do.

It would also not work with SSL, since the encryption state would get
out of sync. And some secondary features like tracking the current
transaction state could get out of sync too. But the basic
query-issuing cycle would probably appear to work as long as only one
child actually had a query in progress at any instant.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #2

On Aug 10, 2004, at 4:41 AM, Peter Eisentraut wrote:
does the following: A daemon process opens a database connection,
forks
children on request, and those children access the database using that
inherited connection. After one request, the child dies. It seems to
me
I've been bit by this sort of thing before. When you fork() the child
inherits all the file descriptors , including the one connected to PG.
But when the child dies that FD will be closed, thus it will also be
closed in the parent causing "odd behavior". (At least, that is what
I've seen in my apps)
that this connection sharing cannot work because the processes would
get
mixed up, but the author claims that he has tested this successfully.
I've
been trying to figure out the semantics from the libc documentation,
but I
can't find a definitive answer. Does anyone know what will happen in
this
situation, and why it actually appears to work?


Does it work for sustained periods of time? Is the parent reconnecting
to PG after each child dies? It *shouldn't* work.
--
Jeff Trout <je**@jefftrout.com>
http://www.jefftrout.com/
http://www.stuarthamm.net/
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #3
Jeff <th******@torgo.978.org> writes:
I've been bit by this sort of thing before. When you fork() the child
inherits all the file descriptors , including the one connected to PG.
But when the child dies that FD will be closed, thus it will also be
closed in the parent causing "odd behavior". (At least, that is what
I've seen in my apps)


That would only happen if the child explicitly does PQfinish() before
exiting, so that the backend sees an 'X' (Terminate) command arrive
and therefore cuts the connection from its end.
If the child just exits the pipe or TCP connection will remain open.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
6
by: shellcode | last post by:
the code: ------fork.c------ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() {
11
by: ramu | last post by:
Hi All, We know that a c function never returns more than one value. Then how come the fork() function returns two values? How it is implemented? Regards
1
by: vduber6er | last post by:
Hi I want to have a wait page while the rest of the cgi does its process, but it seems like the wait page waits till everything is complete and never appears. I've tried forking twice already as...
3
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void...
5
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I...
0
by: muraley | last post by:
Hi, This client-server script does bi-directional communication. Setup i used: The server script on windows 2003 server and the client on a linux machine. Since i faced issues in getting the...
1
by: ohaqqi | last post by:
Hi guys, I'm still working on my shell. I'm trying to implement a function typefile that will take a command line input as follows: > type <file1> This command will implement a catenation of...
7
by: JamesHoward | last post by:
Does anyone know any method to have one program, acting as a server transfer a socket connection to another program? I looked into transferring the connection via xml rpc to no avail. It seems to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.