LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 05-09-2008, 01:58 PM   #1
Spirals
LQ Newbie
 
Registered: Jun 2006
Posts: 28

Rep: Reputation: 15
Question how can I repeatedly attach a shared memory segment at the same address?


I am porting code so I am stuck with the fundamental logic.

A shared segment is created in the first program that has a linked list with data, next and back pointers. The first program loads data into the chain list (there are a number of different lists), and before it exits it print out the all the data elements of all the lists to prove they are loaded OK. This works.

When the second program starts up it attaches the shared memory segment
and tries to walk the pointers printing out the stored data. THIS FAILS.

I have determined that the shared memory segment attaches at a different address each time. I conclude the pointer chain points to junk. It also gives "shmatt failed: error no 22, error message: Invalid argument" with line 18 below as the shmat line.

I have the following test rig that shows the requested address is as requested but if I call it twice it fails the second time.

Any suggestions please.

1 #include <stdio.h>
2 #include <sys/shm.h>
3 #include <errno.h>
4 #include <string.h>
5
6
7
8 int main()
9 {
10 int shm_id, ret;
11 void *shm_address;
12
13 /* Get the shared memory segment using MY_SHM_ID */
14 shm_id = shmget( 999, 0, 0 );
15
16 if ( shm_id >= 0 )
17i {
18 shm_address = shmat (shm_id, (void *) (SHMLBA * 0xB7E3B), SHM_RND);
19
20 if ( (int) shm_address != -1 ) {
21
22 printf( "Shared memory was attached in our address space at %p\n", shm_address);
23
24 ret = shmdt( shm_address );
25
26 if (ret == 0)
27 {
28 printf("Successfully detached memory\n");
29
30 } else {
31
32 printf("Memory detached Failed (%d)\n", errno);
33
34 }
35
36 } else {
37
38 printf ("DEBUG: shmatt failed: error no %d, error message: %s\n", errno, strerror(errno));
39
40 }
41
42 } else {
43
44 printf( "Shared memory segment not found.\n" );
45
46 }
47
48 return 0;
49 }
50
~
 
Old 05-09-2008, 02:56 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by Spirals View Post
A shared segment is created in the first program that has a linked list with data, next and back pointers. The first program loads data into the chain list (there are a number of different lists), and before it exits
What keeps the shared segment in existence when the first program exits?

You made it sound like the first program exits before the second program starts. But maybe I misunderstood you.
 
Old 05-09-2008, 04:08 PM   #3
Spirals
LQ Newbie
 
Registered: Jun 2006
Posts: 28

Original Poster
Rep: Reputation: 15
more details

You are correct. The first process dies before the second one starts. Once the shared memory segment is created it is persistent in memory unless the machine crashes or its removed with ipcrm -m xxx, so it is still there when process two starts.

The first process runs infrequently to load new data and create a new image.

Another background process (process no 3) saves the image of the shared memory to a file every 20 seconds. this is started by process two.

The second process will load the image into shared memory if it is not there already on startup that is before it starts process 3.

At the moment while I am testing. when I start the second process it sees a valid shared memory image (correct key etc) so it uses that.

The first linked list process two trys to read PAT has a known address fixed realative to the start of shared memory address shm_address.
Process two then executes the following:

psrs *current, pat_head;
pat_head = shm_address + PAT_OFFSET;
printf(".......", pat_head);
current = pat_head;
while (current!=(pars *) NULL)
{
printf(".......", current->p_last, current, current->next);
printf("........",p_data);

current = current->par_next;
}
The PAT is a linked list of psrs [psr structures] .

What I see is first time round the loop it correctly prints last as NULL, and current as a new pat_head, but current->next point to some odd-ball address, and it then drops through the bottom of the while.

Any suggestions?
 
Old 06-07-2008, 06:26 AM   #4
wellison123
LQ Newbie
 
Registered: Jun 2008
Posts: 1

Rep: Reputation: 0
The shmat() call failing with "Invalid argument" is most likely due to something else having already been mapped into the same virtual address range (from the attach address plus the size of the segment - 1) to which you are attempting to attach the shared memory segment. One solution is to force the initial attach of the shared memory to a high virtual address that should be vacant in each process when it comes times for each process to attach. The process which creates the shared memory segment first creates/attaches a temporary shared memory segment of a particular size, then creates/attaches the actual shared memory segment for your application, then finally detaches/destroys the temporary shared memory segment. The application shared memory segment should have been attached at an address after the temporary segment (higher). The creating process stores the attached address in the shared memory segment for the other processes to use in their shmat() calls.

You can get an idea of how large to make the temporary shared memory segment (how high you need to push the application shared memory segment) by inspecting the virtual address mappings of your application processes using pmap to find out their highest mappings when it comes time to attach.

I've never seen this problem on Solaris, HP-UX or AIX. I haven't been bothered yet to figure out why this is, but my guess is that they map shared memory segments into a particularly high region of the virtual address space, out of range of the normal heap growth. Linux seems to map them to the next available address.
 
  


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
ORA-24123: unable to attach to shared memory segment yrkhond Linux - Newbie 1 03-17-2008 07:17 PM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 04:05 AM
getting the virtual address of the .data segment ravi Linux - Newbie 0 01-31-2006 01:35 AM
Creating Ora10g database on FC2 gives unable to create shared memory segment.... deanbrown3d Linux - Software 1 10-23-2004 11:40 AM
Identify a shared memory segment gremlin2000 Linux - General 3 04-03-2004 11:19 AM


All times are GMT -5. The time now is 02:54 AM.

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