#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[100];
};
#define leng sizeof(struct msgbuf)-sizeof(long)
int main(int argc, const char *argv[])
{
pid_t pid=fork();
if(pid>0)
{
key_t key=ftok("./",'a');//创建key值
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID=msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgget");
return -1;
}
struct msgbuf send;
while(1)
{
printf("请输入类型:");
scanf("%ld",&send.mtype);
getchar();
printf("传入的内容:");
fgets(send.mtext,sizeof(send.mtext),stdin);
msgsnd(msgID,&send,leng,0);
if(strcmp(send.mtext,"!\n")==0)
{
break;
}
}
if(msgctl(msgID,IPC_RMID,NULL)==-1)
{
printf("删除队列失败\n");
return -1;
}
}
else if(pid==0)
{
key_t key=ftok("./",'b');//创建key值
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID=msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgget");
return -1;
}
struct msgbuf rev;
while(1)
{
msgrcv(msgID,&rev,leng,0,0);
printf("%s",rev.mtext);
if(strcmp(rev.mtext,"!\n")==0)
{
break;
}
}
}
else
{
perror("pid");
return -1;
}
return 0;
}
#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[100];
};
#define leng sizeof(struct msgbuf)-sizeof(long)
int main(int argc, const char *argv[])
{
pid_t pid=fork();
if(pid>0)
{
key_t key=ftok("./",'b');//创建key值
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID=msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgget");
return -1;
}
struct msgbuf send;
while(1)
{
printf("请输入类型:");
scanf("%ld",&send.mtype);
getchar();
printf("传入的内容:");
fgets(send.mtext,sizeof(send.mtext),stdin);
msgsnd(msgID,&send,leng,0);
if(strcmp(send.mtext,"!\n")==0)
{
break;
}
}
if(msgctl(msgID,IPC_RMID,NULL)==-1)
{
printf("删除队列失败\n");
return -1;
}
}
else if(pid==0)
{
key_t key=ftok("./",'a');//创建key值
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID=msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgget");
return -1;
}
struct msgbuf rev;
while(1)
{
msgrcv(msgID,&rev,leng,0,0);
printf("%s",rev.mtext);
if(strcmp(rev.mtext,"!\n")==0)
{
break;
}
}
}
else
{
perror("pid");
return -1;
}
return 0;
}
思维导图