Re: shm_open + mmap?
Re: shm_open + mmap?
- Subject: Re: shm_open + mmap?
- From: Matt Watson <email@hidden>
- Date: Thu, 24 Nov 2005 19:03:32 -0800
On Nov 24, 2005, at 6:35 PM, Brad Parker wrote:
Solra Bizna wrote:
You must first increase the length of the file by one means or
another. shm_open creates a 0-length file.
I tried that using ftruncate and it's didn't help.
Are you calling ftruncate() with the size you are passing in to mmap()?
This seems to work:
#include <stdio.h>
#include <sys/file.h>
#include <sys/mman.h>
static const char *shm_filename = "/tmp/flarm";
#define MAX_BITMAP_OFFSET 8192
int
main(void)
{
int ret, shm_fd, mprot, mflags, ps;
size_t size;
void *shm_fb;
ret = shm_unlink(shm_filename);
printf("shm_unlink ret %d\n", ret);
shm_fd = shm_open(shm_filename, O_RDWR | O_CREAT, 0666);
if (shm_fd < 0) {
perror("shm_open");
return 1;
}
size = MAX_BITMAP_OFFSET*4;
mprot = PROT_READ|PROT_WRITE;
mflags = MAP_FILE|MAP_SHARED;
ps = getpagesize();
printf("pagesize %d\n", ps);
size = ((size+ps-1)/ps) * ps;
if (ftruncate(shm_fd, size) < 0) {
perror("ftruncate");
return 1;
}
printf("shm_fd %d, size %d\n", shm_fd, size);
shm_fb = mmap(NULL, size, mprot, mflags, shm_fd, (off_t)0);
if (shm_fb == (void *)-1) {
fprintf(stderr, "mmap of %s failed\n", shm_filename);
perror("mmap");
return 1;
}
printf("shm_fb = %p\n", shm_fb);
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden