

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <sys/unistd.h>
#include <sys/msg.h>
using namespace std;
class msgqueue
{
private:
long mtype;
char* mtext;
int len;
public:
msgqueue(long num,const char* str=NULL)
{
if(str==NULL)
{
mtext=new char[1];
len=0;
}
else
{
len=strlen(str);
mtext=new char[len+1];
strcpy(mtext,str);
//创建秘钥
key_t key=ftok("./",200);
if(key==-1)
{
cout<<"ftok error"<<endl;
return;
}
//更具秘钥创建一个消息队列
int msgid = msgget(key,IPC_CREAT|0664);
if(msgid ==-1)
{
cout<<"msgget error"<<endl;
return;
}
mtype=num;
msgsnd(msgid,&mtext,len,0);
cout<<mtext<<endl;
}
}
/*~msgqueue(){
msgctl(msgid,IPC_RMID,NULL);
}*/
};
int main(int argc, const char *argv[])
{
msgqueue msg(1,"hello");
return 0;
}