shared memory ?

Hi:

Can this fuction be uesd like this?:
fd=shm_open("/databuffer",O_RDWR|O_CREAT,S_IRWXU);
addr = mmap( 0,
262144,
PROT_READ|PROT_WRITE|PROT_NOCACHE,
MAP_PHYS|MAP_ANON,
fd,
0 );

because I want to alloc a physically contiguous memory for DMA transfer,and use this memory like shared memory that can be used between threads or processes.

We tried similar, as soon we put MAP_ANON into there was no access anymore, since it returns a memory pointer outside your process-space.
Also we did not yet manage using real DMA access for copying from host-cpu-memory into client-cpu-memory (which we woul like to do^^).

In your example there was one more function to call i think: mem_offset ();

Err, i forgot, yes it looks like a regular call to shm_open and mmap :stuck_out_tongue_winking_eye:

I believe you have to ftruncate() it before you can mmap(). Take a look of the sample code in Library Reference.

in fact my program step is like this( I refered to the sample code in shm_ctl() function .):
addr = mmap(0,sizeof(shmem_t),PROT_READ|PROT_WRITE|PROT_NOCACHE,MAP_PHYS|MAP_SHARED,NOFD,0);

if (addr == MAP_FAILED)
{
fprintf(stderr, “mmap failed : %s\n”, strerror(errno));
return EXIT_FAILURE;
}
mem_offset(addr,NOFD,1,&phy_addr,0);
fd=shm_open("/databuffer",O_RDWR|O_CREAT,/S_IRWXU/0);
if( addr == MAP_FAILED )
{
fprintf( stderr, “mmap failed: %s\n”,strerror( errno ));
return EXIT_FAILURE;
}
if ( shm_ctl( fd, SHMCTL_PHYS,phy_addr, sizeof(shmem_t) ) == -1 )
{
printf( “shm_ctl failed: %s\n”, strerror(errno) );
close( fd );
exit( EXIT_FAILURE );
}

when I run this program ,system appears error :“shm_ctl failed: Function not implemented”
I don’t konw what’s wrong.

I do miss something in here.

your call to mmap, is before shm_open and with parameter NOFD, this implements you would like to map physical memory. Therefore the last parameter must not be 0 (am i wrong). The last parameter can be retrieved through the pci_* functions for example with pci-cards.

You also do not have to use mem_offset() in this example (afaik).

The rest looks like it is written in the example code, but i am a bit confused about the phy_addr pointer ^^
(I will try that here with our card, may this wil also solve some of our problems ^^)

if you just want to have a 256K shared memory file, use your old code and as mentioned by xtang use ftruncate

fd=shm_open("/databuffer",O_RDWR|O_CREAT,S_IRWXU);
ftruncate(fd,262144);
addr = mmap( 0, 262144, PROT_READ|PROT_WRITE|PROT_NOCACHE, MAP_SHARED, fd, 0 );

The code shown:

  1. calls mmap() with invalid arguments
  2. calls shm_ctl() with a different physical address than that supplied to mmap() (which, as stated in 1, isn’t a valid invocation in any case).

If you want to map physical memory, you need to supply the same physical address to a correctly formed mmap() call, as well as to the shm_ctl() call. The code from the example works fine.

I want alloc a 2M shared memory file ,and this memory could be a physically contiguous memory for DMA transfer.In the sample code in library reference ,these two kinds of methods have respective specific.But I down how to make them coorperate each other in one function.

I’m sorry for my awful English. :blush:

I have made some changes in my code according to micro and rgallen’s advice.but the programme didn’t work too.Because I still don’t know the key point about the problem.

to map physical memory means you want to map memory on a special hardware (graphics card, client cpu ram, other pci/usb/whatever plugged in devices)
if you just need 2M shared RAM-memory try:

fd=shm_open("/databuffer",O_RDWR|O_CREAT,S_IRWXU);
ftruncate(fd,210241024);
addr = mmap( 0, 210241024, PROT_READ|PROT_WRITE|PROT_NOCACHE, MAP_SHARED, fd, 0 );

This should map your 2Megs of shared mem. For continous RAM try MAP_SHARED | MAP_ANON (but as i mentioned, we hat trouble with MAP_ANON)