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

来源:互联网 发布:淘宝怎么找指定客服 编辑:程序博客网 时间:2024/06/08 10:35
消息队列概念可以参考前面《进程间通讯之概念》

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      32typedef 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      32typedef 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      32typedef 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
原创粉丝点击