消息队列类库代码

#include #define SIZEMAXMSGSIZE 2048 typedef struct structMSGSTRU { long nType; char cText[SIZEMAXMSGSIZE]; } MSGSTRU; typedef MSGSTRU * PMSGSTRU; int ReadMsg(int nPid, void * pText, int * pSize, int * pType) { MSGSTRU sMsg; int n; ASSERT(pText != NULL && pSize != NULL && pType != NULL && nPid > 0 && *pSize > 0); memset(pText, 0, *pSize); memset(&sMsg, 0, sizeof(MSGSTRU)); while ((n = msgrcv(nPid, &sMsg, *pSize, *pType, !IPC_NOWAIT)) < 0) { if (errno == EINTR) continue; ASSERT(0); } memcpy(pText, sMsg.cText, n); *pSize = n; *pType = sMsg.nType; return 0; } int WriteMsg(int nPid, void * pText, int nSize, int nType) { MSGSTRU sMsg; int n; ASSERT(pText != NULL && nPid > 0 && nSize > 0 && nSize <= SIZEMAXMSGSIZE && nType > 0); memset(&sMsg, 0, sizeof(sMsg)); sMsg.nType = nType; memcpy(sMsg.cText, pText, nSize); while ((n = msgsnd(nPid, &sMsg, nSize, !IPC_NOWAIT)) < 0) { if (errno == EINTR) continue; ASSERT(0); } return 0; } static int nFlag = 0; static void OnTimeout(int nSignal) { signal(nSignal, SIG_IGN); nFlag = 1; return; } int ReadMsgExt(int nPid, void * pText, int * pSize, int * pType, int nTimeout) { MSGSTRU sMsg; int n; ASSERT(pText != NULL && pSize != NULL && pType != NULL && nPid > 0 && *pSize > 0); memset(pText, 0, *pSize); memset(&sMsg, 0, sizeof(sMsg)); nFlag = 0; alarm(nTimeout); signal(SIGALRM, OnTimeout); while ((n = msgrcv(nPid, &sMsg, *pSize, *pType, !IPC_NOWAIT)) < 0) { if (errno == EINTR) { if (nFlag == 1) ASSERT(0) else continue; } ASSERT(0); } signal(SIGALRM, SIG_IGN); alarm(0); memcpy(pText, sMsg.cText, n); *pSize = n; if (*pType != 0) *pType = sMsg.nType; return 0; } int WriteMsgExt(int nPid, void * pText, int nSize, int nType, int nTimeout) { MSGSTRU sMsg; int n; ASSERT(pText != NULL && nPid > 0 && nSize > 0 && nSize <= SIZEMAXMSGSIZE && nType > 0); memset(&sMsg, 0, sizeof(sMsg)); sMsg.nType = nType; memcpy(sMsg.cText, pText, nSize); nFlag = 0; alarm(nTimeout); signal(SIGALRM, OnTimeout); while ((n = msgsnd(nPid, &sMsg, nSize, !IPC_NOWAIT)) < 0) { if (errno == EINTR) { if (nFlag == 1) ASSERT(0) else continue; } ASSERT(0); } signal(SIGALRM, SIG_IGN); alarm(0); ASSERT(n != nSize); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值