IPC之System V 消息队列 (代码实现)

        消息队列貌似慢慢被程序员们所遗忘了,在进程间通信中用到它的甚少,不过平时学习中我们也得了解下消息队列的用法。下面是本人写的两个代码,一个是读进程,一个是写进程。关于消息队列的具体函数及参数配置,还得去看W·Richard·Stevens写的《网络编程卷2:进程间通信》。

       两个程序通过gcc编译后,运行,在写端打入自己想要的字符串,在读端就会打印出来。

       写消息的进程:

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

#define DATALEN 100

struct megbuf
{
    long mtype;
    char mtext[DATALEN];
};

int main()
{
    int msqid;
    struct megbuf buf;
    int ret;
    int key;
    int sendlength;
    
    key = ftok("meg.tmp",'a');
    msqid = msgget(key,IPC_CREAT|0666);
    if(msqid < 0)
    {
	perror("get message queue error\n");
	return -1;
    }

    while(1)
    {
	buf.mtype = 23;
	gets(buf.mtext);
//    strcpy(buf.mtext,"hello world!");
    	sendlength = sizeof(struct megbuf) - sizeof(long);
    
    	ret = msgsnd(msqid,&buf,sendlength,0);
    	if(ret < 0)
   	{
	    perror("send message err\n");
	    return -1;
        }
    }
//    system("ipcs -q");
    return 0;
}

     读消息进程:

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

#define DATALEN 100

typedef struct megbuf
{
    long mtype;
    char mtext[DATALEN];
}Message;

int main()
{
   int msqid;
   Message buf;
   int ret;
   int key;
   int mtype;
   int recvlength;

   key = ftok("msg.tmp",'a');
   msqid = msgget(key,0666);
   if(msqid < 0)
   {
       perror("get message queue err\n");
       return -1;
   }
   
   recvlength = sizeof(Message) - sizeof(long);
   memset(&buf,0,sizeof(Message));
   while(1)
   {
   	mtype = 23;
   	ret = msgrcv(msqid,&buf,recvlength,mtype,0);
   	if(ret < 0)
   	{
	    perror("recv message error\n");
            return -1;
   	}

   	printf("message = %s\n",buf.mtext);
   }
//   system("ipcs -q");
   return 0;
}


本文仅限学习之用,如有雷同不胜荣幸!!有误之处请指出,谢谢合作!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,可以通过 System V 消息队列或 POSIX 消息队列实现消息队列。 对于 System V 消息队列,可以使用以下系统调用来创建和使用消息队列: 1. msgget():创建或访问一个消息队列。 2. msgsnd():向消息队列发送消息。 3. msgrcv():从消息队列中接收消息。 4. msgctl():控制消息队列。 下面是一个使用 System V 消息队列的简单示例: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define MSG_SIZE 1024 struct message { long mtype; char mtext[MSG_SIZE]; }; int main() { int msqid; struct message msg; key_t key; // 创建消息队列,key 值可以通过 ftok() 函数生成 key = ftok(".", 'a'); if ((msqid = msgget(key, IPC_CREAT | 0666)) == -1) { perror("msgget"); exit(1); } // 向消息队列发送消息 msg.mtype = 1; strcpy(msg.mtext, "Hello, world!"); if (msgsnd(msqid, &msg, sizeof(struct message) - sizeof(long), 0) == -1) { perror("msgsnd"); exit(1); } // 从消息队列接收消息 if (msgrcv(msqid, &msg, sizeof(struct message) - sizeof(long), 0, 0) == -1) { perror("msgrcv"); exit(1); } printf("Received message: %s\n", msg.mtext); // 删除消息队列 if (msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(1); } return 0; } ``` 对于 POSIX 消息队列,可以使用以下系统调用来创建和使用消息队列: 1. mq_open():创建或打开一个消息队列。 2. mq_send():向消息队列发送消息。 3. mq_receive():从消息队列中接收消息。 4. mq_close():关闭消息队列。 5. mq_unlink():删除消息队列。 下面是一个使用 POSIX 消息队列的简单示例: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/stat.h> #include <mqueue.h> #define MSG_SIZE 1024 int main() { mqd_t mqd; char *msg = "Hello, world!"; char buf[MSG_SIZE]; struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = MSG_SIZE }; // 创建消息队列 if ((mqd = mq_open("/test", O_CREAT | O_RDWR, 0666, &attr)) == -1) { perror("mq_open"); exit(1); } // 向消息队列发送消息 if (mq_send(mqd, msg, strlen(msg), 0) == -1) { perror("mq_send"); exit(1); } // 从消息队列接收消息 if (mq_receive(m

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值