shm_open and mmap: Invalid argument?
shm_open and mmap: Invalid argument?
- Subject: shm_open and mmap: Invalid argument?
- From: Ethan Tira-Thompson <email@hidden>
- Date: Sun, 20 Feb 2005 14:51:43 -0500
I am trying to implement some shared memory between a set of processes. I don't really care if it's file-backed or not -- these regions are not going to be particularly large, I just want low latency communication between the processes.
I've already got things working with the SysV interfaces shmget and shmat, but Mac OS X is configured with a very low number of concurrently active regions per process (I think 8?) and I don't want other users to have to tinker with kernal configuration settings, so I'm switching to the POSIX interface instead. (It was also quite annoying that SysV regions are left in memory after all references are gone. Further, OS X doesn't provide the ipcs utility, I had to download it myself in order to kill the regions manually. (thanks to whoever ported that utility... I had trouble finding it the first time and I can't find it again now :( )
Anyway, now mmap always returns Invalid Argument when it is passed a file descriptor produced by shm_open. I saw somewhere that OS X/darwin doesn't support mmap with device files, does this imply no non-file backed regions? Further, calls to close() and shm_unlink() after the mmap error return Invalid Argument themselves, and then on the next run (using the same name) shm_open returns File Exists. So it appears the POSIX interface will also leave these regions floating around after the process ends and all references are removed, although I had read somewhere it wouldn't. (Especially annoying since I did make the calls to close and shm_unlink)
What's going on?
Here's the code sample for the region in question:
int fd;
if(name.size()>=MAX_NAME_LEN)
cerr << "*** WARNING RCRegion named " << name << " will be clipped to " << name.substr(0,MAX_NAME_LEN-2) << endl;
id.key[0]='/'; //one page I read said for best portability, names should start with '/'... ?
strncpy(id.key+1,name.c_str(),MAX_NAME_LEN-2);
id.key[MAX_NAME_LEN-1]='\0';
cout << "Name is " << id.key << endl;
if(create) {
if((fd=shm_open(id.key,O_RDWR|O_CREAT|O_EXCL,0666))<0) {
perror("Getting new region (shm_open)");
exit(EXIT_FAILURE);
}
} else {
if((fd=shm_open(id.key,O_RDWR,0666))<0) {
perror("Getting existing region (shm_open)");
exit(EXIT_FAILURE);
}
}
cout << "fd is " << fd << endl;
base=static_cast<char*>(mmap(NULL,sz,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0));
if (base == reinterpret_cast<char*>(-1)) {
perror("Attaching region (mmap)");
if(close(fd)<0)
perror("Warning: Closing temporary file descriptor from shm_open");
if(shm_unlink(id.key)<0)
perror("Warning: Shared memory unlink (shm_unlink)");
exit(EXIT_FAILURE);
}
if(close(fd)<0) {
perror("Warning: Closing temporary file descriptor from shm_open");
}
Thank you for your time!
-ethan
_______________________________________________
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