自己如何实现消息队列
1、使用步骤:1)创建:key_t key=ftok(PATH,proj); int msgid=msgget(key,IPC_CREAT| 0600)2)发送:msgsnd(msgid,&msg,strlen(msg.mtext),0)3)接收:msgrcv(msgid,&msg,strlen(msg.mtext),msg.mtype,0)4)回收:msgctl(msgid,IPC_RMID,NULL)

3、实现写消息的代码:#include <stdio.h>#include <unistd.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/ipc.h>#include <sys/msg.h>#define TXT_SIZE 100 struct msgbuf{ long type; char txt[TXT_SIZE];};int main(){ key_t key; //生成KEY key=ftok("/home/lbh",10); int msgid; if(-1 == (msgid=msgget(key,IPC_CREAT | 0660)))//创建 { perror("msgget"); exit(-1); } struct msgbuf msg; bzero(&msg,sizeof(msg)); msg.type=1; strcpy(msg.txt,"hello"); msgsnd(msgid,&msg,strlen(msg.txt),0);//发送消息 return 0;}
