Hi
Apologies for the long post. Am a Cython n00b.
Am trying to mmap a named shared memory. Am using cython 0.11.2 with freebsd 8.2 and python 2.6.6.
The following is my simple pyx code:
mmap.pyx
========
mmapDone = False
cdef extern from "sys/_null.h":
cdef enum:
NULL "NULL"
cdef extern from "sys/mman.h":
void * mmap(void *, size_t, int, int, int, off_t)
int shm_open(char *, int, mode_t)
cdef enum:
PROT_READ "PROT_READ"
MAP_SHARED "MAP_SHARED"
MAP_FAILED "MAP_FAILED"
cdef extern from "sys/fcntl.h":
cdef enum:
O_RDONLY "O_RDONLY"
cdef extern from "sys/stat.h":
cdef enum:
S_IRUSR "S_IRUSR"
def mmap_shared_area():
cdef int fd
cdef int fsize
cdef void* mmapStart
print 'Attempting to shm_open....'
fd = shm_open("/shared_area", O_RDONLY, S_IRUSR)
if (fd == -1):
raise Exception('Unable to open Shared Memory. File Descriptor is -1')
return (0)
else:
print 'SUCCESS fd = %d' %(fd)
print 'Attempting to MMAP...'
fsize = 500
mmapStart = <void*> mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0)
if (MAP_FAILED == <int>mmapStart):
print 'MMAP failed miserably for no reason'
return (0)
mmapDone = True
return (1)
After compiling and generating a shared library - mymmap.so, I test the module using:
test.py
=====
#!/usr/local/bin/python
import mymmap as mm
print 'Returned value %d' %(mm.mmap_shared_area())
Output
=====
Attempting to shm_open....
SUCCESS fd = 3
Attempting to MMAP...
MMAP failed miserably for no reason
Returned value 0
An equivalent C implementation succeeds:
shm_client.c
=========
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
int main()
{
int fd;
fd = shm_open("/prox_staging_area", O_RDONLY, S_IRUSR | S_IWUSR);
if (-1 == fd) {
printf("\n Unable to open shared memory. Error number %d Error string %s\n", errno, strerror(errno));
exit(1);
}
printf("\n Opened Shared memory with fd %d\n", fd);
void *mmapStart;
mmapStart = (void*) mmap(NULL, 500, PROT_READ, MAP_SHARED, fd, 0);
printf("\n mmapStart is %d\n", mmapStart);
if (MAP_FAILED == mmapStart)
printf("\nUnable to mmap\n");
sleep(1000);
}
Output of C client
============
Opened Shared memory with fd 3
Any ideas why the C client succeeds where as the Cython client fails? Am I missing something?
Thanks
Karthik