![[ 永远的UNIX::UNIX技术资料的宝库 ]](/images/title.gif)
|
| 首页 > 编程技术 > 源码天堂 > 正文 |
 |
| C源码:四种UNIX消息队列操作函数 |
| 本文出自:http://mly363.363.net 作者: (2001-08-20 07:00:00) |
/*
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 */
(http://www.fanqiang.com)
进入【UNIX论坛】
|
|
| 相关文章 |
|
|
|
|
 |
★ 樊强制作 欢迎分享 ★ |