进程间通讯之消息队列(实例)
来源:互联网 发布:淘宝怎么找指定客服 编辑:程序博客网 时间: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
- 进程间通讯之消息队列(实例)
- Linux进程间通讯之消息队列
- Linux进程间通讯之消息队列
- Linux进程间通讯之消息队列
- 进程间通讯之消息队列
- 进程间通讯(三)消息队列
- 进程间通讯(三)消息队列
- 进程间通讯--消息队列
- 进程间通讯-消息队列
- 进程间通讯:消息队列
- 进程间通讯(二)IPC之消息队列
- 进程间通信::消息队列(实例)
- 进程间通讯--消息队列
- 进程间通讯--消息队列
- 进程间通讯--消息队列
- linux进程间通讯-消息队列
- Linux进程间通讯三--消息队列
- 进程间通讯——消息队列
- CodeForces 629 D.Babaei and Birthday Cake(dp+线段树)
- poj3252——Round Numbers(数位DP)
- GDB调试
- 【SSH进阶之路】Spring的IOC逐层深入——源码解析之IoC的根本BeanFactory(五)
- poj
- 进程间通讯之消息队列(实例)
- Swift控制转移语句—break语句
- 学习笔记-POJ-1011
- 复化求积
- web前端常用网站
- spring @Qualifier注解
- 【SSH进阶之路】Spring的AOP逐层深入——AOP的基本原理(六)
- 克隆虚拟机
- N0413. Arithmetic Slices