建立两个.c 建立子父进程,父进程发送消息到队列,子进程读取队列,另一个同样。
send.c
#include<stdio.h>
#include<string.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdlib.h>
#include <sys/wait.h>
#if 1
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1000]; /* message data */
};
#endif
int main(int argc, const char *argv[])
{
pid_t pid;
pid=fork();
if(pid>0)
{
key_t key=ftok("./",'B');
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID=msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgID");
return -1;
}
struct msgbuf data1;
while(1)
{
printf("请输入消息类型:");
fflush(stdout);
read(0,&data1.mtype,sizeof(data1.mtype));
printf("请输入数据:");
fflush(stdout);
read(0,data1.mtext,sizeof(data1.mtext));
data1.mtext[strlen(data1.mtext)-1]='\0';
int len= sizeof(struct msgbuf)-sizeof(long);
msgsnd(msgID,&data1,len,0);
if(strcmp(data1.mtext,"quit")==0)
{
break;
}
}
if(msgctl(msgID,IPC_RMID,NULL)==-1)
{
printf("rcv.c文件父进程消息删除失败\n");
return -1;
}
printf("rcv.c文件父进程消息删除成功\n");
}
else if(pid==0)
{
key_t key=ftok("./",'A');
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID=msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgID");
return -1;
}
struct msgbuf data2_rcv;
while(1)
{
int len= sizeof(struct msgbuf)-sizeof(long);
msgrcv(msgID,&data2_rcv,len,0,0);
printf("%s\n",data2_rcv.mtext);
fflush(stdout);
if(strcmp(data2_rcv.mtext,"quit")==0)
{
break;
}
}
printf("rcv.c文件子进程消息删除成功\n");
exit(EXIT_SUCCESS);
}
else
{
perror("fork");
return -1;
}
wait(NULL);
return 0;
}
rcv.c
#include<stdio.h>
#include<string.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdlib.h>
#include <sys/wait.h>
#if 1
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1000]; /* message data */
};
#endif
int main(int argc, const char *argv[])
{
pid_t pid;
pid=fork();
if(pid>0)
{
key_t key=ftok("./",'A');
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID=msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgID");
return -1;
}
struct msgbuf data1;
while(1)
{
printf("请输入消息类型:");
scanf("%ld",&data1.mtype);
getchar();
//fflush(stdout);
//read(0,&data1.mtype,sizeof(data1.mtype));
printf("请输入数据:");
fflush(stdout);
read(0,data1.mtext,sizeof(data1.mtext));
data1.mtext[strlen(data1.mtext)-1]='\0';
int len= sizeof(struct msgbuf)-sizeof(long);
msgsnd(msgID,&data1,len,0);
if(strcmp(data1.mtext,"quit")==0)
{
break;
}
}
if(msgctl(msgID,IPC_RMID,NULL)==-1)
{
printf("send.c文件父进程消息删除失败\n");
return -1;
}
printf("send.c文件父进程消息删除成功\n");
}
else if(pid==0)
{
key_t key=ftok("./",'B');
if(key==-1)
{
perror("ftok");
return -1;
}
int msgID=msgget(key,IPC_CREAT|0664);
if(msgID==-1)
{
perror("msgID");
return -1;
}
struct msgbuf data2_rcv;
while(1)
{
int len= sizeof(struct msgbuf)-sizeof(long);
msgrcv(msgID,&data2_rcv,len,0,0);
printf("%s",data2_rcv.mtext);
fflush(stdout);
if(strcmp(data2_rcv.mtext,"quit")==0)
{
break;
}
}
printf("send.c文件子进程消息删除成功\n");
exit(EXIT_SUCCESS);
}
else
{
perror("fork");
return -1;
}
wait(NULL);
return 0;
}