I am responsible for a system, running with Linux, using Linux message queue communication between modules. There is now a place to have a single communication module. I want to write a communication interface. I originally wanted to write with C , and later considering it feels fast with Python, followed by it. Look at the message queue related module under the Internet. Find the module written by Walter De Jong: http://www.xs4all.nl/~walj/python-iPC/ Real test, found that it is not very convenient to use. I feel that there is a change in it. Through this module, I know how Swig is used, and I downloaded a new version installation from the official website of SWIG. Add IPC modules under Linux to Python with SWIG
1. Download the latest version of SWIG-1.3.22 TAR ZXVF SWIG-1.3.22.tar.gz CD swig-1.3.22 / ./configure make make install At this time, SWIG is installed to / usr / local / bin directory . The old version of SWIG needs to be renamed. MV / usr / bin / swig /usr/bin/swig1.1 2, the following is the file files after the modified IPC package, mainly 3 files IPC.H, IPC.I, Makefile IPC.H: / * ipc.h WJ103
This file is a rip-off of the standard incrudes: /usr/include/sys/types.h /usr/include/sys/ipc.h /usr/include/sys/shm.h / usr / include / bits / shm .h etc.
If Things Do Not Work, Check for Difference Between Those Files and this One. I'm Sorry, I Could Not get it done in any other Way ... * /
#include
#define ipc_creat 01000 / * Create Key if key does not exist. * / # define IPC_EXCL 02000 / * Fail if Key exists. * / # define ipc_nowait 04000 / * Return Error on Wait. * /
/ * Control Commands for `msgctl ',` semctl', and `shmctl '. * / # Define IPC_rmid 0 / * Remove Identifier. * / # Define IPC_SET 1 / * SET` IPC_PERM' Options. * / # Define IPC_STAT 2 / * Get `IPC_Perm 'Options. * / # Define IPC_INFO 3 / * See IPCS. * /
/ * Special Key Values. * / # Define IPC_Private ((key_t) 0) / * private key. * /
/ * Permission flag for shmget. * / # Define shm_r 0400 / * or s_irugo from
/ * Flags for `shmat '. * / # Define SHM_RDONLY 010000 / * attach read-only else read-write * / # define SHM_RND 020000 / * round attach address to SHMLBA * / # define SHM_REMAP 040000 / * take-over region on Attach * // * Commands for `shmctl '. * / # define shm_lock 11 / * Lock segment (root only) * / # define shm_unlock 12 / * unlock segment (root only) * /
/ * ipcs ctl commands * / # define shm_stat 13 # define SHM_INFO 14
/ * SHM_MODE UPPER BYTE FLAGS * / # Define SHM_DEST 01000 / * Segment Will Be Destroyed On Last Detach * / # Define SHM_LOCKED 02000 / * Segment Will Not Be swapped * /
/ * Permission struct * / struct ipc_perm {key_t __key;. / * Key * / unsigned short int uid;. / * Owner's user ID * / unsigned short int gid;. / * Owner's group ID * / unsigned short int cuid; / * Creator's user ID. * / Unsigned short int cgid; / * creator's group id. * / Unsigned short int mode; / * read / write permission. * / Unsigned short int __seq; / * sequence number. * /};
/ * Data structure describing a set of semaphores * / struct shmid_ds {struct ipc_perm shm_perm;. / * Operation permission struct * / int shm_segsz; / * size of segment in bytes * / time_t shm_atime; / * time of last shmat () * / Time_t shm_dtime; / * time of last shmdt () * / time_t shm_ctime; / * time of last change by shmctl () * / pid_t shm_cpid; / * pid of create * / pid_t shm_lpid; / * pid of last shmop * / unsigned short int shm_nattch; / * number of current attaches * / unsigned short int __shm_npages; / * size of segment (pages) * / unsigned long int * __ shm_pages; / * array of ptrs to frames -> SHMMAX * / void * __ attaches; / * Descriptors for attachs * /};
struct shminfo {int shmmax; int shmmin; int shmmni; int shmseg; int shmall;}; struct shm_info {int used_ids; unsigned long int shm_tot; / * total allocated shm * / unsigned long int shm_rss; / * total resident shm * / Unsigned long int shm_swp; / * total swapped shm * / unsigned long int swap_attempts; unsigned long int swap_successes;
/ * Flags for `semop '. * / # Define sem_undo 0x1000 / * undo the Operation on exit * /
/ * Commands for `semctl '. * / # Define getpid 11 / * get sempid * / # define getVal 12 / * get semval * / # define getAll 13 / * get all semval's * / # define getncnt 14 / * get semncnt * / # define getzcnt 15 / * get semzcnt * / # define setval 16 / * set semval * / # define setall 17 / * set all semval's * /
/ * Data structure describing a set of semaphores * / struct semid_ds {struct ipc_perm sem_perm;. / * Operation permission struct * / time_t sem_otime; / * last semop () time * / time_t sem_ctime; / * last time changed by semctl () * / struct sem * __ sembase; / * ptr to first semaphore in array * / struct sem_queue * __ sem_pending; / * pending operations * / struct sem_queue * __ sem_pending_last; / * last pending operation * / struct sem_undo * __ undo; / * ondo requests on This array * / unsigned short int SEM_NSEMS; / * Number of semaphores in set * /};
/ * According to X / Open you have to define the union semun yourself. What the crap is that all about anyway ??? It's probably just because of its name. Well, here it is. I define the semun myself. Ha!
--Walter * / union semun {int val; / * value for setval * / struct semid_ds * buf; / * buffer for ipc_stat & ipc_set * / unsigned short int * array; / * array for getall & setall * / struct seminfo * __buf; / * buffer for ipc_info * /}; / * ipcs ctl cmds * / # define sem_stat 18 # Define Sem_info 19
Struct Seminfo {Int SemMap; Int Semmni; Int Semmns; Int SemMnu; Int Semmsl; Int Semopm; Int Semume; Int Semusz; Int Semvmx; Int Semaem;
/ * Note: use the helper function mkmsgbuf () to create a message buffer This structure is merely used for typecasting a larger object * / struct msgbuf {long int mtype; / * type of received / sent message * / char mtext [2048] ; / * Text of the message * /};
/ * Define Options for message. * / # Define msg_noerror 010000 / * no error ife message is too big * / # define msg_except 020000 / * Recv ANY MSG Except of Specified Type * /
/ * Structure of record for one message inside the kernel The type `struct msg 'is opaque * / struct msqid_ds {struct ipc_perm msg_perm;.. / * Structure describing operation permission * / struct msg * __ msg_first; / * pointer to first message on queue * / struct msg * __ msg_last; / * pointer to last message on queue * / time_t msg_stime; / * time of last msgsnd command * / time_t msg_rtime; / * time of last msgrcv command * / time_t msg_ctime; / * time of last Change * / void * __ wwait; / * ??? * / void * __ rwait; / * ??? * / unsigned short int __msg_cBytes; / * current number of bytes on queue * / unsigned short int msg_qnum; / * Number of Messages currently on queue * / unsigned short int msg_qbytes; / * max number of bytes allowed on queue * / pid_t msg_lspid; / * pid of last msgsnd () * / pid_t msg_lrpid; / * pid of last msgrcv () * /};
/ * # define msg_cBytes __msg_cbytes * /
/ * ipcs ctl commands * / # define msg_stat 11 # define msg_info 12
/ * buffer for msgctl calls IPC_INFO, MSG_INFO * / STRUCT MSGINFO {Int msgpool; int msgmap; int msgmax; int msgMnb; int msgmni; int msgssz; int msgtql; unsigned short int msgseg;};
/ * EOB * / SWIG interface definition file: ipc.i //// ipc.i wj103 //
% module ipc% {# incdude "ipc.h"%}
// produce constants and helper functions for structures and unions% include "ipc.h"
INT FTOK (Char *, char);
INT shmget (int, int, int); void * shmat (int, void *, int); int shmdt (void *); int shmctl (int, int, struct shmid_ds *);
INT SEMGET (INT, INT, INT); INT SemctL (int, int, int, union semun); int SMOP (int, struct sembuf *, unsigned int);
INT MSGGET (int, int); int, int, struct msqid_ds *); int, struct msgbuf *, int, int, int, intend
% inline% {%}
// EOB compilation definition file Makefile ## makefile ## python ipc module by walter de jong
CC = gcccopt = -g # -o2 -winlinecflags = $ (COPT) -wallcflagswrap = $ (COPT) -fpiccflagsso = $ (Copt) -shared
SWIG = swigpython_include = / usr / include / python2.2
Objs = IPC_Wrap.o
All: _ipc.so
_IPC.SO: $ (OBJS) $ (cc) $ (cflagsso) $ (objs) -o _ipc.so
IPC_Wrap.O: IPC_Wrap.c $ (cc) $ (cflagswrap) -c ipc_wrap.c -dbool = char -i. -i $ (python_include)
IPC_Wrap.c: IPC.I $ (SWIG) -python ipc.i
.c.o: $ (cc) $ (cflags) -c $
Clean: RM -F $ (OBJS) _IPC.so ipc.py ipc.pyc ipc_wrap.c ipc_wrap.doc
# Eob
Compile: Implement the make program compile: IPC.py, ipc.pyc module file, and module support library _iPC.so3, usage introduction: Since I only use message queue-related functions, there is no test of other functions!
>>> Import IPC >>> DIR (IPC) ['getall', 'getncnt', 'getpid', 'getVal', 'getzcnt', 'ipc_creat', 'ipc_excl', 'IPC_INFO', 'IPC_NOWAIT', ' IPC_RMID ',' IPC_SET ',' IPC_STAT ',' MSG_EXCEPT ',' MSG_INFO ',' MSG_NOERROR ',' MSG_STAT ',' SEM_INFO ',' SEM_STAT ',' SEM_UNDO ',' SETALL ',' SETVAL ',' SHM_DEST ' , 'SHM_INFO', 'SHM_LOCK', 'SHM_LOCKED', 'SHM_R', 'SHM_RDONLY', 'SHM_REMAP', 'SHM_RND', 'SHM_STAT', 'SHM_UNLOCK', 'SHM_W', '__builtins__', '__doc__', ' __file__ ',' __name__ ',' _ipc ',' _newclass', '_object', '_swig_getattr', '_swig_setattr', 'ftok', 'ipc_perm', 'ipc_permPtr', 'msgbuf', 'msgbufPtr', 'msgctl' , 'msgget', 'msginfo', 'msginfoPtr', 'msgrcv', 'msgsnd', 'msqid_ds',' msqid_dsPtr ',' semctl ',' semget ',' semid_ds', 'semid_dsPtr', 'seminfo', ' seminfoPtr ',' semop ',' semun ',' semunPtr ',' shm_info ',' shm_infoPtr ',' shmat ',' shmctl ',' shmdt ',' shmget ',' shmid_ds', 'shmid_dsPtr', 'shminfo' , 'shminfoptr'] Create a message queue: >>> msgid = ipc.msgget (0x10012, 0666 | ipc.iPc_creat) >>> MSGID65538 This point with IPCS view, you can see that the message queue has created successfully sent message: >>> MBUF = ipc.msgbuf () >>> MBUF