Is it safe to use the address of the semaphore in child process obtained in the parent?
According to the man page it's not allowed:
References to copies of the semaphore produce undefined results.
That is true in general, the code below shouldn't work correctly.
int main() { sem_t *sem = sem_open("key",O_CREAT,S_IRUSR|S_IWUSR,0); switch(fork()) { case -1: perror("fork()"); return 1; case 0: { printf("Child %u waiting for semaphore(%p)...\n",getpid(),sem); sem_wait(sem); printf("Child: Done\n"); return 0; } } sleep(1); printf("Parent %u setting semaphore(%p)...\n",getpid(),sem); sem_post(sem); printf("Parent: Set\n"); return 0; }But it works. At least in current implementation of linux and glibc.
sem_t is a pointer. You can found out that it's defined as a union but it's used only as a pointer. The union is used for alignment.
Looking into the sem_open.c in glibc sources you may find out that the return value of sem_open is actually the address returned from mmap call.
(sem_t *) mmap(NULL, sizeof (sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);mmap maps the file in /dev/shm/(or where shmfs is mounted), with name "sem."+name from the sem_open first argument. In case if the semaphore had been already created sem_open searches for its address in the tree of opened semaphores. For the search it uses tuple of name of the file, inode and device.
The tree of opened semaphores is declared in sem_open.c. It's local for the process.
The call to sem_open in the child process will refer to the same tree of the opened semaphores(as the address space is being copied in case of fork) and will return the address from the previous call of sem_open in parent process.
Interesting that the address returned from sem_open will be the same if it was called in the child and in the parent process independently:
#include <stdio.h> #include <error.h> #include <errno.h> #include <semaphore.h> #include <sys/stat.h> #include <fcntl.h> int main() { switch(fork()) { case -1: perror("fork()"); return 1; case 0: { sem_t *sem = sem_open("key",O_CREAT,S_IRUSR|S_IWUSR,0); printf("Child %u waiting for semaphore(%p)...\n",getpid(),sem); sem_wait(sem); printf("Child: Done\n"); return 0; } } sleep(1); sem_t *sem = sem_open("key",O_CREAT,S_IRUSR|S_IWUSR,0); printf("Parent %u setting semaphore(%p)...\n",getpid(),sem); sem_post(sem); printf("Parent: Set\n"); return 0; }
$gcc sem-test.c -lrt -o test $./test Child 16116 waiting for semaphore(0xb8062000)... Parent 16115 setting semaphore(0xb8062000)... Parent: Set Child: DoneThat is because memory mapped by mmap is preserved across fork, with the same set of attributes.
No comments:
Post a Comment