LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 11-16-2011, 04:21 PM   #1
programlight
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Rep: Reputation: 0
Apache 2.2.3 shared memory implementation


Hello Everyone,
I am trying to understand how to implement sharing of objects in memory between various processes in apache. I have been using the routines defined in <sys/mman.h> to implement the shared memory mapping functionality. However, I am not seeing any actual sharing of the objects in my output.

I have attached the code segment of my module containing the request handler:

Code:
#include "httpd.h"
#include "http_core.h"
#include "http_config.h"
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include "sessions.h"
#include <regex.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


void set_shared_region(int *shared_int)
{
   int fd;

   /* Create shared memory object and set its size */
   fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
   if(fd == -1)
      fprintf(stderr,"shm_open error");

   if(ftruncate(fd, sizeof(int)) == -1)
       fprintf(stderr,"ftruncate error");

    /* Map shared memory object */
   shared_int = mmap(NULL, sizeof(int),PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
   if(shared_int == MAP_FAILED)
      fprintf(stderr,"mmap error");
}

int start_mutex()
{
   if(initial_mutex_value == 0)
   {
      if(sem_init(&session_mutex,1,1) < 0)
      {
         fprintf(stderr,"error initializing semaphore");
         return 0;
      }
      initial_mutex_value = 1;
   }
   return 1;
}

static int counter_handler(request_rec* r) 
{
   char time_buffer[30]; 
   char entry[1024];
   long int tid,pid;
   apr_ctime(time_buffer,r->request_time);
   start_mutex();
   sem_wait(&session_mutex);
   set_shared_region(&ctr);
   ctr++;
   sem_post(&session_mutex);
   //tid = (long int)getthreadid();
   pid = (long int)getpid();
   sprintf(entry,"counter: %i, thread: %ld, process: %ld, request: %s,  time: %s;",ctr,(long int)0,pid,r->the_request,time_buffer);
   tempToDB(entry);

   return DECLINED;
}

static void counter_register_hooks (apr_pool_t *p)
{
   ap_hook_handler(counter_handler, NULL, NULL, APR_HOOK_REALLY_FIRST);
}

module AP_MODULE_DECLARE_DATA counter_module = 
{
  STANDARD20_MODULE_STUFF,
  NULL,//uvds_metrics_dir_conf,      /* Per-Directory Configuration */
  NULL,//uvds_metrics_dir_merge,           /* Directory Config Merger */
  NULL,//uvds_metrics_server_conf,         /* Per-Server Configuration */
  NULL,//uvds_metrics_server_merge,  /* Server Config Merger */
  NULL,//uvds_metrics_cmds,                /* Command Table (Directives) */
  counter_register_hooks             /* Registering Hooks */
};

The handler code uses the variable ctr which is defined in the header file “sessions.h” . Here’s the relevant segment in sessions.h:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libpq-fe.h"
#include <semaphore.h>

sem_t session_mutex;
int initial_mutex_value = 0;
int ctr = 0;

void tempToDB(char *entry);


Here’s the output of my code:

Quote:
"counter: 1, thread: 0, process: 18194, request: POST /login?destination=login HTTP/1.1, time: Wed Nov 16 19:56:04 2011;"
"counter: 2, thread: 0, process: 18194, request: POST /login?destination=login HTTP/1.1, time: Wed Nov 16 19:56:04 2011;"
"counter: 3, thread: 0, process: 18194, request: GET /login HTTP/1.1, time: Wed Nov 16 19:56:05 2011;"
"counter: 4, thread: 0, process: 18194, request: GET /login HTTP/1.1, time: Wed Nov 16 19:56:05 2011;"
"counter: 5, thread: 0, process: 18194, request: GET /dashboard HTTP/1.1, time: Wed Nov 16 19:56:06 2011;"
"counter: 6, thread: 0, process: 18194, request: GET /dashboard HTTP/1.1, time: Wed Nov 16 19:56:06 2011;"
"counter: 7, thread: 0, process: 18194, request: GET /viewfeeds HTTP/1.1, time: Wed Nov 16 19:56:07 2011;"
"counter: 8, thread: 0, process: 18194, request: GET /viewfeeds HTTP/1.1, time: Wed Nov 16 19:56:07 2011;"
"counter: 9, thread: 0, process: 18194, request: GET /viewfeeds/dosort&sortby=none HTTP/1.1, time: Wed Nov 16 19:56:07 2011;"
"counter: 10, thread: 0, process: 18194, request: GET /viewfeeds/dosort&sortby=none HTTP/1.1, time: Wed Nov 16 19:56:07 2011;"
"counter: 1, thread: 0, process: 18201, request: GET /misc/viewfeeds/images/sidebar_05.png HTTP/1.1, time: Wed Nov 16 19:56:07 2011;"
"counter: 2, thread: 0, process: 18201, request: GET /misc/viewfeeds/images/sidebar_05.png HTTP/1.1, time: Wed Nov 16 19:56:07 2011;"
"counter: 11, thread: 0, process: 18194, request: GET /recordings HTTP/1.1, time: Wed Nov 16 19:56:08 2011;"
"counter: 12, thread: 0, process: 18194, request: GET /recordings HTTP/1.1, time: Wed Nov 16 19:56:08 2011;"
"counter: 13, thread: 0, process: 18194, request: GET /cop HTTP/1.1, time: Wed Nov 16 19:56:09 2011;"
"counter: 14, thread: 0, process: 18194, request: GET /cop HTTP/1.1, time: Wed Nov 16 19:56:09 2011;"
"counter: 15, thread: 0, process: 18194, request: GET /logout HTTP/1.1, time: Wed Nov 16 19:56:10 2011;"
"counter: 16, thread: 0, process: 18194, request: GET /logout HTTP/1.1, time: Wed Nov 16 19:56:10 2011;"
"counter: 17, thread: 0, process: 18194, request: GET / HTTP/1.1, time: Wed Nov 16 19:56:11 2011;"

As you can see, the counter is incremented within the same process correctly, but is not shared among different processes. Sorry, for the overly verbose message, but can anybody tell me how to fix the sharing of my variable ctr so that it is shared among all process?


Respectfully,
Pranesh

Last edited by programlight; 11-16-2011 at 04:40 PM.
 
Old 11-16-2011, 07:04 PM   #2
programlight
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Fixed the problem. I needed to the signature of the set_shared_region function to :set_shared_region(int **shared_int) and then change the call to the function accordingly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a shared memory and some shared objects in linux? jeremy28 Programming 6 06-11-2010 05:21 AM
Shared memory C++ implementation pankajdev Linux - General 0 07-16-2009 12:18 AM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 04:05 AM
wireless shared internet service: which authentication implementation? F1uX Linux - Security 3 01-21-2004 06:43 AM


All times are GMT -5. The time now is 08:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration