进程间通讯之消息队列(实例)

消息队列概念可以参考前面《进程间通讯之概念》

1、msgget

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

void get_msginfo(int msgid);

int main(void)
{
    int msgid,ret;
    //key=ftok();

    //msgid=msgget(IPC_PRIVATE,0600);
    msgid=msgget(0x555,IPC_CREAT|0600);//create or get
    if(msgid==-1)
    {
      perror("msgget");
      return 1;
    }
    printf("msgid:%d\n",msgid);

    get_msginfo(msgid);


    //del
    msgctl(msgid,IPC_RMID,NULL);
    return 0;
}
void get_msginfo(int msgid)
{
    struct msqid_ds buf;
    msgctl(msgid,IPC_STAT,&buf);

    printf("max:%d bytes:%d num:%d\n",buf.msg_qbytes,buf.__msg_cbytes,buf.msg_qnum);
}


2、发送端和接收端

2.1、发送端:

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

#define     SIZE      32

typedef struct{
    int    id;
    char   name[SIZE];
    float  math;
}Student_t;

struct msg{
    long      type;
    Student_t stu;
};

void init_msg(struct msg *s);
void show_msginfo(int msgid);

int main(void)
{
    int ret,msgid;

    msgid=msgget(0x555,IPC_CREAT|0646);
    if(msgid==-1)    return 1;
    printf("get or create msg queue success\n");

    show_msginfo(msgid);
//------------------send msg--------------------
    struct msg mymsg;
    init_msg(&mymsg);

    ret=msgsnd(msgid,&mymsg,sizeof(Student_t),0);
    if(ret==-1)
    {
       perror("msgsnd");
       return 2;
    }
//-----------------------------------------------
    show_msginfo(msgid);
    return 0;
}
void show_msginfo(int msgid)
{
    struct msqid_ds buf;
    msgctl(msgid,IPC_STAT,&buf);

    printf("max:%d bytes:%d num:%d\n",buf.msg_qbytes,buf.__msg_cbytes,buf.msg_qnum);
}
void init_msg(struct msg *s)
{
    printf("type :");
    scanf("%ld",&s->type);
   
    printf("id   :");
    scanf("%d",&s->stu.id);
    printf("name :");
    scanf("%s",s->stu.name);
    printf("math :");
    scanf("%f",&s->stu.math);
}

2.2、接收端

接收端1:(接收到的消息会在队列中取走)
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define     SIZE      32

typedef struct{
    int    id;
    char   name[SIZE];
    float  math;
}Student_t;

struct msg{
    long      type;
    Student_t stu;
};

void show_msginfo(int msgid);

int main(void)
{
    int ret,msgid;

    msgid=msgget(0x555,0444);(客户端都是只读就可以了)
    if(msgid==-1){
       perror("msgget");
       return 1;
    }
    printf("get or create msg queue success\n");

    show_msginfo(msgid);
//------------------send msg--------------------
    struct msg mymsg;

    ret=msgrcv(msgid,&mymsg,sizeof(Student_t),-4,IPC_NOWAIT);
    printf("==rcv ret:%d\n",ret);
    if(ret==-1)
    {
       perror("msgsnd");
       return 2;
    }
    printf("type:%ld id:%d name:%s\n",mymsg.type,mymsg.stu.id,mymsg.stu.name);
//-----------------------------------------------
    show_msginfo(msgid);
    return 0;
}
void show_msginfo(int msgid)
{
    struct msqid_ds buf;
    msgctl(msgid,IPC_STAT,&buf);

    printf("max:%d bytes:%d num:%d\n",buf.msg_qbytes,buf.__msg_cbytes,buf.msg_qnum);
}

接收端2:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define     SIZE      32

typedef struct{
    int    id;
    char   name[SIZE];
    float  math;
}Student_t;

void show_msginfo(int msgid);

int main(void)
{
    int ret,msgid;

    msgid=msgget(0x555,0644);
    if(msgid==-1){
       perror("msgget");
       return 1;
    }
    printf("get or create msg queue success\n");

    show_msginfo(msgid);
//------------------recv msg--------------------
    char buf[64];
    //ret=msgrcv(msgid,buf,20,0,MSG_NOERROR | IPC_NOWAIT);
    ret=msgrcv(msgid,buf,64,0,IPC_NOWAIT);
    printf("==rcv ret:%d\n",ret);
    if(ret==-1)
    {
       perror("msgsnd");
       return 2;
    }
    Student_t *s=(Student_t *)(buf+sizeof(long));
    printf("type:%ld id:%d name:%s math:%f\n",*(long *)buf,s->id,s->name,s->math);
//-----------------------------------------------
    show_msginfo(msgid);
    return 0;
}
void show_msginfo(int msgid)
{
    struct msqid_ds buf;
    msgctl(msgid,IPC_STAT,&buf);

    printf("max:%d bytes:%d num:%d\n",buf.msg_qbytes,buf.__msg_cbytes,buf.msg_qnum);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值