?? 四種unix消息隊列操作函數.txt
字號:
/*
UNIXMSG.C --- Functions for Unix messages process
M.L.Y 1999.12.13
MODIFIED (YYYY.MM.DD)
M.L.Y 1999.12.13 - Creation
M.L.Y 2000.07.27 - Change for HP-UX, add msgq_rmid()
*/
#include "UNIXMSG.H"
MsgQueue MsgQ;
/* ------------------------------------------------------------------------- */
int openmsg(long MsgKey)
{
int MsgId;
if((MsgId = msgget(MsgKey, MSGPERMS | IPC_CREAT)) < 0)
printf("Message error: creat message!\n");
return MsgId;
}
/* ------------------------------------------------------------------------- */
int msgq_rmid(int MsgId)
{
#if __hpux /* HP 9000 */ \
| (i386 & M_I386 & unix & __unix & M_UNIX & (_SCO_COFF | _SCO_ELF))
/* SCO UNIX */
struct msqid_ds buf;
return msgctl(MsgId, IPC_RMID, &buf);
#else
return msgctl(MsgId, IPC_RMID);
#endif
}
/* ------------------------------------------------------------------------- */
int rcvmsg_nowait(long MsgKey, USGC *Buff, long *MsgType)
{
int MsgId, MsgLen;
MsgId = openmsg(MsgKey);
MsgLen = msgrcv(MsgId, &MsgQ, MAX_MSGQ_SIZE, 0, IPC_NOWAIT);
if(MsgLen < 0) return MsgLen;
*MsgType = MsgQ.type;
memcpy(Buff, MsgQ.buf, MsgLen);
return MsgLen;
}
/* ------------------------------------------------------------------------- */
int rcvmsg_type_nowait(long MsgKey, USGC *Buff, long MsgType)
{
int MsgId, MsgLen;
MsgId = openmsg(MsgKey);
MsgLen = msgrcv(MsgId, &MsgQ, MAX_MSGQ_SIZE, MsgType, IPC_NOWAIT);
if(MsgLen < 0) return MsgLen; /* -1: no message received */
memcpy(Buff, MsgQ.buf, MsgLen);
return MsgLen;
}
/* ------------------------------------------------------------------------- */
int rcvmsg_wait(long MsgKey, USGC *Buff)
{
int MsgId, MsgLen;
MsgId = openmsg(MsgKey);
MsgLen = msgrcv(MsgId, &MsgQ, MAX_MSGQ_SIZE, 0, MSG_NOERROR);
if(MsgLen < 0)
printf("Message error: recive message!\n");
memcpy(Buff, MsgQ.buf, MsgLen);
return MsgLen;
}
/* ------------------------------------------------------------------------- */
int rcvmsg_type(long MsgKey, USGC *Buff, long *MsgType)
{
int MsgId, MsgLen;
MsgId = openmsg(MsgKey);
MsgLen = msgrcv(MsgId, &MsgQ, MAX_MSGQ_SIZE, 0, MSG_NOERROR);
if(MsgLen < 0)
printf("Message error: recive message!\n");
*MsgType = MsgQ.type;
memcpy(Buff, MsgQ.buf, MsgLen);
return MsgLen;
}
/* ------------------------------------------------------------------------- */
int sndmsg(long MsgKey, USGC *Buff, int MsgLen)
{
int MsgId, rc;
if(MsgLen < 0) return -1;
MsgId = openmsg(MsgKey);
memcpy(MsgQ.buf, Buff, MsgLen);
MsgQ.type = 1;
rc = msgsnd(MsgId, &MsgQ, MsgLen, 0);
if(rc < 0) return rc;
return MsgLen;
}
/* ------------------------------------------------------------------------- */
int sndmsg_type(long MsgKey, USGC *Buff, long MsgType, int MsgLen)
{
int MsgId, rc;
if(MsgLen < 0) return -1;
MsgId = openmsg(MsgKey);
memcpy(MsgQ.buf, Buff, MsgLen);
MsgQ.type = MsgType;
rc = msgsnd(MsgId, &MsgQ, MsgLen, 0);
if(rc < 0) return rc;
return MsgLen;
}
/* ------------------------------------------------------------------------- */
int delmsg(long MsgKey)
{
int MsgId;
/*
if((MsgId = msgget(MsgKey, MSGPERMS)) < 0 ||
msgq_rmid(MsgId) < 0)
{
printf("Message error: no this message queue or remove it error!\n");
return -1;
}
*/
if((MsgId = msgget(MsgKey, MSGPERMS)) < 0)
{
printf("Message error: no this message queue!\n");
return MsgId;
}
if(msgq_rmid(MsgId) < 0)
{
printf("Message error: remove msgq!\n");
return -1;
}
return MsgId;
}
/* End of file */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -