typedef struct
{
long mtype;
char mtext[100];
}msgbut;
#define leng sizeof(msgbut) -sizeof(long)
int main(int argc,const char*argv[])
{
key_t key = ftok("./",'w');
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID = msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgget");
return -1;
}
msgbut send;
pid_t ID;
ID = fork();
if(ID>0)
{
while(1)
{
printf("请输入消息的类型:");
scanf("%ld",&send.mtype);
getchar();
printf("请输入消息的内容:");
fgets(send.mtext,leng,stdin);
msgsnd(msgID,&send,leng,0);
if(strcmp(send.mtext,"quit\n")==0)
{
break;
}
}
}
if(ID==0)
{
msgbut rev;
key = ftok("./",'A');
msgID = msgget(key,IPC_CREAT|0664);
while(1)
{
msgrcv(msgID,&rev,leng,0,0);
fputs(rev.mtext,stdout);
if(strcmp(rev.mtext,"quit\n")==0)
{
break;
}
}
if(msgctl(msgID,IPC_RMID,NULL)==-1)
{
printf("删除队列失败\n");
return -1;
}
}
return 0;
}
typedef struct
{
long mtype;
char mtext[100];
}msgbut;
#define leng sizeof(msgbut) -sizeof(long)
int main(int argc,const char*argv[])
{
key_t key = ftok("./",'w');
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID = msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgget");
return -1;
}
msgbut send;
pid_t ID;
ID = fork();
if(ID>0)
{
key = ftok("./",'A');
msgID = msgget(key,IPC_CREAT|0664);
while(1)
{
printf("请输入消息的类型:");
scanf("%ld",&send.mtype);
getchar();
printf("请输入消息的内容:");
fgets(send.mtext,leng,stdin);
msgsnd(msgID,&send,leng,0);
if(strcmp(send.mtext,"quit\n")==0)
{
break;
}
}
}
if(ID==0)
{
msgbut rev;
while(1)
{
msgrcv(msgID,&rev,leng,0,0);
fputs(rev.mtext,stdout);
if(strcmp(rev.mtext,"quit\n")==0)
{
break;
}
}
if(msgctl(msgID,IPC_RMID,NULL)==-1)
{
printf("删除队列失败\n");
return -1;
}
}
return 0;
}