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 08-02-2015, 06:31 PM   #1
dantetezza
LQ Newbie
 
Registered: Aug 2015
Posts: 3

Rep: Reputation: Disabled
Utilizing shared memory (posix)


Hello,


I am trying to create a program that access a shared memory area but doesnt know its size. At the beginning of the area, there is a variable(int) that contains the full area size. So I am trying to map enough space to read the total size, and remap the whole area. I am using POSIX (realtime) shared memort functions: shm_open(), ftruncate(), mmap(), munmap(), shm_unlink().
The problem is that if I truncate a small area (enough to read the full size), and than truncate the file descriptor again with its new size, the previous content of the shared memory area gets overwritten with 0.
I could truncate a bigger area than the shared memory possibly could be (limiting the maximum size of it), read the size, and than truncate (with a smaller size this time). It works, but I dont think this is an elegant solution since I am maping a big memmory area, just to read few bytes and than unmaped it.

Follow a simplified version of the code to express what I intent to do. I am using the solution I mentioned above (using a bigger area):

int shm_id;
char *memory;
int full_size;

shm_id=shm_open(name, O_RDWR , 0777);
if(shm_id==-1)
{
….
}
ftruncate(shm_id,MAX_SIZE);
memory=(char *)mmap(NULL,MAX_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_id, 0);

full_size=*(int*)&memory[0];
munmap(shm_id, MAX_SIZE);

ftruncate(shm_id, full_size);
memory=(char *)mmap(NULL,full_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_id, 0);


Has anyone tried to do something similar? Any suggestions? I also tried closing the shared memory descriptor and opening again. But also didnt work. I am running under OpenSuse 13.2.
Thank you guys,
Dante
 
Old 08-03-2015, 12:20 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
ftruncate should be called only once, after the very first creation of share memory
 
Old 08-03-2015, 05:53 PM   #3
dantetezza
LQ Newbie
 
Registered: Aug 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
So,do I only need to truncate on the process where I create the shared memory? And not on processes where I access an already existing one?

Thank you for the quick response NevemTeve!
 
Old 08-03-2015, 10:32 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That means you have to know if it was a first creation or not.
 
Old 08-04-2015, 04:25 PM   #5
dantetezza
LQ Newbie
 
Registered: Aug 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
It is solved, I did some studying and I believe I understood the concept.
Thankou again NevemTeve.
 
Old 08-04-2015, 04:49 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Your logic should be as follows:

(1) Attempt to open the shared-memory segment by name.
(2) If (RC == -1) the segment does not exist, then you are the first: create it, then "ftruncate()" it to its maximum size.
(3) In any case, map the memory, or some appropriately-sized "window" thereunto, into each process.
 
1 members found this post helpful.
Old 08-05-2015, 11:43 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That, and some synchronization to avoid race conditions:
Code:
wait-semaphor
use-exisiting-shmem OR create-and-initialize-shmem
send-semaphor
 
  


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
Posix shared memory usage devilszone Linux - Hardware 2 12-31-2010 12:14 PM
distributed posix shared memory hrmb Linux - Hardware 0 01-01-2010 04:27 PM
posix shared memory regions caverdave Linux - Newbie 2 07-09-2009 09:00 AM
POSIX Shared Memory noiz354 Linux - Hardware 1 02-27-2009 01:10 PM
POSIX shared memory confusion MeMooMeM Programming 0 05-25-2005 04:32 PM

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

All times are GMT -5. The time now is 10:23 PM.

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