LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-19-2005, 07:17 AM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
Shared memory question


This question is aimed at anyone who has experience using X/OPEN shared memory.

I notice that under redhat 9, If I create a block of shared memory in one program and then attempt to access that memory with another program, the entire block has been shifted back by 8192 bytes. This doesn't happen in FreeBSD

Is this behaviour standardised?
 
Old 02-21-2005, 09:41 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
I'm not sure what you are seeing. Processes can access memory using an absolute physical address, or with a virtual address. There is no standard that I know about that says you have to use one or the other to access shared memory.

More to the point why is this a problem - it sounds like you developed code with the expectation that you had physical addresses. Is the Linux version of your coe not working?
 
Old 02-21-2005, 04:57 PM   #3
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
yeah, I have a bunchof pointers that get messed up when the shift occours.
 
Old 03-02-2005, 02:10 AM   #4
nkhambal
LQ Newbie
 
Registered: Mar 2005
Posts: 8

Rep: Reputation: 1
Hi,

Sorry if I am stepping on your question. But I am also seeing the similar problem. I have a C program that forks multiple processes. Basically its a program that accepts TCP network connetions and spawns a one process for each connection.

When the first new connection is arrived parent process allocates a shared memory and stores the connection info such as src/dst ip, src/dst ports and process that is handling the connection.

The data structure also has a next pointer which points to the next connection entry which is created when subsequent connections arrive. Basically it creates a linked list of connections.

I have a code in program which prints this list but somehow I am not able to get it printed. The list goes in infinite loop with next pointer of my first node pointing back to itself. Now I am sure I am coding it right way. When I tried to print the pointers returned by a function that allocates shared memory, I found one problem. Following is the output of program for 5 connections that are received.

Quote:
*flow: 0x40019000
Head flow Added
startXlistener : PID-30711: Display TCP Connection from Source 101.0.0.5:43217 Destination 192.168.0.4:6003 Socket: 5
*flow: 0x4001a000
Flow Added
startXlistener : PID-30712: Display TCP Connection from Source 101.0.0.5:43218 Destination 192.168.0.4:6003 Socket: 5
startXlistener : PID-30712: Display TCP Connection disconnected on Socket: 5
*flow: 0x4001b000
Flow Added
startXlistener : PID-30713: Display TCP Connection from Source 101.0.0.5:43219 Destination 192.168.0.4:6003 Socket: 5
*flow: 0x4001c000
Flow Added
startXlistener : PID-30714: Display TCP Connection from Source 101.0.0.5:43220 Destination 192.168.0.4:6003 Socket: 5
startXlistener : PID-30713: Display TCP Connection disconnected on Socket: 5
*flow: 0x4001d000
Flow Added
startXlistener : PID-30715: Display TCP Connection from Source 101.0.0.5:43221 Destination 192.168.0.4:6003 Socket: 5
startXlistener : PID-30714: Display TCP Connection disconnected on Socket: 5
Flow pointer is the pointer return with "shmat()". I maintain a list (an array) which stores the id to each shared memory allocation (returned via "shmget()"). In my print function when i try to attach to the first memory pointer (head), I see that I am getting attached to the second pointer.

following is my print function code.

Code:
void printFlow()
{
  flow_t *flow=NULL;
  int i,flowid;
  key_t key=1112;
  
  flow = (flow_t *)shmat(shareddb->flowdbid[0],NULL,SHM_RDONLY);
  
 if (flow == (flow_t *)(-1))
  {
    perror("\nshmat:printdb");
    terminateall(__FUNCTION__);
    exit(1);
  } else {
    memset(debugstr,'\0',sizeof(debugstr));
    sprintf(debugstr,"Shared memory pointer acquired for flow database: address = 0x%08x\n",&flow);
    dbgtrace(__FUNCTION__,debugstr);
  }
  
  printf("\nheadptr : 0x%08x\n",flow);
  if (flow==NULL)
  {
    fprintf(stderr,"\nNo Connections.\n");
    return;
  }
  
  fprintf(stderr,"\nTCP Display connections database\n");
  fprintf(stderr,"Received %d Active %d \n",shareddb->displayreceived,shareddb->displayactive);
    
  while (flow != NULL)
  {
    fprintf(stderr,"PID: %d From: %s:%d To: %s:%d \n",flow->pid,flow->srcIP,flow->srcPort,flow->dstIP,flow->dstPort);
    if (flow == flow->next)
    {
		printf("\nlooped\n");
		return;
    } else {
		flow=flow->next;
		continue;
	}
  }
Following is a the connection data stucture.
Code:
typedef struct _flow_t flow_t;

typedef struct _flow_t
{
	pid_t pid;
	CARD8 srcIP[20];
	CARD16 srcPort;
	CARD8 dstIP[20];
	CARD16 dstPort;
	flow_t *next;
}flow_t_;
Following the output of printflow function.
Quote:
headptr : 0x4001a000

TCP Display connections database
Received 5 Active 2
PID: 30711 From: 101.0.0.5:43217 To: 192.168.0.4:6003

looped
As you can see the the headptr in printFlow() function (0x4001a000) return by reattaching to the head shared memory is different than the one actually returned by shmat when it is first created(0x40019000). Instead its a second pointer.

I am not an expert on shared and I must admit I am coding shared memory for the first time. Can some one let me know where the problem is.?

Thanks,
 
  


Reply



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
is shared memory expandable in memory size? Thinking Programming 4 08-16-2005 09:57 AM
c + shared memory dilberim82 Programming 3 03-07-2005 07:49 PM
shared memory blackzone Programming 1 10-14-2004 11:52 AM
About Shared Memory...? aegis_shiva Programming 1 08-25-2004 05:05 AM
shared memory socket9001 Programming 4 02-06-2004 02:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration