/****************************************************************************
*
* « shmmodule.c © 1997, 1998 by INRIA. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY WARRANTIES, EXPRESS OR IMPLIED,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
*
* IN NO EVENT SHALL THE INRIA OR THE AUTHORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES,
* INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION, HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT,
* INCLUDING NEGLIGENCE OR OTHERWISE, ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. »
*
***************************************************************************/
/* Python Shared Memory module by Vladimir Marangozov */
/*
This module provides an interface to System V shared memory IPC.
Version history:
- 1.0 - Released by Mr. Marangozov in the 1990s.
- 1.1 - Released by me. Fixes a few bugs (including some memory leaks) and adds the ability
to read the blocking flag on semaphores.
- 1.1.1 - Updated setup.py to handle compile options for Linux users.
See http://NikitaTheSpider.com/python/shm/ for more thorough documentation, updates, contact
info, setup.py, a more Pythonic wrapper, etc.
Philip Semanchuk, Feb. 2007
Module interface:
- shm.create_memory(int Key, int Size [,int Perm=0666]) --> object
- shm.create_semaphore(int Key [,int Value=1 [,int Perm=0666]]) --> object
- shm.error
- shm.ftok(string Path, int ProjId) --> int
- shm.getsemid(int Key) --> int
- shm.getshmid(int Key) --> int
- shm.memory(int Shmid) --> object
- shm.memory_haskey(int Key) --> int
- shm.remove_memory(int Shmid) --> None
- shm.remove_semaphore(int Semid) --> None
- shm.semaphore(int Semid) --> object
- shm.semaphore_haskey(int Key) --> int
Memory Objects:
+ Members:
- m.addr - attachment address in the process address space
- m.attached - 0|1
- m.cgid - gid of creator
- m.cpid - pid of creator
- m.cuid - uid of creator
- m.gid - gid of owner
- m.key - segment key or IPC_PRIVATE (=0)
- m.lpid - pid of last shmop
- m.nattch - current # of attached processes
- m.perm - operation permission
- m.shmid - shared memory segment id
- m.size - segment size
- m.uid - uid of owner
+ Methods:
- m.attach([int Addr=0 [,int How=0]]) --> None
- m.detach() --> None
- m.read(int Nbytes [,int Offset=0]) --> string
- m.setgid(int Gid) --> None
- m.setperm(int Perm) --> None
- m.setuid(int Uid) --> None
- m.write(string Data [,int Offset=0]) --> None
Semaphore Objects:
+ Members:
- s.cgid - gid of creator
- s.cuid - uid of creator
- s.gid - gid of owner
- s.key - semaphore key or IPC_PRIVATE (=0)
- s.lpid - pid of last semop
- s.ncnt - current # of processes waiting for s.val > 0
- s.perm - operation permission
- s.semid - semaphore id
- s.uid - uid of owner
- s.val - value of the semaphore counter
- s.zcnt - current # of processes waiting for s.val == 0
- s.blocking - whether or not the semaphore is in blocking mode
+ Methods:
- s.P() --> None - blocks if s.val == 0; decrements s.val
- s.V() --> None - increments s.val
- s.Z() --> None - blocks until s.val == 0
- s.setblocking(0|1) --> None
- s.setgid(int Gid) --> None
- s.setperm(int Perm) --> None
- s.setuid(int Uid) --> None
- s.setundo(0|1) --> None
- s.setval(int Value) --> None
*/
/* ------------------------------------------------------------------------- */
#include "Python.h"
#include "structmember.h"
/* v1.1 - added */
//#ifdef __FreeBSD__
//#include <machine/param.h> /* for system definition of PAGE_SIZE */
//#endif
#include <sys/types.h>
#include <sys/ipc.h> /* for system's IPC_xxx definitions */
#include <sys/shm.h> /* for shmget, shmat, shmdt, shmctl */
#include <sys/sem.h> /* for semget, semctl, semop */
#if defined(__GLIBC__)
#define key __key
#endif /* __GLIBC__ */
/*
-- Exception type for errors detected by this module.
*/
static PyObject *PyShm_Error;
/*
-- Convenience function to raise an error according to errno.
*/
static PyObject *
PyShm_Err(void)
{
return PyErr_SetFromErrno(PyShm_Error);
}
/*
-- The object holding a shared memory segment
*/
typedef struct {
PyObject_HEAD
int shmid; /* shared memory id */
int mode; /* attachment mode */
void *addr; /* shmseg start address */
struct shmid_ds ds; /* data structure */
} PyShmMemoryObject;
staticforward PyTypeObject PyShmMemory_Type;
#define PyShmObj PyShmMemoryObject
#define PyShmMemory_Check(op) ((op)->ob_type == &PyShmMemory_Type)
/*
-- The object holding a semaphore
*/
typedef struct {
PyObject_HEAD
int semid; /* semaphore id */
short opflag; /* IPC_NOWAIT, SEM_UNDO */
struct semid_ds ds; /* data structure */
} PyShmSemaphoreObject;
#ifndef HAVE_UNION_SEMUN
union semun {
int val; /* used for SETVAL only */
struct semid_ds *buf; /* for IPC_STAT and IPC_SET */
unsigned short *array; /* used for GETALL and SETALL */
};
#endif
typedef union semun semctl_arg;
|