![]() |
| | #1 |
| Registered User Join Date: Oct 2006
Posts: 263
| child process exits and closes parent's connections |
| Elkvis is offline | |
| | #2 |
| Jaxom's & Imriel's Dad Join Date: Aug 2006 Location: Alabama
Posts: 801
| Code: fd = open("myfile", O_RDWR);
if ((pid = fork()) == 0){
callsomething();
}
callsomethingelse();
return 0;
|
| Kennedy is offline | |
| | #3 |
| Registered User Join Date: Oct 2006
Posts: 263
| |
| Elkvis is offline | |
| | #4 |
| Jaxom's & Imriel's Dad Join Date: Aug 2006 Location: Alabama
Posts: 801
| By that I'm assuming that you mean the parent opens the connection above where you do your fork(). Does the child actually perform a close operation to the connection? |
| Kennedy is offline | |
| | #5 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| I'm actually working on a network client right now that needs to share it's connected socket amongst multiple process which are temporary. I'm using pthreads and just made the socket fd a global and this works fine. If you haven't used pthreads before, it is very very simple to substitute it for fork in this circumstance. You just make your process a function and launch it this way: Code: pthread_t mythread; pthread_create(&mythread,NULL,myfunc,NULL); pthread_detach(mythread); Code: pthread_exit(NULL); You could switch over to that sort of model quite easily I think, it would not require many changes, and it gives you some more interesting opportunities too...
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 09-04-2009 at 11:34 AM. |
| MK27 is online now | |
| | #6 | |
| Registered User Join Date: Oct 2006
Posts: 263
| Quote:
MK27: I can't use pthreads because the credit card processing program runs as a 32-bit process, while the parent is 64-bit, and the credit card processing library is only available as a 32-bit library, so I can't link it into my main program. it has to be a separate process. | |
| Elkvis is offline | |
| | #7 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #8 |
| Registered User Join Date: Oct 2006
Posts: 263
| |
| Elkvis is offline | |
| | #9 | |
| Registered User Join Date: Oct 2007
Posts: 22
| Quote:
Did child need those or any other file descriptors from parents at all ? If not - and assuming your are using Linux (who use something else for servers anyway ![]() you can instead of fork use clone with flag CLONE_FILES turn off In this case parent and child will not share file descriptors. If child really uses some file descriptors from parent, then turning of CLONE_FILES is not an option. In this case you can write small shared libraries and run your child process with this libraries LD_PRELOADed. Shared library should implement 'close' function. Those function should do nothing for network connection and database connection file descriptors and behaves like original close for any other | |
| Valery Reznic is offline | |
| | #10 | |
| Registered User Join Date: Oct 2006
Posts: 263
| Quote:
in any case, I've come up with a pretty workable solution to this problem. I'm going to re-write the credit card processor as a standalone server that listens on a unix-domain socket (AF_UNIX) and then I won't have to worry about it. the two processes will be unrelated, and therefore, share no file descriptors, and my problem will magically disappear. | |
| Elkvis is offline | |
| | #11 |
| Jaxom's & Imriel's Dad Join Date: Aug 2006 Location: Alabama
Posts: 801
| It almost sounds like you need a third process running that communicates with your DB. Let the parent communicate with that process and fork children as necessary. Then the child process doesn't get that opened connection ever. |
| Kennedy is offline | |
| | #12 | |
| Registered User Join Date: Oct 2006
Posts: 263
| Quote:
the existing server code will run as it always has, and the credit card processing program will be its own standalone server program (the new process) that can listen on a unix-domain socket and handle requests to process card transactions. | |
| Elkvis is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|