Re: block size limit in mmap()
Re: block size limit in mmap()
- Subject: Re: block size limit in mmap()
- From: Terry Lambert <email@hidden>
- Date: Tue, 11 Apr 2006 15:29:43 -0700
On Apr 11, 2006, at 11:47 AM, Roberto Toro wrote:
I'm trying to make some processes share a quite large block of memory
(~8MB). I started using shmget(). It worked well for files ~800KB but
not for 8MB. So I moved to shm_open() and mmap(), and now I can't
allocate more than a single page of memory, 4096 bytes !! This code
shows the problem:
size_t len=4096;
[ ... ]
Any help/hint/code-snippet will be *very* appreciated.
It's unreported compilation warnings that are biting you; the minimum
change required is:
off_t len = 4096;
since the length argument to mmap() and ftruncate() is an off_t (64
bits), not a size_t (32 bits).
The variable fails to be automatically coerced because you don't have
the ftruncate() prototype in scope, since you failed to include
<unistd.h>. You should also include <string.h> for the prototype for
the strerror() function.
Here are the warning flags I tend to use when compiling my own code to
catch things like this in the compiler:
CFLAGS= -Wall -Wno-format-y2k -W -Wstrict-prototypes -Wmissing-
prototypes \
-Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -
Wswitch \
-Wshadow -Wcast-align -Wbad-function-cast -Wchar-subscripts -Winline \
-Wnested-externs -Wredundant-decls -Werror
The last option makes warnings a fatal compilation error; in this
case, you'd also need to have <sys/cdefs.h> (which is pretty much
automatic) and add the __unused attribute before the argc/argv
parameter declarations, since your code doesn't use them. Here's a
compilation warning free version of the code that works like you want
it to work:
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int
main(__unused int argc, __unused char *argv[])
{
int err, fd;
off_t len = 4097;
char *shm;
fd = shm_open("/1234",O_CREAT|O_RDWR|O_EXCL,0666);
if(fd<0)
printf("shm_open() failed: %s\n",strerror(errno));
err = ftruncate(fd,len);
if(err<0)
printf("ftruncate() failed: %s\n",strerror(errno));
shm = mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,(off_t)0);
/*
* technically MAP_FAILED should be compared here, instead of (char
*)-1;
* but I'm not sure if it's defined in the <sys/mman.h> header in your
* version of the SDK that you're using here.
*/
if (shm==(char*)-1)
printf("mmap() failed: %s\n",strerror(errno));
err = munmap(shm,len);
if(err<0)
printf("munmap() failed: %s\n",strerror(errno));
err = shm_unlink("/1234");
if(err<0)
printf("shm_unlink() failed: %s\n",strerror(errno));
err = close(fd);
if(err<0)
printf("clos() failed: %s\n",strerror(errno));
return 0;
}
-- Terry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden