message queue

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

typedef struct {
 long mtype; //消息类型必须大于0
 char mtext[BUFSIZ];
}msg_info;

int creat_msg_queue()
{
        key_t key;
        int proj_id;
        int msqid;

        struct msqid_ds buffer;

        proj_id=2;
        key=ftok("/home/program",proj_id);
        if(key==-1){
                perror("cannot generate the IPC key");
                return -1;
        }

        msqid=msgget(key,IPC_CREAT | 0660);
        if(msqid==-1){
                perror("cannot create message queue resource");
                return -1;
        }

 return msqid;
}


int send_msg(int msqid,char* msg)
{
 int result;
 
 msg_info buf;
 
 buf.mtype=100;
 strcpy(buf.mtext,msg);
 
 result=msgsnd(msqid,&buf,strlen(msg),0);

 //最后一个参数 flag 既可以是0也可以是IPC_NOWAIT :如果没有存放消息的空间,就立马返回。同理,若msgrcv如设置这个IPC_NOWAIT 标志,在没有消息得到的情况下,就会立即返回一个ENOMSG错误。
1)
  perror("cannot send message to the message queue");
 
 return result;
}

int show_msg_queue_stat(int msqid)
{
 struct msqid_ds buffer;
 int flg;

//提供一个消息队列上的各种控制操作 IPC_STAT :通过给buffer 返回与所指定消息队列对应的当前msgqi_ds结构信息。

 flg=msgctl(msqid,IPC_STAT,&buffer);
 if(flg==-1){
  perror("cannot get status of the message queue");
  return -1;
 } 

        printf("=======Message Queue Info========\n");
        printf("effective user id : %d \n",buffer.msg_perm.uid);
        printf("effective group id: %d \n",buffer.msg_perm.gid);
        printf("message queue's creator user id : %d \n",buffer.msg_perm.cuid);
        printf("message queue's creator group id : %d \n",buffer.msg_perm.cgid);
        printf("access mode : %x\n",buffer.msg_perm.mode);

        printf("Maximum number of bytes allowed in message queue : %d \n",buffer.msg_qbytes);
        printf("Current number of bytes in message queue (non-standard) : %d \n",buffer.__msg_cbytes);
        printf("Current number of messages in message queue : %d \n",buffer.msg_qnum);

 return 0;
}

int main(int argc,char* argv[])

 int msqid;

 if(msqid=creat_msg_queue()<0){
  printf("create msg queue fail");
  return -1;
 }

 if(send_msg(msqid,"test message")<0){
  printf("send message fail");
  return -1;
 } 

 if(show_msg_queue_stat(msqid)<0){
  printf("show message queue fail");
  return -1;
 }

 return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、消息处理中的主要概念 “消息”是在两台计算机间传送的数据单位。消息可以非常简单,例如只包含文本字符串;也可以更复杂,可能包含嵌入对象。 消息被发送到队列中。“消息队列”是在消息的传输过程中保存消息的容器。消息队列管理器在将消息从它的源中继到它的目标时充当中间人。队列的主要目的是提供路由并保证消息的传递;如果发送消息时接收者不可用,消息队列会保留消息,直到可以成功地传递它。 “消息队列”是 Microsoft 的消息处理技术,它在任何安装了 Microsoft Windows 的计算机组合中,为任何应用程序提供消息处理和消息队列功能,无论这些计算机是否在同一个网络上或者是否同时联机。 “消息队列网络”是能够相互间来回发送消息的任何一组计算机。网络中的不同计算机在确保消息顺利处理的过程中扮演不同的角色。它们中有些提供路由信息以确定如何发送消息,有些保存整个网络的重要信息,而有些只是发送和接收消息。 “消息队列”安装期间,管理员确定哪些服务器可以互相通信,并设置特定服务器的特殊角色。构成此“消息队列”网络的计算机称为“站点”,它们之间通过“站点链接”相互连接。每个站点链接都有一个关联的“开销”,它由管理员确定,指示了经过此站点链接传递消息的频率。 “消息队列”管理员还在网络中设置一台或多台作为“路由服务器”的计算机。路由服务器查看各站点链接的开销,确定经过多个站点传递消息的最快和最有效的方法,以此决定如何传递消息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值