Welcome to MacForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Shared memory

 
   Macintosh computer (Home) -> General Discussion RSS
Next:  ?  
Author Message
Jan E. Schotsman1

External


Since: Feb 18, 2004
Posts: 9



(Msg. 1) Posted: Fri Aug 06, 2004 6:54 am
Post subject: Shared memory
Archived from groups: comp>sys>mac>programmer>misc (more info?)

Hello all,

If you're not on the beach maybe you can give me a little help.
I need to implement shared memory between my application and a helper
tool.
At first I tried CFMessageProts. This works, but if you transport huge
amounts of data the data get all paged out and after a few GB I crash.
Then I tred shmget and friends. This works, but I can get only 1-2 MB of
memory this way. I need at least 8MB and I would prefer no limit atall.
So now I am trying mapping a file but I can't get this to work.
The following bare-bones XCode Carbon application crashes. Please tell
me what is wrong.

Jan.


#include <sys/types.h>
#include <sys/uio.h>
#include <sys/fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

#define kBufferSize 960000

int main( void )
{
char sharedMemPath[] = "/Users/jeschot/Documents/Shared_memory_file_1";

int fd = open( sharedMemPath, O_CREAT+O_WRONLY, 0666 );
Ptr videoBuffer, buffer;
int size;
long x = 10;


printf( "\nfd = %d\n", fd );
if ( fd != -1 )
{ // Set size of shared file by writing to it and closing it
buffer = malloc( kBufferSize );

size = write( fd, buffer, kBufferSize );

close( fd );
free( buffer );
}

fd = open( sharedMemPath, O_WRONLY, 0666 ); // Reopen

printf( "fd = %d\n", fd );

if ( fd != -1 )
{ // Map entire file to memory
videoBuffer = mmap( 0, kBufferSize, PROT_WRITE, MAP_FILE, fd, 0 );
printf( "videoBuffer = 0x%lx", (long)videoBuffer );

if ( -1 == (int)videoBuffer )
{
int error = errno;
fprintf( stderr, "mmap error: %d (0x%08x)\n", error, error );
}
else videoBuffer[0] = x; // Crash
}
printf( "finished" );


return 0;
}

 >> Stay informed about: Shared memory 
Back to top
Login to vote
Sean McBride

External


Since: Jan 05, 2004
Posts: 311



(Msg. 2) Posted: Fri Aug 06, 2004 12:55 pm
Post subject: Re: Shared memory [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article ,
"Jan E. Schotsman" <j_e_schotsman RemoveThis @compuserve.com> wrote:

 > So now I am trying mapping a file but I can't get this to work.

Maybe this will be useful:

<http://developer.apple.com/samplecode/MapLargeFile/MapLargeFile.html>

 >> Stay informed about: Shared memory 
Back to top
Login to vote
Jan E. Schotsman1

External


Since: Feb 18, 2004
Posts: 9



(Msg. 3) Posted: Fri Aug 06, 2004 2:54 pm
Post subject: Re: Shared memory [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Sean McBride wrote:

Maybe this will be useful:

 >
 > <http://developer.apple.com/samplecode/MapLargeFile/MapLargeFile.html>

No, it only shows reading, not writing to the mapped file ;-)

Jan.
 >> Stay informed about: Shared memory 
Back to top
Login to vote
Michael Rice2

External


Since: Aug 07, 2004
Posts: 2



(Msg. 4) Posted: Sat Aug 07, 2004 3:50 pm
Post subject: Re: Shared memory [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Jan E. Schotsman wrote:
 > Hello all,
 >
 > If you're not on the beach maybe you can give me a little help.
 > I need to implement shared memory between my application and a helper
 > tool.
 > At first I tried CFMessageProts. This works, but if you transport huge
 > amounts of data the data get all paged out and after a few GB I crash.
 > Then I tred shmget and friends. This works, but I can get only 1-2 MB of
 > memory this way. I need at least 8MB and I would prefer no limit atall.
 > So now I am trying mapping a file but I can't get this to work.
 > The following bare-bones XCode Carbon application crashes. Please tell
 > me what is wrong.
 >
 > Jan.
 >
 >

As for the shmget and shmat services, did you use the sysctl command to
check the maximums set for the kernel.

Check the output of: sysctl -a | grep shm

the kern.sysv.shmmax parameter is the maximum size of a segment.

The following code tests the shm services - pass the size on the command
line:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <stdio.h>

int main(int argc, char** argv)
{
int key = 0x1000;
int id;
void* sharedmem;

if(argc != 2)
{
printf("Usage: shmtest <size>\n");
return 0;
}

int size = atoi(argv[1]);
if(size <= 0)
{
printf("Invalid size: %d\n", size);
return 0;
}

id = shmget(key, size, IPC_CREAT|SHM_R|SHM_W);
if(key > 0)
{
sharedmem = shmat(id, 0, 0);
if(sharedmem != (void*)-1)
{
printf("Success\n");

shmdt(sharedmem);
}
else
{
printf("Failed shmat - %d\n", errno);
}

shmctl(id, IPC_RMID, 0);
}
else
{
printf("Failed shmget - %d\n", errno);
}

return 0;
}
 >> Stay informed about: Shared memory 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Installation routine and shared libraries ? - Hi, I am struggling with my installation routine while trying to put my application + shared lib tree to any place the user wishes. It should be easily tried out and similary easy removable, but I cannot use static linked application. Assume my install...

shared object unloading order trouble - Hi, I have detected a different behaviour between Windows and Mac OS X shared object unloading order. Using a static class object for each shared object showed me the order of loading and unloading, because I print out simple messages of their..

CopyDeepMask corrupts memory in 8.1? - I'm using a simple CopyDeepMask call to blit a transparent image from an offscreen GWorld to the screen. It uses a srcCopy transfer mode and a NULL maskRgn. The code works fine on all systems that I have access to, which is 7.6, 9.2, and 10.2. One bet...

auto check memory or other handle error for mac - who know what tool that auto check memory leak or array bounds read/write or Unhandled Exception for mac osx?This tool function same as purify for mac.

modify OS X keyboard mappings - Hello, I need to have access to the french characters. I can do this with the Canadian - CSA keyboard. However, that keyboard has a "," as a decimal. I need a ".". Changing it in the international control panel does not change...
   Macintosh computer (Home) -> General Discussion All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]