The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shared memory for shared library otheus High Level Programming 0 09-03-2008 04:55 AM
Shared memory in shared library DreamWarrior High Level Programming 12 05-30-2007 05:33 PM
memory sharing - not shared memory - elzalem High Level Programming 9 05-02-2007 08:45 AM
Shared memory shortage but lots of unused memory cjcamaro UNIX for Advanced & Expert Users 1 10-13-2004 06:10 PM
Shared memory joseph_shibu High Level Programming 1 10-24-2001 09:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-25-2008
hurricane86 hurricane86 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
[C] Problems with shared memory

Hi everbody, i have a problem with shared memory and child-processes in C (unix).

I have a server that do forks to create (N) child processes. This processes work with a shared "stuct" thanks to shared memory and a semaphore. The problem is when a child modify the shared memory and the others can't see its changes...but if i done it in the main process it work fine.

finally, the question is:

can a child-process "realloc" a shared memory and the others can see it?

Sorry for my bad english, thanks a lot bye.
  #2 (permalink)  
Old 11-25-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,784
From the POSIX specification for realloc:
Quote:
The realloc() function shall change the size of the memory object pointed to by ptr to the size specified by size. The contents of the object shall remain unchanged up to the lesser of the new and old sizes. If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed.
This means the child copy of the pointer is no longer referencing valid memory.
The minimum shared memory segment size is a page - 4096 bytes typically. Simply allocate several pages of memory, park the struct and other data in there and do not "realloc".
  #3 (permalink)  
Old 11-26-2008
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,960
Quote:
Originally Posted by hurricane86 View Post
Hi everbody, i have a problem with shared memory and child-processes in C (unix).

I have a server that do forks to create (N) child processes. This processes work with a shared "stuct" thanks to shared memory and a semaphore. The problem is when a child modify the shared memory and the others can't see its changes...but if i done it in the main process it work fine.

finally, the question is:

can a child-process "realloc" a shared memory and the others can see it?

Sorry for my bad english, thanks a lot bye.
Once new processes fork off, their heap and stack spaces are not shared. In fact, neither are their memory mappings -- changing the mapping of one process won't change the mapping in the others, so you can't enlarge the memory in one and expect the others to mirror it. So somehow, they all have to have the right mapping in the first place.

One way this could work is to mmap() a file into memory with the MAP_SHARED flag, and give the mapping a much longer length than the length of the file. It will let you do this, it'll just give you a bus error signal when you use memory outside the bounds of the file. You can extend the size of the file with truncate() when you need to. You'll need to remember which bits of file/memory are being used by yourself.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0