On Thu, Dec 11, 2008 at 19:55, Ray Morris <[email protected]> wrote:
> On 12/10/2008 02:09:19 AM, Sorin Manolache wrote:
>
>> I would propose the following approach:
>>
>> Hook post_config. This one is executed once by the parent process
>> before any child is spawned. Therefore, any resource opened there is
>> inherited by all child processes. Thus, I would create _shared_
>> memory
>> structures (see apr_shm.h, not malloc).
> ...
>> Next, in child_init, _attach_ to the shared memory segment (see
> ...
>> I hope this helps. You can contact me for a code sample
>> if you want. I don't have it handy for the moment, but
>> I can find it.
>
> I've seen several variations of this same
> question posted, so any sample code you may be
> able to locate may be very helpful to a lot of
> people. I'm sure I'll need it at some point,
> so I'd file it for future use. With your permission
> I'd post it somewhere for Google to find when people
> want to know about ways of sharing data in Apache.
Attached.
Sorin
>
> On 12/10/2008 02:09:19 AM, Sorin Manolache wrote:
>> On Wed, Dec 10, 2008 at 06:00, Jayasingh Samuel
>> <[email protected]> wrote:
>> >
>> >
>> > I have 100's of files stored in different directories and i load
>> all
>> these in the memory in arrays and hash table. and i want to reload
>> the
>> memory automatically using some handlers. When i do this in only the
>> particular child thread is having the updated one and the other
>> request are showing me the old Datas.
>> >
>> > 1. The shared memory to store all the 100's of files in array and
>> hash table which is dynamically malloced and stored will be too
>> costly
>> also the synchronization.
>> > IS there any other way we can overcome this.
>> >
>> > 2. Is there any way a handler can directly access the parent
>> process, updating the memory and removing the child process which has
>> the old Data and spawning the new child process. Can we use the
>> mod_balancing technique of blocking the request to the server and
>> then
>> update the parent and kill the child and spawn new childrens with the
>> updated memory.
>> >
>> > Please guide me with you ideas.
>>
>> Killing processes at each update is not efficient.
>>
>> I would propose the following approach:
>>
>> Hook post_config. This one is executed once by the parent process
>> before any child is spawned. Therefore, any resource opened there is
>> inherited by all child processes. Thus, I would create _shared_
>> memory
>> structures (see apr_shm.h, not malloc).
>>
>> Beware, post_config is run every time the server configuration is
>> reloaded. Therefore, such shared memory structures would be created
>> after each config reload. In order to avoid eating up the system's
>> resources, you'll have to make sure that each created structure is
>> also destroyed. In order to do this, _register_ a function (see
>> apr_pool_register_cleanup) that is called when the conf pool is
>> destroyed. This function would destroy the shared memory structures.
>>
>> Next, in child_init, _attach_ to the shared memory segment (see
>> apr_shm.h). From then on, the shared memory structures are shared
>> among all child processes. Each change is visible instantly in all
>> processes.
>>
>> Obviously, think of synchronisation. When not every atomic change
>> results in a consistent state of the shared data, you'll have to
>> protect the shared data with mutexes. See apr_thread_mutex.h and
>> apr_thread_rwlock.h and maybe others. Mutexes should be created in
>> post_config too.
>>
>> I hope this helps. You can contact me for a code sample if you want.
>> I
>> don't have it handy for the moment, but I can find it.
>>
>> S
>>
>>
>> --
>> A: Because it reverses the logical flow of conversation.
>> Q: Why is top-posting frowned upon?
>> A: Top-posting.
>> Q: What is the most annoying thing in e-mail?
>>
>>
>
>
>
--
A: Because it reverses the logical flow of conversation.
Q: Why is top-posting frowned upon?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
#include <httpd.h>
#include <http_log.h>
#include <http_config.h>
#include <unistd.h>
#include <apr_errno.h>
#include <apr_pools.h>
#include <apr_shm.h>
typedef int shared_struct_t;
static apr_shm_t *shm;
static shared_struct_t *shared_object;
static apr_status_t
conf_pool_cleanup(apr_shm_t *shm) {
return apr_shm_destroy(shm);
}
static int
post_config(apr_pool_t *conf_pool, apr_pool_t *log_pool,
apr_pool_t *temp_pool, server_rec *s) {
apr_status_t r;
r = apr_shm_create(&shm, sizeof(shared_struct_t), NULL, conf_pool);
if (APR_SUCCESS != r)
return (int)r;
shared_object = (shared_struct_t *)apr_shm_baseaddr_get(shm);
apr_pool_cleanup_register(conf_pool, shm,
(apr_status_t (*)(void *))&conf_pool_cleanup,
NULL);
return OK;
}
static int
handler(request_rec *r) {
if (NULL != r->args)
*shared_object = atoi(r->args);
else
ap_rprintf(r, "%d\n", *shared_object);
return OK;
}
static int
log_transaction(request_rec *r) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "PID: %d", getpid());
return OK;
}
static void
register_hooks(apr_pool_t *p) {
ap_hook_post_config(post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_handler(handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_log_transaction(log_transaction, NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA shm_example_module = {
STANDARD20_MODULE_STUFF,
NULL, // create_dir_config
NULL, // merge_dir_config
NULL, // create_server_config,
NULL, // merge_server_config
NULL, // cmds
register_hooks
};
/*
Put
<Location /shm>
</Location>
in /etc/apache2/httpd.conf.
Continuously get
http://host_name/shm
From time to time, in a different terminal, get
http://host_name/shm?integer_value
See how the continuously running requests retrieve
the newly set integer_value.
Check the error logs to see that the requests were served by different
apache processes.
*/