From: Manfred Schlager (mschlager_at_go.com)
Date: 25 Jul 2004 08:02:09 -0700
Hello, everyone. I have looked everywhere for a solution but haven't
found one. It seems my problem is not the typical one, so if someone
knows of a good solution, I'd really appreciate some pointers.
This is the scenario. I am writing a daemon in C, which as expected,
will be providing a service. This daemon has to prepare some
information needed in servicing its clients. It does this by reading
a number of files which are then scanned and a dynamic structure
created with information about the contents of the files.
It is desired, almost required, that the daemon starts servicing
(listening on a socket) its clients as soon as it is started. The
obvious solution is then to create a child process that takes care of
the job of scanning the files and creating the structure. At the end,
the child must return (in some way) the whole structure to the parent,
which will spawn servicing childs as needed.
In fact, during the creation of the structure, the parent should
already be able to read it. Yes, the parent can use incomplete
information when servicing the clients, it just affects the quality of
the service, but it does not prevent it from giving it. BTW, only the
child writes to the structure, the parent only reads from it, so there
is no need for exclusive access.
Not giving enough thought to the matter, I imagined that just creating
the root of the dynamic structure before forking the child process
would be enough for both to be able to see the structure. When in
fact, of course, the child is working on a copy of the root, and all
the pointers are to its own memory space, so the parent isn't really
aware of the work of the child.
I've been reading about shared memory, but it seems it is not what I
need either. For two compelling reasons: 1- There is no way to
calculate the size of the structure prior to shmget'ting the required
block (that's why I'm using a dynamic structure in the first place,
duh!) 2- Even if there was, since there is no way to do malloc's on
this block of memory because it is already allocated, it means that I
would have to memory-manage the block myself (yikes!).
In reality, if the original block of shared memory requested by the
parent was not enough to store the structure in the child, it cannot
grow, and if it was too big, it would mean wasting memory. Again,
that is why I am using a dynamic structure.
Pipes don't seem to be an option either, because of the sequential
nature of them, passing the whole structure (pointers coded in some
way), is far worse a problem than just having the server load the
structure by itself prior to servicing any client. Unless, that is, I
had some way of packing all the memory space of the child in a
sequential block and pipe it to the parent.
Keeping the child running and have the parent (and its siblings) talk
to it to request information stored in the structure seems a far more
reasonable solution, but it means having a server within a server, and
it doesn't seem very attractive. It does seem the best solution
though, because later on, when forking the servicing children, init
will need to copy the whole structure for each forked child (and it is
Sorry for the length of this briefing, but I am trying to cover enough
detail so that you can understand the problem.
Thanks for any suggestions/comments you may have.