POSIX shm_unlink() problem
POSIX shm_unlink() problem
- Subject: POSIX shm_unlink() problem
- From: "Jeff W. Boote" <email@hidden>
- Date: Thu, 04 Nov 2004 15:26:50 -0700
I am having problems using shm_unlink() on Panther. Is anyone using this
system call successfully on OS X?
After failure, errno is set to EINVAL (which is not even one of the
documented possible errors).
I'm attaching a fairly simple test case that works on FreeBSD and Linux.
Thanks,
jeff
% gcc -g tstshm.c
% ./a.out
shm_unlink(): Invalid argument
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/stat.h>
main(){
int fd;
pid_t pid;
/*
* linux doesn't like sub-paths, and FreeBSD likes "real" directory
* permissions for shared mem names...
*/
#ifdef linux
char memname[] = "/MEMNAME1";
#else
char memname[] = "/tmp/MEMNAME9";
#endif
char hipop[] = "Hello Parent Process";
char *tst;
fd = shm_open(memname,O_RDWR|O_CREAT|O_EXCL,0666);
if(fd < 0){
perror("shm_open():");
exit(1);
}
if(shm_unlink(memname) != 0){
perror("shm_unlink()");
exit(1);
}
pid = fork();
if(pid < 0){
perror("fork(): ");
exit(1);
}
if(pid > 0){
/*
* parent
*/
int wstatus=0;
struct stat sbuf;
/*
* Wait for child to exit.
*/
WAIT_AGAIN:
if(waitpid(pid,&wstatus,0) != pid){
if(errno == EINTR)
goto WAIT_AGAIN;
perror("waitpid(): ");
exit(1);
}
if(!WIFEXITED(wstatus) || WEXITSTATUS(wstatus)){
perror("Child process exited in strange state.");
exit(1);
}
/*
* Determine size of the message child left behind.
*/
if(fstat(fd,&sbuf) != 0){
perror("fstat(): ");
exit(1);
}
/*
* Map the child message into 'tst'.
*/
tst = mmap(NULL,sbuf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(tst == MAP_FAILED){
perror("Parent:mmap(): ");
exit(1);
}
fprintf(stdout,"Parent read \"%s\"\n",tst);
exit(0);
}
/*
* child
*/
/*
* Set the size of the shm segment.
*/
if(ftruncate(fd,sizeof(hipop)) != 0){
perror("ftruncate(): ");
exit(1);
}
/*
* Map it as a char *
*/
tst = mmap(NULL,sizeof(hipop),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(tst == MAP_FAILED){
perror("child:mmap(): ");
exit(1);
}
/*
* Leave a message in the shm for the parent process to read after
* exit.
*/
strcpy(tst,hipop);
exit(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