进程间通讯之消息队列(实例)
2017-04-20 22:00
363 查看
消息队列概念可以参考前面《进程间通讯之概念》
接收端2:
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); }
相关文章推荐
- Linux进程间通讯之消息队列
- .Net下的进程间的通讯 -- Windows消息队列
- 进程间通讯--消息队列
- Linux进程间通讯之消息队列
- 消息队列实例(同一进程消息队列通信)
- 进程间的通讯方式_消息队列
- 进程间通讯:消息队列
- 进程间通讯————消息队列
- 进程间通讯——消息队列
- Linux进程间通讯之消息队列
- 进程间通讯的消息队列和共享内存方式的实现
- 进程组间通讯(消息队列)
- 进程间通讯----消息队列和共享内存方式的实现
- Linux进程间通讯之消息队列
- 用消息队列实现进程间通讯
- Linux:进程通信之消息队列Message实例
- Linux笔记_进程通讯——消息队列
- 进程间通讯——消息队列
- 用线程和消息队列实现连个进程间的通讯
- linux-基础-进程通讯(二)-消息队列/信号量