/*
*删除 msgctl(msgid,IPC_RMID,0); ipcrm -q id ipcs -q 查看
*创建 msgqueue 时指定 key 为 IPC_PRIVATE 读取时将不能获得key指定的队列,所以需要自己定key
*/
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<memory.h>
#include<string.h>
char path[100]={0};
typedef struct{
long mtype;
char buff[1024];
}MSG;
key_t getKey(){
getcwd(path,sizeof(path)-1);
return ftok(path,0);
}
int main(){
int msgid=msgget(getKey(),IPC_CREAT|0666);
MSG msg;
memset(&msg,0,sizeof(msg));
msg.mtype=1;
char buff[100]="1234567890asdfghjklqwertyuiopzxcvbnm,./";
memcpy(msg.buff,buff,sizeof(buff));
msgsnd(msgid,&msg,sizeof(MSG)-8,0);
}
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<memory.h>
#include<string.h>
char path[100]={0};
typedef struct{
long mtype;
char buff[1024];
}MSG;
key_t getKey(){
getcwd(path,sizeof(path)-1);
return ftok(path,0);
}
int main(){
int msgid=msgget(getKey(),0666);
printf("msg id is %d\n",msgid);
MSG msg;
memset(&msg,0,sizeof(msg));
msgrcv(msgid,&msg,sizeof(msg)-8,1,IPC_NOWAIT);
printf("rcv %s\n",msg.buff);
}