hey guys,
i'm in terrible with these shared memory.
I tried to write a basic module by looking at the examples that basic
module just holds a counter and prints it to the client.
when i compile the code attached, i got no error messages. But in apache
error.log file i got
lots of
[notice] child pid 32653 exit signal Segmentation fault (11)
and my module does not working.
if you have time to look at my code and send me your advices i'll be
appreciated. Because i'm getting crazy about these shared memory concepts.
I just want to define a global variable and process it in each request from
different sources.

Regards,
Oğuzhan TOPGÜL


On Tue, Nov 22, 2011 at 9:14 PM, Oğuzhan TOPGÜL <oguzhantop...@gmail.com>wrote:

> Thank you guys so much.
> What i want to do with shared memory is to hold the requester IPs and a
> counter that holds how many times an IP made request. I'm planning to hold
> them in a binary tree.
> I thought holding these IPs and counters in a file is slower than holding
> them in a shared memory because of the file I/O loss. And using binary tree
> as a data structure is going to make my search process faster and easier.
> Do you have any suggestions about shared memory-file usage or data
> structure usage.
>
> Regards
> Oğuzhan TOPGÜL
>
>
>
> On Tue, Nov 22, 2011 at 5:15 PM, Nick Kew <n...@apache.org> wrote:
>
>> On Tue, 22 Nov 2011 09:41:02 -0500
>> "Pranesh Vadhirajan" <vadhira...@teralogics.com> wrote:
>>
>> > Nick, can you suggest some of these higher-level abstractions, please?
>>  I have been trying to make a module of mine work with a POSIX shared
>> memory implementation, but I'm going nowhere with that.  Are you referring
>> to the apache shared memory implementation (apr_shm_...) or something else?
>>  Either way, if you could suggest what I should look into, it would be
>> greatly appreciated.
>>
>> apr_shm is the old way of doing it.
>>
>> Today I'd recommend looking at mod_slotmem, and the socache modules.
>> I used the latter for mod_authn_socache, which is a simple example.
>>
>> --
>> Nick Kew
>>
>
>
#include <stdio.h>
#include <stdlib.h>
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
#include <apr_shm.h>
#include <apr_global_mutex.h>
#include "util_mutex.h"
#include "http_log.h"  //APLOG_MARK, APLOG_ERR 

#include "apr.h"
#include "apr_strings.h"
#include "http_core.h"
#include "ap_config.h"

apr_shm_t *mod_basic_shm;            /* Pointer to shared memory block */ /* Shared memory structure */
apr_global_mutex_t *mod_basic_mutex; /* Lock around shared memory segment access */
static const char *mod_basic_mutex_type="mod-basic-mutex-shm";


typedef struct mod_basic_data{
    int count;
}counter_data;


static apr_status_t shm_cleanup_wrapper(void *unused){
    if(mod_basic_shm)
        return apr_shm_destroy(mod_basic_shm);
    return OK;
}


static int mod_basic_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp){
    ap_mutex_register(pconf, mod_basic_mutex_type, NULL, APR_LOCK_DEFAULT, 0);
    return OK;
}


static int mod_basic_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s){
    apr_status_t rs;
    counter_data *counter;
      
    //Create shared memory segment
    rs = apr_shm_create(&mod_basic_shm, sizeof(counter_data), NULL, pconf);
  
    //counter equals to zero
    counter=(counter_data*)apr_shm_baseaddr_get(mod_basic_shm);
    counter->count=0;
    
    //Create mutex
    rs=ap_global_mutex_create(&mod_basic_mutex, NULL, mod_basic_mutex_type, NULL, s, pconf, 0);
    
    //Destroy the shm segment when the configuration pool gets destroyed
    apr_pool_cleanup_register(pconf, NULL, shm_cleanup_wrapper, apr_pool_cleanup_null);
    return OK;
}

static void mod_basic_child_init(apr_pool_t *p, server_rec *s){
    apr_status_t rs;
    
    //open mutex to init child
    rs=apr_global_mutex_child_init(&mod_basic_mutex, NULL, p);
    exit(1);
    
}

static int basic_handler(request_rec *r)
{
   apr_status_t rs;
   counter_data *counter;
   
   if (strcmp(r->handler, "basic")) {
        return DECLINED;
    }
   
   //lock the global variable before proceed it
   rs=apr_global_mutex_lock(mod_basic_mutex); 
  
   //lets increase the counter
   counter = (counter_data*)apr_shm_baseaddr_get(mod_basic_shm);
   counter->count++;
   ap_rprintf(r,"%i",counter->count);
   
   // Unlock mutex after increase the counter
   rs=apr_global_mutex_unlock(mod_basic_mutex);
    
return OK;
}


static void basic_hooks(apr_pool_t *pool)
{
    ap_hook_pre_config(mod_basic_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_post_config(mod_basic_post_config, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_child_init(mod_basic_child_init, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_handler(basic_handler, NULL, NULL, APR_HOOK_MIDDLE);
}


module AP_MODULE_DECLARE_DATA basic_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
basic_hooks
};

Reply via email to