Re: fork() children and share memory with them

From: nobody (nobody_at_nowhere.non)
Date: 10/28/03


Date: Tue, 28 Oct 2003 05:19:50 GMT


"jetmarc" <jetmarc@hotmail.com> wrote in message
news:af3f5bb5.0310271200.23f50a35@posting.google.com...
> Hi..
>
> For a small server application, I want the main process to fork()
-------^^^^^
Don't try to hide a fact that you want to do something non-trivial.

> children for every incoming connection. I want the children to
> communicate status information back to the parent, and the parent

What kind of status info? Anyway, what you shouldn't overlook
is that parent will likely be blocked in accept() /see your code below/
waiting for another (client's) request (that's why you are spawning
dedicated process for handling each request, right?), and if server is
single-threaded (which I assume it is, otherwise you wouldn't be
fork()ing, right?), and e.g. there is no further request (don't call us,
we will call you:-), parent just blocks there and *can't* communicate
with children. You would have to use select() or something else.

> should be able to tell them to drop all connections (eg when the
> server exits).
>
Parent can send e.g. SIGTERM to every child (it has to keep track of them,
as every good parent would do, and children will have to "listen" for
/parent's/
signals, as good children do:-). All open descriptors will be closed (if
this is
what you mean by "drop all connections") when processes exit(). And don't
forget that parent should catch SIGCHLD, so executing ps -ef won't
produce script for zombie movie.

> However, fork() creates copies of all classes and variables, so
> the communication can not take place.
>
Communication still can take place, even if memory is copied.

> Is there any way to make existing memory SHARED? When researching
There is. It would be somewhat more difficult to make non-existing
memory shared:-) const char* s = "researching"; printf("%s", s+2);

> this, I found lots of references about how to allocate shared
> memory - but my classes are ALREADY allocated, and I wouldn't
> know how to create a class in pre-allocated memory anyway. At
> the moment my accept() loop is like this:
>
> nLen = sizeof(from);
> hFrom = accept(hServerSocket,(struct sockaddr *)&from,&nLen);
> printf("LOG: Incoming connection from
> %s:%d\r\n",inet_ntoa(from.sin_addr),ntohs(from.sin_port));
> CClient* client = new CClient(hFrom);
> close(hFrom);
>
> The fork() is done in the CClient::CClient construction.
>
IMHO:
1. usually it's not good idea to put "heavy stuff" like fork() in ctor
2. class name is misleading, it will become just another "instance" of
server process (after fork())
3. if you expect handling a lot (>40 or such) concurrent requests, maybe
you should reconsider your idea and go multithreaded. Which is another
can of worms

> How can I solve this problem? My children are quite useless

Depends on many things, as David suggested in his post.

> because they have no way to communicate with the server parent.
>
They do have a way. You just have to write code for it. Pipes were
working sufficiently for me for this purpose.

What you wan't to do is not as simple as it sounds (with all gotchas
involved). Maybe you would want to read first, perhaps one of Stevens'
books (I found "Advanced Programming in the UNIX Environment"
very helpful - specifically about IPC). I assume that you could find
also his "Internetworking with TCP/IP, Volume III" quite useful, with
lots of code examples.

Disclaimer: I know it looks like I didn't help you much. I just hope
it's enough to get you started in right direction (or stopped early
enough:-).



Relevant Pages

  • Re: client server programming
    ... server it will fork and creates a child for that request and parent ...
    (comp.os.linux.development.apps)
  • client server programming
    ... I am writing chat application when new request comes to ... server it will fork and creates a child for that request and parent ...
    (comp.os.linux.development.apps)
  • trying to understand fork and wait
    ... I would really appreciate some feedback on a Perl program I'm trying ... My plan is to have a single Perl script running while the ... I still do not understand how to use fork and wait ... } #end parent while loop ...
    (comp.lang.perl.misc)
  • Re: Virtualized SBS / WS /TS
    ... If you are seriously concerned about resource usage of the parent, run Server Core or the Hyper-V Server for the parent. ... My ML350 has 16 GB of RAM. ... It will happily run SBS 2008 plus a Terminal Server plus several other things. ...
    (microsoft.public.windows.server.sbs)
  • Re: timing a fork
    ... if ($kid) { ... That is why I want the child process to be detached. ... But, I'm new to modperl, and I think a fork doesn't copy the ... What it does is say that the parent isn't interested in ...
    (perl.beginners)