linux msg queue 基础操作

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

void msg_stat(int,struct msqid_ds );
int main()
{
    int gflags,sflags,rflags;
    key_t key;
    int msgid;
    int reval;
    struct msgsbuf{
        int mtype;
        char mtext[1];
    }msg_sbuf;
    struct msgmbuf
    {
        int mtype;
        char mtext[10];
    }msg_rbuf;
    struct msqid_ds msg_ginfo,msg_sinfo;
    char* msgpath="/unix/msgqueue";
    key=ftok(msgpath,'a');
    gflags=IPC_CREAT|IPC_EXCL;
    msgid=msgget(key,gflags|00666);
    if(msgid==-1)
    {
        printf("msg create error\n");
        return;
    }
    /*创建一个消息队列后,输出消息队列缺省属性*/
    msg_stat(msgid,msg_ginfo);
    sflags=IPC_NOWAIT;
    msg_sbuf.mtype=10;
    msg_sbuf.mtext[0]='a';
    reval=msgsnd(msgid,&msg_sbuf,sizeof(msg_sbuf.mtext),sflags);
    if(reval==-1)
    {
        printf("message send error\n");
    }
    /*发送一个消息后,输出消息队列属性*/
    msg_stat(msgid,msg_ginfo);
    rflags=IPC_NOWAIT|MSG_NOERROR;
    reval=msgrcv(msgid,&msg_rbuf,4,10,rflags);
    if(reval==-1)
        printf("read msg error\n");
    else
        printf("read from msg queue %d bytes\n",reval);
    /*从消息队列中读出消息后,输出消息队列属性*/
    msg_stat(msgid,msg_ginfo);
    msg_sinfo.msg_perm.uid=8;/*just a try
                               msg_sinfo.msg_perm.gid=8;/*
                               msg_sinfo.msg_qbytes=16388;
                               /*此处验证超级用户可以更改消息队列的缺省msg_qbytes*/
    /*注意这里设置的值大于缺省值*/
    reval=msgctl(msgid,IPC_SET,&msg_sinfo);
    if(reval==-1)
    {
        printf("msg set info error\n");
        return;
    }
    msg_stat(msgid,msg_ginfo);
    /*验证设置消息队列属性*/
    reval=msgctl(msgid,IPC_RMID,NULL);/*删除消息队列*/
    if(reval==-1)
    {
        printf("unlink msg queue error\n");
        return;
    }
}
void msg_stat(int msgid,struct msqid_ds msg_info)
{
    int reval;
    sleep(1);/*只是为了后面输出时间的方便*/
    reval=msgctl(msgid,IPC_STAT,&msg_info);
    if(reval==-1)
    {
        printf("get msg info error\n");
        return;
    }
    printf("\n");
    printf("current number of bytes on queue is %d\n",msg_info.msg_cbytes);
    printf("number of messages in queue is %d\n",msg_info.msg_qnum);
    printf("max number of bytes on queue is %d\n",msg_info.msg_qbytes);
    /*每个消息队列的容量(字节数)都有限制MSGMNB,值的大小因系统而异。在创建新的消息队列时,msg_qbytes的缺省值就是MSGMNB*/
    printf("pid of last msgsnd is %d\n",msg_info.msg_lspid);
    printf("pid of last msgrcv is %d\n",msg_info.msg_lrpid);
    printf("last msgsnd time is %s", ctime(&(msg_info.msg_stime)));
    printf("last msgrcv time is %s", ctime(&(msg_info.msg_rtime)));
    printf("last change time is %s", ctime(&(msg_info.msg_ctime)));
    printf("msg uid is %d\n",msg_info.msg_perm.uid);
    printf("msg gid is %d\n",msg_info.msg_perm.gid);
}

转载于:https://www.cnblogs.com/xianqingzh/archive/2011/01/11/1932984.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用Linux msg消息队列的简单例程,它演示了如何发送和接收消息: 发送消息的代码: ```c #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define MSGKEY 9876 struct msgbuf { long mtype; // 消息类型,必须是正整数 char mtext[256]; // 消息正文,最大长度为256字节 }; int main() { int msgid; struct msgbuf msg; // 创建消息队列 msgid = msgget(MSGKEY, IPC_CREAT | 0666); if (msgid == -1) { perror("msgget error"); return -1; } // 填充消息数据 msg.mtype = 1; sprintf(msg.mtext, "Hello, message queue!"); // 发送消息 if (msgsnd(msgid, &msg, sizeof(msg.mtext), 0) == -1) { perror("msgsnd error"); return -1; } printf("Message sent successfully.\n"); return 0; } ``` 接收消息的代码: ```c #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define MSGKEY 9876 struct msgbuf { long mtype; char mtext[256]; }; int main() { int msgid; struct msgbuf msg; // 打开消息队列 msgid = msgget(MSGKEY, 0666); if (msgid == -1) { perror("msgget error"); return -1; } // 接收消息 if (msgrcv(msgid, &msg, sizeof(msg.mtext), 0, 0) == -1) { perror("msgrcv error"); return -1; } printf("Received message: %s\n", msg.mtext); // 关闭消息队列 if (msgctl(msgid, IPC_RMID, NULL) == -1) { perror("msgctl error"); return -1; } return 0; } ``` 运行发送消息的程序后,再运行接收消息的程序,就可以看到从消息队列中接收到消息了。注意,在使用msg消息队列时,消息类型必须是正整数,并且发送和接收消息的长度必须相同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值