⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msg.c

📁 《精通Unix下C语言编程与项目实践》的源代码
💻 C
字号:
#include <comlib.h>#define SIZEMAXMSGSIZE 2048typedef 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;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -