ipc.c
上传用户:sdtbys
上传日期:2009-12-06
资源大小:13k
文件大小:10k
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/sem.h>
- #include <sys/shm.h>
- #include <memory.h>
- #include <errno.h>
- extern int errno;
- #include "ipc.h"
- /* typedef long key_t (IPC KEY) */
- /* ipcs|ipcrm IPC msg|sem|shm IPC ID IPC KEY */
- /* struct ipc_perm { key_t key;
- ushort uid;
- ushort gid;
- ushort cid;
- ushort cgid;
- ushort mode;}
- */
- /**********************************************************
- Function msgCreate
- Create message queue
- Parameters :
- key : key which identify message queue
- Return :
- iMsgId : message queue id
- ***********************************************************/
- int msgCreate(key_t key)
- {
- int iMsgId;
-
- iMsgId=msgget(key,IPC_CREAT | IPC_EXCL | QPERM);
- if (iMsgId<0) {
- if (errno==EEXIST) {
- ShowMsg("msg has exist! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- iMsgId=msgget(key,QPERM);
- if (iMsgId<0)
- {
- ShowMsg("msg open failed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- return(MsgOpenFail);
- }
- return iMsgId;
- }
- ShowMsg("msg create failed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- return MsgCreateFail;
- };
- return iMsgId;
- }
- /**********************************************************
- Function msgOpen
- Open message queue , this queue must be existed .
- Parameters :
- key : key which identify message queue
- Return :
- iMsgId : message queue id
- ***********************************************************/
- int msgOpen(key_t key)
- {
- int iMsgId;
- if ((iMsgId=msgget(key,QPERM))==-1) {
- ShowMsg("msg open failed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- return MsgOpenFail;
- }
- return iMsgId;
- }
- /**********************************************************
- Function msgStatus
- Output message queue status to a file
- Parameters :
- iMsgId : message queue id
- fpOut : output file handle
- ***********************************************************/
- int msgStatus(int iMsgId)
- {
- struct msqid_ds msg_status;
- int i;
- if (msgctl(iMsgId,IPC_STAT,&msg_status) < 0 ) {
- return MsgStateFail;
- }
- /*printf("nThe Msg Status is:"); */
- ShowMsg("msg id=%inuid=%ingid=%inmode=%on"
- "msgnum=%inmsgqbytes=%incbytes=%in"
- "lspid=%inlrpid=%in",iMsgId,
- msg_status.msg_perm.uid,msg_status.msg_perm.gid,
- msg_status.msg_perm.mode,msg_status.msg_qnum,
- msg_status.msg_qbytes,msg_status.msg_cbytes,msg_status.msg_lspid,
- msg_status.msg_lrpid);
- return(0);
- }
- /**********************************************************
- Function msgSendBuf
- Send info to message queue
- Parameters :
- iMsgId : message queue id
- lType : message type identify message info in the same queue
- pszInfo : info to be sent
- iInfoLen : length of info
- ***********************************************************/
- int msgSend(int iMsgId , long lType , char *pszInfo , int iInfoLen)
- {
- Mesg mesg;
- if (iInfoLen>MaxMesgLen)
- { ShowMsg("msg len error! len=%d,maxlen=%dn",iInfoLen,MaxMesgLen);
- return (MsgMesgLenBad);}
- mesg.MesgId=lType;
- memcpy(mesg.MesgText,pszInfo,iInfoLen);
- if (msgsnd(iMsgId,&mesg,iInfoLen,0)==-1) {
- ShowMsg("msg send failed! errno=%d error=%sn",errno,strerror(errno));
- return MsgSendFail;
- }
- return (0);
- }
- /**********************************************************
- Function msgRecvBuf
- Receive info from message queue
- Parameters :
- iMsgId : message queue id
- lType : message type identify message info in the same queue
- pszInfo : info buffer to be received
- iRecvLen : length of info
- iTimeOut : timeout , if =0 , nowait .
- ***********************************************************/
- int msgRecv(int iMsgId , long *lType , char *pszInfo , int iExptLen)
- {
- Mesg mesg;
- int iRecvLen;
- if (iExptLen>MaxMesgLen)
- { ShowMsg("msg len error! len=%d,maxlen=%dn",iExptLen,MaxMesgLen);
- return (MsgMesgLenBad);}
- /*if ((iRecvLen=msgrcv(iMsgId,&mesg,iExptLen,lType,IPC_NOWAIT))==-1) {
- return MsgRecvFail;
- }*/
- if ((iRecvLen=msgrcv(iMsgId,&mesg,iExptLen,*lType,0))==-1) {
- ShowMsg("msg recv failed! errno=%d error=%sn",errno,strerror(errno));
- return MsgRecvFail;
- }
- memcpy(pszInfo,mesg.MesgText,iRecvLen);
- *lType=mesg.MesgId;
- return iRecvLen;
- }
- /**********************************************************
- Function msgRemove
- Remove message queue
- Parameters :
- iMsgId : message queue id
- ***********************************************************/
- int msgRemove(int iMsgId)
- {
- if (msgctl(iMsgId,IPC_RMID,0) <0) {
- ShowMsg("msg remove failed! errno=%d error=%sn",errno,strerror(errno));
- return MsgRemoveFail;
- }
- return (0);
- }
- /*************************************************************
- Function semCreate
- Create one array semaphores
- Parameters
- key : key of identifier
- *************************************************************/
- int semCreate(key_t key)
- {
- int iSemId,iRet;
- iSemId=semget(key,1,IPC_CREAT | IPC_EXCL | QPERM);
- if (iSemId<0) {
- if (errno==EEXIST) {
- ShowMsg("sem existed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- iSemId=semget(key,0,QPERM);
- if (iSemId<0)
- {
- ShowMsg("sem open failed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- return(SemOpenFail);
- }
- /************************************
- iRet=semctl(iSemId,0,SETVAL,0);
- if(iRet<0) return(SemSetValFail);
- ************************************/
- return(iSemId);
- }
- ShowMsg("sem create failed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- return(SemCreateFail);
- }
- iRet=semctl(iSemId,0,SETVAL,0);
- if(iRet<0)
- {
- ShowMsg("sem init 0 failed! key=%d sem=%d errno=%d error=%sn",key,iSemId,errno,strerror(errno));
- return(SemSetValFail);
- }
- return(iSemId);
- }
- /*************************************************************
- Function semOpen
- Open one array semaphores
- Parameters
- key : key of identifier
- *************************************************************/
- int semOpen(key_t key)
- {
- int iSemId,iRet;
- iSemId=semget(key,0,QPERM);
- if (iSemId<0) {
- ShowMsg("sem open failed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- return(SemOpenFail);
- }
- /*********************************
- iRet=semctl(iSemId,0,SETVAL,0);
- if(iRet<0) return(SemSetValFail);
- **********************************/
- return(iSemId);
- }
- /*************************************************************
- Function semOn
- if value of semp is 1 , set to 0
- if not return false
- Parameters
- iSemId : handle of semp
- *************************************************************/
- int semLock(int iSemId)
- {
- struct sembuf sembuf;
- sembuf.sem_num=0;
- sembuf.sem_op=-1;
- /*sembuf.sem_flg=IPC_NOWAIT | SEM_UNDO ;*/
- sembuf.sem_flg=0;
- if(semop(iSemId,&sembuf,1)<0)
- {
- ShowMsg("sem lock failed! errno=%d error=%sn",errno,strerror(errno));
- return(SemLockFail);
- }
- return(0);
- }
- /*************************************************************
- Function semOff
- if value of semp is 0 , add to 1
- if not return false
- Parameters
- iSemId : handle of semp
- *************************************************************/
- int semUnlock(int iSemId)
- {
- int iRet;
-
- struct sembuf sembuf;
- sembuf.sem_num=0;
- sembuf.sem_op=1;
- /*sembuf.sem_flg=IPC_NOWAIT | SEM_UNDO ;*/
- sembuf.sem_flg=0;
- if(semop(iSemId,&sembuf,1)<0)
- {
- ShowMsg("sem unlock failed! errno=%d error=%sn",errno,strerror(errno));
- return(SemUnlockFail);
- }
-
- /*iRet=semctl(iSemId,0,SETVAL,1);
- if(iRet<0) return(SemSetValFail);*/
- return(0);
- }
- /*************************************************************
- Function semRemove
- remove semaphores
- Parameters
- iSemId : handle of semp
- *************************************************************/
- int semRemove(int iSemId)
- {
- int iRet;
- iRet=semctl(iSemId,0,IPC_RMID,0);
- if (iRet<0) {
- ShowMsg("sem Remove Failed errno=%d error=%sn",errno,strerror(errno));
- return(SemRemoveFail);
- }
- return(0);
- }
- int semGetVal(int iSemId)
- { int semval;
- semval=semctl(iSemId,0,GETVAL,0);
- return (semval);
- }
- int semSetVal(int iSemId,int val)
- {
- /*union semun semopt;
- semopt.val=val;*/
- semctl(iSemId,0,SETVAL,val);
- }
- int shmCreate(key_t key,int size)
- {
- int iShmId;
-
- iShmId=shmget(key,size,IPC_CREAT | IPC_EXCL | QPERM);
- if (iShmId<0) {
- if (errno==EEXIST) {
- ShowMsg("shm has exist! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- iShmId=shmget(key,size,QPERM);
- if (iShmId<0)
- {
- ShowMsg("shm open failed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- return(ShmOpenFail);
- }
- return iShmId;
- }
- ShowMsg("shm create failed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- return ShmCreateFail;
- };
- return iShmId;
- }
- int shmOpen(key_t key,int size)
- {
- int iShmId;
- if ((iShmId=shmget(key,size,QPERM))==-1) {
- ShowMsg("shm open failed! key=%d errno=%d error=%sn",key,errno,strerror(errno));
- return ShmOpenFail;
- }
- return iShmId;
- }
- int shmRemove(int iShmId)
- {
- int iRet;
- iRet=shmctl(iShmId,IPC_RMID,0);
- if (iRet<0) {
- ShowMsg("shm Remove Failed errno=%d error=%sn",errno,strerror(errno));
- return(ShmRemoveFail);
- }
- return(0);
- }
- char *shmConnect(int iShmId)
- { char *addr;
- addr=shmat(iShmId,0,0);
- if (addr==NULL)
- {
- ShowMsg("shm Connect Failed errno=%d error=%sn",errno,strerror(errno));
- return(NULL);
- }
- return(addr);
- }
- int shmDisConnect(char *addr)
- {
- if (shmdt(addr)!=0)
- {
- ShowMsg("shm DisConnect Failed errno=%d error=%sn",errno,strerror(errno));
- return(ShmDisConnectF);
- }
- return(0);
- }