进程间的消息队列及msgtype的用法

1.消息结构体

  struct msgbuf {
      long mtype;/* message type, must be > 0 */
      data_t  data;/* message data */
  };


data_t  data; 是自己定义的


2.进程间消息队列发送消息函数msgsnd


  #include <sys/types.h>
       #include <sys/ipc.h>
       #include <sys/msg.h>


       int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

发消息时的msgtype用法:

指定发送的消息类型 ,消息类型大于0


3.进程间消息队列接收消息函数msgsnd
  #include <sys/types.h>
       #include <sys/ipc.h>
       #include <sys/msg.h>

       ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
     int msgflg);


接收消息时的msgtype的用法:

(1)msgtype > 0

接收消息类型为msgtype的消息

(2)msgtype==0

接收消息队列中最前面的那个消息


下面举个例子,客户端发送消息类型为 1 、2 、3共三种类型,然后服务端指定接收类型2

客户端代码

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>

extern int errno;

#define ClientKey 1234 

typedef struct stu_msgbuf_s
{
	long msgtype;
	char msgtext[1024];
}stu_msgbuf_t;

int sendmsg(int msgid ,const char * pchBuf, int len,int msgtype)
{
	stu_msgbuf_t stuSendMsg = {0};
	int ret = -1;

	if(NULL == pchBuf ||
		strlen(pchBuf) == 0 ||
		len <=0)
	{
	    printf("\nparam error [sendmsg]\n");
		return -1;
	}


	stuSendMsg.msgtype = msgtype;
	memset(stuSendMsg.msgtext,0,sizeof(stuSendMsg.msgtext));
	strncpy(stuSendMsg.msgtext,pchBuf,sizeof(stuSendMsg.msgtext)-1);

	ret = msgsnd(msgid,&stuSendMsg,sizeof(stuSendMsg.msgtext),IPC_NOWAIT);
	if(-1 == ret)
	{
		printf("\n msgsnd gs_nClientMsgid [%d] errno=%d  [%s]\n",msgid,errno,strerror(errno));
		return -1;
	}
	else
	{
		printf("\nmsg snd  [%s]\n",stuSendMsg.msgtext);
	}
	
	return 0;
}

int init(int *pMsgId)
{
	int msgid = -1;
	key_t ktmp = ftok("/share/1.tmp",ClientKey);
	if(ktmp == -1)
	{
		printf("\n if(ktmp == -1) errno=%d  [%s]\n",errno,strerror(errno));
		return -1;
	}
	else
	{
		printf("\n ftok ktmp[%d]\n",ktmp);
	}
	
	msgid = msgget(ktmp, 0666|IPC_CREAT);

	if(msgid < 0)
	{
		printf("\n if(gs_nClientMsgid == -1) errno=%d  [%s]\n",errno,strerror(errno));
		return -1;
	}

	printf("\n msgget sucess %d  ktmp[%d]\n",msgid,ktmp);

	*pMsgId = msgid;
	return 0;

}


int main()
{
	int ret =-1;
	int msgid =-1;
	ret = init(&msgid);
	if(ret == -1)
	{
		return 0;
	}
	while(1)
	{
		char chStr[20] = {0};
		int type = 0;
		static int i = 0;
		type = i%3+1;
		
		i++;
		printf("\ntype[%d]\n",type);
		snprintf(chStr,sizeof(chStr),"type[%d]--[%ld]",type,time(NULL));
		sendmsg(msgid,chStr, strlen(chStr)+1,type);
		sleep(2);
	}
	
}




服务端代码

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>

extern int errno;

#define lientKey 1234

static int  gs_nClientMsgid = -1; 

typedef struct stu_msgbuf_s
{
	long msgtype;
	char msgtext[1024];
}stu_msgbuf_t;

int rcvmsg(int msgid,const char * pchBuf, int len, int nMsgType)
{
	stu_msgbuf_t stuSendMsg = {0};
	int ret = -1;

	stuSendMsg.msgtype = nMsgType;
	ret = msgrcv(msgid, &stuSendMsg, sizeof(stuSendMsg.msgtext), stuSendMsg.msgtype ,IPC_NOWAIT);
	if(-1 == ret)
	{
		printf("\n msgrcvg s_nClientMsgid [%d] errno=%d  [%s]\n", msgid ,errno,strerror(errno));
		return -1;
	}
	else
	{
		printf("\n msgrcv  [%s]\n",stuSendMsg.msgtext);
	}
	
	return 0;
}

int init(int *pMsgId)
{
	int msgid = -1;
	key_t ktmp = ftok("/share/1.tmp",lientKey);
	if(ktmp == -1)
	{
		printf("\n if(ktmp == -1) errno=%d  [%s]\n",errno,strerror(errno));
		return -1;
	}
	else
	{
		printf("\n ftok ktmp[%d]\n",ktmp);
	}
	
	msgid = msgget(ktmp, 0666|IPC_CREAT);

	if(msgid < 0)
	{
		printf("\n if(gs_nClientMsgid == -1) errno=%d  [%s]\n",errno,strerror(errno));
		return -1;
	}

	printf("\n msgget sucess %d  ktmp[%d]\n",msgid,ktmp);

	*pMsgId = msgid;
	return 0;

}

int main()
{
	int ret =-1;
	int msgid =-1;
	ret = init(&msgid);
	if(ret == -1)
	{
		return 0;
	}
	while(1)
	{
		char chStr[20] = {0};
		int msgtype = 2;
		
		//printf("\ni[%d]\n",i);
		//snprintf(chStr,sizeof(chStr),"%d --- %ld",i,time(NULL));
		rcvmsg(msgid,chStr, strlen(chStr)+1,msgtype);
		sleep(2);
	}
	
}






由此可见,服务端只接收了类型2。



在 Windows 平台下,可以使用消息队列(Message Queue)机制来实现多进程的消息传递。以下是实现步骤: 1. 定义消息结构体 定义一个消息结构体,用来存储需要传递的消息内容。例如: ``` struct message { int msgType; // 消息类型 char data[1024]; // 消息内容 }; ``` 2. 创建消息队列 使用 `CreateMailslot` 函数创建一个邮槽(Mailslot),作为消息队列。例如: ``` HANDLE hMailslot = CreateMailslot("\\\\.\\mailslot\\my_mailslot", 0, MAILSLOT_WAIT_FOREVER, NULL); ``` 其中,`"\\\\.\\mailslot\\my_mailslot"` 是邮槽名称,可以自己定义。 3. 发送消息 使用 `WriteFile` 函数向邮槽中写入消息。例如: ``` message msg; msg.msgType = 1; strcpy(msg.data, "hello world"); WriteFile(hMailslot, &msg, sizeof(msg), NULL, NULL); ``` 其中,`msgType` 是消息类型,可以自己定义。`data` 是消息内容。 4. 接收消息 使用 `ReadFile` 函数从邮槽中读取消息。例如: ``` message msg; DWORD numRead; BOOL success = ReadFile(hMailslot, &msg, sizeof(msg), &numRead, NULL); if (success && numRead == sizeof(msg)) { // 处理消息 } ``` 其中,`success` 表示是否成功读取消息,`numRead` 表示实际读取的字节数。 注意:如果多个进程都要读取同一个邮槽中的消息,需要在每个进程中创建一个邮槽,并使用相同的邮槽名称。这样,所有进程都可以向同一个邮槽中写入消息,也可以从同一个邮槽中读取消息。 还需要注意的是,邮槽机制虽然可以实现多进程的消息传递,但是它是一种异步的方式,即发送方不知道消息是否被接收方接收到。如果需要实现同步的消息传递,可以使用其他的机制,例如共享内存和事件等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值