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

📄 svmsg.cpp

📁 在Linux使用systemV测试消息
💻 CPP
字号:
#include <unistd.h>#include <sys/ipc.h>#include <sys/types.h>#include <sys/msg.h>
//Create or get a message queue
//In:
//	i_kMsgKey:
//	iMesgFlag:IPC_CREAT,IPC_EXCL,IPC_NOWAIT
//Return:
//	-1: Error;
//	>0: Message ID
int Mesg_get(key_t i_kMsgKey,int i_iMsgFlag){
	int msgid;	msgid = msgget(i_kMsgKey,i_iMsgFlag);	return msgid;
}

//Send a message
//In:
//	i_MesgID:
//	i_strupMesg:
//	iMesgFlag:0,IPC_NOWAIT
//Return:
/* return sent #bytes of data */
/*	-1: Error
	0: sent #bytes of message data successfully*/
int Mesg_send(int i_MesgId, struct Mesg_Struct *i_strupMesg,int iMesgFlag)
{
	int nbytes;
	nbytes = msgsnd(i_MesgId, &(i_strupMesg->iMesgType), sizeof(long)+i_strupMesg->iMesgLen, iMesgFlag);	return nbytes;}
//Receive a message
//In:
//	i_MesgID:
//	io_strupMesg:
//	iMesgFlag:0,IPC_NOWAIT,IPC_EXCEPT,IPC_NOERROR
//Out:
//	io_strupMesg:
//Return:
/* return received #bytes of data */
/*	-1: Error
	0:  EOF
	>0: received #bytes of message data successfully
*/
int Mesg_recv(int i_MesgId, struct Mesg_Struct *io_strupMesg,int iMesgFlag)
{
	long len,nbytes;	len=nbytes=-1;	nbytes = msgrcv(i_MesgId, &(io_strupMesg->iMesgType), sizeof(struct Mesg_Struct), io_strupMesg->iMesgType, iMesgFlag);	if(nbytes<sizeof(long)) return -1;	len=nbytes-sizeof(long);	io_strupMesg->iMesgLen =len;	return len;}
//Control a message queue
//In:
//	i_MesgId:
//	i_Cmd: IPC_STAT,IPC_SET,IPC_RMID
//	io_strupMsgid: for PC_SET
//Out:
//	io_strupMsgid: for IPC_STAT
//Return:
//	-1: Error
//	0:  Success
int Mesg_ctrl(int i_MesgId,int i_Cmd,struct msqid_ds *io_strupMsqid){
	int ret;
	ret=msgctl(i_MesgId,i_Cmd,io_strupMsqid);
	return ret;
}

⌨️ 快捷键说明

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